1 //===--- RedundantStrcatCallsCheck.cpp - clang-tidy------------------------===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 #include "RedundantStrcatCallsCheck.h"
10 #include "clang/AST/ASTContext.h"
11 #include "clang/ASTMatchers/ASTMatchFinder.h"
14 using namespace clang::ast_matchers
;
16 namespace clang::tidy::abseil
{
18 // TODO: Features to add to the check:
19 // - Make it work if num_args > 26.
20 // - Remove empty literal string arguments.
21 // - Collapse consecutive literal string arguments into one (remove the ,).
22 // - Replace StrCat(a + b) -> StrCat(a, b) if a or b are strings.
23 // - Make it work in macros if the outer and inner StrCats are both in the
26 void RedundantStrcatCallsCheck::registerMatchers(MatchFinder
* Finder
) {
27 const auto CallToStrcat
=
28 callExpr(callee(functionDecl(hasName("::absl::StrCat"))));
29 const auto CallToStrappend
=
30 callExpr(callee(functionDecl(hasName("::absl::StrAppend"))));
31 // Do not match StrCat() calls that are descendants of other StrCat calls.
32 // Those are handled on the ancestor call.
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);
43 struct StrCatCheckResult
{
45 std::vector
<FixItHint
> Hints
;
48 void 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))));
61 const 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
);
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
) {
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()) {
118 // Ignore calls within macros.
119 // In many cases the outer StrCat part of the macro and the inner StrCat is
120 // a macro argument. Removing the inner StrCat() converts one macro
121 // argument into many.
125 const StrCatCheckResult CheckResult
= processCall(RootCall
, IsAppend
, Result
);
126 if (CheckResult
.NumCalls
== 1) {
127 // Just one call, so nothing to fix.
131 diag(RootCall
->getBeginLoc(),
132 "multiple calls to 'absl::StrCat' can be flattened into a single call")
133 << CheckResult
.Hints
;
136 } // namespace clang::tidy::abseil