clang-tools 20.0.0git
StringFindStrContainsCheck.cpp
Go to the documentation of this file.
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//===----------------------------------------------------------------------===//
8
10
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"
17
18// FixItHint - Hint to check documentation script to mark this check as
19// providing a FixIt.
20
21using namespace clang::ast_matchers;
22
23namespace clang::tidy::abseil {
24
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;
32
33AST_MATCHER(Type, isCharType) { return Node.isCharType(); }
34
35static const char DefaultStringLikeClasses[] = "::std::basic_string;"
36 "::std::basic_string_view;"
37 "::absl::string_view";
38static const char DefaultAbseilStringsMatchHeader[] = "absl/strings/match.h";
39
40static transformer::RewriteRuleWith<std::string>
41makeRewriteRule(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")));
61
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;
81}
82
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));
92}
93
95 const LangOptions &LangOpts) const {
96 return LangOpts.CPlusPlus11;
97}
98
101 TransformerClangTidyCheck::storeOptions(Opts);
102 Options.store(Opts, "StringLikeClasses",
103 utils::options::serializeStringList(StringLikeClassesOption));
104 Options.store(Opts, "AbseilStringsMatchHeader",
105 AbseilStringsMatchHeaderOption);
106}
107
108} // namespace clang::tidy::abseil
llvm::SmallString< 256U > Name
NodeType Type
::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)
void setRule(transformer::RewriteRuleWith< std::string > R)
Set the rule that this check implements.
static transformer::RewriteRuleWith< std::string > makeRewriteRule(ArrayRef< StringRef > StringLikeClassNames, StringRef AbseilStringsMatchHeader)
static const char DefaultAbseilStringsMatchHeader[]
std::string serializeStringList(ArrayRef< StringRef > Strings)
Serialize a sequence of names that can be parsed by parseStringList.
llvm::StringMap< ClangTidyValue > OptionMap