blob: fb53167d3dd64d6fecc9649bd9880335691d4cb7 [file] [log] [blame]
Alexander Kornienkodcbf57d2016-08-03 23:06:03 +00001//===--- 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
10#include "InefficientStringConcatenationCheck.h"
11#include "clang/AST/ASTContext.h"
12#include "clang/ASTMatchers/ASTMatchFinder.h"
13
14using namespace clang::ast_matchers;
15
16namespace clang {
17namespace tidy {
18namespace performance {
19
20void InefficientStringConcatenationCheck::storeOptions(
21 ClangTidyOptions::OptionMap &Opts) {
22 Options.store(Opts, "StrictMode", StrictMode);
23}
24
25InefficientStringConcatenationCheck::InefficientStringConcatenationCheck(
26 StringRef Name, ClangTidyContext *Context)
Alexander Kornienko0055d972017-05-29 13:59:27 +000027 : ClangTidyCheck(Name, Context),
28 StrictMode(Options.getLocalOrGlobal("StrictMode", 0)) {}
Alexander Kornienkodcbf57d2016-08-03 23:06:03 +000029
30void InefficientStringConcatenationCheck::registerMatchers(
31 MatchFinder *Finder) {
32 if (!getLangOpts().CPlusPlus)
33 return;
34
35 const auto BasicStringType =
36 hasType(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
70void InefficientStringConcatenationCheck::check(
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