summaryrefslogtreecommitdiff
path: root/compilerplugins/clang/implinheritancehelper.cxx
blob: 1eb1db60a90bf027272058c6c8f8bd7ee724f263 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
 * This file is part of the LibreOffice project.
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 */

#include <string>
#include <iostream>

#include "check.hxx"
#include "plugin.hxx"
#include "config_clang.h"
#include "clang/AST/CXXInheritance.h"

/**

Look for places where we should be using ImplInheritanceHelper

*/

namespace
{
class ImplInheritanceHelper : public loplugin::FilteringPlugin<ImplInheritanceHelper>
{
public:
    explicit ImplInheritanceHelper(loplugin::InstantiationData const& data)
        : FilteringPlugin(data)
    {
    }

    virtual bool preRun() override { return true; }

    virtual void run() override
    {
        if (preRun())
            TraverseDecl(compiler.getASTContext().getTranslationUnitDecl());
    }

    bool VisitCXXRecordDecl(const CXXRecordDecl*);
};

bool ImplInheritanceHelper::VisitCXXRecordDecl(const CXXRecordDecl* cxxRecordDecl)
{
    if (ignoreLocation(cxxRecordDecl))
        return true;
    if (!cxxRecordDecl->isThisDeclarationADefinition())
        return true;
    if (cxxRecordDecl->isDependentContext())
        return true;

    // ignore the utility template classes
    SourceLocation spellingLocation
        = compiler.getSourceManager().getSpellingLoc(cxxRecordDecl->getBeginLoc());
    StringRef fileName = getFilenameOfLocation(spellingLocation);
    if (loplugin::hasPathnamePrefix(fileName, SRCDIR "/include/cppu"))
        return true;
    if (loplugin::isSamePathname(fileName, SRCDIR "/include/comphelper/compbase.hxx"))
        return true;

    // not sure why this extends XPropertyState but does not support it in queryInterface.
    if (loplugin::DeclCheck(cxxRecordDecl)
            .Class("ChainablePropertySet")
            .Namespace("comphelper")
            .GlobalNamespace())
        return true;
    // in these cases the UNO interface is optional
    if (loplugin::DeclCheck(cxxRecordDecl).Class("OFSInputStreamContainer").GlobalNamespace())
        return true;
    if (loplugin::DeclCheck(cxxRecordDecl)
            .Class("OPropertyBrowserController")
            .Namespace("pcr")
            .GlobalNamespace())
        return true;
    if (loplugin::DeclCheck(cxxRecordDecl).Class("SdDrawPage").GlobalNamespace())
        return true;
    if (loplugin::DeclCheck(cxxRecordDecl).Class("SdMasterPage").GlobalNamespace())
        return true;

    // check if this class extends cppu::WeakImplHelper
    if (!loplugin::isDerivedFrom(cxxRecordDecl, [](Decl const* decl) -> bool {
            return bool(loplugin::DeclCheck(decl)
                            .Class("WeakImplHelper")
                            .Namespace("cppu")
                            .GlobalNamespace());
        }))
        return true;
    // check if this class directly inherits from a UNO interface class
    bool foundOne = false;
    for (auto const& i : cxxRecordDecl->bases())
    {
        auto rt = i.getType()->getAs<RecordType>();
        if (!rt)
            continue;
        auto const bd = cast<CXXRecordDecl>(rt->getDecl())->getDefinition();
        auto ctx = bd->getDeclContext();
        if (!ctx->isNamespace())
            break;
        auto ns = dyn_cast<NamespaceDecl>(ctx);
        while (ns)
        {
            if (ns->getIdentifier() && ns->getName() == "star")
            {
                foundOne = true;
                break;
            }
            ns = dyn_cast_or_null<NamespaceDecl>(ns->getParent());
        }
    }
    if (!foundOne)
        return true;
    report(DiagnosticsEngine::Warning, "can probably use ImplInheritanceHelper here",
           cxxRecordDecl->getLocation())
        << cxxRecordDecl->getSourceRange();
    return true;
}

loplugin::Plugin::Registration<ImplInheritanceHelper>
    implinheritancehelper("implinheritancehelper");
}

/* vim:set shiftwidth=4 softtabstop=4 expandtab: */