Version 7.6.3.2-android, tag libreoffice-7.6.3.2-android
[LibreOffice.git] / compilerplugins / clang / implinheritancehelper.cxx
blobfb24ed96f2dd6f35ca0b888da44b927fad1ba2bc
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 <string>
11 #include <iostream>
13 #include "check.hxx"
14 #include "plugin.hxx"
15 #include "config_clang.h"
16 #include "clang/AST/CXXInheritance.h"
18 /**
20 Look for places where we should be using ImplInheritanceHelper
24 namespace
26 class ImplInheritanceHelper : public loplugin::FilteringPlugin<ImplInheritanceHelper>
28 public:
29 explicit ImplInheritanceHelper(loplugin::InstantiationData const& data)
30 : FilteringPlugin(data)
34 virtual bool preRun() override { return true; }
36 virtual void run() override
38 if (preRun())
39 TraverseDecl(compiler.getASTContext().getTranslationUnitDecl());
42 bool VisitCXXRecordDecl(const CXXRecordDecl*);
45 bool ImplInheritanceHelper::VisitCXXRecordDecl(const CXXRecordDecl* cxxRecordDecl)
47 if (ignoreLocation(cxxRecordDecl))
48 return true;
49 if (!cxxRecordDecl->isThisDeclarationADefinition())
50 return true;
51 if (cxxRecordDecl->isDependentContext())
52 return true;
54 // ignore the utility template classes
55 SourceLocation spellingLocation
56 = compiler.getSourceManager().getSpellingLoc(cxxRecordDecl->getBeginLoc());
57 StringRef fileName = getFilenameOfLocation(spellingLocation);
58 if (loplugin::hasPathnamePrefix(fileName, SRCDIR "/include/cppu"))
59 return true;
60 if (loplugin::isSamePathname(fileName, SRCDIR "/include/comphelper/compbase.hxx"))
61 return true;
63 // not sure why this extends XPropertyState but does not support it in queryInterface.
64 if (loplugin::DeclCheck(cxxRecordDecl)
65 .Class("ChainablePropertySet")
66 .Namespace("comphelper")
67 .GlobalNamespace())
68 return true;
69 // in these cases the UNO interface is optional
70 if (loplugin::DeclCheck(cxxRecordDecl).Class("OFSInputStreamContainer").GlobalNamespace())
71 return true;
72 if (loplugin::DeclCheck(cxxRecordDecl)
73 .Class("OPropertyBrowserController")
74 .Namespace("pcr")
75 .GlobalNamespace())
76 return true;
78 // check if this class extends cppu::WeakImplHelper
79 if (!loplugin::isDerivedFrom(cxxRecordDecl, [](Decl const* decl) -> bool {
80 return bool(loplugin::DeclCheck(decl)
81 .Class("WeakImplHelper")
82 .Namespace("cppu")
83 .GlobalNamespace());
84 }))
85 return true;
86 // check if this class directly inherits from a UNO interface class
87 bool foundOne = false;
88 for (auto const& i : cxxRecordDecl->bases())
90 auto rt = i.getType()->getAs<RecordType>();
91 if (!rt)
92 continue;
93 auto const bd = cast<CXXRecordDecl>(rt->getDecl())->getDefinition();
94 auto ctx = bd->getDeclContext();
95 if (!ctx->isNamespace())
96 break;
97 auto ns = dyn_cast<NamespaceDecl>(ctx);
98 while (ns)
100 if (ns->getIdentifier() && ns->getName() == "star")
102 foundOne = true;
103 break;
105 ns = dyn_cast_or_null<NamespaceDecl>(ns->getParent());
108 if (!foundOne)
109 return true;
110 report(DiagnosticsEngine::Warning, "can probably use ImplInheritanceHelper here",
111 cxxRecordDecl->getLocation())
112 << cxxRecordDecl->getSourceRange();
113 return true;
116 loplugin::Plugin::Registration<ImplInheritanceHelper>
117 implinheritancehelper("implinheritancehelper");
120 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */