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 loplugin::FilteringPlugin
<StaticAccess
>
49 explicit StaticAccess(loplugin::InstantiationData
const & data
):
50 FilteringPlugin(data
) {}
53 { TraverseDecl(compiler
.getASTContext().getTranslationUnitDecl()); }
55 bool VisitMemberExpr(MemberExpr
const * expr
);
58 bool StaticAccess::VisitMemberExpr(MemberExpr
const * expr
) {
59 if (ignoreLocation(expr
)) {
62 auto decl
= expr
->getMemberDecl();
64 if (!isStatic(decl
, &me
)) {
67 auto const loc
= expr
->getExprLoc();
68 if (compiler
.getSourceManager().isMacroBodyExpansion(loc
)) {
69 auto const name
= Lexer::getImmediateMacroName(
70 loc
, compiler
.getSourceManager(), compiler
.getLangOpts());
71 if (name
== "BEGIN_COM_MAP" || name
== "DEFAULT_REFLECTION_HANDLER") {
72 // .../VC/Tools/MSVC/14.14.26428/atlmfc/include\atlcom.h(2226,10): note: expanded from
73 // macro 'BEGIN_COM_MAP'
74 // return this->InternalQueryInterface(this, _GetEntries(), iid, ppvObject);
75 // ^~~~~~~~~~~~~~~~~~~~~~~~~~~~
76 // .../VC/Tools/MSVC/14.14.26428/atlmfc/include\atlwin.h(2890,5): note: expanded from
77 // macro 'DEFAULT_REFLECTION_HANDLER'
78 // if(this->DefaultReflectionHandler(hWnd, uMsg, wParam, lParam, lResult))
79 // ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
84 DiagnosticsEngine::Warning
,
85 ("accessing %select{static class member|member enumerator}0 through"
86 " class member access syntax, use a qualified name like '%1' instead"),
87 compat::getBeginLoc(expr
))
88 << me
<< decl
->getQualifiedNameAsString() << expr
->getSourceRange();
92 loplugin::Plugin::Registration
<StaticAccess
> X("staticaccess");
96 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */