summaryrefslogtreecommitdiff
path: root/compilerplugins/clang/mustoverride.cxx
blob: bd1fee6298700784a07df1547bd3bc91af33bf3c (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
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; fill-column: 100 -*- */
/*
 * 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/.
 */

#ifndef LO_CLANG_SHARED_PLUGINS

// Find classes deriving from other classes with virtual member functions annotated with
// SAL_LOPLUGIN_ANNOTATE("mustoverride"), but where the derived class fails to directly override
// them.

#include <set>

#include "plugin.hxx"

namespace
{
void findMustOverride(CXXRecordDecl const* decl, std::set<CXXMethodDecl const*>* mustOverride)
{
    for (auto const& base : decl->bases())
    {
        auto const d = base.getType()->getAsCXXRecordDecl();
        if (d == nullptr)
        {
            continue;
        }
        findMustOverride(d, mustOverride);
        for (auto const meth : d->methods())
        {
            if (!meth->isVirtual())
            {
                continue;
            }
            for (auto const attr : meth->specific_attrs<AnnotateAttr>())
            {
                if (attr->getAnnotation() == "loplugin:mustoverride")
                {
                    mustOverride->insert(meth);
                    break;
                }
            }
        }
    }
}

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

    bool VisitCXXRecordDecl(CXXRecordDecl const* decl)
    {
        if (ignoreLocation(decl))
        {
            return true;
        }
        if (!decl->isThisDeclarationADefinition())
        {
            return true;
        }
        if (!decl->isPolymorphic())
        {
            return true;
        }
        std::set<CXXMethodDecl const*> mustOverride;
        findMustOverride(decl, &mustOverride);
        for (auto const meth : mustOverride)
        {
            if (meth->getCorrespondingMethodDeclaredInClass(decl) != nullptr)
            {
                continue;
            }
            report(DiagnosticsEngine::Warning, "%0 does not override %1 from %2",
                   decl->getLocation())
                << decl << meth << meth->getParent() << decl->getSourceRange();
            report(DiagnosticsEngine::Note, "declared here", meth->getLocation())
                << meth->getSourceRange();
        }
        return true;
    }

private:
    void run() override
    {
        if (compiler.getLangOpts().CPlusPlus)
        {
            TraverseDecl(compiler.getASTContext().getTranslationUnitDecl());
        }
    }
};

loplugin::Plugin::Registration<MustOverride> mustoverride("mustoverride");
}

#endif

/* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */