11 #include "../utils/OptionsUtils.h"
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"
17 using namespace clang::ast_matchers;
21 namespace performance {
25 llvm::Optional<std::string> MakeCharacterLiteral(
const StringLiteral *Literal) {
28 llvm::raw_string_ostream OS(Result);
29 Literal->outputString(OS);
32 auto pos = Result.find_first_of(
'"');
33 if (pos == Result.npos)
return llvm::None;
35 pos = Result.find_last_of(
'"');
36 if (pos == Result.npos)
return llvm::None;
43 return hasType(qualType(anyOf(substTemplateTypeParmType(),
44 hasDescendant(substTemplateTypeParmType()))));
49 FasterStringFindCheck::FasterStringFindCheck(StringRef
Name,
53 Options.get(
"StringLikeClasses",
"std::basic_string"))) {
65 const auto SingleChar =
66 expr(ignoringParenCasts(stringLiteral(hasSize(1)).bind(
"literal")));
68 const auto StringFindFunctions =
69 anyOf(hasName(
"find"), hasName(
"rfind"), hasName(
"find_first_of"),
70 hasName(
"find_first_not_of"), hasName(
"find_last_of"),
71 hasName(
"find_last_not_of"));
73 llvm::Optional<ast_matchers::internal::Matcher<NamedDecl>> IsStringClass;
75 for (
const auto &ClassName : StringLikeClasses) {
76 const auto HasName = hasName(ClassName);
77 IsStringClass = IsStringClass ? anyOf(*IsStringClass, HasName) : HasName;
83 callee(functionDecl(StringFindFunctions).bind(
"func")),
84 anyOf(argumentCountIs(1), argumentCountIs(2)),
85 hasArgument(0, SingleChar),
86 on(expr(hasType(recordDecl(*IsStringClass)),
87 unless(hasSubstitutedType())))),
93 const auto *Literal = Result.Nodes.getNodeAs<StringLiteral>(
"literal");
94 const auto *FindFunc = Result.Nodes.getNodeAs<FunctionDecl>(
"func");
96 auto Replacement = MakeCharacterLiteral(Literal);
100 diag(Literal->getLocStart(),
"%0 called with a string literal consisting of "
101 "a single character; consider using the more "
102 "effective overload accepting a character")
103 << FindFunc << FixItHint::CreateReplacement(
104 CharSourceRange::getTokenRange(Literal->getLocStart(),
105 Literal->getLocEnd()),
LangOptions getLangOpts() const
Returns the language options from the context.
std::string serializeStringList(ArrayRef< std::string > Strings)
Serialize a sequence of names that can be parsed by parseStringList.
std::unique_ptr< ast_matchers::MatchFinder > Finder
Base class for all clang-tidy checks.
std::vector< std::string > parseStringList(StringRef Option)
Parse a semicolon separated list of strings.
AST_MATCHER_FUNCTION(ast_matchers::TypeMatcher, isReferenceToConst)
void store(ClangTidyOptions::OptionMap &Options, StringRef LocalName, StringRef Value) const
Stores an option with the check-local name LocalName with string value Value to Options.
std::map< std::string, std::string > OptionMap
ClangTidyContext & Context
Every ClangTidyCheck reports errors through a DiagnosticsEngine provided by this context.
DiagnosticBuilder diag(SourceLocation Loc, StringRef Description, DiagnosticIDs::Level Level=DiagnosticIDs::Warning)
Add a diagnostic with the check's name.