23 unsigned NameSpecifierNestingLevel = 1;
24 for (NestedNameSpecifier Qualifier = QType->getPrefix(); ;
25 ++NameSpecifierNestingLevel) {
26 switch (Qualifier.getKind()) {
27 case NestedNameSpecifier::Kind::Null:
28 return NameSpecifierNestingLevel;
29 case NestedNameSpecifier::Kind::Global:
30 case NestedNameSpecifier::Kind::MicrosoftSuper:
31 return NameSpecifierNestingLevel + 1;
32 case NestedNameSpecifier::Kind::Namespace:
33 Qualifier = Qualifier.getAsNamespaceAndPrefix().Prefix;
35 case NestedNameSpecifier::Kind::Type:
36 Qualifier = Qualifier.getAsType()->getPrefix();
39 llvm_unreachable(
"unhandled nested name specifier kind");
59 const MatchFinder::MatchResult &Result) {
60 const auto *MemberExpression =
61 Result.Nodes.getNodeAs<MemberExpr>(
"memberExpression");
63 if (MemberExpression->getBeginLoc().isMacroID())
66 const Expr *BaseExpr = MemberExpression->getBase();
68 const QualType BaseType =
69 BaseExpr->getType()->isPointerType()
70 ? BaseExpr->getType()->getPointeeType().getUnqualifiedType()
71 : BaseExpr->getType().getUnqualifiedType();
73 const ASTContext *AstContext = Result.Context;
74 PrintingPolicy PrintingPolicyWithSuppressedTag(AstContext->getLangOpts());
75 PrintingPolicyWithSuppressedTag.SuppressTagKeyword =
true;
76 PrintingPolicyWithSuppressedTag.SuppressUnwrittenScope =
true;
78 PrintingPolicyWithSuppressedTag.PrintAsCanonical =
79 !BaseExpr->getType()->isTypedefNameType();
81 std::string BaseTypeName =
82 BaseType.getAsString(PrintingPolicyWithSuppressedTag);
85 const RecordDecl *RecDecl = BaseType->getAsCXXRecordDecl();
86 if (!RecDecl || RecDecl->getIdentifier() ==
nullptr)
90 if (StringRef(BaseTypeName).starts_with(
"__cuda_builtin_"))
93 SourceLocation MemberExprStartLoc = MemberExpression->getBeginLoc();
94 auto CreateFix = [&] {
95 return FixItHint::CreateReplacement(
96 CharSourceRange::getCharRange(MemberExprStartLoc,
97 MemberExpression->getMemberLoc()),
103 diag(MemberExprStartLoc,
"static member accessed through instance");
108 if (!BaseExpr->HasSideEffects(*AstContext,
115 diag(MemberExprStartLoc,
"member base expression may carry some side effects",
116 DiagnosticIDs::Level::Note)
117 << BaseExpr->getSourceRange() << CreateFix();