10#include "clang/AST/ASTContext.h"
11#include "clang/ASTMatchers/ASTMatchFinder.h"
26 const auto CallToStrcat =
27 callExpr(callee(functionDecl(hasName(
"::absl::StrCat"))));
28 const auto CallToStrappend =
29 callExpr(callee(functionDecl(hasName(
"::absl::StrAppend"))));
32 const auto CallToEither = callExpr(
33 callee(functionDecl(hasAnyName(
"::absl::StrCat",
"::absl::StrAppend"))));
35 callExpr(CallToStrcat, unless(hasAncestor(CallToEither))).bind(
"StrCat"),
37 Finder->addMatcher(CallToStrappend.bind(
"StrAppend"),
this);
42struct StrCatCheckResult {
47void removeCallLeaveArgs(
const CallExpr *Call, StrCatCheckResult *CheckResult) {
48 if (Call->getNumArgs() == 0)
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))));
60const 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(TK_AsIs,
70 anyOf(cxxConstructExpr(IsAlphanum,
71 hasArgument(0, IsStrcat)),
73 *Arg->IgnoreParenImpCasts(), *Result.Context))) {
74 removeCallLeaveArgs(SubStrcatCall, CheckResult);
80StrCatCheckResult 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);
106 bool IsAppend =
false;
108 const CallExpr *RootCall =
nullptr;
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 = processCall(RootCall, IsAppend, Result);
125 if (CheckResult.NumCalls == 1) {
130 diag(RootCall->getBeginLoc(),
131 "multiple calls to 'absl::StrCat' can be flattened into a single call")
132 << 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)