clang-tools 8.0.0

RedundantStrcatCallsCheck.cpp
Go to the documentation of this file.
1 //===--- RedundantStrcatCallsCheck.cpp - clang-tidy------------------------===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9
10 #include "RedundantStrcatCallsCheck.h"
11 #include "clang/AST/ASTContext.h"
12 #include "clang/ASTMatchers/ASTMatchFinder.h"
13
14 using namespace clang::ast_matchers;
15
16 namespace clang {
17 namespace tidy {
18 namespace abseil {
19
20 // TODO: Features to add to the check:
21 // - Make it work if num_args > 26.
22 // - Remove empty literal string arguments.
23 // - Collapse consecutive literal string arguments into one (remove the ,).
24 // - Replace StrCat(a + b) -> StrCat(a, b) if a or b are strings.
25 // - Make it work in macros if the outer and inner StrCats are both in the
26 // argument.
27
28 void RedundantStrcatCallsCheck::registerMatchers(MatchFinder* Finder) {
29 if (!getLangOpts().CPlusPlus)
30 return;
31 const auto CallToStrcat =
32 callExpr(callee(functionDecl(hasName("::absl::StrCat"))));
33 const auto CallToStrappend =
34 callExpr(callee(functionDecl(hasName("::absl::StrAppend"))));
35 // Do not match StrCat() calls that are descendants of other StrCat calls.
36 // Those are handled on the ancestor call.
37 const auto CallToEither = callExpr(
38 callee(functionDecl(hasAnyName("::absl::StrCat", "::absl::StrAppend"))));
39 Finder->addMatcher(
40 callExpr(CallToStrcat, unless(hasAncestor(CallToEither))).bind("StrCat"),
41 this);
42 Finder->addMatcher(CallToStrappend.bind("StrAppend"), this);
43 }
44
45 namespace {
46
47 struct StrCatCheckResult {
48 int NumCalls = 0;
49 std::vector<FixItHint> Hints;
50 };
51
52 void RemoveCallLeaveArgs(const CallExpr* Call, StrCatCheckResult* CheckResult) {
53 // Remove 'Foo('
54 CheckResult->Hints.push_back(
55 FixItHint::CreateRemoval(CharSourceRange::getCharRange(
56 Call->getBeginLoc(), Call->getArg(0)->getBeginLoc())));
57 // Remove the ')'
58 CheckResult->Hints.push_back(
59 FixItHint::CreateRemoval(CharSourceRange::getCharRange(
60 Call->getRParenLoc(), Call->getEndLoc().getLocWithOffset(1))));
61 }
62
63 const clang::CallExpr* ProcessArgument(const Expr* Arg,
64 const MatchFinder::MatchResult& Result,
65 StrCatCheckResult* CheckResult) {
66 const auto IsAlphanum = hasDeclaration(cxxMethodDecl(hasName("AlphaNum")));
67 static const auto* const Strcat = new auto(hasName("::absl::StrCat"));
68 const auto IsStrcat = cxxBindTemporaryExpr(
69 has(callExpr(callee(functionDecl(*Strcat))).bind("StrCat")));
70 if (const auto* SubStrcatCall = selectFirst<const CallExpr>(
71 "StrCat",
72 match(stmt(anyOf(
73 cxxConstructExpr(IsAlphanum, 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 =
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
clang::ast_matchers
Definition: AbseilMatcher.h:15
RedundantStrcatCallsCheck.h
NumCalls
int NumCalls
Definition: RedundantStrcatCallsCheck.cpp:48
Result
llvm::Optional< llvm::Expected< tooling::AtomicChanges > > Result
Definition: ClangdServer.cpp:60
clang
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
Definition: ChangeNamespace.cpp:18
Hints
std::vector< FixItHint > Hints
Definition: RedundantStrcatCallsCheck.cpp:49