[docs] Add LICENSE.txt to the root of the mono-repo
[llvm-project.git] / clang-tools-extra / clang-tidy / abseil / RedundantStrcatCallsCheck.cpp
blob15171be5de2d85a7881960ac3e53b6108b06330a
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 //===----------------------------------------------------------------------===//
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 {
16 namespace tidy {
17 namespace abseil {
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.
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);
42 namespace {
44 struct StrCatCheckResult {
45 int NumCalls = 0;
46 std::vector<FixItHint> Hints;
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))));
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;
79 return nullptr;
82 StrCatCheckResult processCall(const CallExpr *RootCall, bool IsAppend,
83 const MatchFinder::MatchResult &Result) {
84 StrCatCheckResult CheckResult;
85 std::deque<const CallExpr*> CallsToProcess = {RootCall};
87 while (!CallsToProcess.empty()) {
88 ++CheckResult.NumCalls;
90 const CallExpr* CallExpr = CallsToProcess.front();
91 CallsToProcess.pop_front();
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);
103 return CheckResult;
105 } // namespace
107 void RedundantStrcatCallsCheck::check(const MatchFinder::MatchResult& Result) {
108 bool IsAppend;
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;
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;
126 const StrCatCheckResult CheckResult = processCall(RootCall, IsAppend, Result);
127 if (CheckResult.NumCalls == 1) {
128 // Just one call, so nothing to fix.
129 return;
132 diag(RootCall->getBeginLoc(),
133 "multiple calls to 'absl::StrCat' can be flattened into a single call")
134 << CheckResult.Hints;
137 } // namespace abseil
138 } // namespace tidy
139 } // namespace clang