tdf#161878 Ignore nested SYMBOL field inside IF field
[LibreOffice.git] / compilerplugins / clang / weakbase.cxx
blobf6f7c8db01be717ff7e7900cea9a03b111e0c34e
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 <string>
13 #include <iostream>
14 #include <map>
15 #include <set>
17 #include "plugin.hxx"
18 #include "check.hxx"
19 #include "clang/AST/CXXInheritance.h"
21 /**
22 * Check for multiple copies of WeakBase in base classes
24 namespace
26 class WeakBase : public loplugin::FilteringPlugin<WeakBase>
28 public:
29 explicit WeakBase(loplugin::InstantiationData const& data)
30 : FilteringPlugin(data)
34 bool preRun() override { return compiler.getLangOpts().CPlusPlus; }
36 void run() override
38 if (preRun())
40 TraverseDecl(compiler.getASTContext().getTranslationUnitDecl());
44 bool VisitCXXRecordDecl(CXXRecordDecl const*);
47 bool WeakBase::VisitCXXRecordDecl(CXXRecordDecl const* recordDecl)
49 if (ignoreLocation(recordDecl))
51 return true;
53 // StringRef aFileName = getFilenameOfLocation(
54 // compiler.getSourceManager().getSpellingLoc(fieldDecl->getBeginLoc()));
56 // if (loplugin::hasPathnamePrefix(aFileName, SRCDIR "/chart2/source/"))
57 // return true;
58 // if (loplugin::isSamePathname(aFileName, SRCDIR "/include/sfx2/recentdocsview.hxx"))
59 // return true;
60 // if (loplugin::isSamePathname(aFileName, SRCDIR "/include/sfx2/templatelocalview.hxx"))
61 // return true;
62 // if (loplugin::isSamePathname(aFileName, SRCDIR "/store/source/stortree.hxx")
63 // || loplugin::isSamePathname(aFileName, SRCDIR "/store/source/stordata.hxx"))
64 // return true;
65 // if (loplugin::isSamePathname(aFileName, SRCDIR "/sw/source/uibase/inc/dbtree.hxx"))
66 // return true;
68 recordDecl = recordDecl->getCanonicalDecl();
69 if (!recordDecl->hasDefinition())
70 return true;
72 int noWeakBases = 0;
73 int noWeakObjects = 0;
74 bool foundVirtualWeakBase = false;
75 bool foundVirtualOWeakObject = false;
76 std::string basePaths1;
77 std::string basePaths2;
78 auto BaseMatchesCallback = [&](const CXXBaseSpecifier* cxxBaseSpecifier, CXXBasePath& Paths) {
79 if (!cxxBaseSpecifier->getType().getTypePtr())
80 return false;
81 const CXXRecordDecl* baseCXXRecordDecl = cxxBaseSpecifier->getType()->getAsCXXRecordDecl();
82 if (!baseCXXRecordDecl)
83 return false;
84 if (baseCXXRecordDecl->isInvalidDecl())
85 return false;
86 bool isWeakBase(loplugin::DeclCheck(baseCXXRecordDecl)
87 .Struct("WeakBase")
88 .Namespace("tools")
89 .GlobalNamespace());
90 bool isOWeakObject(loplugin::DeclCheck(baseCXXRecordDecl)
91 .Class("OWeakObject")
92 .Namespace("cppu")
93 .GlobalNamespace());
94 if (isWeakBase)
96 if (cxxBaseSpecifier->isVirtual())
97 foundVirtualWeakBase = true;
98 else
99 ++noWeakBases;
101 else if (isOWeakObject)
103 if (cxxBaseSpecifier->isVirtual())
104 foundVirtualOWeakObject = true;
105 else
106 ++noWeakObjects;
108 else
109 return false;
110 std::string sPath;
111 for (CXXBasePathElement const& pathElement : Paths)
113 if (!sPath.empty())
115 sPath += "->";
117 if (pathElement.Class->hasDefinition())
118 sPath += pathElement.Class->getNameAsString();
119 else
120 sPath += "???";
122 sPath += "->";
123 sPath += baseCXXRecordDecl->getNameAsString();
124 if (isWeakBase)
126 if (!basePaths1.empty())
127 basePaths1 += ", ";
128 basePaths1 += sPath;
130 else
132 if (!basePaths2.empty())
133 basePaths2 += ", ";
134 basePaths2 += sPath;
136 return false;
139 CXXBasePaths aPaths;
140 recordDecl->lookupInBases(BaseMatchesCallback, aPaths);
142 if (foundVirtualWeakBase && noWeakBases > 0)
143 report(DiagnosticsEngine::Warning,
144 "found one virtual base and one or more normal bases of tools::WeakBase, through "
145 "inheritance paths %0",
146 recordDecl->getBeginLoc())
147 << basePaths1;
148 else if (!foundVirtualWeakBase && noWeakBases > 1)
149 report(DiagnosticsEngine::Warning,
150 "found multiple copies of tools::WeakBase, through inheritance paths %0",
151 recordDecl->getBeginLoc())
152 << basePaths1;
154 if (foundVirtualOWeakObject && noWeakObjects > 0)
155 report(DiagnosticsEngine::Warning,
156 "found one virtual base and one or more normal bases of cppu::OWeakObject, through "
157 "inheritance paths %0",
158 recordDecl->getBeginLoc())
159 << basePaths2;
160 else if (!foundVirtualOWeakObject && noWeakObjects > 1)
161 report(DiagnosticsEngine::Warning,
162 "found multiple copies of cppu::OWeakObject, through inheritance paths %0",
163 recordDecl->getBeginLoc())
164 << basePaths2;
166 return true;
169 loplugin::Plugin::Registration<WeakBase> weakbase("weakbase");
171 } // namespace
173 #endif // LO_CLANG_SHARED_PLUGINS
175 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */