1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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/.
16 bool isStatic(ValueDecl
const * decl
, bool * memberEnumerator
) {
17 assert(memberEnumerator
!= nullptr);
18 // clang::MemberExpr::getMemberDecl is documented to return either a
19 // FieldDecl or a CXXMethodDecl, but can apparently also return a VarDecl
20 // (as C++ static data members are modeled by VarDecl, not FieldDecl) or an
21 // EnumConstantDecl (struct { enum {E}; } s; s.E;), see
22 // <https://reviews.llvm.org/D23907> "Fix documentation of
23 // MemberExpr::getMemberDecl":
24 auto fd
= dyn_cast
<FieldDecl
>(decl
);
26 *memberEnumerator
= false;
29 auto vd
= dyn_cast
<VarDecl
>(decl
);
31 *memberEnumerator
= false;
32 assert(vd
->isStaticDataMember());
35 auto md
= dyn_cast
<CXXMethodDecl
>(decl
);
37 *memberEnumerator
= false;
38 return md
->isStatic();
40 assert(dyn_cast
<EnumConstantDecl
>(decl
) != nullptr);
41 *memberEnumerator
= true;
46 public RecursiveASTVisitor
<StaticAccess
>, public loplugin::Plugin
49 explicit StaticAccess(InstantiationData
const & data
): Plugin(data
) {}
52 { TraverseDecl(compiler
.getASTContext().getTranslationUnitDecl()); }
54 bool VisitMemberExpr(MemberExpr
const * expr
);
57 bool StaticAccess::VisitMemberExpr(MemberExpr
const * expr
) {
58 if (ignoreLocation(expr
)) {
61 auto decl
= expr
->getMemberDecl();
63 if (!isStatic(decl
, &me
)) {
67 DiagnosticsEngine::Warning
,
68 ("accessing %select{static class member|member enumerator}0 through"
69 " class member access syntax, use a qualified name like '%1' instead"),
71 << me
<< decl
->getQualifiedNameAsString() << expr
->getSourceRange();
75 loplugin::Plugin::Registration
<StaticAccess
> X("staticaccess");
79 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */