clang-tools  4.0.0
InefficientStringConcatenationCheck.cpp
Go to the documentation of this file.
1 //===--- InefficientStringConcatenationCheck.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 
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 performance {
19 
20 void InefficientStringConcatenationCheck::storeOptions(
22  Options.store(Opts, "StrictMode", StrictMode);
23 }
24 
25 InefficientStringConcatenationCheck::InefficientStringConcatenationCheck(
26  StringRef Name, ClangTidyContext *Context)
27  : ClangTidyCheck(Name, Context), StrictMode(Options.get("StrictMode", 0)) {}
28 
30  MatchFinder *Finder) {
31  if (!getLangOpts().CPlusPlus)
32  return;
33 
34  const auto BasicStringType =
35  hasType(cxxRecordDecl(hasName("::std::basic_string")));
36 
37  const auto BasicStringPlusOperator = cxxOperatorCallExpr(
38  hasOverloadedOperatorName("+"),
39  hasAnyArgument(ignoringImpCasts(declRefExpr(BasicStringType))));
40 
41  const auto PlusOperator =
42  cxxOperatorCallExpr(
43  hasOverloadedOperatorName("+"),
44  hasAnyArgument(ignoringImpCasts(declRefExpr(BasicStringType))),
45  hasDescendant(BasicStringPlusOperator))
46  .bind("plusOperator");
47 
48  const auto AssignOperator = cxxOperatorCallExpr(
49  hasOverloadedOperatorName("="),
50  hasArgument(0, declRefExpr(BasicStringType,
51  hasDeclaration(decl().bind("lhsStrT")))
52  .bind("lhsStr")),
53  hasArgument(1, stmt(hasDescendant(declRefExpr(
54  hasDeclaration(decl(equalsBoundNode("lhsStrT"))))))),
55  hasDescendant(BasicStringPlusOperator));
56 
57  if (StrictMode) {
58  Finder->addMatcher(cxxOperatorCallExpr(anyOf(AssignOperator, PlusOperator)),
59  this);
60  } else {
61  Finder->addMatcher(
62  cxxOperatorCallExpr(anyOf(AssignOperator, PlusOperator),
63  hasAncestor(stmt(anyOf(cxxForRangeStmt(),
64  whileStmt(), forStmt())))),
65  this);
66  }
67 }
68 
70  const MatchFinder::MatchResult &Result) {
71  const auto *LhsStr = Result.Nodes.getNodeAs<DeclRefExpr>("lhsStr");
72  const auto *PlusOperator =
73  Result.Nodes.getNodeAs<CXXOperatorCallExpr>("plusOperator");
74  const auto DiagMsg =
75  "string concatenation results in allocation of unnecessary temporary "
76  "strings; consider using 'operator+=' or 'string::append()' instead";
77 
78  if (LhsStr)
79  diag(LhsStr->getExprLoc(), DiagMsg);
80  else if (PlusOperator)
81  diag(PlusOperator->getExprLoc(), DiagMsg);
82 }
83 
84 } // namespace performance
85 } // namespace tidy
86 } // namespace clang
const std::string Name
Definition: USRFinder.cpp:164
LangOptions getLangOpts() const
Returns the language options from the context.
Definition: ClangTidy.h:187
std::unique_ptr< ast_matchers::MatchFinder > Finder
Definition: ClangTidy.cpp:262
void registerMatchers(ast_matchers::MatchFinder *Finder) override
Override this to register AST matchers with Finder.
Base class for all clang-tidy checks.
Definition: ClangTidy.h:127
std::map< std::string, std::string > OptionMap
ClangTidyContext & Context
Definition: ClangTidy.cpp:87
Every ClangTidyCheck reports errors through a DiagnosticsEngine provided by this context.
void check(const ast_matchers::MatchFinder::MatchResult &Result) override
ClangTidyChecks that register ASTMatchers should do the actual work in here.
DiagnosticBuilder diag(SourceLocation Loc, StringRef Description, DiagnosticIDs::Level Level=DiagnosticIDs::Warning)
Add a diagnostic with the check's name.
Definition: ClangTidy.cpp:403
const NamedDecl * Result
Definition: USRFinder.cpp:162