bump product version to 5.0.4.1
[LibreOffice.git] / compilerplugins / clang / store / derivedclass.cxx
blob95f0296b45379c9f9a5609124e20c35c5499fb8e
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 "plugin.hxx"
12 namespace {
14 class DerivedClass:
15 public RecursiveASTVisitor<DerivedClass>,
16 public loplugin::Plugin
18 public:
19 explicit DerivedClass(InstantiationData const & data):
20 Plugin(data) {}
22 virtual void run() override
23 { TraverseDecl(compiler.getASTContext().getTranslationUnitDecl()); }
25 bool VisitCXXRecordDecl(CXXRecordDecl const * decl);
28 bool BaseCheck(const CXXRecordDecl *BaseDefinition, void *BaseClassName) {
29 // print warning about deriving from this classes
30 // the name has to contain namespace, e.g. foo::bar::ClassName
31 const char *BaseClasses[] = {
32 "Dialog",
33 "ProgressBar",
34 "SfxToolBoxControl",
35 "StatusBar",
38 for (int i = 0; BaseClasses[i]; i++)
39 if (BaseDefinition->getQualifiedNameAsString().compare(BaseClasses[i]) == 0) {
40 *(const char **)BaseClassName = BaseClasses[i];
41 return false;
43 return true;
46 bool DerivedClass::VisitCXXRecordDecl(CXXRecordDecl const * decl) {
47 const char *BaseClassName = 0;
48 // checking for decl->hasDefinition() avoids crash in decl->forallBases
49 if (decl->hasDefinition() &&
50 // not sure what hasAnyDependentBases() does,
51 // but it avoids classes we don't want, e.g. WeakAggComponentImplHelper1
52 !decl->hasAnyDependentBases() &&
53 !decl->forallBases(BaseCheck, &BaseClassName)) {
54 string warning_msg("class %0 derives from ");
55 // no idea how BaseClassName can be 0 sometimes..
56 if (BaseClassName)
57 warning_msg += BaseClassName;
58 report(
59 DiagnosticsEngine::Warning,
60 warning_msg,
61 decl->getLocStart())
62 << decl->getQualifiedNameAsString() << decl->getSourceRange();
64 return true;
67 loplugin::Plugin::Registration<DerivedClass> X("derivedclass");
71 /* vim:set shiftwidth=4 softtabstop=4 tabstop=4 expandtab: */