blob: 9e03fbc139ed6d6e5502ca01b18b882b3a48c828 [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)
27 : ClangTidyCheck(Name, Context), StrictMode(Options.get("StrictMode", 0)) {}
28
29void InefficientStringConcatenationCheck::registerMatchers(
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
69void InefficientStringConcatenationCheck::check(
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