1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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/.
10 #ifndef LO_CLANG_SHARED_PLUGINS
19 #include "clang/AST/CXXInheritance.h"
22 * Check for multiple copies of WeakBase in base classes
26 class WeakBase
: public loplugin::FilteringPlugin
<WeakBase
>
29 explicit WeakBase(loplugin::InstantiationData
const& data
)
30 : FilteringPlugin(data
)
34 bool preRun() override
{ return compiler
.getLangOpts().CPlusPlus
; }
40 TraverseDecl(compiler
.getASTContext().getTranslationUnitDecl());
44 bool VisitCXXRecordDecl(CXXRecordDecl
const*);
47 bool WeakBase::VisitCXXRecordDecl(CXXRecordDecl
const* recordDecl
)
49 if (ignoreLocation(recordDecl
))
53 // StringRef aFileName = getFilenameOfLocation(
54 // compiler.getSourceManager().getSpellingLoc(fieldDecl->getBeginLoc()));
56 // if (loplugin::hasPathnamePrefix(aFileName, SRCDIR "/chart2/source/"))
58 // if (loplugin::isSamePathname(aFileName, SRCDIR "/include/sfx2/recentdocsview.hxx"))
60 // if (loplugin::isSamePathname(aFileName, SRCDIR "/include/sfx2/templatelocalview.hxx"))
62 // if (loplugin::isSamePathname(aFileName, SRCDIR "/store/source/stortree.hxx")
63 // || loplugin::isSamePathname(aFileName, SRCDIR "/store/source/stordata.hxx"))
65 // if (loplugin::isSamePathname(aFileName, SRCDIR "/sw/source/uibase/inc/dbtree.hxx"))
68 recordDecl
= recordDecl
->getCanonicalDecl();
69 if (!recordDecl
->hasDefinition())
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())
81 const CXXRecordDecl
* baseCXXRecordDecl
= cxxBaseSpecifier
->getType()->getAsCXXRecordDecl();
82 if (!baseCXXRecordDecl
)
84 if (baseCXXRecordDecl
->isInvalidDecl())
86 bool isWeakBase(loplugin::DeclCheck(baseCXXRecordDecl
)
90 bool isOWeakObject(loplugin::DeclCheck(baseCXXRecordDecl
)
96 if (cxxBaseSpecifier
->isVirtual())
97 foundVirtualWeakBase
= true;
101 else if (isOWeakObject
)
103 if (cxxBaseSpecifier
->isVirtual())
104 foundVirtualOWeakObject
= true;
111 for (CXXBasePathElement
const& pathElement
: Paths
)
117 if (pathElement
.Class
->hasDefinition())
118 sPath
+= pathElement
.Class
->getNameAsString();
123 sPath
+= baseCXXRecordDecl
->getNameAsString();
126 if (!basePaths1
.empty())
132 if (!basePaths2
.empty())
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())
148 else if (!foundVirtualWeakBase
&& noWeakBases
> 1)
149 report(DiagnosticsEngine::Warning
,
150 "found multiple copies of tools::WeakBase, through inheritance paths %0",
151 recordDecl
->getBeginLoc())
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())
160 else if (!foundVirtualOWeakObject
&& noWeakObjects
> 1)
161 report(DiagnosticsEngine::Warning
,
162 "found multiple copies of cppu::OWeakObject, through inheritance paths %0",
163 recordDecl
->getBeginLoc())
169 loplugin::Plugin::Registration
<WeakBase
> weakbase("weakbase");
173 #endif // LO_CLANG_SHARED_PLUGINS
175 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */