11 #include "clang/AST/ASTContext.h" 12 #include "clang/ASTMatchers/ASTMatchFinder.h" 18 namespace performance {
20 void InefficientStringConcatenationCheck::storeOptions(
22 Options.store(Opts,
"StrictMode", StrictMode);
25 InefficientStringConcatenationCheck::InefficientStringConcatenationCheck(
28 StrictMode(Options.getLocalOrGlobal(
"StrictMode", 0)) {}
31 MatchFinder *Finder) {
35 const auto BasicStringType =
36 hasType(qualType(hasUnqualifiedDesugaredType(recordType(
37 hasDeclaration(cxxRecordDecl(hasName(
"::std::basic_string")))))));
39 const auto BasicStringPlusOperator = cxxOperatorCallExpr(
40 hasOverloadedOperatorName(
"+"),
41 hasAnyArgument(ignoringImpCasts(declRefExpr(BasicStringType))));
43 const auto PlusOperator =
45 hasOverloadedOperatorName(
"+"),
46 hasAnyArgument(ignoringImpCasts(declRefExpr(BasicStringType))),
47 hasDescendant(BasicStringPlusOperator))
48 .bind(
"plusOperator");
50 const auto AssignOperator = cxxOperatorCallExpr(
51 hasOverloadedOperatorName(
"="),
52 hasArgument(0, declRefExpr(BasicStringType,
53 hasDeclaration(decl().bind(
"lhsStrT")))
55 hasArgument(1, stmt(hasDescendant(declRefExpr(
56 hasDeclaration(decl(equalsBoundNode(
"lhsStrT"))))))),
57 hasDescendant(BasicStringPlusOperator));
60 Finder->addMatcher(cxxOperatorCallExpr(anyOf(AssignOperator, PlusOperator)),
64 cxxOperatorCallExpr(anyOf(AssignOperator, PlusOperator),
65 hasAncestor(stmt(anyOf(cxxForRangeStmt(),
66 whileStmt(), forStmt())))),
72 const MatchFinder::MatchResult &Result) {
73 const auto *LhsStr = Result.Nodes.getNodeAs<DeclRefExpr>(
"lhsStr");
74 const auto *PlusOperator =
75 Result.Nodes.getNodeAs<CXXOperatorCallExpr>(
"plusOperator");
77 "string concatenation results in allocation of unnecessary temporary " 78 "strings; consider using 'operator+=' or 'string::append()' instead";
81 diag(LhsStr->getExprLoc(), DiagMsg);
82 else if (PlusOperator)
83 diag(PlusOperator->getExprLoc(), DiagMsg);
LangOptions getLangOpts() const
Returns the language options from the context.
Base class for all clang-tidy checks.
std::map< std::string, std::string > OptionMap
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
Every ClangTidyCheck reports errors through a DiagnosticsEngine provided by this context.
DiagnosticBuilder diag(SourceLocation Loc, StringRef Description, DiagnosticIDs::Level Level=DiagnosticIDs::Warning)
Add a diagnostic with the check's name.