clang-tools  10.0.0
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 
13 using namespace clang::ast_matchers;
14 
15 namespace clang {
16 namespace tidy {
17 namespace performance {
18 
19 void InefficientStringConcatenationCheck::storeOptions(
21  Options.store(Opts, "StrictMode", StrictMode);
22 }
23 
24 InefficientStringConcatenationCheck::InefficientStringConcatenationCheck(
25  StringRef Name, ClangTidyContext *Context)
26  : ClangTidyCheck(Name, Context),
27  StrictMode(Options.getLocalOrGlobal("StrictMode", 0)) {}
28 
30  MatchFinder *Finder) {
31  if (!getLangOpts().CPlusPlus)
32  return;
33 
34  const auto BasicStringType =
35  hasType(qualType(hasUnqualifiedDesugaredType(recordType(
36  hasDeclaration(cxxRecordDecl(hasName("::std::basic_string")))))));
37 
38  const auto BasicStringPlusOperator = cxxOperatorCallExpr(
39  hasOverloadedOperatorName("+"),
40  hasAnyArgument(ignoringImpCasts(declRefExpr(BasicStringType))));
41 
42  const auto PlusOperator =
43  cxxOperatorCallExpr(
44  hasOverloadedOperatorName("+"),
45  hasAnyArgument(ignoringImpCasts(declRefExpr(BasicStringType))),
46  hasDescendant(BasicStringPlusOperator))
47  .bind("plusOperator");
48 
49  const auto AssignOperator = cxxOperatorCallExpr(
50  hasOverloadedOperatorName("="),
51  hasArgument(0, declRefExpr(BasicStringType,
52  hasDeclaration(decl().bind("lhsStrT")))
53  .bind("lhsStr")),
54  hasArgument(1, stmt(hasDescendant(declRefExpr(
55  hasDeclaration(decl(equalsBoundNode("lhsStrT"))))))),
56  hasDescendant(BasicStringPlusOperator));
57 
58  if (StrictMode) {
59  Finder->addMatcher(cxxOperatorCallExpr(anyOf(AssignOperator, PlusOperator)),
60  this);
61  } else {
62  Finder->addMatcher(
63  cxxOperatorCallExpr(anyOf(AssignOperator, PlusOperator),
64  hasAncestor(stmt(anyOf(cxxForRangeStmt(),
65  whileStmt(), forStmt())))),
66  this);
67  }
68 }
69 
71  const MatchFinder::MatchResult &Result) {
72  const auto *LhsStr = Result.Nodes.getNodeAs<DeclRefExpr>("lhsStr");
73  const auto *PlusOperator =
74  Result.Nodes.getNodeAs<CXXOperatorCallExpr>("plusOperator");
75  const auto DiagMsg =
76  "string concatenation results in allocation of unnecessary temporary "
77  "strings; consider using 'operator+=' or 'string::append()' instead";
78 
79  if (LhsStr)
80  diag(LhsStr->getExprLoc(), DiagMsg);
81  else if (PlusOperator)
82  diag(PlusOperator->getExprLoc(), DiagMsg);
83 }
84 
85 } // namespace performance
86 } // namespace tidy
87 } // namespace clang
void registerMatchers(ast_matchers::MatchFinder *Finder) override
Override this to register AST matchers with Finder.
Base class for all clang-tidy checks.
const LangOptions & getLangOpts() const
Returns the language options from the context.
static constexpr llvm::StringLiteral Name
std::map< std::string, std::string > OptionMap
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
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&#39;s name.