llvm-project
98 строк · 3.8 Кб
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>,21InnerMatcher) {22const Stmt *E = &Node;23while (true) {24if (const auto *MTE = dyn_cast<MaterializeTemporaryExpr>(E))25E = MTE->getSubExpr();26if (const auto *BTE = dyn_cast<CXXBindTemporaryExpr>(E))27E = BTE->getSubExpr();28if (const auto *ICE = dyn_cast<ImplicitCastExpr>(E))29E = ICE->getSubExpr();30else31break;32}33
34return InnerMatcher.matches(*E, Finder, Builder);35}
36
37} // namespace38
39// TODO: str += StrCat(...)
40// str.append(StrCat(...))
41
42void StrCatAppendCheck::registerMatchers(MatchFinder *Finder) {43const auto StrCat = functionDecl(hasName("::absl::StrCat"));44// The arguments of absl::StrCat are implicitly converted to AlphaNum. This45// matches to the arguments because of that behavior.46const auto AlphaNum = IgnoringTemporaries(cxxConstructExpr(47argumentCountIs(1), hasType(cxxRecordDecl(hasName("::absl::AlphaNum"))),48hasArgument(0, ignoringImpCasts(declRefExpr(to(equalsBoundNode("LHS")),49expr().bind("Arg0"))))));50
51const auto HasAnotherReferenceToLhs =52callExpr(hasAnyArgument(expr(hasDescendant(declRefExpr(53to(equalsBoundNode("LHS")), unless(equalsBoundNode("Arg0")))))));54
55// Now look for calls to operator= with an object on the LHS and a call to56// StrCat on the RHS. The first argument of the StrCat call should be the same57// as the LHS. Ignore calls from template instantiations.58Finder->addMatcher(59traverse(TK_AsIs,60cxxOperatorCallExpr(61unless(isInTemplateInstantiation()),62hasOverloadedOperatorName("="),63hasArgument(0, declRefExpr(to(decl().bind("LHS")))),64hasArgument(651, IgnoringTemporaries(66callExpr(callee(StrCat), hasArgument(0, AlphaNum),67unless(HasAnotherReferenceToLhs))68.bind("Call"))))69.bind("Op")),70this);71}
72
73void StrCatAppendCheck::check(const MatchFinder::MatchResult &Result) {74const auto *Op = Result.Nodes.getNodeAs<CXXOperatorCallExpr>("Op");75const auto *Call = Result.Nodes.getNodeAs<CallExpr>("Call");76assert(Op != nullptr && Call != nullptr && "Matcher does not work as expected");77
78// Handles the case 'x = absl::StrCat(x)', which has no effect.79if (Call->getNumArgs() == 1) {80diag(Op->getBeginLoc(), "call to 'absl::StrCat' has no effect");81return;82}83
84// Emit a warning and emit fixits to go from85// x = absl::StrCat(x, ...)86// to87// absl::StrAppend(&x, ...)88diag(Op->getBeginLoc(),89"call 'absl::StrAppend' instead of 'absl::StrCat' when appending to a "90"string to avoid a performance penalty")91<< FixItHint::CreateReplacement(92CharSourceRange::getTokenRange(Op->getBeginLoc(),93Call->getCallee()->getEndLoc()),94"absl::StrAppend")95<< FixItHint::CreateInsertion(Call->getArg(0)->getBeginLoc(), "&");96}
97
98} // namespace clang::tidy::abseil99