[clang][modules] Don't prevent translation of FW_Private includes when explicitly...
[llvm-project.git] / clang-tools-extra / clang-tidy / abseil / StringFindStrContainsCheck.cpp
blob28b2f81fdc8c84ae561c9852538f1b08fe3256dc
1 //===--- StringFindStrContainsCheck.cc - clang-tidy------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
9 #include "StringFindStrContainsCheck.h"
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"
18 // FixItHint - Hint to check documentation script to mark this check as
19 // providing a FixIt.
21 using namespace clang::ast_matchers;
23 namespace clang::tidy::abseil {
25 using ::clang::transformer::addInclude;
26 using ::clang::transformer::applyFirst;
27 using ::clang::transformer::cat;
28 using ::clang::transformer::changeTo;
29 using ::clang::transformer::makeRule;
30 using ::clang::transformer::node;
31 using ::clang::transformer::RewriteRuleWith;
33 AST_MATCHER(Type, isCharType) { return Node.isCharType(); }
35 static const char DefaultStringLikeClasses[] = "::std::basic_string;"
36 "::std::basic_string_view;"
37 "::absl::string_view";
38 static const char DefaultAbseilStringsMatchHeader[] = "absl/strings/match.h";
40 static transformer::RewriteRuleWith<std::string>
41 makeRewriteRule(ArrayRef<StringRef> StringLikeClassNames,
42 StringRef AbseilStringsMatchHeader) {
43 auto StringLikeClass = cxxRecordDecl(hasAnyName(StringLikeClassNames));
44 auto StringType =
45 hasUnqualifiedDesugaredType(recordType(hasDeclaration(StringLikeClass)));
46 auto CharStarType =
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(
52 callee(cxxMethodDecl(
53 hasName("find"), parameterCountIs(2),
54 hasParameter(
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(
63 {makeRule(
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")),
71 makeRule(
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"))});
80 return Rule;
83 StringFindStrContainsCheck::StringFindStrContainsCheck(
84 StringRef Name, ClangTidyContext *Context)
85 : TransformerClangTidyCheck(Name, Context),
86 StringLikeClassesOption(utils::options::parseStringList(
87 Options.get("StringLikeClasses", DefaultStringLikeClasses))),
88 AbseilStringsMatchHeaderOption(Options.get(
89 "AbseilStringsMatchHeader", DefaultAbseilStringsMatchHeader)) {
90 setRule(
91 makeRewriteRule(StringLikeClassesOption, AbseilStringsMatchHeaderOption));
94 bool StringFindStrContainsCheck::isLanguageVersionSupported(
95 const LangOptions &LangOpts) const {
96 return LangOpts.CPlusPlus11;
99 void StringFindStrContainsCheck::storeOptions(
100 ClangTidyOptions::OptionMap &Opts) {
101 TransformerClangTidyCheck::storeOptions(Opts);
102 Options.store(Opts, "StringLikeClasses",
103 utils::options::serializeStringList(StringLikeClassesOption));
104 Options.store(Opts, "AbseilStringsMatchHeader",
105 AbseilStringsMatchHeaderOption);
108 } // namespace clang::tidy::abseil