11 #include "clang/AST/ASTContext.h" 12 #include "clang/ASTMatchers/ASTMatchFinder.h" 23 AST_MATCHER_P(Stmt, IgnoringTemporaries, ast_matchers::internal::Matcher<Stmt>,
25 const Stmt *E = &Node;
27 if (
const auto *MTE = dyn_cast<MaterializeTemporaryExpr>(E))
28 E = MTE->getTemporary();
29 if (
const auto *BTE = dyn_cast<CXXBindTemporaryExpr>(E))
30 E = BTE->getSubExpr();
31 if (
const auto *ICE = dyn_cast<ImplicitCastExpr>(E))
32 E = ICE->getSubExpr();
37 return InnerMatcher.matches(*E, Finder, Builder);
45 void StrCatAppendCheck::registerMatchers(MatchFinder *Finder) {
46 if (!getLangOpts().CPlusPlus)
48 const auto StrCat = functionDecl(hasName(
"::absl::StrCat"));
51 const auto AlphaNum = IgnoringTemporaries(cxxConstructExpr(
52 argumentCountIs(1), hasType(cxxRecordDecl(hasName(
"::absl::AlphaNum"))),
53 hasArgument(0, ignoringImpCasts(declRefExpr(to(equalsBoundNode(
"LHS")),
54 expr().bind(
"Arg0"))))));
56 const auto HasAnotherReferenceToLhs =
57 callExpr(hasAnyArgument(expr(hasDescendant(declRefExpr(
58 to(equalsBoundNode(
"LHS")), unless(equalsBoundNode(
"Arg0")))))));
65 unless(isInTemplateInstantiation()), hasOverloadedOperatorName(
"="),
66 hasArgument(0, declRefExpr(to(decl().bind(
"LHS")))),
67 hasArgument(1, IgnoringTemporaries(
68 callExpr(callee(StrCat), hasArgument(0, AlphaNum),
69 unless(HasAnotherReferenceToLhs))
75 void StrCatAppendCheck::check(
const MatchFinder::MatchResult &
Result) {
76 const auto *Op = Result.Nodes.getNodeAs<CXXOperatorCallExpr>(
"Op");
77 const auto *Call = Result.Nodes.getNodeAs<CallExpr>(
"Call");
78 assert(Op !=
nullptr && Call !=
nullptr &&
"Matcher does not work as expected");
81 if (Call->getNumArgs() == 1) {
82 diag(Op->getBeginLoc(),
"call to 'absl::StrCat' has no effect");
90 diag(Op->getBeginLoc(),
91 "call 'absl::StrAppend' instead of 'absl::StrCat' when appending to a " 92 "string to avoid a performance penalty")
93 << FixItHint::CreateReplacement(
94 CharSourceRange::getTokenRange(Op->getBeginLoc(),
95 Call->getCallee()->getEndLoc()),
97 << FixItHint::CreateInsertion(Call->getArg(0)->getBeginLoc(),
"&");
llvm::Optional< llvm::Expected< tooling::AtomicChanges > > Result
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
AST_MATCHER_P(FunctionDecl, throws, internal::Matcher< Type >, InnerMatcher)