bump product version to 6.3.0.0.beta1
[LibreOffice.git] / compilerplugins / clang / staticaccess.cxx
blob6b15bfb153495f2e2167a1e4e9481dcf696e9267
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 #include <cassert>
12 #include "plugin.hxx"
14 namespace {
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);
25 if (fd != nullptr) {
26 *memberEnumerator = false;
27 return false;
29 auto vd = dyn_cast<VarDecl>(decl);
30 if (vd != nullptr) {
31 *memberEnumerator = false;
32 assert(vd->isStaticDataMember());
33 return true;
35 auto md = dyn_cast<CXXMethodDecl>(decl);
36 if (md != nullptr) {
37 *memberEnumerator = false;
38 return md->isStatic();
40 assert(dyn_cast<EnumConstantDecl>(decl) != nullptr);
41 *memberEnumerator = true;
42 return true;
45 class StaticAccess:
46 public loplugin::FilteringPlugin<StaticAccess>
48 public:
49 explicit StaticAccess(loplugin::InstantiationData const & data):
50 FilteringPlugin(data) {}
52 void run() override
53 { TraverseDecl(compiler.getASTContext().getTranslationUnitDecl()); }
55 bool VisitMemberExpr(MemberExpr const * expr);
58 bool StaticAccess::VisitMemberExpr(MemberExpr const * expr) {
59 if (ignoreLocation(expr)) {
60 return true;
62 auto decl = expr->getMemberDecl();
63 bool me;
64 if (!isStatic(decl, &me)) {
65 return true;
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 // ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
80 return true;
83 report(
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();
89 return true;
92 loplugin::Plugin::Registration<StaticAccess> X("staticaccess");
96 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */