11#include "../utils/OptionsUtils.h"
12#include "clang/AST/ASTContext.h"
13#include "clang/ASTMatchers/ASTMatchers.h"
14#include "clang/Frontend/CompilerInstance.h"
15#include "clang/Lex/Lexer.h"
16#include "clang/Lex/Preprocessor.h"
23 "::std::basic_string;::std::basic_string_view";
28 StringLikeClasses(utils::options::parseStringList(
30 IncludeInserter(Options.getLocalOrGlobal(
"IncludeStyle",
31 utils::IncludeSorter::IS_LLVM),
32 areDiagsSelfContained()),
33 AbseilStringsMatchHeader(
34 Options.get(
"AbseilStringsMatchHeader",
"absl/strings/match.h")) {}
37 auto ZeroLiteral = integerLiteral(equals(0));
38 auto StringClassMatcher = cxxRecordDecl(hasAnyName(StringLikeClasses));
39 auto StringType = hasUnqualifiedDesugaredType(
40 recordType(hasDeclaration(StringClassMatcher)));
42 auto StringFind = cxxMemberCallExpr(
44 callee(cxxMethodDecl(hasName(
"find")).bind(
"findfun")),
45 on(hasType(StringType)),
47 hasArgument(0, expr().bind(
"needle")),
49 anyOf(hasArgument(1, ZeroLiteral), hasArgument(1, cxxDefaultArgExpr())));
54 hasAnyOperatorName(
"==",
"!="),
55 hasOperands(ignoringParenImpCasts(ZeroLiteral),
56 ignoringParenImpCasts(StringFind.bind(
"findexpr"))))
60 auto StringRFind = cxxMemberCallExpr(
62 callee(cxxMethodDecl(hasName(
"rfind")).bind(
"findfun")),
63 on(hasType(StringType)),
65 hasArgument(0, expr().bind(
"needle")),
67 hasArgument(1, ZeroLiteral));
73 hasAnyOperatorName(
"==",
"!="),
74 hasOperands(ignoringParenImpCasts(ZeroLiteral),
75 ignoringParenImpCasts(StringRFind.bind(
"findexpr"))))
81 const ASTContext &Context = *Result.Context;
82 const SourceManager &Source = Context.getSourceManager();
85 const auto *ComparisonExpr = Result.Nodes.getNodeAs<BinaryOperator>(
"expr");
86 assert(ComparisonExpr !=
nullptr);
87 const auto *Needle = Result.Nodes.getNodeAs<Expr>(
"needle");
88 assert(Needle !=
nullptr);
89 const Expr *Haystack = Result.Nodes.getNodeAs<CXXMemberCallExpr>(
"findexpr")
90 ->getImplicitObjectArgument();
91 assert(Haystack !=
nullptr);
92 const auto *FindFun = Result.Nodes.getNodeAs<CXXMethodDecl>(
"findfun");
93 assert(FindFun !=
nullptr);
95 bool Rev = FindFun->getName().contains(
"rfind");
97 if (ComparisonExpr->getBeginLoc().isMacroID())
102 const StringRef NeedleExprCode = Lexer::getSourceText(
103 CharSourceRange::getTokenRange(Needle->getSourceRange()), Source,
104 Context.getLangOpts());
105 const StringRef HaystackExprCode = Lexer::getSourceText(
106 CharSourceRange::getTokenRange(Haystack->getSourceRange()), Source,
107 Context.getLangOpts());
110 bool Neg = ComparisonExpr->getOpcode() == BO_NE;
114 diag(ComparisonExpr->getBeginLoc(),
115 "use %select{absl::StartsWith|!absl::StartsWith}0 "
116 "instead of %select{find()|rfind()}1 %select{==|!=}0 0")
120 ComparisonExpr->getSourceRange(),
121 ((Neg ?
"!absl::StartsWith(" :
"absl::StartsWith(") + HaystackExprCode +
122 ", " + NeedleExprCode +
")")
128 Source.getFileID(ComparisonExpr->getBeginLoc()),
129 AbseilStringsMatchHeader);
133 const SourceManager &SM, Preprocessor *PP, Preprocessor *ModuleExpanderPP) {
142 Options.
store(Opts,
"AbseilStringsMatchHeader", AbseilStringsMatchHeader);
llvm::SmallString< 256U > Name
DiagnosticCallback Diagnostic
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.
Base class for all clang-tidy checks.
DiagnosticBuilder diag(SourceLocation Loc, StringRef Description, DiagnosticIDs::Level Level=DiagnosticIDs::Warning)
Add a diagnostic with the check's name.
Every ClangTidyCheck reports errors through a DiagnosticsEngine provided by this context.
void storeOptions(ClangTidyOptions::OptionMap &Opts) override
Should store all options supported by this check with their current values or default values for opti...
void registerMatchers(ast_matchers::MatchFinder *Finder) override
Override this to register AST matchers with Finder.
StringFindStartswithCheck(StringRef Name, ClangTidyContext *Context)
void registerPPCallbacks(const SourceManager &SM, Preprocessor *PP, Preprocessor *ModuleExpanderPP) override
Override this to register PPCallbacks in the preprocessor.
void check(const ast_matchers::MatchFinder::MatchResult &Result) override
ClangTidyChecks that register ASTMatchers should do the actual work in here.
void registerPreprocessor(Preprocessor *PP)
Registers this with the Preprocessor PP, must be called before this class is used.
std::optional< FixItHint > createIncludeInsertion(FileID FileID, llvm::StringRef Header)
Creates a Header inclusion directive fixit in the File FileID.
IncludeSorter::IncludeStyle getStyle() const
const auto DefaultStringLikeClasses
std::string serializeStringList(ArrayRef< StringRef > Strings)
Serialize a sequence of names that can be parsed by parseStringList.
llvm::StringMap< ClangTidyValue > OptionMap