clang-tools 8.0.0

StrCatAppendCheck.cpp
Go to the documentation of this file.
1 //===--- StrCatAppendCheck.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 "StrCatAppendCheck.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 namespace {
21 // Skips any combination of temporary materialization, temporary binding and
22 // implicit casting.
23 AST_MATCHER_P(Stmt, IgnoringTemporaries, ast_matchers::internal::Matcher<Stmt>,
24 InnerMatcher) {
25 const Stmt *E = &Node;
26 while (true) {
27 if (const auto *MTE = dyn_cast<MaterializeTemporaryExpr>(E))
28 E = MTE->getTemporary();
29 if (const auto *BTE = dyn_cast<CXXBindTemporaryExpr>(E))
30 E = BTE->getSubExpr();
31 if (const auto *ICE = dyn_cast<ImplicitCastExpr>(E))
32 E = ICE->getSubExpr();
33 else
34 break;
35 }
36
37 return InnerMatcher.matches(*E, Finder, Builder);
38 }
39
40 } // namespace
41
42 // TODO: str += StrCat(...)
43 // str.append(StrCat(...))
44
45 void StrCatAppendCheck::registerMatchers(MatchFinder *Finder) {
46 if (!getLangOpts().CPlusPlus)
47 return;
48 const auto StrCat = functionDecl(hasName("::absl::StrCat"));
49 // The arguments of absl::StrCat are implicitly converted to AlphaNum. This
50 // matches to the arguments because of that behavior.
51 const auto AlphaNum = IgnoringTemporaries(cxxConstructExpr(
52 argumentCountIs(1), hasType(cxxRecordDecl(hasName("::absl::AlphaNum"))),
53 hasArgument(0, ignoringImpCasts(declRefExpr(to(equalsBoundNode("LHS")),
54 expr().bind("Arg0"))))));
55
56 const auto HasAnotherReferenceToLhs =
57 callExpr(hasAnyArgument(expr(hasDescendant(declRefExpr(
58 to(equalsBoundNode("LHS")), unless(equalsBoundNode("Arg0")))))));
59
60 // Now look for calls to operator= with an object on the LHS and a call to
61 // StrCat on the RHS. The first argument of the StrCat call should be the same
62 // as the LHS. Ignore calls from template instantiations.
63 Finder->addMatcher(
64 cxxOperatorCallExpr(
65 unless(isInTemplateInstantiation()), hasOverloadedOperatorName("="),
66 hasArgument(0, declRefExpr(to(decl().bind("LHS")))),
67 hasArgument(1, IgnoringTemporaries(
68 callExpr(callee(StrCat), hasArgument(0, AlphaNum),
69 unless(HasAnotherReferenceToLhs))
70 .bind("Call"))))
71 .bind("Op"),
72 this);
73 }
74
75 void StrCatAppendCheck::check(const MatchFinder::MatchResult &Result) {
76 const auto *Op = Result.Nodes.getNodeAs<CXXOperatorCallExpr>("Op");
77 const auto *Call = Result.Nodes.getNodeAs<CallExpr>("Call");
78 assert(Op != nullptr && Call != nullptr && "Matcher does not work as expected");
79
80 // Handles the case 'x = absl::StrCat(x)', which has no effect.
81 if (Call->getNumArgs() == 1) {
82 diag(Op->getBeginLoc(), "call to 'absl::StrCat' has no effect");
83 return;
84 }
85
86 // Emit a warning and emit fixits to go from
87 // x = absl::StrCat(x, ...)
88 // to
89 // absl::StrAppend(&x, ...)
90 diag(Op->getBeginLoc(),
91 "call 'absl::StrAppend' instead of 'absl::StrCat' when appending to a "
92 "string to avoid a performance penalty")
93 << FixItHint::CreateReplacement(
94 CharSourceRange::getTokenRange(Op->getBeginLoc(),
95 Call->getCallee()->getEndLoc()),
96 "absl::StrAppend")
97 << FixItHint::CreateInsertion(Call->getArg(0)->getBeginLoc(), "&");
98 }
99
100 } // namespace abseil
101 } // namespace tidy
102 } // namespace clang
clang::ast_matchers
Definition: AbseilMatcher.h:15
Result
llvm::Optional< llvm::Expected< tooling::AtomicChanges > > Result
Definition: ClangdServer.cpp:60
clang
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
Definition: ChangeNamespace.cpp:18
clang::ast_matchers::AST_MATCHER_P
AST_MATCHER_P(FunctionDecl, throws, internal::Matcher< Type >, InnerMatcher)
Definition: ExceptionEscapeCheck.cpp:139
StrCatAppendCheck.h