10#include "clang/AST/ASTContext.h"
11#include "clang/ASTMatchers/ASTMatchFinder.h"
27 const auto CallToStrcat =
28 callExpr(callee(functionDecl(hasName(
"::absl::StrCat"))));
29 const auto CallToStrappend =
30 callExpr(callee(functionDecl(hasName(
"::absl::StrAppend"))));
33 const auto CallToEither = callExpr(
34 callee(functionDecl(hasAnyName(
"::absl::StrCat",
"::absl::StrAppend"))));
36 callExpr(CallToStrcat, unless(hasAncestor(CallToEither))).bind(
"StrCat"),
38 Finder->addMatcher(CallToStrappend.bind(
"StrAppend"),
this);
43struct StrCatCheckResult {
48void removeCallLeaveArgs(
const CallExpr *Call, StrCatCheckResult *CheckResult) {
49 if (Call->getNumArgs() == 0)
52 CheckResult->Hints.push_back(
53 FixItHint::CreateRemoval(CharSourceRange::getCharRange(
54 Call->getBeginLoc(), Call->getArg(0)->getBeginLoc())));
56 CheckResult->Hints.push_back(
57 FixItHint::CreateRemoval(CharSourceRange::getCharRange(
58 Call->getRParenLoc(), Call->getEndLoc().getLocWithOffset(1))));
61const clang::CallExpr *processArgument(
const Expr *Arg,
62 const MatchFinder::MatchResult &Result,
63 StrCatCheckResult *CheckResult) {
64 const auto IsAlphanum = hasDeclaration(cxxMethodDecl(hasName(
"AlphaNum")));
65 static const auto*
const Strcat =
new auto(hasName(
"::absl::StrCat"));
66 const auto IsStrcat = cxxBindTemporaryExpr(
67 has(callExpr(callee(functionDecl(*Strcat))).bind(
"StrCat")));
68 if (
const auto *SubStrcatCall = selectFirst<const CallExpr>(
70 match(stmt(traverse(TK_AsIs,
71 anyOf(cxxConstructExpr(IsAlphanum,
72 hasArgument(0, IsStrcat)),
74 *Arg->IgnoreParenImpCasts(), *Result.Context))) {
75 removeCallLeaveArgs(SubStrcatCall, CheckResult);
81StrCatCheckResult processCall(
const CallExpr *RootCall,
bool IsAppend,
82 const MatchFinder::MatchResult &Result) {
83 StrCatCheckResult CheckResult;
84 std::deque<const CallExpr*> CallsToProcess = {RootCall};
86 while (!CallsToProcess.empty()) {
87 ++CheckResult.NumCalls;
89 const CallExpr* CallExpr = CallsToProcess.front();
90 CallsToProcess.pop_front();
92 int StartArg = CallExpr == RootCall && IsAppend;
93 for (
const auto *Arg : CallExpr->arguments()) {
96 if (
const clang::CallExpr *Sub =
97 processArgument(Arg, Result, &CheckResult)) {
98 CallsToProcess.push_back(Sub);
107 bool IsAppend =
false;
109 const CallExpr *RootCall =
nullptr;
110 if ((RootCall = Result.Nodes.getNodeAs<CallExpr>(
"StrCat")))
112 else if ((RootCall = Result.Nodes.getNodeAs<CallExpr>(
"StrAppend")))
117 if (RootCall->getBeginLoc().isMacroID()) {
125 const StrCatCheckResult CheckResult = 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;
std::vector< FixItHint > Hints
DiagnosticBuilder diag(SourceLocation Loc, StringRef Description, DiagnosticIDs::Level Level=DiagnosticIDs::Warning)
Add a diagnostic with the check's name.
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.
std::vector< std::string > match(const SymbolIndex &I, const FuzzyFindRequest &Req, bool *Incomplete)