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

source code of clang-tools-extra/clang-tidy/abseil/StrCatAppendCheck.cpp