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
;
15 namespace clang::tidy::abseil
{
17 // TODO: Features to add to the check:
18 // - Make it work if num_args > 26.
19 // - Remove empty literal string arguments.
20 // - Collapse consecutive literal string arguments into one (remove the ,).
21 // - Replace StrCat(a + b) -> StrCat(a, b) if a or b are strings.
22 // - Make it work in macros if the outer and inner StrCats are both in the
25 void RedundantStrcatCallsCheck::registerMatchers(MatchFinder
* Finder
) {
26 const auto CallToStrcat
=
27 callExpr(callee(functionDecl(hasName("::absl::StrCat"))));
28 const auto CallToStrappend
=
29 callExpr(callee(functionDecl(hasName("::absl::StrAppend"))));
30 // Do not match StrCat() calls that are descendants of other StrCat calls.
31 // Those are handled on the ancestor call.
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);
42 struct StrCatCheckResult
{
44 std::vector
<FixItHint
> Hints
;
47 void 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))));
60 const 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
);
80 StrCatCheckResult
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
);
105 void RedundantStrcatCallsCheck::check(const MatchFinder::MatchResult
& Result
) {
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()) {
117 // Ignore calls within macros.
118 // In many cases the outer StrCat part of the macro and the inner StrCat is
119 // a macro argument. Removing the inner StrCat() converts one macro
120 // argument into many.
124 const StrCatCheckResult CheckResult
= processCall(RootCall
, IsAppend
, Result
);
125 if (CheckResult
.NumCalls
== 1) {
126 // Just one call, so nothing to fix.
130 diag(RootCall
->getBeginLoc(),
131 "multiple calls to 'absl::StrCat' can be flattened into a single call")
132 << CheckResult
.Hints
;
135 } // namespace clang::tidy::abseil