clang-tools  14.0.0git
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  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 
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(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 
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 
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 = processCall(RootCall, IsAppend, Result);
127  if (CheckResult.NumCalls == 1) {
128  // Just one call, so nothing to fix.
129  return;
130  }
131 
132  diag(RootCall->getBeginLoc(),
133  "multiple calls to 'absl::StrCat' can be flattened into a single call")
134  << CheckResult.Hints;
135 }
136 
137 } // namespace abseil
138 } // namespace tidy
139 } // 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::clangd::check
bool check(llvm::StringRef File, llvm::function_ref< bool(const Position &)> ShouldCheckLine, const ThreadsafeFS &TFS, const ClangdLSPServer::Options &Opts, bool EnableCodeCompletion)
Definition: Check.cpp:259
clang
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
Definition: ApplyReplacements.h:27