Debian Codegraph @

llvm-project/clang-tools-extra/clang-tidy/readability/StaticAccessedThroughInstanceCheck.cpp

raw
1 //===--- StaticAccessedThroughInstanceCheck.cpp - clang-tidy---------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8
9 #include "StaticAccessedThroughInstanceCheck.h"
10 #include "clang/AST/ASTContext.h"
11 #include "clang/ASTMatchers/ASTMatchFinder.h"
12 #include "llvm/ADT/StringRef.h"
13
14 using namespace clang::ast_matchers;
15
16 namespace clang::tidy::readability {
17
18 static unsigned getNameSpecifierNestingLevel(const QualType &QType) {
19 if (const ElaboratedType *ElType = QType->getAs<ElaboratedType>()) {
20 if (const NestedNameSpecifier *NestedSpecifiers = ElType->getQualifier()) {
21 unsigned NameSpecifierNestingLevel = 1;
22 do {
23 NameSpecifierNestingLevel++;
24 NestedSpecifiers = NestedSpecifiers->getPrefix();
25 } while (NestedSpecifiers);
26
27 return NameSpecifierNestingLevel;
28 }
29 }
30 return 0;
31 }
32
33 void StaticAccessedThroughInstanceCheck::storeOptions(
34 ClangTidyOptions::OptionMap &Opts) {
35 Options.store(Opts, "NameSpecifierNestingThreshold",
36 NameSpecifierNestingThreshold);
37 }
38
39 void StaticAccessedThroughInstanceCheck::registerMatchers(MatchFinder *Finder) {
40 Finder->addMatcher(
41 memberExpr(hasDeclaration(anyOf(cxxMethodDecl(isStaticStorageClass()),
42 varDecl(hasStaticStorageDuration()))))
43 .bind("memberExpression"),
44 this);
45 }
46
47 void StaticAccessedThroughInstanceCheck::check(
48 const MatchFinder::MatchResult &Result) {
49 const auto *MemberExpression =
50 Result.Nodes.getNodeAs<MemberExpr>("memberExpression");
51
52 if (MemberExpression->getBeginLoc().isMacroID())
53 return;
54
55 const Expr *BaseExpr = MemberExpression->getBase();
56
57 // Do not warn for overloaded -> operators.
58 if (isa<CXXOperatorCallExpr>(BaseExpr))
59 return;
60
61 QualType BaseType =
62 BaseExpr->getType()->isPointerType()
63 ? BaseExpr->getType()->getPointeeType().getUnqualifiedType()
64 : BaseExpr->getType().getUnqualifiedType();
65
66 const ASTContext *AstContext = Result.Context;
67 PrintingPolicy PrintingPolicyWithSupressedTag(AstContext->getLangOpts());
68 PrintingPolicyWithSupressedTag.SuppressTagKeyword = true;
69 PrintingPolicyWithSupressedTag.SuppressUnwrittenScope = true;
70
71 PrintingPolicyWithSupressedTag.PrintCanonicalTypes =
72 !BaseExpr->getType()->isTypedefNameType();
73
74 std::string BaseTypeName =
75 BaseType.getAsString(PrintingPolicyWithSupressedTag);
76
77 // Do not warn for CUDA built-in variables.
78 if (StringRef(BaseTypeName).startswith("__cuda_builtin_"))
79 return;
80
81 SourceLocation MemberExprStartLoc = MemberExpression->getBeginLoc();
82 auto Diag =
83 diag(MemberExprStartLoc, "static member accessed through instance");
84
85 if (BaseExpr->HasSideEffects(*AstContext) ||
86 getNameSpecifierNestingLevel(BaseType) > NameSpecifierNestingThreshold)
87 return;
88
89 Diag << FixItHint::CreateReplacement(
90 CharSourceRange::getCharRange(MemberExprStartLoc,
91 MemberExpression->getMemberLoc()),
92 BaseTypeName + "::");
93 }
94
95 } // namespace clang::tidy::readability