1 //===--- RedundantStrcatCallsCheck.cpp - clang-tidy------------------------===//
2 //
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
6 //
7 //===----------------------------------------------------------------------===//
8
9 #include "RedundantStrcatCallsCheck.h"
10 #include "clang/AST/ASTContext.h"
11 #include "clang/ASTMatchers/ASTMatchFinder.h"
12
13 using namespace clang::ast_matchers;
14
15 namespace clang {
16 namespace tidy {
17 namespace abseil {
18
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
25 // argument.
26
registerMatchers(MatchFinder * Finder)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"))));
36 Finder->addMatcher(
37 callExpr(CallToStrcat, unless(hasAncestor(CallToEither))).bind("StrCat"),
38 this);
39 Finder->addMatcher(CallToStrappend.bind("StrAppend"), this);
40 }
41
42 namespace {
43
44 struct StrCatCheckResult {
45 int NumCalls = 0;
46 std::vector<FixItHint> Hints;
47 };
48
RemoveCallLeaveArgs(const CallExpr * Call,StrCatCheckResult * CheckResult)49 void RemoveCallLeaveArgs(const CallExpr* Call, StrCatCheckResult* CheckResult) {
50 if (Call->getNumArgs() == 0)
51 return;
52 // Remove 'Foo('
53 CheckResult->Hints.push_back(
54 FixItHint::CreateRemoval(CharSourceRange::getCharRange(
55 Call->getBeginLoc(), Call->getArg(0)->getBeginLoc())));
56 // Remove the ')'
57 CheckResult->Hints.push_back(
58 FixItHint::CreateRemoval(CharSourceRange::getCharRange(
59 Call->getRParenLoc(), Call->getEndLoc().getLocWithOffset(1))));
60 }
61
ProcessArgument(const Expr * Arg,const MatchFinder::MatchResult & Result,StrCatCheckResult * CheckResult)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>(
70 "StrCat",
71 match(stmt(traverse(ast_type_traits::TK_AsIs,
72 anyOf(cxxConstructExpr(IsAlphanum,
73 hasArgument(0, IsStrcat)),
74 IsStrcat))),
75 *Arg->IgnoreParenImpCasts(), *Result.Context))) {
76 RemoveCallLeaveArgs(SubStrcatCall, CheckResult);
77 return SubStrcatCall;
78 }
79 return nullptr;
80 }
81
ProcessCall(const CallExpr * RootCall,bool IsAppend,const MatchFinder::MatchResult & Result)82 StrCatCheckResult ProcessCall(const CallExpr* RootCall, bool IsAppend,
83 const MatchFinder::MatchResult& Result) {
84 StrCatCheckResult CheckResult;
85 std::deque<const CallExpr*> CallsToProcess = {RootCall};
86
87 while (!CallsToProcess.empty()) {
88 ++CheckResult.NumCalls;
89
90 const CallExpr* CallExpr = CallsToProcess.front();
91 CallsToProcess.pop_front();
92
93 int StartArg = CallExpr == RootCall && IsAppend;
94 for (const auto *Arg : CallExpr->arguments()) {
95 if (StartArg-- > 0)
96 continue;
97 if (const clang::CallExpr* Sub =
98 ProcessArgument(Arg, Result, &CheckResult)) {
99 CallsToProcess.push_back(Sub);
100 }
101 }
102 }
103 return CheckResult;
104 }
105 } // namespace
106
check(const MatchFinder::MatchResult & Result)107 void RedundantStrcatCallsCheck::check(const MatchFinder::MatchResult& Result) {
108 bool IsAppend;
109
110 const CallExpr* RootCall;
111 if ((RootCall = Result.Nodes.getNodeAs<CallExpr>("StrCat")))
112 IsAppend = false;
113 else if ((RootCall = Result.Nodes.getNodeAs<CallExpr>("StrAppend")))
114 IsAppend = true;
115 else
116 return;
117
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.
123 return;
124 }
125
126 const StrCatCheckResult CheckResult =
127 ProcessCall(RootCall, IsAppend, Result);
128 if (CheckResult.NumCalls == 1) {
129 // Just one call, so nothing to fix.
130 return;
131 }
132
133 diag(RootCall->getBeginLoc(),
134 "multiple calls to 'absl::StrCat' can be flattened into a single call")
135 << CheckResult.Hints;
136 }
137
138 } // namespace abseil
139 } // namespace tidy
140 } // namespace clang
141