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/.
10 #ifndef LO_CLANG_SHARED_PLUGINS
18 bool isStatic(ValueDecl
const * decl
, bool * memberEnumerator
) {
19 assert(memberEnumerator
!= nullptr);
20 // clang::MemberExpr::getMemberDecl is documented to return either a
21 // FieldDecl or a CXXMethodDecl, but can apparently also return a VarDecl
22 // (as C++ static data members are modeled by VarDecl, not FieldDecl) or an
23 // EnumConstantDecl (struct { enum {E}; } s; s.E;), see
24 // <https://reviews.llvm.org/D23907> "Fix documentation of
25 // MemberExpr::getMemberDecl":
26 auto fd
= dyn_cast
<FieldDecl
>(decl
);
28 *memberEnumerator
= false;
31 auto vd
= dyn_cast
<VarDecl
>(decl
);
33 *memberEnumerator
= false;
34 assert(vd
->isStaticDataMember());
37 auto md
= dyn_cast
<CXXMethodDecl
>(decl
);
39 *memberEnumerator
= false;
40 return md
->isStatic();
42 assert(dyn_cast
<EnumConstantDecl
>(decl
) != nullptr);
43 *memberEnumerator
= true;
48 public loplugin::FilteringPlugin
<StaticAccess
>
51 explicit StaticAccess(loplugin::InstantiationData
const & data
):
52 FilteringPlugin(data
) {}
55 { TraverseDecl(compiler
.getASTContext().getTranslationUnitDecl()); }
57 bool VisitMemberExpr(MemberExpr
const * expr
);
60 bool StaticAccess::VisitMemberExpr(MemberExpr
const * expr
) {
61 if (ignoreLocation(expr
)) {
64 auto decl
= expr
->getMemberDecl();
66 if (!isStatic(decl
, &me
)) {
69 auto const loc
= expr
->getExprLoc();
70 if (compiler
.getSourceManager().isMacroBodyExpansion(loc
)) {
71 auto const name
= Lexer::getImmediateMacroName(
72 loc
, compiler
.getSourceManager(), compiler
.getLangOpts());
73 if (name
== "BEGIN_COM_MAP" || name
== "DEFAULT_REFLECTION_HANDLER") {
74 // .../VC/Tools/MSVC/14.14.26428/atlmfc/include\atlcom.h(2226,10): note: expanded from
75 // macro 'BEGIN_COM_MAP'
76 // return this->InternalQueryInterface(this, _GetEntries(), iid, ppvObject);
77 // ^~~~~~~~~~~~~~~~~~~~~~~~~~~~
78 // .../VC/Tools/MSVC/14.14.26428/atlmfc/include\atlwin.h(2890,5): note: expanded from
79 // macro 'DEFAULT_REFLECTION_HANDLER'
80 // if(this->DefaultReflectionHandler(hWnd, uMsg, wParam, lParam, lResult))
81 // ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
86 DiagnosticsEngine::Warning
,
87 ("accessing %select{static class member|member enumerator}0 through"
88 " class member access syntax, use a qualified name like '%1' instead"),
89 compat::getBeginLoc(expr
))
90 << me
<< decl
->getQualifiedNameAsString() << expr
->getSourceRange();
94 loplugin::Plugin::Registration
<StaticAccess
> staticaccess("staticaccess");
98 #endif // LO_CLANG_SHARED_PLUGINS
100 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */