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/Tooling/Transformer/RewriteRule.h"
16#include "clang/Tooling/Transformer/Stencil.h"
25using ::clang::transformer::addInclude;
26using ::clang::transformer::applyFirst;
27using ::clang::transformer::cat;
28using ::clang::transformer::changeTo;
29using ::clang::transformer::makeRule;
30using ::clang::transformer::node;
31using ::clang::transformer::RewriteRuleWith;
36 "::std::basic_string_view;"
37 "::absl::string_view";
40static transformer::RewriteRuleWith<std::string>
42 StringRef AbseilStringsMatchHeader) {
43 auto StringLikeClass = cxxRecordDecl(hasAnyName(StringLikeClassNames));
45 hasUnqualifiedDesugaredType(recordType(hasDeclaration(StringLikeClass)));
47 hasUnqualifiedDesugaredType(pointerType(pointee(isAnyCharacter())));
48 auto CharType = hasUnqualifiedDesugaredType(isCharType());
49 auto StringNpos = declRefExpr(
50 to(varDecl(hasName(
"npos"), hasDeclContext(StringLikeClass))));
51 auto StringFind = cxxMemberCallExpr(
53 hasName(
"find"), parameterCountIs(2),
55 0, parmVarDecl(anyOf(hasType(StringType), hasType(CharStarType),
56 hasType(CharType)))))),
57 on(hasType(StringType)), hasArgument(0, expr().bind(
"parameter_to_find")),
58 anyOf(hasArgument(1, integerLiteral(equals(0))),
59 hasArgument(1, cxxDefaultArgExpr())),
60 onImplicitObjectArgument(expr().bind(
"string_being_searched")));
62 RewriteRuleWith<std::string> Rule = applyFirst(
64 binaryOperator(hasOperatorName(
"=="),
65 hasOperands(ignoringParenImpCasts(StringNpos),
66 ignoringParenImpCasts(StringFind))),
67 {changeTo(cat(
"!absl::StrContains(", node(
"string_being_searched"),
68 ", ", node(
"parameter_to_find"),
")")),
69 addInclude(AbseilStringsMatchHeader)},
70 cat(
"use !absl::StrContains instead of find() == npos")),
72 binaryOperator(hasOperatorName(
"!="),
73 hasOperands(ignoringParenImpCasts(StringNpos),
74 ignoringParenImpCasts(StringFind))),
75 {changeTo(cat(
"absl::StrContains(", node(
"string_being_searched"),
76 ", ", node(
"parameter_to_find"),
")")),
77 addInclude(AbseilStringsMatchHeader)},
78 cat(
"use absl::StrContains instead "
79 "of find() != npos"))});
85 : TransformerClangTidyCheck(
Name, Context),
86 StringLikeClassesOption(utils::options::parseStringList(
88 AbseilStringsMatchHeaderOption(Options.get(
91 makeRewriteRule(StringLikeClassesOption, AbseilStringsMatchHeaderOption));
95 const LangOptions &LangOpts)
const {
96 return LangOpts.CPlusPlus11;
101 TransformerClangTidyCheck::storeOptions(Opts);
105 AbseilStringsMatchHeaderOption);
llvm::SmallString< 256U > Name
::clang::DynTypedNode Node
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.
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...
bool isLanguageVersionSupported(const LangOptions &LangOpts) const override
Override this to disable registering matchers and PP callbacks if an invalid language version is bein...
StringFindStrContainsCheck(StringRef Name, ClangTidyContext *Context)
static transformer::RewriteRuleWith< std::string > makeRewriteRule(ArrayRef< StringRef > StringLikeClassNames, StringRef AbseilStringsMatchHeader)
AST_MATCHER(Type, isCharType)
static const char DefaultAbseilStringsMatchHeader[]
const auto DefaultStringLikeClasses
std::string serializeStringList(ArrayRef< StringRef > Strings)
Serialize a sequence of names that can be parsed by parseStringList.
llvm::StringMap< ClangTidyValue > OptionMap