InefficientStringConcatenationCheck.cpp 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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. #include "InefficientStringConcatenationCheck.h"
  9. #include "clang/AST/ASTContext.h"
  10. #include "clang/ASTMatchers/ASTMatchFinder.h"
  11. using namespace clang::ast_matchers;
  12. namespace clang::tidy::performance {
  13. void InefficientStringConcatenationCheck::storeOptions(
  14. ClangTidyOptions::OptionMap &Opts) {
  15. Options.store(Opts, "StrictMode", StrictMode);
  16. }
  17. InefficientStringConcatenationCheck::InefficientStringConcatenationCheck(
  18. StringRef Name, ClangTidyContext *Context)
  19. : ClangTidyCheck(Name, Context),
  20. StrictMode(Options.getLocalOrGlobal("StrictMode", false)) {}
  21. void InefficientStringConcatenationCheck::registerMatchers(
  22. MatchFinder *Finder) {
  23. const auto BasicStringType =
  24. hasType(qualType(hasUnqualifiedDesugaredType(recordType(
  25. hasDeclaration(cxxRecordDecl(hasName("::std::basic_string")))))));
  26. const auto BasicStringPlusOperator = cxxOperatorCallExpr(
  27. hasOverloadedOperatorName("+"),
  28. hasAnyArgument(ignoringImpCasts(declRefExpr(BasicStringType))));
  29. const auto PlusOperator =
  30. cxxOperatorCallExpr(
  31. hasOverloadedOperatorName("+"),
  32. hasAnyArgument(ignoringImpCasts(declRefExpr(BasicStringType))),
  33. hasDescendant(BasicStringPlusOperator))
  34. .bind("plusOperator");
  35. const auto AssignOperator = cxxOperatorCallExpr(
  36. hasOverloadedOperatorName("="),
  37. hasArgument(0, declRefExpr(BasicStringType,
  38. hasDeclaration(decl().bind("lhsStrT")))
  39. .bind("lhsStr")),
  40. hasArgument(1, stmt(hasDescendant(declRefExpr(
  41. hasDeclaration(decl(equalsBoundNode("lhsStrT"))))))),
  42. hasDescendant(BasicStringPlusOperator));
  43. if (StrictMode) {
  44. Finder->addMatcher(cxxOperatorCallExpr(anyOf(AssignOperator, PlusOperator)),
  45. this);
  46. } else {
  47. Finder->addMatcher(
  48. cxxOperatorCallExpr(anyOf(AssignOperator, PlusOperator),
  49. hasAncestor(stmt(anyOf(cxxForRangeStmt(),
  50. whileStmt(), forStmt())))),
  51. this);
  52. }
  53. }
  54. void InefficientStringConcatenationCheck::check(
  55. const MatchFinder::MatchResult &Result) {
  56. const auto *LhsStr = Result.Nodes.getNodeAs<DeclRefExpr>("lhsStr");
  57. const auto *PlusOperator =
  58. Result.Nodes.getNodeAs<CXXOperatorCallExpr>("plusOperator");
  59. const char *DiagMsg =
  60. "string concatenation results in allocation of unnecessary temporary "
  61. "strings; consider using 'operator+=' or 'string::append()' instead";
  62. if (LhsStr)
  63. diag(LhsStr->getExprLoc(), DiagMsg);
  64. else if (PlusOperator)
  65. diag(PlusOperator->getExprLoc(), DiagMsg);
  66. }
  67. } // namespace clang::tidy::performance