bump product version to 6.4.0.3
[LibreOffice.git] / compilerplugins / clang / typedefparam.cxx
blob7ea4df79d2afd857ed57e1dd1aaba8cb718ad388
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 */
10 #ifndef LO_CLANG_SHARED_PLUGINS
12 #include <cassert>
13 #include <string>
14 #include <iostream>
15 #include <fstream>
16 #include <set>
18 #include "check.hxx"
19 #include "plugin.hxx"
21 /**
22 Check that parameters at the declaration site and the definition site are the same.
23 This can be important when refactoring, and can sometimes make a difference when compiling
24 for platforms with different pointer-sizes (e.g. 32 vs 64 bit)
26 namespace
28 class TypedefParam : public loplugin::FilteringRewritePlugin<TypedefParam>
30 public:
31 explicit TypedefParam(loplugin::InstantiationData const& data)
32 : FilteringRewritePlugin(data)
36 void run() override { TraverseDecl(compiler.getASTContext().getTranslationUnitDecl()); }
38 bool VisitFunctionDecl(FunctionDecl const*);
39 bool VisitCXXMethodDecl(CXXMethodDecl const*);
42 static bool areTypesEqual(QualType lhs, QualType rhs);
44 bool TypedefParam::VisitFunctionDecl(FunctionDecl const* functionDecl)
46 if (ignoreLocation(functionDecl))
47 return true;
48 if (functionDecl->isFunctionTemplateSpecialization())
49 return true;
50 auto canonicalDecl = functionDecl->getCanonicalDecl();
51 if (canonicalDecl == functionDecl)
52 return true;
54 for (unsigned i = 0; i < functionDecl->getNumParams(); ++i)
56 ParmVarDecl const* thisParam = functionDecl->getParamDecl(i);
57 ParmVarDecl const* canonicalParam = canonicalDecl->getParamDecl(i);
58 if (!areTypesEqual(thisParam->getType(), canonicalParam->getType()))
60 #if defined _WIN32
61 // SAL_IMPLEMENT_MAIN (include/sal/main.h) declares the third parameter of WinMain to be
62 // of type 'char *' rather than 'LPSTR', but using that typedef there would require
63 // including windows.h, which would require including include/prewin.h and
64 // include/postwin.h (to undo macros like Yield defined in windows.h) but which (unlike
65 // include/sal/main.h) are not part of the stable URE interface; so just ignore that
66 // here:
67 if (loplugin::DeclCheck(functionDecl).Function("WinMain").GlobalNamespace())
69 continue;
71 #endif
72 report(DiagnosticsEngine::Warning,
73 "function param %0 at definition site does not match function param at "
74 "declaration site, %1 vs %2",
75 thisParam->getLocation())
76 << (i + 1) << thisParam->getType() << canonicalParam->getType()
77 << functionDecl->getSourceRange();
78 report(DiagnosticsEngine::Note, "declaration site here", canonicalParam->getLocation())
79 << canonicalDecl->getSourceRange();
83 if (!areTypesEqual(functionDecl->getReturnType(), canonicalDecl->getReturnType()))
85 report(DiagnosticsEngine::Warning,
86 "function return type at definition site does not match function param at "
87 "declaration site, %0 vs %1",
88 compat::getBeginLoc(functionDecl))
89 << functionDecl->getReturnType() << canonicalDecl->getReturnType()
90 << functionDecl->getSourceRange();
91 report(DiagnosticsEngine::Note, "declaration site here", compat::getBeginLoc(canonicalDecl))
92 << canonicalDecl->getSourceRange();
94 return true;
97 bool TypedefParam::VisitCXXMethodDecl(CXXMethodDecl const* methodDecl)
99 if (ignoreLocation(methodDecl))
100 return true;
101 if (methodDecl->isFunctionTemplateSpecialization())
102 return true;
103 // only warn on the declaration site
104 if (methodDecl->getCanonicalDecl() != methodDecl)
105 return true;
107 StringRef aFileName = getFilenameOfLocation(
108 compiler.getSourceManager().getSpellingLoc(compat::getBeginLoc(methodDecl)));
109 // seems to be using typedefs as a form of documentation for method params
110 if (loplugin::hasPathnamePrefix(aFileName, SRCDIR "/sw/source/filter/ww8/ww8scan.hxx"))
111 return true;
112 // necessary to work around changes in popplers API
113 if (loplugin::hasPathnamePrefix(aFileName, SRCDIR
114 "/sdext/source/pdfimport/xpdfwrapper/pdfioutdev_gpl.hxx"))
115 return true;
117 for (auto superMethodIt = methodDecl->begin_overridden_methods();
118 superMethodIt != methodDecl->end_overridden_methods(); ++superMethodIt)
120 const CXXMethodDecl* superMethodDecl = *superMethodIt;
121 int i = 0;
122 for (const ParmVarDecl* superParmVarDecl : superMethodDecl->parameters())
124 const ParmVarDecl* parmVarDecl = methodDecl->getParamDecl(i);
125 if (!areTypesEqual(parmVarDecl->getType(), superParmVarDecl->getType()))
127 report(DiagnosticsEngine::Warning,
128 "method param %0 does not match overridden method param "
129 "%1 vs %2",
130 parmVarDecl->getLocation())
131 << (i + 1) << parmVarDecl->getType() << superParmVarDecl->getType()
132 << methodDecl->getSourceRange();
133 report(DiagnosticsEngine::Note, "super-class method here",
134 superParmVarDecl->getLocation())
135 << superMethodDecl->getSourceRange();
137 ++i;
140 // it is quite normal to override a method and return a more specific pointer/reference type
141 auto returnType = methodDecl->getReturnType();
142 if (!returnType->isPointerType() && !returnType->isReferenceType())
143 if (!areTypesEqual(returnType, superMethodDecl->getReturnType()))
145 report(DiagnosticsEngine::Warning,
146 "method return type does not match overridden method "
147 "%0 vs %1",
148 compat::getBeginLoc(methodDecl))
149 << methodDecl->getReturnType() << superMethodDecl->getReturnType()
150 << methodDecl->getSourceRange();
151 report(DiagnosticsEngine::Note, "super-class method here",
152 compat::getBeginLoc(superMethodDecl))
153 << superMethodDecl->getSourceRange();
156 return true;
159 static bool areTypesEqual(QualType lhs, QualType rhs)
161 if (lhs == rhs)
162 return true;
164 // ignore template stuff
165 if (lhs->isDependentType() || rhs->isDependentType())
166 return true;
167 // there are some places, e.g. chart2/source/controller/chartapiwrapper/WrappedSymbolProperties.cxx
168 // where just unwrapping these types does not work, so just ignore them
169 if (isa<SubstTemplateTypeParmType>(lhs) || isa<SubstTemplateTypeParmType>(rhs))
170 return true;
172 auto lhsType = lhs.getTypePtr();
173 auto rhsType = rhs.getTypePtr();
175 // this is the carve-out exception for the "typedef struct S {...} T" idiom we use in the UNO code
176 if (auto lhsPointer = dyn_cast<clang::PointerType>(lhsType))
178 if (auto rhsPointer = dyn_cast<clang::PointerType>(rhsType))
180 auto extractRecordType = [](clang::QualType type) {
181 auto recordType = dyn_cast<RecordType>(type);
182 if (recordType)
183 return recordType;
184 auto elaboratedType = dyn_cast<ElaboratedType>(type);
185 if (!elaboratedType)
186 return static_cast<const clang::RecordType*>(nullptr);
187 return dyn_cast<RecordType>(elaboratedType->desugar());
189 auto containsTypedefToRecord = [](clang::QualType type, RecordType const* recordType) {
190 TypedefType const* typedefType = dyn_cast<TypedefType>(type);
191 if (!typedefType)
192 return false;
193 auto tmp = typedefType->desugar();
194 if (auto elaboratedType = dyn_cast<ElaboratedType>(tmp))
195 tmp = elaboratedType->desugar();
196 return tmp.getTypePtr() == recordType;
198 if (auto recordType = extractRecordType(lhsPointer->getPointeeType()))
199 if (containsTypedefToRecord(rhsPointer->getPointeeType(), recordType))
200 return true;
201 if (auto recordType = extractRecordType(rhsPointer->getPointeeType()))
202 if (containsTypedefToRecord(lhsPointer->getPointeeType(), recordType))
203 return true;
207 if (auto lhsElaborated = dyn_cast<ElaboratedType>(lhsType))
209 return areTypesEqual(lhsElaborated->getNamedType(), rhs);
211 if (auto rhsElaborated = dyn_cast<ElaboratedType>(rhsType))
213 return areTypesEqual(lhs, rhsElaborated->getNamedType());
215 if (auto lhsTemplate = dyn_cast<TemplateSpecializationType>(lhsType))
217 return areTypesEqual(lhsTemplate->desugar(), rhs);
219 if (auto rhsTemplate = dyn_cast<TemplateSpecializationType>(rhsType))
221 return areTypesEqual(lhs, rhsTemplate->desugar());
224 if (auto lhsLValue = dyn_cast<LValueReferenceType>(lhsType))
226 auto rhsLValue = dyn_cast<LValueReferenceType>(rhsType);
227 if (!rhsLValue)
228 return false;
229 return areTypesEqual(lhsLValue->getPointeeType(), rhsLValue->getPointeeType());
231 if (auto lhsRValue = dyn_cast<RValueReferenceType>(lhsType))
233 auto rhsRValue = dyn_cast<RValueReferenceType>(rhsType);
234 if (!rhsRValue)
235 return false;
236 return areTypesEqual(lhsRValue->getPointeeType(), rhsRValue->getPointeeType());
238 if (auto lhsPointer = dyn_cast<clang::PointerType>(lhsType))
240 auto rhsPointer = dyn_cast<clang::PointerType>(rhsType);
241 if (!rhsPointer)
242 return false;
243 return areTypesEqual(lhsPointer->getPointeeType(), rhsPointer->getPointeeType());
245 if (auto lhsTypedef = dyn_cast<TypedefType>(lhsType))
247 auto rhsTypedef = dyn_cast<TypedefType>(rhsType);
248 if (!rhsTypedef)
249 return false;
250 // comparing the underlying Decl's here doesn't work, they are not unique
251 if (lhsTypedef->getDecl()->getName() != rhsTypedef->getDecl()->getName())
252 return false;
253 #if defined __APPLE__
254 // My Clang trunk .../lib/clang/9.0.0/include/stddef.h has a
256 // typedef long unsigned int size_t;
258 // while /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/
259 // SDKs/MacOSX10.14.sdk/usr/include/sys/_types/_size_t.h has a
261 // typedef __darwin_size_t size_t;
263 // where __darwin_size_t is a typedef for long unsigned int, too, so that, depending on the
264 // order in which those two files get included, either of those two typedefs can act as a
265 // redeclaration of the other one. However, areTypesEqual would unhelpfully consider such
266 // different occurrences of size_t to be non-equal, so filter them out here. And, at least
267 // with my libcxx trunk .../include/c++/v1/cstddef, std::size_t is a using declaration that
268 // brings size_t from the global namespace into namespace std, so that the above checks for
269 // ElaboratedType sugar will already have unwrapped those to the size_t typedefs in the
270 // global namespace here.
271 if (loplugin::TypeCheck(lhsTypedef).Typedef("size_t").GlobalNamespace()
272 && loplugin::TypeCheck(rhsTypedef).Typedef("size_t").GlobalNamespace())
274 return true;
276 #endif
277 return areTypesEqual(lhsTypedef->desugar(), rhsTypedef->desugar());
279 if (auto lhsTemplate = dyn_cast<TemplateSpecializationType>(lhsType))
281 auto rhsTemplate = dyn_cast<TemplateSpecializationType>(rhsType);
282 if (!rhsTemplate)
283 return false;
284 return areTypesEqual(lhsTemplate->desugar(), rhsTemplate->desugar());
286 if (auto lhsMember = dyn_cast<MemberPointerType>(lhsType))
288 auto rhsMember = dyn_cast<MemberPointerType>(rhsType);
289 if (!rhsMember)
290 return false;
291 if (lhsMember->getClass() != rhsMember->getClass())
292 return true;
293 return areTypesEqual(lhsMember->getPointeeType(), rhsMember->getPointeeType());
295 if (auto lhsParen = dyn_cast<ParenType>(lhsType))
297 auto rhsParen = dyn_cast<ParenType>(rhsType);
298 if (!rhsParen)
299 return false;
300 return areTypesEqual(lhsParen->getInnerType(), rhsParen->getInnerType());
302 if (dyn_cast<FunctionProtoType>(lhsType))
304 auto rhsFunction = dyn_cast<FunctionProtoType>(rhsType);
305 if (!rhsFunction)
306 return false;
307 return true; // TODO
309 if (auto lhsDecayed = dyn_cast<DecayedType>(lhsType))
311 auto rhsDecayed = dyn_cast<DecayedType>(rhsType);
312 if (!rhsDecayed)
313 return false;
314 return areTypesEqual(lhsDecayed->getAdjustedType(), rhsDecayed->getAdjustedType());
316 if (auto lhsAttr = dyn_cast<AttributedType>(lhsType))
318 auto rhsAttr = dyn_cast<AttributedType>(rhsType);
319 if (!rhsAttr)
320 return false;
321 return areTypesEqual(lhsAttr->getModifiedType(), rhsAttr->getModifiedType());
323 return lhsType == rhsType;
326 loplugin::Plugin::Registration<TypedefParam> typedefparam("typedefparam");
328 } // namespace
330 #endif // LO_CLANG_SHARED_PLUGINS
332 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */