10 #include "../utils/OptionsUtils.h" 11 #include "clang/AST/ASTContext.h" 12 #include "clang/ASTMatchers/ASTMatchFinder.h" 13 #include "llvm/ADT/Optional.h" 14 #include "llvm/Support/raw_ostream.h" 20 namespace performance {
24 llvm::Optional<std::string> MakeCharacterLiteral(
const StringLiteral *Literal) {
27 llvm::raw_string_ostream OS(Result);
28 Literal->outputString(OS);
31 auto pos = Result.find_first_of(
'"');
32 if (pos == Result.npos)
35 pos = Result.find_last_of(
'"');
36 if (pos == Result.npos)
44 return hasType(qualType(anyOf(substTemplateTypeParmType(),
45 hasDescendant(substTemplateTypeParmType()))));
50 FasterStringFindCheck::FasterStringFindCheck(StringRef
Name,
54 Options.get(
"StringLikeClasses",
"std::basic_string"))) {}
65 const auto SingleChar =
66 expr(ignoringParenCasts(stringLiteral(hasSize(1)).bind(
"literal")));
67 const auto StringFindFunctions =
68 hasAnyName(
"find",
"rfind",
"find_first_of",
"find_first_not_of",
69 "find_last_of",
"find_last_not_of");
73 callee(functionDecl(StringFindFunctions).bind(
"func")),
74 anyOf(argumentCountIs(1), argumentCountIs(2)),
75 hasArgument(0, SingleChar),
77 hasType(hasUnqualifiedDesugaredType(recordType(hasDeclaration(
78 recordDecl(hasAnyName(SmallVector<StringRef, 4>(
79 StringLikeClasses.begin(), StringLikeClasses.end()))))))),
80 unless(hasSubstitutedType())))),
85 const auto *Literal = Result.Nodes.getNodeAs<StringLiteral>(
"literal");
86 const auto *FindFunc = Result.Nodes.getNodeAs<FunctionDecl>(
"func");
88 auto Replacement = MakeCharacterLiteral(Literal);
92 diag(Literal->getBeginLoc(),
"%0 called with a string literal consisting of " 93 "a single character; consider using the more " 94 "effective overload accepting a character")
96 << FixItHint::CreateReplacement(
98 Literal->getEndLoc()),
std::string serializeStringList(ArrayRef< std::string > Strings)
Serialize a sequence of names that can be parsed by parseStringList.
AST_MATCHER_FUNCTION(ast_matchers::internal::Matcher< FunctionDecl >, DurationConversionFunction)
Base class for all clang-tidy checks.
const LangOptions & getLangOpts() const
Returns the language options from the context.
std::vector< std::string > parseStringList(StringRef Option)
Parse a semicolon separated list of strings.
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.
static constexpr llvm::StringLiteral Name
std::map< std::string, std::string > OptionMap
llvm::Optional< Range > getTokenRange(const SourceManager &SM, const LangOptions &LangOpts, SourceLocation TokLoc)
Returns the taken range at TokLoc.
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
Every ClangTidyCheck reports errors through a DiagnosticsEngine provided by this context.
llvm::Optional< llvm::Expected< tooling::AtomicChanges > > Result
DiagnosticBuilder diag(SourceLocation Loc, StringRef Description, DiagnosticIDs::Level Level=DiagnosticIDs::Warning)
Add a diagnostic with the check's name.