1 //===--- InefficientStringConcatenationCheck.cpp - clang-tidy--------------===//
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
7 //===----------------------------------------------------------------------===//
9 #include "InefficientStringConcatenationCheck.h"
10 #include "clang/AST/ASTContext.h"
11 #include "clang/ASTMatchers/ASTMatchFinder.h"
13 using namespace clang::ast_matchers
;
15 namespace clang::tidy::performance
{
17 void InefficientStringConcatenationCheck::storeOptions(
18 ClangTidyOptions::OptionMap
&Opts
) {
19 Options
.store(Opts
, "StrictMode", StrictMode
);
22 InefficientStringConcatenationCheck::InefficientStringConcatenationCheck(
23 StringRef Name
, ClangTidyContext
*Context
)
24 : ClangTidyCheck(Name
, Context
),
25 StrictMode(Options
.getLocalOrGlobal("StrictMode", false)) {}
27 void InefficientStringConcatenationCheck::registerMatchers(
28 MatchFinder
*Finder
) {
29 const auto BasicStringType
=
30 hasType(qualType(hasUnqualifiedDesugaredType(recordType(
31 hasDeclaration(cxxRecordDecl(hasName("::std::basic_string")))))));
33 const auto BasicStringPlusOperator
= cxxOperatorCallExpr(
34 hasOverloadedOperatorName("+"),
35 hasAnyArgument(ignoringImpCasts(declRefExpr(BasicStringType
))));
37 const auto PlusOperator
=
39 hasOverloadedOperatorName("+"),
40 hasAnyArgument(ignoringImpCasts(declRefExpr(BasicStringType
))),
41 hasDescendant(BasicStringPlusOperator
))
42 .bind("plusOperator");
44 const auto AssignOperator
= cxxOperatorCallExpr(
45 hasOverloadedOperatorName("="),
46 hasArgument(0, declRefExpr(BasicStringType
,
47 hasDeclaration(decl().bind("lhsStrT")))
49 hasArgument(1, stmt(hasDescendant(declRefExpr(
50 hasDeclaration(decl(equalsBoundNode("lhsStrT"))))))),
51 hasDescendant(BasicStringPlusOperator
));
54 Finder
->addMatcher(cxxOperatorCallExpr(anyOf(AssignOperator
, PlusOperator
)),
58 cxxOperatorCallExpr(anyOf(AssignOperator
, PlusOperator
),
59 hasAncestor(stmt(anyOf(cxxForRangeStmt(),
60 whileStmt(), forStmt())))),
65 void InefficientStringConcatenationCheck::check(
66 const MatchFinder::MatchResult
&Result
) {
67 const auto *LhsStr
= Result
.Nodes
.getNodeAs
<DeclRefExpr
>("lhsStr");
68 const auto *PlusOperator
=
69 Result
.Nodes
.getNodeAs
<CXXOperatorCallExpr
>("plusOperator");
71 "string concatenation results in allocation of unnecessary temporary "
72 "strings; consider using 'operator+=' or 'string::append()' instead";
75 diag(LhsStr
->getExprLoc(), DiagMsg
);
76 else if (PlusOperator
)
77 diag(PlusOperator
->getExprLoc(), DiagMsg
);
80 } // namespace clang::tidy::performance