xref: /llvm-project/clang-tools-extra/clang-tidy/abseil/RedundantStrcatCallsCheck.cpp (revision 48ef912e2b32798b704af242e551a7090102c750)
1ca5f775dSAaron Ballman //===--- RedundantStrcatCallsCheck.cpp - clang-tidy------------------------===//
2ca5f775dSAaron Ballman //
32946cd70SChandler Carruth // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
42946cd70SChandler Carruth // See https://llvm.org/LICENSE.txt for license information.
52946cd70SChandler Carruth // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6ca5f775dSAaron Ballman //
7ca5f775dSAaron Ballman //===----------------------------------------------------------------------===//
8ca5f775dSAaron Ballman 
9ca5f775dSAaron Ballman #include "RedundantStrcatCallsCheck.h"
10ca5f775dSAaron Ballman #include "clang/AST/ASTContext.h"
11ca5f775dSAaron Ballman #include "clang/ASTMatchers/ASTMatchFinder.h"
12*48ef912eSNikita Popov #include <deque>
13ca5f775dSAaron Ballman 
14ca5f775dSAaron Ballman using namespace clang::ast_matchers;
15ca5f775dSAaron Ballman 
167d2ea6c4SCarlos Galvez namespace clang::tidy::abseil {
17ca5f775dSAaron Ballman 
18ca5f775dSAaron Ballman // TODO: Features to add to the check:
19ca5f775dSAaron Ballman //  - Make it work if num_args > 26.
20ca5f775dSAaron Ballman //  - Remove empty literal string arguments.
21ca5f775dSAaron Ballman //  - Collapse consecutive literal string arguments into one (remove the ,).
22ca5f775dSAaron Ballman //  - Replace StrCat(a + b)  ->  StrCat(a, b)  if a or b are strings.
23ca5f775dSAaron Ballman //  - Make it work in macros if the outer and inner StrCats are both in the
24ca5f775dSAaron Ballman //    argument.
25ca5f775dSAaron Ballman 
registerMatchers(MatchFinder * Finder)26ca5f775dSAaron Ballman void RedundantStrcatCallsCheck::registerMatchers(MatchFinder* Finder) {
27ca5f775dSAaron Ballman   const auto CallToStrcat =
28ca5f775dSAaron Ballman       callExpr(callee(functionDecl(hasName("::absl::StrCat"))));
29ca5f775dSAaron Ballman   const auto CallToStrappend =
30ca5f775dSAaron Ballman       callExpr(callee(functionDecl(hasName("::absl::StrAppend"))));
31ca5f775dSAaron Ballman   // Do not match StrCat() calls that are descendants of other StrCat calls.
32ca5f775dSAaron Ballman   // Those are handled on the ancestor call.
33ca5f775dSAaron Ballman   const auto CallToEither = callExpr(
34ca5f775dSAaron Ballman       callee(functionDecl(hasAnyName("::absl::StrCat", "::absl::StrAppend"))));
35ca5f775dSAaron Ballman   Finder->addMatcher(
36ca5f775dSAaron Ballman       callExpr(CallToStrcat, unless(hasAncestor(CallToEither))).bind("StrCat"),
37ca5f775dSAaron Ballman       this);
38ca5f775dSAaron Ballman   Finder->addMatcher(CallToStrappend.bind("StrAppend"), this);
39ca5f775dSAaron Ballman }
40ca5f775dSAaron Ballman 
41ca5f775dSAaron Ballman namespace {
42ca5f775dSAaron Ballman 
43ca5f775dSAaron Ballman struct StrCatCheckResult {
44ca5f775dSAaron Ballman   int NumCalls = 0;
45ca5f775dSAaron Ballman   std::vector<FixItHint> Hints;
46ca5f775dSAaron Ballman };
47ca5f775dSAaron Ballman 
removeCallLeaveArgs(const CallExpr * Call,StrCatCheckResult * CheckResult)48ab2d3ce4SAlexander Kornienko void removeCallLeaveArgs(const CallExpr *Call, StrCatCheckResult *CheckResult) {
49af0d607eSHaojian Wu   if (Call->getNumArgs() == 0)
50af0d607eSHaojian Wu     return;
51ca5f775dSAaron Ballman   // Remove 'Foo('
52ca5f775dSAaron Ballman   CheckResult->Hints.push_back(
53ca5f775dSAaron Ballman       FixItHint::CreateRemoval(CharSourceRange::getCharRange(
54ca5f775dSAaron Ballman           Call->getBeginLoc(), Call->getArg(0)->getBeginLoc())));
55ca5f775dSAaron Ballman   // Remove the ')'
56ca5f775dSAaron Ballman   CheckResult->Hints.push_back(
57ca5f775dSAaron Ballman       FixItHint::CreateRemoval(CharSourceRange::getCharRange(
58ca5f775dSAaron Ballman           Call->getRParenLoc(), Call->getEndLoc().getLocWithOffset(1))));
59ca5f775dSAaron Ballman }
60ca5f775dSAaron Ballman 
processArgument(const Expr * Arg,const MatchFinder::MatchResult & Result,StrCatCheckResult * CheckResult)61ab2d3ce4SAlexander Kornienko const clang::CallExpr *processArgument(const Expr *Arg,
62ca5f775dSAaron Ballman                                        const MatchFinder::MatchResult &Result,
63ca5f775dSAaron Ballman                                        StrCatCheckResult *CheckResult) {
64ca5f775dSAaron Ballman   const auto IsAlphanum = hasDeclaration(cxxMethodDecl(hasName("AlphaNum")));
65ca5f775dSAaron Ballman   static const auto* const Strcat = new auto(hasName("::absl::StrCat"));
66ca5f775dSAaron Ballman   const auto IsStrcat = cxxBindTemporaryExpr(
67ca5f775dSAaron Ballman       has(callExpr(callee(functionDecl(*Strcat))).bind("StrCat")));
68ca5f775dSAaron Ballman   if (const auto *SubStrcatCall = selectFirst<const CallExpr>(
69ca5f775dSAaron Ballman           "StrCat",
70027899daSAlexander Kornienko           match(stmt(traverse(TK_AsIs,
71a72307c3SStephen Kelly                               anyOf(cxxConstructExpr(IsAlphanum,
72a72307c3SStephen Kelly                                                      hasArgument(0, IsStrcat)),
73a72307c3SStephen Kelly                                     IsStrcat))),
74ca5f775dSAaron Ballman                 *Arg->IgnoreParenImpCasts(), *Result.Context))) {
75ab2d3ce4SAlexander Kornienko     removeCallLeaveArgs(SubStrcatCall, CheckResult);
76ca5f775dSAaron Ballman     return SubStrcatCall;
77ca5f775dSAaron Ballman   }
78ca5f775dSAaron Ballman   return nullptr;
79ca5f775dSAaron Ballman }
80ca5f775dSAaron Ballman 
processCall(const CallExpr * RootCall,bool IsAppend,const MatchFinder::MatchResult & Result)81ab2d3ce4SAlexander Kornienko StrCatCheckResult processCall(const CallExpr *RootCall, bool IsAppend,
82ca5f775dSAaron Ballman                               const MatchFinder::MatchResult &Result) {
83ca5f775dSAaron Ballman   StrCatCheckResult CheckResult;
84ca5f775dSAaron Ballman   std::deque<const CallExpr*> CallsToProcess = {RootCall};
85ca5f775dSAaron Ballman 
86ca5f775dSAaron Ballman   while (!CallsToProcess.empty()) {
87ca5f775dSAaron Ballman     ++CheckResult.NumCalls;
88ca5f775dSAaron Ballman 
89ca5f775dSAaron Ballman     const CallExpr* CallExpr = CallsToProcess.front();
90ca5f775dSAaron Ballman     CallsToProcess.pop_front();
91ca5f775dSAaron Ballman 
92ca5f775dSAaron Ballman     int StartArg = CallExpr == RootCall && IsAppend;
93ca5f775dSAaron Ballman     for (const auto *Arg : CallExpr->arguments()) {
94ca5f775dSAaron Ballman       if (StartArg-- > 0)
95ca5f775dSAaron Ballman       	continue;
96ca5f775dSAaron Ballman       if (const clang::CallExpr *Sub =
97ab2d3ce4SAlexander Kornienko               processArgument(Arg, Result, &CheckResult)) {
98ca5f775dSAaron Ballman         CallsToProcess.push_back(Sub);
99ca5f775dSAaron Ballman       }
100ca5f775dSAaron Ballman     }
101ca5f775dSAaron Ballman   }
102ca5f775dSAaron Ballman   return CheckResult;
103ca5f775dSAaron Ballman }
104ca5f775dSAaron Ballman }  // namespace
105ca5f775dSAaron Ballman 
check(const MatchFinder::MatchResult & Result)106ca5f775dSAaron Ballman void RedundantStrcatCallsCheck::check(const MatchFinder::MatchResult& Result) {
107cbdc3e1bSPiotr Zegar   bool IsAppend = false;
108ca5f775dSAaron Ballman 
109cbdc3e1bSPiotr Zegar   const CallExpr *RootCall = nullptr;
110ca5f775dSAaron Ballman   if ((RootCall = Result.Nodes.getNodeAs<CallExpr>("StrCat")))
111ca5f775dSAaron Ballman   	IsAppend = false;
112ca5f775dSAaron Ballman   else if ((RootCall = Result.Nodes.getNodeAs<CallExpr>("StrAppend")))
113ca5f775dSAaron Ballman   	IsAppend = true;
114ca5f775dSAaron Ballman   else
115ca5f775dSAaron Ballman   	return;
116ca5f775dSAaron Ballman 
117ca5f775dSAaron Ballman   if (RootCall->getBeginLoc().isMacroID()) {
118ca5f775dSAaron Ballman     // Ignore calls within macros.
119ca5f775dSAaron Ballman     // In many cases the outer StrCat part of the macro and the inner StrCat is
120ca5f775dSAaron Ballman     // a macro argument. Removing the inner StrCat() converts one macro
121ca5f775dSAaron Ballman     // argument into many.
122ca5f775dSAaron Ballman     return;
123ca5f775dSAaron Ballman   }
124ca5f775dSAaron Ballman 
125ab2d3ce4SAlexander Kornienko   const StrCatCheckResult CheckResult = processCall(RootCall, IsAppend, Result);
126ca5f775dSAaron Ballman   if (CheckResult.NumCalls == 1) {
127ca5f775dSAaron Ballman     // Just one call, so nothing to fix.
128ca5f775dSAaron Ballman     return;
129ca5f775dSAaron Ballman   }
130ca5f775dSAaron Ballman 
131ca5f775dSAaron Ballman   diag(RootCall->getBeginLoc(),
132ca5f775dSAaron Ballman   	   "multiple calls to 'absl::StrCat' can be flattened into a single call")
133ca5f775dSAaron Ballman       << CheckResult.Hints;
134ca5f775dSAaron Ballman }
135ca5f775dSAaron Ballman 
1367d2ea6c4SCarlos Galvez } // namespace clang::tidy::abseil
137