FasterStringFindCheck.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. //===--- FasterStringFindCheck.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 "FasterStringFindCheck.h"
  9. #include "../utils/OptionsUtils.h"
  10. #include "clang/AST/ASTContext.h"
  11. #include "clang/ASTMatchers/ASTMatchFinder.h"
  12. #include "llvm/Support/raw_ostream.h"
  13. #include <optional>
  14. using namespace clang::ast_matchers;
  15. namespace clang::tidy::performance {
  16. namespace {
  17. std::optional<std::string> makeCharacterLiteral(const StringLiteral *Literal) {
  18. std::string Result;
  19. {
  20. llvm::raw_string_ostream OS(Result);
  21. Literal->outputString(OS);
  22. }
  23. // Now replace the " with '.
  24. auto Pos = Result.find_first_of('"');
  25. if (Pos == Result.npos)
  26. return std::nullopt;
  27. Result[Pos] = '\'';
  28. Pos = Result.find_last_of('"');
  29. if (Pos == Result.npos)
  30. return std::nullopt;
  31. Result[Pos] = '\'';
  32. return Result;
  33. }
  34. AST_MATCHER_FUNCTION(ast_matchers::internal::Matcher<Expr>,
  35. hasSubstitutedType) {
  36. return hasType(qualType(anyOf(substTemplateTypeParmType(),
  37. hasDescendant(substTemplateTypeParmType()))));
  38. }
  39. } // namespace
  40. FasterStringFindCheck::FasterStringFindCheck(StringRef Name,
  41. ClangTidyContext *Context)
  42. : ClangTidyCheck(Name, Context),
  43. StringLikeClasses(utils::options::parseStringList(
  44. Options.get("StringLikeClasses",
  45. "::std::basic_string;::std::basic_string_view"))) {}
  46. void FasterStringFindCheck::storeOptions(ClangTidyOptions::OptionMap &Opts) {
  47. Options.store(Opts, "StringLikeClasses",
  48. utils::options::serializeStringList(StringLikeClasses));
  49. }
  50. void FasterStringFindCheck::registerMatchers(MatchFinder *Finder) {
  51. const auto SingleChar =
  52. expr(ignoringParenCasts(stringLiteral(hasSize(1)).bind("literal")));
  53. const auto StringFindFunctions =
  54. hasAnyName("find", "rfind", "find_first_of", "find_first_not_of",
  55. "find_last_of", "find_last_not_of");
  56. Finder->addMatcher(
  57. cxxMemberCallExpr(
  58. callee(functionDecl(StringFindFunctions).bind("func")),
  59. anyOf(argumentCountIs(1), argumentCountIs(2)),
  60. hasArgument(0, SingleChar),
  61. on(expr(hasType(hasUnqualifiedDesugaredType(recordType(hasDeclaration(
  62. recordDecl(hasAnyName(StringLikeClasses)))))),
  63. unless(hasSubstitutedType())))),
  64. this);
  65. }
  66. void FasterStringFindCheck::check(const MatchFinder::MatchResult &Result) {
  67. const auto *Literal = Result.Nodes.getNodeAs<StringLiteral>("literal");
  68. const auto *FindFunc = Result.Nodes.getNodeAs<FunctionDecl>("func");
  69. auto Replacement = makeCharacterLiteral(Literal);
  70. if (!Replacement)
  71. return;
  72. diag(Literal->getBeginLoc(), "%0 called with a string literal consisting of "
  73. "a single character; consider using the more "
  74. "effective overload accepting a character")
  75. << FindFunc
  76. << FixItHint::CreateReplacement(
  77. CharSourceRange::getTokenRange(Literal->getBeginLoc(),
  78. Literal->getEndLoc()),
  79. *Replacement);
  80. }
  81. } // namespace clang::tidy::performance