clang-tools  11.0.0
RedundantStrcatCallsCheck.cpp
Go to the documentation of this file.
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 
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 
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 
49 void RemoveCallLeaveArgs(const CallExpr* Call, StrCatCheckResult* CheckResult) {
50  // Remove 'Foo('
51  CheckResult->Hints.push_back(
52  FixItHint::CreateRemoval(CharSourceRange::getCharRange(
53  Call->getBeginLoc(), Call->getArg(0)->getBeginLoc())));
54  // Remove the ')'
55  CheckResult->Hints.push_back(
56  FixItHint::CreateRemoval(CharSourceRange::getCharRange(
57  Call->getRParenLoc(), Call->getEndLoc().getLocWithOffset(1))));
58 }
59 
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>(
68  "StrCat",
69  match(stmt(traverse(ast_type_traits::TK_AsIs,
70  anyOf(cxxConstructExpr(IsAlphanum,
71  hasArgument(0, IsStrcat)),
72  IsStrcat))),
73  *Arg->IgnoreParenImpCasts(), *Result.Context))) {
74  RemoveCallLeaveArgs(SubStrcatCall, CheckResult);
75  return SubStrcatCall;
76  }
77  return nullptr;
78 }
79 
80 StrCatCheckResult ProcessCall(const CallExpr* RootCall, bool IsAppend,
81  const MatchFinder::MatchResult& Result) {
82  StrCatCheckResult CheckResult;
83  std::deque<const CallExpr*> CallsToProcess = {RootCall};
84 
85  while (!CallsToProcess.empty()) {
86  ++CheckResult.NumCalls;
87 
88  const CallExpr* CallExpr = CallsToProcess.front();
89  CallsToProcess.pop_front();
90 
91  int StartArg = CallExpr == RootCall && IsAppend;
92  for (const auto *Arg : CallExpr->arguments()) {
93  if (StartArg-- > 0)
94  continue;
95  if (const clang::CallExpr* Sub =
96  ProcessArgument(Arg, Result, &CheckResult)) {
97  CallsToProcess.push_back(Sub);
98  }
99  }
100  }
101  return CheckResult;
102 }
103 } // namespace
104 
105 void RedundantStrcatCallsCheck::check(const MatchFinder::MatchResult& Result) {
106  bool IsAppend;
107 
108  const CallExpr* RootCall;
109  if ((RootCall = Result.Nodes.getNodeAs<CallExpr>("StrCat")))
110  IsAppend = false;
111  else if ((RootCall = Result.Nodes.getNodeAs<CallExpr>("StrAppend")))
112  IsAppend = true;
113  else
114  return;
115 
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.
121  return;
122  }
123 
124  const StrCatCheckResult CheckResult =
125  ProcessCall(RootCall, IsAppend, Result);
126  if (CheckResult.NumCalls == 1) {
127  // Just one call, so nothing to fix.
128  return;
129  }
130 
131  diag(RootCall->getBeginLoc(),
132  "multiple calls to 'absl::StrCat' can be flattened into a single call")
133  << CheckResult.Hints;
134 }
135 
136 } // namespace abseil
137 } // namespace tidy
138 } // namespace clang
Hints
std::vector< FixItHint > Hints
Definition: RedundantStrcatCallsCheck.cpp:46
RedundantStrcatCallsCheck.h
clang::ast_matchers
Definition: AbseilMatcher.h:14
clang::clangd::match
std::vector< std::string > match(const SymbolIndex &I, const FuzzyFindRequest &Req, bool *Incomplete)
Definition: TestIndex.cpp:94
NumCalls
int NumCalls
Definition: RedundantStrcatCallsCheck.cpp:45
clang
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
Definition: ApplyReplacements.h:27