Alexander Kornienko | dcbf57d | 2016-08-03 23:06:03 +0000 | [diff] [blame^] | 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 | |
| 10 | #include "InefficientStringConcatenationCheck.h" |
| 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( |
| 21 | ClangTidyOptions::OptionMap &Opts) { |
| 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 | |
| 29 | void 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 | |
| 69 | void 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 |