1 //===--- StringFindStrContainsCheck.cc - clang-tidy------------------------===//
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
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
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
));
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"))});
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
)) {
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