clang-tools 19.0.0git
InefficientStringConcatenationCheck.cpp
Go to the documentation of this file.
1//===--- InefficientStringConcatenationCheck.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
10#include "clang/AST/ASTContext.h"
11#include "clang/ASTMatchers/ASTMatchFinder.h"
12
13using namespace clang::ast_matchers;
14
16
19 Options.store(Opts, "StrictMode", StrictMode);
20}
21
23 StringRef Name, ClangTidyContext *Context)
24 : ClangTidyCheck(Name, Context),
25 StrictMode(Options.getLocalOrGlobal("StrictMode", false)) {}
26
28 MatchFinder *Finder) {
29 const auto BasicStringType =
30 hasType(qualType(hasUnqualifiedDesugaredType(recordType(
31 hasDeclaration(cxxRecordDecl(hasName("::std::basic_string")))))));
32
33 const auto BasicStringPlusOperator = cxxOperatorCallExpr(
34 hasOverloadedOperatorName("+"),
35 hasAnyArgument(ignoringImpCasts(declRefExpr(BasicStringType))));
36
37 const auto PlusOperator =
38 cxxOperatorCallExpr(
39 hasOverloadedOperatorName("+"),
40 hasAnyArgument(ignoringImpCasts(declRefExpr(BasicStringType))),
41 hasDescendant(BasicStringPlusOperator))
42 .bind("plusOperator");
43
44 const auto AssignOperator = cxxOperatorCallExpr(
45 hasOverloadedOperatorName("="),
46 hasArgument(0, declRefExpr(BasicStringType,
47 hasDeclaration(decl().bind("lhsStrT")))
48 .bind("lhsStr")),
49 hasArgument(1, stmt(hasDescendant(declRefExpr(
50 hasDeclaration(decl(equalsBoundNode("lhsStrT"))))))),
51 hasDescendant(BasicStringPlusOperator));
52
53 if (StrictMode) {
54 Finder->addMatcher(cxxOperatorCallExpr(anyOf(AssignOperator, PlusOperator)),
55 this);
56 } else {
57 Finder->addMatcher(
58 cxxOperatorCallExpr(anyOf(AssignOperator, PlusOperator),
59 hasAncestor(stmt(anyOf(cxxForRangeStmt(),
60 whileStmt(), forStmt())))),
61 this);
62 }
63}
64
66 const MatchFinder::MatchResult &Result) {
67 const auto *LhsStr = Result.Nodes.getNodeAs<DeclRefExpr>("lhsStr");
68 const auto *PlusOperator =
69 Result.Nodes.getNodeAs<CXXOperatorCallExpr>("plusOperator");
70 const char *DiagMsg =
71 "string concatenation results in allocation of unnecessary temporary "
72 "strings; consider using 'operator+=' or 'string::append()' instead";
73
74 if (LhsStr)
75 diag(LhsStr->getExprLoc(), DiagMsg);
76 else if (PlusOperator)
77 diag(PlusOperator->getExprLoc(), DiagMsg);
78}
79
80} // namespace clang::tidy::performance
llvm::SmallString< 256U > Name
void store(ClangTidyOptions::OptionMap &Options, StringRef LocalName, StringRef Value) const
Stores an option with the check-local name LocalName with string value Value to Options.
Base class for all clang-tidy checks.
DiagnosticBuilder diag(SourceLocation Loc, StringRef Description, DiagnosticIDs::Level Level=DiagnosticIDs::Warning)
Add a diagnostic with the check's name.
Every ClangTidyCheck reports errors through a DiagnosticsEngine provided by this context.
void storeOptions(ClangTidyOptions::OptionMap &Opts) override
Should store all options supported by this check with their current values or default values for opti...
void registerMatchers(ast_matchers::MatchFinder *Finder) override
Override this to register AST matchers with Finder.
void check(const ast_matchers::MatchFinder::MatchResult &Result) override
ClangTidyChecks that register ASTMatchers should do the actual work in here.
llvm::StringMap< ClangTidyValue > OptionMap