10 #include "clang/AST/ASTContext.h" 11 #include "clang/ASTMatchers/ASTMatchFinder.h" 22 AST_MATCHER_P(Stmt, IgnoringTemporaries, ast_matchers::internal::Matcher<Stmt>,
24 const Stmt *
E = &Node;
26 if (
const auto *MTE = dyn_cast<MaterializeTemporaryExpr>(E))
27 E = MTE->getSubExpr();
28 if (
const auto *BTE = dyn_cast<CXXBindTemporaryExpr>(E))
29 E = BTE->getSubExpr();
30 if (
const auto *ICE = dyn_cast<ImplicitCastExpr>(E))
31 E = ICE->getSubExpr();
36 return InnerMatcher.matches(*E, Finder,
Builder);
44 void StrCatAppendCheck::registerMatchers(MatchFinder *Finder) {
45 if (!getLangOpts().CPlusPlus)
47 const auto StrCat = functionDecl(hasName(
"::absl::StrCat"));
50 const auto AlphaNum = IgnoringTemporaries(cxxConstructExpr(
51 argumentCountIs(1), hasType(cxxRecordDecl(hasName(
"::absl::AlphaNum"))),
52 hasArgument(0, ignoringImpCasts(declRefExpr(to(equalsBoundNode(
"LHS")),
53 expr().bind(
"Arg0"))))));
55 const auto HasAnotherReferenceToLhs =
56 callExpr(hasAnyArgument(expr(hasDescendant(declRefExpr(
57 to(equalsBoundNode(
"LHS")), unless(equalsBoundNode(
"Arg0")))))));
64 unless(isInTemplateInstantiation()), hasOverloadedOperatorName(
"="),
65 hasArgument(0, declRefExpr(to(decl().bind(
"LHS")))),
66 hasArgument(1, IgnoringTemporaries(
67 callExpr(callee(StrCat), hasArgument(0, AlphaNum),
68 unless(HasAnotherReferenceToLhs))
74 void StrCatAppendCheck::check(
const MatchFinder::MatchResult &Result) {
75 const auto *Op = Result.Nodes.getNodeAs<CXXOperatorCallExpr>(
"Op");
76 const auto *Call = Result.Nodes.getNodeAs<CallExpr>(
"Call");
77 assert(Op !=
nullptr && Call !=
nullptr &&
"Matcher does not work as expected");
80 if (Call->getNumArgs() == 1) {
81 diag(Op->getBeginLoc(),
"call to 'absl::StrCat' has no effect");
89 diag(Op->getBeginLoc(),
90 "call 'absl::StrAppend' instead of 'absl::StrCat' when appending to a " 91 "string to avoid a performance penalty")
92 << FixItHint::CreateReplacement(
94 Call->getCallee()->getEndLoc()),
96 << FixItHint::CreateInsertion(Call->getArg(0)->getBeginLoc(),
"&");
llvm::Optional< Range > getTokenRange(const SourceManager &SM, const LangOptions &LangOpts, SourceLocation TokLoc)
Returns the taken range at TokLoc.
CodeCompletionBuilder Builder
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
AST_MATCHER_P(CXXMethodDecl, hasCanonicalDecl, ast_matchers::internal::Matcher< CXXMethodDecl >, InnerMatcher)