bump product version to 6.4.0.3
[LibreOffice.git] / compilerplugins / clang / staticaccess.cxx
blob9c1abbc5cba4af4caab6a1c3c4b6e3dc3f618d9c
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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/.
8 */
10 #ifndef LO_CLANG_SHARED_PLUGINS
12 #include <cassert>
14 #include "plugin.hxx"
16 namespace {
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);
27 if (fd != nullptr) {
28 *memberEnumerator = false;
29 return false;
31 auto vd = dyn_cast<VarDecl>(decl);
32 if (vd != nullptr) {
33 *memberEnumerator = false;
34 assert(vd->isStaticDataMember());
35 return true;
37 auto md = dyn_cast<CXXMethodDecl>(decl);
38 if (md != nullptr) {
39 *memberEnumerator = false;
40 return md->isStatic();
42 assert(dyn_cast<EnumConstantDecl>(decl) != nullptr);
43 *memberEnumerator = true;
44 return true;
47 class StaticAccess:
48 public loplugin::FilteringPlugin<StaticAccess>
50 public:
51 explicit StaticAccess(loplugin::InstantiationData const & data):
52 FilteringPlugin(data) {}
54 void run() override
55 { TraverseDecl(compiler.getASTContext().getTranslationUnitDecl()); }
57 bool VisitMemberExpr(MemberExpr const * expr);
60 bool StaticAccess::VisitMemberExpr(MemberExpr const * expr) {
61 if (ignoreLocation(expr)) {
62 return true;
64 auto decl = expr->getMemberDecl();
65 bool me;
66 if (!isStatic(decl, &me)) {
67 return true;
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 // ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
82 return true;
85 report(
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();
91 return true;
94 loplugin::Plugin::Registration<StaticAccess> staticaccess("staticaccess");
96 } // namespace
98 #endif // LO_CLANG_SHARED_PLUGINS
100 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */