10 #include "clang/AST/ASTContext.h" 11 #include "clang/ASTMatchers/ASTMatchFinder.h" 27 void RedundantStrcatCallsCheck::registerMatchers(MatchFinder* Finder) {
28 if (!getLangOpts().CPlusPlus)
30 const auto CallToStrcat =
31 callExpr(callee(functionDecl(hasName(
"::absl::StrCat"))));
32 const auto CallToStrappend =
33 callExpr(callee(functionDecl(hasName(
"::absl::StrAppend"))));
36 const auto CallToEither = callExpr(
37 callee(functionDecl(hasAnyName(
"::absl::StrCat",
"::absl::StrAppend"))));
39 callExpr(CallToStrcat, unless(hasAncestor(CallToEither))).bind(
"StrCat"),
41 Finder->addMatcher(CallToStrappend.bind(
"StrAppend"),
this);
46 struct StrCatCheckResult {
51 void RemoveCallLeaveArgs(
const CallExpr* Call, StrCatCheckResult* CheckResult) {
53 CheckResult->Hints.push_back(
54 FixItHint::CreateRemoval(CharSourceRange::getCharRange(
55 Call->getBeginLoc(), Call->getArg(0)->getBeginLoc())));
57 CheckResult->Hints.push_back(
58 FixItHint::CreateRemoval(CharSourceRange::getCharRange(
59 Call->getRParenLoc(), Call->getEndLoc().getLocWithOffset(1))));
62 const clang::CallExpr* ProcessArgument(
const Expr* Arg,
63 const MatchFinder::MatchResult& Result,
64 StrCatCheckResult* CheckResult) {
65 const auto IsAlphanum = hasDeclaration(cxxMethodDecl(hasName(
"AlphaNum")));
66 static const auto*
const Strcat =
new auto(hasName(
"::absl::StrCat"));
67 const auto IsStrcat = cxxBindTemporaryExpr(
68 has(callExpr(callee(functionDecl(*Strcat))).bind(
"StrCat")));
69 if (
const auto* SubStrcatCall = selectFirst<const CallExpr>(
72 cxxConstructExpr(IsAlphanum, hasArgument(0, IsStrcat)),
74 *Arg->IgnoreParenImpCasts(), *Result.Context))) {
75 RemoveCallLeaveArgs(SubStrcatCall, CheckResult);
81 StrCatCheckResult 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);
106 void RedundantStrcatCallsCheck::check(
const MatchFinder::MatchResult& Result) {
109 const CallExpr* RootCall;
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 =
126 ProcessCall(RootCall, IsAppend, Result);
127 if (CheckResult.NumCalls == 1) {
132 diag(RootCall->getBeginLoc(),
133 "multiple calls to 'absl::StrCat' can be flattened into a single call")
134 << CheckResult.Hints;
std::vector< std::string > match(const SymbolIndex &I, const FuzzyFindRequest &Req, bool *Incomplete)
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
std::vector< FixItHint > Hints