11 #include "clang/AST/ASTContext.h" 12 #include "clang/ASTMatchers/ASTMatchFinder.h" 18 namespace readability {
21 if (
const ElaboratedType *ElType = QType->getAs<ElaboratedType>()) {
22 const NestedNameSpecifier *NestedSpecifiers = ElType->getQualifier();
23 unsigned NameSpecifierNestingLevel = 1;
25 NameSpecifierNestingLevel++;
26 NestedSpecifiers = NestedSpecifiers->getPrefix();
27 }
while (NestedSpecifiers);
29 return NameSpecifierNestingLevel;
34 void StaticAccessedThroughInstanceCheck::storeOptions(
36 Options.store(Opts,
"NameSpecifierNestingThreshold",
37 NameSpecifierNestingThreshold);
40 void StaticAccessedThroughInstanceCheck::registerMatchers(MatchFinder *Finder) {
42 memberExpr(hasDeclaration(anyOf(cxxMethodDecl(isStaticStorageClass()),
43 varDecl(hasStaticStorageDuration()))),
44 unless(isInTemplateInstantiation()))
45 .bind(
"memberExpression"),
49 void StaticAccessedThroughInstanceCheck::check(
50 const MatchFinder::MatchResult &
Result) {
51 const auto *MemberExpression =
52 Result.Nodes.getNodeAs<MemberExpr>(
"memberExpression");
54 if (MemberExpression->getBeginLoc().isMacroID())
57 const Expr *BaseExpr = MemberExpression->getBase();
60 if (isa<CXXOperatorCallExpr>(BaseExpr))
64 BaseExpr->getType()->isPointerType()
65 ? BaseExpr->getType()->getPointeeType().getUnqualifiedType()
66 : BaseExpr->getType().getUnqualifiedType();
68 const ASTContext *AstContext = Result.Context;
69 PrintingPolicy PrintingPolicyWithSupressedTag(AstContext->getLangOpts());
70 PrintingPolicyWithSupressedTag.SuppressTagKeyword =
true;
71 std::string BaseTypeName =
72 BaseType.getAsString(PrintingPolicyWithSupressedTag);
74 SourceLocation MemberExprStartLoc = MemberExpression->getBeginLoc();
76 diag(MemberExprStartLoc,
"static member accessed through instance");
78 if (BaseExpr->HasSideEffects(*AstContext) ||
82 Diag << FixItHint::CreateReplacement(
83 CharSourceRange::getCharRange(MemberExprStartLoc,
84 MemberExpression->getMemberLoc()),
std::map< std::string, std::string > OptionMap
llvm::Optional< llvm::Expected< tooling::AtomicChanges > > Result
static unsigned getNameSpecifierNestingLevel(const QualType &QType)
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//