Samuel Benzaquen | 51e1523 | 2016-02-12 19:28:14 +0000 | [diff] [blame] | 1 | //===--- FasterStringFindCheck.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 "FasterStringFindCheck.h" |
Etienne Bergeron | de1ec03 | 2016-05-10 15:31:15 +0000 | [diff] [blame] | 11 | #include "../utils/OptionsUtils.h" |
Samuel Benzaquen | 51e1523 | 2016-02-12 19:28:14 +0000 | [diff] [blame] | 12 | #include "clang/AST/ASTContext.h" |
| 13 | #include "clang/ASTMatchers/ASTMatchFinder.h" |
| 14 | #include "llvm/ADT/Optional.h" |
| 15 | #include "llvm/Support/raw_ostream.h" |
| 16 | |
| 17 | using namespace clang::ast_matchers; |
| 18 | |
| 19 | namespace clang { |
| 20 | namespace tidy { |
| 21 | namespace performance { |
| 22 | |
| 23 | namespace { |
| 24 | |
Samuel Benzaquen | 51e1523 | 2016-02-12 19:28:14 +0000 | [diff] [blame] | 25 | llvm::Optional<std::string> MakeCharacterLiteral(const StringLiteral *Literal) { |
| 26 | std::string Result; |
| 27 | { |
| 28 | llvm::raw_string_ostream OS(Result); |
| 29 | Literal->outputString(OS); |
| 30 | } |
| 31 | // Now replace the " with '. |
| 32 | auto pos = Result.find_first_of('"'); |
Mandeep Singh Grang | 7c7ea7d | 2016-11-08 07:50:19 +0000 | [diff] [blame] | 33 | if (pos == Result.npos) |
| 34 | return llvm::None; |
Samuel Benzaquen | 51e1523 | 2016-02-12 19:28:14 +0000 | [diff] [blame] | 35 | Result[pos] = '\''; |
| 36 | pos = Result.find_last_of('"'); |
Mandeep Singh Grang | 7c7ea7d | 2016-11-08 07:50:19 +0000 | [diff] [blame] | 37 | if (pos == Result.npos) |
| 38 | return llvm::None; |
Samuel Benzaquen | 51e1523 | 2016-02-12 19:28:14 +0000 | [diff] [blame] | 39 | Result[pos] = '\''; |
| 40 | return Result; |
| 41 | } |
| 42 | |
Samuel Benzaquen | 51e1523 | 2016-02-12 19:28:14 +0000 | [diff] [blame] | 43 | AST_MATCHER_FUNCTION(ast_matchers::internal::Matcher<Expr>, |
| 44 | hasSubstitutedType) { |
| 45 | return hasType(qualType(anyOf(substTemplateTypeParmType(), |
| 46 | hasDescendant(substTemplateTypeParmType())))); |
| 47 | } |
| 48 | |
| 49 | } // namespace |
| 50 | |
| 51 | FasterStringFindCheck::FasterStringFindCheck(StringRef Name, |
| 52 | ClangTidyContext *Context) |
| 53 | : ClangTidyCheck(Name, Context), |
Etienne Bergeron | de1ec03 | 2016-05-10 15:31:15 +0000 | [diff] [blame] | 54 | StringLikeClasses(utils::options::parseStringList( |
Mandeep Singh Grang | 7c7ea7d | 2016-11-08 07:50:19 +0000 | [diff] [blame] | 55 | Options.get("StringLikeClasses", "std::basic_string"))) {} |
Samuel Benzaquen | 51e1523 | 2016-02-12 19:28:14 +0000 | [diff] [blame] | 56 | |
| 57 | void FasterStringFindCheck::storeOptions(ClangTidyOptions::OptionMap &Opts) { |
| 58 | Options.store(Opts, "StringLikeClasses", |
Etienne Bergeron | de1ec03 | 2016-05-10 15:31:15 +0000 | [diff] [blame] | 59 | utils::options::serializeStringList(StringLikeClasses)); |
Samuel Benzaquen | 51e1523 | 2016-02-12 19:28:14 +0000 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | void FasterStringFindCheck::registerMatchers(MatchFinder *Finder) { |
| 63 | if (!getLangOpts().CPlusPlus) |
| 64 | return; |
| 65 | |
| 66 | const auto SingleChar = |
Etienne Bergeron | e15ef2f | 2016-05-17 19:36:09 +0000 | [diff] [blame] | 67 | expr(ignoringParenCasts(stringLiteral(hasSize(1)).bind("literal"))); |
Samuel Benzaquen | 51e1523 | 2016-02-12 19:28:14 +0000 | [diff] [blame] | 68 | const auto StringFindFunctions = |
Haojian Wu | 8fd7296 | 2017-04-24 16:41:00 +0000 | [diff] [blame] | 69 | hasAnyName("find", "rfind", "find_first_of", "find_first_not_of", |
| 70 | "find_last_of", "find_last_not_of"); |
Samuel Benzaquen | 51e1523 | 2016-02-12 19:28:14 +0000 | [diff] [blame] | 71 | |
Haojian Wu | 8fd7296 | 2017-04-24 16:41:00 +0000 | [diff] [blame] | 72 | Finder->addMatcher( |
| 73 | cxxMemberCallExpr( |
| 74 | callee(functionDecl(StringFindFunctions).bind("func")), |
| 75 | anyOf(argumentCountIs(1), argumentCountIs(2)), |
| 76 | hasArgument(0, SingleChar), |
Manuel Klimek | 7b9c117 | 2017-08-02 13:13:11 +0000 | [diff] [blame] | 77 | on(expr( |
| 78 | hasType(hasUnqualifiedDesugaredType(recordType(hasDeclaration( |
| 79 | recordDecl(hasAnyName(SmallVector<StringRef, 4>( |
| 80 | StringLikeClasses.begin(), StringLikeClasses.end()))))))), |
| 81 | unless(hasSubstitutedType())))), |
Haojian Wu | 8fd7296 | 2017-04-24 16:41:00 +0000 | [diff] [blame] | 82 | this); |
Samuel Benzaquen | 51e1523 | 2016-02-12 19:28:14 +0000 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | void FasterStringFindCheck::check(const MatchFinder::MatchResult &Result) { |
| 86 | const auto *Literal = Result.Nodes.getNodeAs<StringLiteral>("literal"); |
| 87 | const auto *FindFunc = Result.Nodes.getNodeAs<FunctionDecl>("func"); |
| 88 | |
| 89 | auto Replacement = MakeCharacterLiteral(Literal); |
| 90 | if (!Replacement) |
| 91 | return; |
| 92 | |
Stephen Kelly | 43465bf | 2018-08-09 22:42:26 +0000 | [diff] [blame] | 93 | diag(Literal->getBeginLoc(), "%0 called with a string literal consisting of " |
Samuel Benzaquen | 51e1523 | 2016-02-12 19:28:14 +0000 | [diff] [blame] | 94 | "a single character; consider using the more " |
| 95 | "effective overload accepting a character") |
Stephen Kelly | 43465bf | 2018-08-09 22:42:26 +0000 | [diff] [blame] | 96 | << FindFunc |
| 97 | << FixItHint::CreateReplacement( |
| 98 | CharSourceRange::getTokenRange(Literal->getBeginLoc(), |
Stephen Kelly | c09197e | 2018-08-09 22:43:02 +0000 | [diff] [blame] | 99 | Literal->getEndLoc()), |
Stephen Kelly | 43465bf | 2018-08-09 22:42:26 +0000 | [diff] [blame] | 100 | *Replacement); |
Samuel Benzaquen | 51e1523 | 2016-02-12 19:28:14 +0000 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | } // namespace performance |
| 104 | } // namespace tidy |
| 105 | } // namespace clang |