summaryrefslogtreecommitdiff
path: root/compilerplugins/clang/staticaccess.cxx
blob: 46aa033f0bf23dcbe820d61457acf3128a0893bc (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
/* -*- 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/.
 */

#ifndef LO_CLANG_SHARED_PLUGINS

#include <cassert>

#include "plugin.hxx"

namespace {

bool isStatic(ValueDecl const * decl, bool * memberEnumerator) {
    assert(memberEnumerator != nullptr);
    // clang::MemberExpr::getMemberDecl is documented to return either a
    // FieldDecl or a CXXMethodDecl, but can apparently also return a VarDecl
    // (as C++ static data members are modeled by VarDecl, not FieldDecl) or an
    // EnumConstantDecl (struct { enum {E}; } s; s.E;), see
    // <https://reviews.llvm.org/D23907> "Fix documentation of
    // MemberExpr::getMemberDecl":
    auto fd = dyn_cast<FieldDecl>(decl);
    if (fd != nullptr) {
        *memberEnumerator = false;
        return false;
    }
    auto vd = dyn_cast<VarDecl>(decl);
    if (vd != nullptr) {
        *memberEnumerator = false;
        assert(vd->isStaticDataMember());
        return true;
    }
    auto md = dyn_cast<CXXMethodDecl>(decl);
    if (md != nullptr) {
        *memberEnumerator = false;
        return md->isStatic();
    }
    assert(dyn_cast<EnumConstantDecl>(decl) != nullptr);
    *memberEnumerator = true;
    return true;
}

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

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

    bool VisitMemberExpr(MemberExpr const * expr);
};

bool StaticAccess::VisitMemberExpr(MemberExpr const * expr) {
    if (ignoreLocation(expr)) {
        return true;
    }
    auto decl = expr->getMemberDecl();
    bool me;
    if (!isStatic(decl, &me)) {
        return true;
    }
    auto const loc = expr->getExprLoc();
    if (compiler.getSourceManager().isMacroBodyExpansion(loc)) {
        auto const name = Lexer::getImmediateMacroName(
            loc, compiler.getSourceManager(), compiler.getLangOpts());
        if (name == "BEGIN_COM_MAP" || name == "DEFAULT_REFLECTION_HANDLER") {
            // .../VC/Tools/MSVC/14.14.26428/atlmfc/include\atlcom.h(2226,10):  note: expanded from
            //   macro 'BEGIN_COM_MAP'
            //     return this->InternalQueryInterface(this, _GetEntries(), iid, ppvObject);
            //            ^~~~~~~~~~~~~~~~~~~~~~~~~~~~
            // .../VC/Tools/MSVC/14.14.26428/atlmfc/include\atlwin.h(2890,5):  note: expanded from
            //   macro 'DEFAULT_REFLECTION_HANDLER'
            //     if(this->DefaultReflectionHandler(hWnd, uMsg, wParam, lParam, lResult))
            //        ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
            return true;
        }
    }
    report(
        DiagnosticsEngine::Warning,
        ("accessing %select{static class member|member enumerator}0 through"
         " class member access syntax, use a qualified name like '%1' instead"),
        expr->getBeginLoc())
        << me << decl->getQualifiedNameAsString() << expr->getSourceRange();
    return true;
}

loplugin::Plugin::Registration<StaticAccess> staticaccess("staticaccess");

} // namespace

#endif // LO_CLANG_SHARED_PLUGINS

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