10 #include "clang/AST/ASTContext.h"
11 #include "clang/ASTMatchers/ASTMatchFinder.h"
27 void RedundantStrcatCallsCheck::registerMatchers(MatchFinder* Finder) {
28 const auto CallToStrcat =
29 callExpr(callee(functionDecl(hasName(
"::absl::StrCat"))));
30 const auto CallToStrappend =
31 callExpr(callee(functionDecl(hasName(
"::absl::StrAppend"))));
34 const auto CallToEither = callExpr(
35 callee(functionDecl(hasAnyName(
"::absl::StrCat",
"::absl::StrAppend"))));
37 callExpr(CallToStrcat, unless(hasAncestor(CallToEither))).bind(
"StrCat"),
39 Finder->addMatcher(CallToStrappend.bind(
"StrAppend"),
this);
44 struct StrCatCheckResult {
49 void RemoveCallLeaveArgs(
const CallExpr* Call, StrCatCheckResult* CheckResult) {
51 CheckResult->Hints.push_back(
52 FixItHint::CreateRemoval(CharSourceRange::getCharRange(
53 Call->getBeginLoc(), Call->getArg(0)->getBeginLoc())));
55 CheckResult->Hints.push_back(
56 FixItHint::CreateRemoval(CharSourceRange::getCharRange(
57 Call->getRParenLoc(), Call->getEndLoc().getLocWithOffset(1))));
60 const clang::CallExpr* ProcessArgument(
const Expr* Arg,
61 const MatchFinder::MatchResult& Result,
62 StrCatCheckResult* CheckResult) {
63 const auto IsAlphanum = hasDeclaration(cxxMethodDecl(hasName(
"AlphaNum")));
64 static const auto*
const Strcat =
new auto(hasName(
"::absl::StrCat"));
65 const auto IsStrcat = cxxBindTemporaryExpr(
66 has(callExpr(callee(functionDecl(*Strcat))).bind(
"StrCat")));
67 if (
const auto *SubStrcatCall = selectFirst<const CallExpr>(
69 match(stmt(traverse(ast_type_traits::TK_AsIs,
70 anyOf(cxxConstructExpr(IsAlphanum,
71 hasArgument(0, IsStrcat)),
73 *Arg->IgnoreParenImpCasts(), *Result.Context))) {
74 RemoveCallLeaveArgs(SubStrcatCall, CheckResult);
80 StrCatCheckResult ProcessCall(
const CallExpr* RootCall,
bool IsAppend,
81 const MatchFinder::MatchResult& Result) {
82 StrCatCheckResult CheckResult;
83 std::deque<const CallExpr*> CallsToProcess = {RootCall};
85 while (!CallsToProcess.empty()) {
86 ++CheckResult.NumCalls;
88 const CallExpr* CallExpr = CallsToProcess.front();
89 CallsToProcess.pop_front();
91 int StartArg = CallExpr == RootCall && IsAppend;
92 for (
const auto *Arg : CallExpr->arguments()) {
95 if (
const clang::CallExpr* Sub =
96 ProcessArgument(Arg, Result, &CheckResult)) {
97 CallsToProcess.push_back(Sub);
105 void RedundantStrcatCallsCheck::check(
const MatchFinder::MatchResult& Result) {
108 const CallExpr* RootCall;
109 if ((RootCall = Result.Nodes.getNodeAs<CallExpr>(
"StrCat")))
111 else if ((RootCall = Result.Nodes.getNodeAs<CallExpr>(
"StrAppend")))
116 if (RootCall->getBeginLoc().isMacroID()) {
124 const StrCatCheckResult CheckResult =
125 ProcessCall(RootCall, IsAppend, Result);
126 if (CheckResult.NumCalls == 1) {
131 diag(RootCall->getBeginLoc(),
132 "multiple calls to 'absl::StrCat' can be flattened into a single call")
133 << CheckResult.Hints;