10 #include "clang/AST/ASTContext.h" 11 #include "clang/ASTMatchers/ASTMatchFinder.h" 17 namespace readability {
20 if (
const ElaboratedType *ElType = QType->getAs<ElaboratedType>()) {
21 const NestedNameSpecifier *NestedSpecifiers = ElType->getQualifier();
22 unsigned NameSpecifierNestingLevel = 1;
24 NameSpecifierNestingLevel++;
25 NestedSpecifiers = NestedSpecifiers->getPrefix();
26 }
while (NestedSpecifiers);
28 return NameSpecifierNestingLevel;
33 void StaticAccessedThroughInstanceCheck::storeOptions(
35 Options.store(Opts,
"NameSpecifierNestingThreshold",
36 NameSpecifierNestingThreshold);
39 void StaticAccessedThroughInstanceCheck::registerMatchers(MatchFinder *Finder) {
41 memberExpr(hasDeclaration(anyOf(cxxMethodDecl(isStaticStorageClass()),
42 varDecl(hasStaticStorageDuration()))),
43 unless(isInTemplateInstantiation()))
44 .bind(
"memberExpression"),
48 void StaticAccessedThroughInstanceCheck::check(
49 const MatchFinder::MatchResult &Result) {
50 const auto *MemberExpression =
51 Result.Nodes.getNodeAs<MemberExpr>(
"memberExpression");
53 if (MemberExpression->getBeginLoc().isMacroID())
56 const Expr *BaseExpr = MemberExpression->getBase();
59 if (isa<CXXOperatorCallExpr>(BaseExpr))
63 BaseExpr->getType()->isPointerType()
64 ? BaseExpr->getType()->getPointeeType().getUnqualifiedType()
65 : BaseExpr->getType().getUnqualifiedType();
67 const ASTContext *AstContext = Result.Context;
68 PrintingPolicy PrintingPolicyWithSupressedTag(AstContext->getLangOpts());
69 PrintingPolicyWithSupressedTag.SuppressTagKeyword =
true;
70 PrintingPolicyWithSupressedTag.SuppressUnwrittenScope =
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
static unsigned getNameSpecifierNestingLevel(const QualType &QType)
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//