Version 6.1.0.2, tag libreoffice-6.1.0.2
[LibreOffice.git] / compilerplugins / clang / staticmethods.cxx
blob50d432081f0094bdd8db539f00fe5f8b143f442d
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 #include "clang/AST/Attr.h"
12 #include "check.hxx"
13 #include "plugin.hxx"
16 Look for member functions that can be static
18 namespace {
20 class StaticMethods:
21 public RecursiveASTVisitor<StaticMethods>, public loplugin::Plugin
23 private:
24 bool bVisitedThis;
25 public:
26 explicit StaticMethods(loplugin::InstantiationData const & data): Plugin(data), bVisitedThis(false) {}
28 void run() override
29 { TraverseDecl(compiler.getASTContext().getTranslationUnitDecl()); }
31 bool TraverseCXXMethodDecl(const CXXMethodDecl * decl);
33 bool VisitCXXThisExpr(const CXXThisExpr *) { bVisitedThis = true; return true; }
34 // these two indicate that we hit something that makes our analysis unreliable
35 bool VisitUnresolvedMemberExpr(const UnresolvedMemberExpr *) { bVisitedThis = true; return true; }
36 bool VisitCXXDependentScopeMemberExpr(const CXXDependentScopeMemberExpr *) { bVisitedThis = true; return true; }
37 private:
38 StringRef getFilename(SourceLocation loc);
41 bool BaseCheckNotTestFixtureSubclass(const CXXRecordDecl *BaseDefinition) {
42 if (loplugin::TypeCheck(BaseDefinition).Class("TestFixture").Namespace("CppUnit").GlobalNamespace()) {
43 return false;
45 return true;
48 bool isDerivedFromTestFixture(const CXXRecordDecl *decl) {
49 if (!decl->hasDefinition())
50 return false;
51 if (// not sure what hasAnyDependentBases() does,
52 // but it avoids classes we don't want, e.g. WeakAggComponentImplHelper1
53 !decl->hasAnyDependentBases() &&
54 !decl->forallBases(BaseCheckNotTestFixtureSubclass, true)) {
55 return true;
57 return false;
60 StringRef StaticMethods::getFilename(SourceLocation loc)
62 SourceLocation spellingLocation = compiler.getSourceManager().getSpellingLoc(loc);
63 return compiler.getSourceManager().getFilename(spellingLocation);
66 bool startsWith(const std::string& rStr, const char* pSubStr) {
67 return rStr.compare(0, strlen(pSubStr), pSubStr) == 0;
70 bool StaticMethods::TraverseCXXMethodDecl(const CXXMethodDecl * pCXXMethodDecl) {
71 if (ignoreLocation(pCXXMethodDecl)) {
72 return true;
74 if (!pCXXMethodDecl->isInstance() || pCXXMethodDecl->isVirtual() || !pCXXMethodDecl->doesThisDeclarationHaveABody() || pCXXMethodDecl->isLateTemplateParsed()) {
75 return true;
77 if (pCXXMethodDecl->getOverloadedOperator() != OverloadedOperatorKind::OO_None || pCXXMethodDecl->hasAttr<OverrideAttr>()) {
78 return true;
80 if (isa<CXXConstructorDecl>(pCXXMethodDecl) || isa<CXXDestructorDecl>(pCXXMethodDecl) || isa<CXXConversionDecl>(pCXXMethodDecl)) {
81 return true;
83 if (isInUnoIncludeFile(pCXXMethodDecl)) {
84 return true;
86 if ( pCXXMethodDecl != pCXXMethodDecl->getCanonicalDecl() ) {
87 return true;
90 // the CppUnit stuff uses macros and methods that can't be changed
91 if (isDerivedFromTestFixture(pCXXMethodDecl->getParent())) {
92 return true;
94 // don't mess with the backwards compatibility stuff
95 if (loplugin::isSamePathname(getFilename(pCXXMethodDecl->getLocStart()), SRCDIR "/cppuhelper/source/compat.cxx")) {
96 return true;
98 // the DDE has a dummy implementation on Linux and a real one on Windows
99 auto aFilename = getFilename(pCXXMethodDecl->getCanonicalDecl()->getLocStart());
100 if (loplugin::isSamePathname(aFilename, SRCDIR "/include/svl/svdde.hxx")) {
101 return true;
103 auto cdc = loplugin::DeclCheck(pCXXMethodDecl->getParent());
104 // special case having something to do with static initialisation
105 // sal/osl/all/utility.cxx
106 if (cdc.Class("OGlobalTimer").Namespace("osl").GlobalNamespace()) {
107 return true;
109 // leave the TopLeft() method alone for consistency with the other "corner" methods
110 if (cdc.Class("BitmapInfoAccess").GlobalNamespace()) {
111 return true;
113 // in this case, the code is taking the address of the member function
114 // shell/source/unix/sysshell/recently_used_file_handler.cxx
115 if (cdc.Struct("recently_used_item").AnonymousNamespace().GlobalNamespace())
117 return true;
119 // the unotools and svl config code stuff is doing weird stuff with a reference-counted statically allocated pImpl class
120 if (loplugin::hasPathnamePrefix(aFilename, SRCDIR "/include/unotools/")) {
121 return true;
123 if (loplugin::hasPathnamePrefix(aFilename, SRCDIR "/include/svl/")) {
124 return true;
126 if (loplugin::hasPathnamePrefix(aFilename, SRCDIR "/include/framework/") || loplugin::hasPathnamePrefix(aFilename, SRCDIR "/framework/")) {
127 return true;
129 // there is some odd stuff happening here I don't fully understand, leave it for now
130 if (loplugin::hasPathnamePrefix(aFilename, SRCDIR "/include/canvas/") || loplugin::hasPathnamePrefix(aFilename, SRCDIR "/canvas/")) {
131 return true;
133 // classes that have static data and some kind of weird reference-counting trick in its constructor
134 if (cdc.Class("LinguOptions").GlobalNamespace()
135 || (cdc.Class("EditableExtendedColorConfig").Namespace("svtools")
136 .GlobalNamespace())
137 || (cdc.Class("ExtendedColorConfig").Namespace("svtools")
138 .GlobalNamespace())
139 || cdc.Class("SvtMiscOptions").GlobalNamespace()
140 || cdc.Class("SvtAccessibilityOptions").GlobalNamespace()
141 || cdc.Class("ColorConfig").Namespace("svtools").GlobalNamespace()
142 || cdc.Class("SvtOptionsDrawinglayer").GlobalNamespace()
143 || cdc.Class("SvtMenuOptions").GlobalNamespace()
144 || cdc.Class("SvtToolPanelOptions").GlobalNamespace()
145 || cdc.Class("SvtSlideSorterBarOptions").GlobalNamespace()
146 || (cdc.Class("SharedResources").Namespace("connectivity")
147 .GlobalNamespace())
148 || (cdc.Class("OParseContextClient").Namespace("svxform")
149 .GlobalNamespace())
150 || cdc.Class("OLimitedFormats").Namespace("frm").GlobalNamespace())
152 return true;
154 auto fdc = loplugin::DeclCheck(pCXXMethodDecl);
155 // only empty on Linux, not on windows
156 if ((fdc.Function("GetVisualRepresentationInNativeFormat_Impl")
157 .Class("OleEmbeddedObject").GlobalNamespace())
158 || (fdc.Function("GetRidOfComponent").Class("OleEmbeddedObject")
159 .GlobalNamespace())
160 || (fdc.Function("isProfileLocked").Class("ProfileAccess")
161 .Namespace("mozab").Namespace("connectivity").GlobalNamespace())
162 || cdc.Class("SbxDecimal").GlobalNamespace()
163 || fdc.Function("Call").Class("SbiDllMgr").GlobalNamespace()
164 || fdc.Function("FreeDll").Class("SbiDllMgr").GlobalNamespace()
165 || (fdc.Function("InitializeDde").Class("SfxApplication")
166 .GlobalNamespace())
167 || (fdc.Function("RemoveDdeTopic").Class("SfxApplication")
168 .GlobalNamespace())
169 || (fdc.Function("ReleaseData").Class("ScannerManager")
170 .GlobalNamespace()))
172 return true;
174 // debugging stuff
175 if (fdc.Function("dump").Class("InternalData").Namespace("chart")
176 .GlobalNamespace())
178 return true;
180 // used in a function-pointer-table
181 if ((cdc.Class("SbiRuntime").GlobalNamespace()
182 && startsWith(pCXXMethodDecl->getNameAsString(), "Step"))
183 || (cdc.Class("OoxFormulaParserImpl").Namespace("xls").Namespace("oox")
184 .GlobalNamespace())
185 || cdc.Class("SwTableFormula").GlobalNamespace()
186 || (cdc.Class("BiffFormulaParserImpl").Namespace("xls").Namespace("oox")
187 .GlobalNamespace())
188 || (fdc.Function("Read_F_Shape").Class("SwWW8ImplReader")
189 .GlobalNamespace())
190 || (fdc.Function("Read_Majority").Class("SwWW8ImplReader")
191 .GlobalNamespace())
192 || fdc.Function("Ignore").Class("SwWrtShell").GlobalNamespace())
194 return true;
196 // have no idea why this can't be static, but 'make check' fails with it so...
197 if (fdc.Function("resolveRelationshipsOfTypeFromOfficeDoc").Class("Shape")
198 .Namespace("drawingml").Namespace("oox").GlobalNamespace())
200 return true;
202 // template magic
203 if (fdc.Function("getValue").Class("ColumnBatch").GlobalNamespace()
204 || cdc.Class("TitleImpl").GlobalNamespace()
205 || (fdc.Function("getDefaultPropertyName").Class("DefaultReturnHelper")
206 .Namespace("vba").Namespace("ooo").GlobalNamespace()))
208 return true;
210 // depends on config options
211 if ((fdc.Function("autoInstallFontLangSupport").Class("PrintFontManager")
212 .Namespace("psp").GlobalNamespace())
213 || fdc.Function("AllocateFrame").Class("GtkSalFrame").GlobalNamespace()
214 || (fdc.Function("TriggerPaintEvent").Class("GtkSalFrame")
215 .GlobalNamespace()))
217 return true;
220 bVisitedThis = false;
221 TraverseStmt(pCXXMethodDecl->getBody());
222 if (bVisitedThis) {
223 return true;
226 report(
227 DiagnosticsEngine::Warning,
228 "this member function can be declared static",
229 pCXXMethodDecl->getCanonicalDecl()->getLocation())
230 << pCXXMethodDecl->getCanonicalDecl()->getSourceRange();
231 FunctionDecl const * def;
232 if (pCXXMethodDecl->isDefined(def)
233 && def != pCXXMethodDecl->getCanonicalDecl())
235 report(DiagnosticsEngine::Note, "defined here:", def->getLocation())
236 << def->getSourceRange();
238 return true;
241 loplugin::Plugin::Registration<StaticMethods> X("staticmethods");
245 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */