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"
13 using namespace clang::ast_matchers
;
19 // TODO: Features to add to the check:
20 // - Make it work if num_args > 26.
21 // - Remove empty literal string arguments.
22 // - Collapse consecutive literal string arguments into one (remove the ,).
23 // - Replace StrCat(a + b) -> StrCat(a, b) if a or b are strings.
24 // - Make it work in macros if the outer and inner StrCats are both in the
27 void RedundantStrcatCallsCheck::registerMatchers(MatchFinder
* Finder
) {
28 const auto CallToStrcat
=
29 callExpr(callee(functionDecl(hasName("::absl::StrCat"))));
30 const auto CallToStrappend
=
31 callExpr(callee(functionDecl(hasName("::absl::StrAppend"))));
32 // Do not match StrCat() calls that are descendants of other StrCat calls.
33 // Those are handled on the ancestor call.
34 const auto CallToEither
= callExpr(
35 callee(functionDecl(hasAnyName("::absl::StrCat", "::absl::StrAppend"))));
37 callExpr(CallToStrcat
, unless(hasAncestor(CallToEither
))).bind("StrCat"),
39 Finder
->addMatcher(CallToStrappend
.bind("StrAppend"), this);
44 struct StrCatCheckResult
{
46 std::vector
<FixItHint
> Hints
;
49 void removeCallLeaveArgs(const CallExpr
*Call
, StrCatCheckResult
*CheckResult
) {
50 if (Call
->getNumArgs() == 0)
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
>(
71 match(stmt(traverse(TK_AsIs
,
72 anyOf(cxxConstructExpr(IsAlphanum
,
73 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()) {
119 // Ignore calls within macros.
120 // In many cases the outer StrCat part of the macro and the inner StrCat is
121 // a macro argument. Removing the inner StrCat() converts one macro
122 // argument into many.
126 const StrCatCheckResult CheckResult
= processCall(RootCall
, IsAppend
, Result
);
127 if (CheckResult
.NumCalls
== 1) {
128 // Just one call, so nothing to fix.
132 diag(RootCall
->getBeginLoc(),
133 "multiple calls to 'absl::StrCat' can be flattened into a single call")
134 << CheckResult
.Hints
;
137 } // namespace abseil