cURL: follow redirects
[LibreOffice.git] / compilerplugins / clang / staticaccess.cxx
blob7fa1d392edb8306c3407fffdf661b81c0c5863af
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 RecursiveASTVisitor<StaticAccess>, public loplugin::Plugin
48 public:
49 explicit StaticAccess(InstantiationData const & data): Plugin(data) {}
51 void run() override
52 { TraverseDecl(compiler.getASTContext().getTranslationUnitDecl()); }
54 bool VisitMemberExpr(MemberExpr const * expr);
57 bool StaticAccess::VisitMemberExpr(MemberExpr const * expr) {
58 if (ignoreLocation(expr)) {
59 return true;
61 auto decl = expr->getMemberDecl();
62 bool me;
63 if (!isStatic(decl, &me)) {
64 return true;
66 report(
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"),
70 expr->getLocStart())
71 << me << decl->getQualifiedNameAsString() << expr->getSourceRange();
72 return true;
75 loplugin::Plugin::Registration<StaticAccess> X("staticaccess");
79 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */