StringFindStrContainsCheck.cpp
4.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
//===--- StringFindStrContainsCheck.cc - clang-tidy------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
#include "StringFindStrContainsCheck.h"
#include "../utils/OptionsUtils.h"
#include "clang/AST/ASTContext.h"
#include "clang/ASTMatchers/ASTMatchers.h"
#include "clang/Frontend/CompilerInstance.h"
#include "clang/Tooling/Transformer/RewriteRule.h"
#include "clang/Tooling/Transformer/Stencil.h"
// FixItHint - Hint to check documentation script to mark this check as
// providing a FixIt.
using namespace clang::ast_matchers;
namespace clang {
namespace tidy {
namespace abseil {
using ::clang::transformer::applyFirst;
using ::clang::transformer::cat;
using ::clang::transformer::change;
using ::clang::transformer::makeRule;
using ::clang::transformer::node;
static const char DefaultStringLikeClasses[] = "::std::basic_string;"
"::std::basic_string_view;"
"::absl::string_view";
static const char DefaultAbseilStringsMatchHeader[] = "absl/strings/match.h";
static llvm::Optional<transformer::RewriteRule>
MakeRule(const LangOptions &LangOpts,
const ClangTidyCheck::OptionsView &Options) {
// Parse options.
//
// FIXME(tdl-g): These options are being parsed redundantly with the
// constructor because TransformerClangTidyCheck forces us to provide MakeRule
// before "this" is fully constructed, but StoreOptions requires us to store
// the parsed options in "this". We need to fix TransformerClangTidyCheck and
// then we can clean this up.
const std::vector<std::string> StringLikeClassNames =
utils::options::parseStringList(
Options.get("StringLikeClasses", DefaultStringLikeClasses));
const std::string AbseilStringsMatchHeader =
Options.get("AbseilStringsMatchHeader", DefaultAbseilStringsMatchHeader);
auto StringLikeClass = cxxRecordDecl(hasAnyName(SmallVector<StringRef, 4>(
StringLikeClassNames.begin(), StringLikeClassNames.end())));
auto StringType =
hasUnqualifiedDesugaredType(recordType(hasDeclaration(StringLikeClass)));
auto CharStarType =
hasUnqualifiedDesugaredType(pointerType(pointee(isAnyCharacter())));
auto StringNpos = declRefExpr(
to(varDecl(hasName("npos"), hasDeclContext(StringLikeClass))));
auto StringFind = cxxMemberCallExpr(
callee(cxxMethodDecl(
hasName("find"),
hasParameter(0, parmVarDecl(anyOf(hasType(StringType),
hasType(CharStarType)))))),
on(hasType(StringType)), hasArgument(0, expr().bind("parameter_to_find")),
anyOf(hasArgument(1, integerLiteral(equals(0))),
hasArgument(1, cxxDefaultArgExpr())),
onImplicitObjectArgument(expr().bind("string_being_searched")));
tooling::RewriteRule rule = applyFirst(
{makeRule(binaryOperator(hasOperatorName("=="),
hasOperands(ignoringParenImpCasts(StringNpos),
ignoringParenImpCasts(StringFind))),
change(cat("!absl::StrContains(", node("string_being_searched"),
", ", node("parameter_to_find"), ")")),
cat("use !absl::StrContains instead of find() == npos")),
makeRule(binaryOperator(hasOperatorName("!="),
hasOperands(ignoringParenImpCasts(StringNpos),
ignoringParenImpCasts(StringFind))),
change(cat("absl::StrContains(", node("string_being_searched"),
", ", node("parameter_to_find"), ")")),
cat("use absl::StrContains instead of find() != npos"))});
addInclude(rule, AbseilStringsMatchHeader);
return rule;
}
StringFindStrContainsCheck::StringFindStrContainsCheck(
StringRef Name, ClangTidyContext *Context)
: TransformerClangTidyCheck(&MakeRule, Name, Context),
StringLikeClassesOption(utils::options::parseStringList(
Options.get("StringLikeClasses", DefaultStringLikeClasses))),
AbseilStringsMatchHeaderOption(Options.get(
"AbseilStringsMatchHeader", DefaultAbseilStringsMatchHeader)) {}
bool StringFindStrContainsCheck::isLanguageVersionSupported(
const LangOptions &LangOpts) const {
return LangOpts.CPlusPlus11;
}
void StringFindStrContainsCheck::storeOptions(
ClangTidyOptions::OptionMap &Opts) {
TransformerClangTidyCheck::storeOptions(Opts);
Options.store(Opts, "StringLikeClasses",
utils::options::serializeStringList(StringLikeClassesOption));
Options.store(Opts, "AbseilStringsMatchHeader",
AbseilStringsMatchHeaderOption);
}
} // namespace abseil
} // namespace tidy
} // namespace clang