11 #include "clang/AST/ASTContext.h" 12 #include "clang/ASTMatchers/ASTMatchFinder.h" 28 void RedundantStrcatCallsCheck::registerMatchers(MatchFinder* Finder) {
29 if (!getLangOpts().CPlusPlus)
31 const auto CallToStrcat =
32 callExpr(callee(functionDecl(hasName(
"::absl::StrCat"))));
33 const auto CallToStrappend =
34 callExpr(callee(functionDecl(hasName(
"::absl::StrAppend"))));
37 const auto CallToEither = callExpr(
38 callee(functionDecl(hasAnyName(
"::absl::StrCat",
"::absl::StrAppend"))));
40 callExpr(CallToStrcat, unless(hasAncestor(CallToEither))).bind(
"StrCat"),
42 Finder->addMatcher(CallToStrappend.bind(
"StrAppend"),
this);
47 struct StrCatCheckResult {
52 void RemoveCallLeaveArgs(
const CallExpr* Call, StrCatCheckResult* CheckResult) {
54 CheckResult->Hints.push_back(
55 FixItHint::CreateRemoval(CharSourceRange::getCharRange(
56 Call->getBeginLoc(), Call->getArg(0)->getBeginLoc())));
58 CheckResult->Hints.push_back(
59 FixItHint::CreateRemoval(CharSourceRange::getCharRange(
60 Call->getRParenLoc(), Call->getEndLoc().getLocWithOffset(1))));
63 const clang::CallExpr* ProcessArgument(
const Expr* Arg,
64 const MatchFinder::MatchResult&
Result,
65 StrCatCheckResult* CheckResult) {
66 const auto IsAlphanum = hasDeclaration(cxxMethodDecl(hasName(
"AlphaNum")));
67 static const auto*
const Strcat =
new auto(hasName(
"::absl::StrCat"));
68 const auto IsStrcat = cxxBindTemporaryExpr(
69 has(callExpr(callee(functionDecl(*Strcat))).bind(
"StrCat")));
70 if (
const auto* SubStrcatCall = selectFirst<const CallExpr>(
73 cxxConstructExpr(IsAlphanum, hasArgument(0, IsStrcat)),
75 *Arg->IgnoreParenImpCasts(), *Result.Context))) {
76 RemoveCallLeaveArgs(SubStrcatCall, CheckResult);
82 StrCatCheckResult ProcessCall(
const CallExpr* RootCall,
bool IsAppend,
83 const MatchFinder::MatchResult& Result) {
84 StrCatCheckResult CheckResult;
85 std::deque<const CallExpr*> CallsToProcess = {RootCall};
87 while (!CallsToProcess.empty()) {
88 ++CheckResult.NumCalls;
90 const CallExpr* CallExpr = CallsToProcess.front();
91 CallsToProcess.pop_front();
93 int StartArg = CallExpr == RootCall && IsAppend;
94 for (
const auto *Arg : CallExpr->arguments()) {
97 if (
const clang::CallExpr* Sub =
98 ProcessArgument(Arg, Result, &CheckResult)) {
99 CallsToProcess.push_back(Sub);
107 void RedundantStrcatCallsCheck::check(
const MatchFinder::MatchResult&
Result) {
110 const CallExpr* RootCall;
111 if ((RootCall = Result.Nodes.getNodeAs<CallExpr>(
"StrCat")))
113 else if ((RootCall = Result.Nodes.getNodeAs<CallExpr>(
"StrAppend")))
118 if (RootCall->getBeginLoc().isMacroID()) {
126 const StrCatCheckResult CheckResult =
127 ProcessCall(RootCall, IsAppend, Result);
128 if (CheckResult.NumCalls == 1) {
133 diag(RootCall->getBeginLoc(),
134 "multiple calls to 'absl::StrCat' can be flattened into a single call")
135 << CheckResult.Hints;
llvm::Optional< llvm::Expected< tooling::AtomicChanges > > Result
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
std::vector< FixItHint > Hints