10#include "clang/AST/ASTContext.h"
11#include "clang/ASTMatchers/ASTMatchFinder.h"
12#include "llvm/ADT/StringRef.h"
23 if (
const auto *ElType = QType->getAs<ElaboratedType>()) {
24 if (
const NestedNameSpecifier *NestedSpecifiers = ElType->getQualifier()) {
25 unsigned NameSpecifierNestingLevel = 1;
27 NameSpecifierNestingLevel++;
28 NestedSpecifiers = NestedSpecifiers->getPrefix();
29 }
while (NestedSpecifiers);
31 return NameSpecifierNestingLevel;
40 NameSpecifierNestingThreshold);
45 memberExpr(hasDeclaration(anyOf(cxxMethodDecl(isStatic()),
46 varDecl(hasStaticStorageDuration()),
48 .bind(
"memberExpression"),
53 const MatchFinder::MatchResult &Result) {
54 const auto *MemberExpression =
55 Result.Nodes.getNodeAs<MemberExpr>(
"memberExpression");
57 if (MemberExpression->getBeginLoc().isMacroID())
60 const Expr *BaseExpr = MemberExpression->getBase();
62 const QualType BaseType =
63 BaseExpr->getType()->isPointerType()
64 ? BaseExpr->getType()->getPointeeType().getUnqualifiedType()
65 : BaseExpr->getType().getUnqualifiedType();
67 const ASTContext *AstContext = Result.Context;
68 PrintingPolicy PrintingPolicyWithSuppressedTag(AstContext->getLangOpts());
69 PrintingPolicyWithSuppressedTag.SuppressTagKeyword =
true;
70 PrintingPolicyWithSuppressedTag.SuppressUnwrittenScope =
true;
72 PrintingPolicyWithSuppressedTag.PrintCanonicalTypes =
73 !BaseExpr->getType()->isTypedefNameType();
75 std::string BaseTypeName =
76 BaseType.getAsString(PrintingPolicyWithSuppressedTag);
79 const RecordDecl *RecDecl = BaseType->getAsCXXRecordDecl();
80 if (!RecDecl || RecDecl->getIdentifier() ==
nullptr)
84 if (StringRef(BaseTypeName).starts_with(
"__cuda_builtin_"))
87 SourceLocation MemberExprStartLoc = MemberExpression->getBeginLoc();
88 auto CreateFix = [&] {
89 return FixItHint::CreateReplacement(
90 CharSourceRange::getCharRange(MemberExprStartLoc,
91 MemberExpression->getMemberLoc()),
97 diag(MemberExprStartLoc,
"static member accessed through instance");
102 if (!BaseExpr->HasSideEffects(*AstContext,
109 diag(MemberExprStartLoc,
"member base expression may carry some side effects",
110 DiagnosticIDs::Level::Note)
111 << BaseExpr->getSourceRange() << CreateFix();
::clang::DynTypedNode Node
void store(ClangTidyOptions::OptionMap &Options, StringRef LocalName, StringRef Value) const
Stores an option with the check-local name LocalName with string value Value to Options.
DiagnosticBuilder diag(SourceLocation Loc, StringRef Description, DiagnosticIDs::Level Level=DiagnosticIDs::Warning)
Add a diagnostic with the check's name.
void storeOptions(ClangTidyOptions::OptionMap &Opts) override
Should store all options supported by this check with their current values or default values for opti...
void check(const ast_matchers::MatchFinder::MatchResult &Result) override
ClangTidyChecks that register ASTMatchers should do the actual work in here.
void registerMatchers(ast_matchers::MatchFinder *Finder) override
Override this to register AST matchers with Finder.
AST_MATCHER(CXXMethodDecl, isStatic)
static unsigned getNameSpecifierNestingLevel(const QualType &QType)
llvm::StringMap< ClangTidyValue > OptionMap