Version 7.6.3.2-android, tag libreoffice-7.6.3.2-android
[LibreOffice.git] / compilerplugins / clang / weakbase.cxx
blob666444ff7ffbecb65d1ff6d79cdae06bd554ec09
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 "clang/AST/CXXInheritance.h"
20 /**
21 * Check for multiple copies of WeakBase in base classes
23 namespace
25 class WeakBase : public loplugin::FilteringPlugin<WeakBase>
27 public:
28 explicit WeakBase(loplugin::InstantiationData const& data)
29 : FilteringPlugin(data)
33 bool preRun() override { return compiler.getLangOpts().CPlusPlus; }
35 void run() override
37 if (preRun())
39 TraverseDecl(compiler.getASTContext().getTranslationUnitDecl());
43 bool VisitCXXRecordDecl(CXXRecordDecl const*);
46 bool WeakBase::VisitCXXRecordDecl(CXXRecordDecl const* recordDecl)
48 if (ignoreLocation(recordDecl))
50 return true;
52 // StringRef aFileName = getFilenameOfLocation(
53 // compiler.getSourceManager().getSpellingLoc(fieldDecl->getBeginLoc()));
55 // if (loplugin::hasPathnamePrefix(aFileName, SRCDIR "/chart2/source/"))
56 // return true;
57 // if (loplugin::isSamePathname(aFileName, SRCDIR "/include/sfx2/recentdocsview.hxx"))
58 // return true;
59 // if (loplugin::isSamePathname(aFileName, SRCDIR "/include/sfx2/templatelocalview.hxx"))
60 // return true;
61 // if (loplugin::isSamePathname(aFileName, SRCDIR "/store/source/stortree.hxx")
62 // || loplugin::isSamePathname(aFileName, SRCDIR "/store/source/stordata.hxx"))
63 // return true;
64 // if (loplugin::isSamePathname(aFileName, SRCDIR "/sw/source/uibase/inc/dbtree.hxx"))
65 // return true;
67 recordDecl = recordDecl->getCanonicalDecl();
68 if (!recordDecl->hasDefinition())
69 return true;
71 int noWeakBases = 0;
72 std::string basePaths;
73 auto BaseMatchesCallback = [&](const CXXBaseSpecifier* cxxBaseSpecifier, CXXBasePath& Paths) {
74 if (!cxxBaseSpecifier->getType().getTypePtr())
75 return false;
76 const CXXRecordDecl* baseCXXRecordDecl = cxxBaseSpecifier->getType()->getAsCXXRecordDecl();
77 if (!baseCXXRecordDecl)
78 return false;
79 if (baseCXXRecordDecl->isInvalidDecl())
80 return false;
81 if (baseCXXRecordDecl->getName() != "WeakBase")
82 return false;
83 ++noWeakBases;
84 std::string sPath;
85 for (CXXBasePathElement const& pathElement : Paths)
87 if (!sPath.empty())
89 sPath += "->";
91 if (pathElement.Class->hasDefinition())
92 sPath += pathElement.Class->getNameAsString();
93 else
94 sPath += "???";
96 sPath += "->";
97 sPath += baseCXXRecordDecl->getNameAsString();
98 if (!basePaths.empty())
99 basePaths += ", ";
100 basePaths += sPath;
101 return false;
104 CXXBasePaths aPaths;
105 recordDecl->lookupInBases(BaseMatchesCallback, aPaths);
107 if (noWeakBases > 1)
109 report(DiagnosticsEngine::Warning,
110 "multiple copies of WeakBase, through inheritance paths %0",
111 recordDecl->getBeginLoc())
112 << basePaths << recordDecl->getSourceRange();
114 return true;
117 loplugin::Plugin::Registration<WeakBase> weakbase("weakbase");
119 } // namespace
121 #endif // LO_CLANG_SHARED_PLUGINS
123 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */