10#include "../utils/OptionsUtils.h"
11#include "clang/AST/ASTContext.h"
12#include "clang/ASTMatchers/ASTMatchFinder.h"
13#include "clang/ASTMatchers/ASTMatchers.h"
14#include "clang/Tooling/FixIt.h"
15#include "llvm/ADT/StringRef.h"
22static const StringRef
CompareMessage =
"do not use 'compare' to test equality "
23 "of strings; use the string equality "
27 "::std::basic_string_view";
32 StringLikeClasses(
optutils::parseStringList(
41 if (StringLikeClasses.empty()) {
44 const auto StrCompare = cxxMemberCallExpr(
45 callee(cxxMethodDecl(hasName(
"compare"), ofClass(cxxRecordDecl(hasAnyName(
46 StringLikeClasses))))),
47 hasArgument(0, expr().bind(
"str2")), argumentCountIs(1),
48 callee(memberExpr().bind(
"str1")));
53 implicitCastExpr(hasImplicitDestinationType(booleanType()),
60 binaryOperator(hasAnyOperatorName(
"==",
"!="),
61 hasOperands(StrCompare.bind(
"compare"),
62 integerLiteral(equals(0)).bind(
"zero")))
68 if (
const auto *Matched = Result.Nodes.getNodeAs<Stmt>(
"match1")) {
73 if (
const auto *Matched = Result.Nodes.getNodeAs<Stmt>(
"match2")) {
74 const ASTContext &Ctx = *Result.Context;
76 if (
const auto *Zero = Result.Nodes.getNodeAs<Stmt>(
"zero")) {
77 const auto *Str1 = Result.Nodes.getNodeAs<MemberExpr>(
"str1");
78 const auto *Str2 = Result.Nodes.getNodeAs<Stmt>(
"str2");
79 const auto *Compare = Result.Nodes.getNodeAs<Stmt>(
"compare");
84 Diag << FixItHint::CreateInsertion(Str1->getBeginLoc(),
"*");
86 Diag << tooling::fixit::createReplacement(*Zero, *Str2, Ctx)
87 << tooling::fixit::createReplacement(*Compare, *Str1->getBase(),
llvm::SmallString< 256U > Name
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.
Base class for all clang-tidy checks.
DiagnosticBuilder diag(SourceLocation Loc, StringRef Description, DiagnosticIDs::Level Level=DiagnosticIDs::Warning)
Add a diagnostic with the check's name.
Every ClangTidyCheck reports errors through a DiagnosticsEngine provided by this context.
void registerMatchers(ast_matchers::MatchFinder *Finder) override
Override this to register AST matchers with Finder.
void check(const ast_matchers::MatchFinder::MatchResult &Result) override
ClangTidyChecks that register ASTMatchers should do the actual work in here.
StringCompareCheck(StringRef Name, ClangTidyContext *Context)
void storeOptions(ClangTidyOptions::OptionMap &Opts) override
Should store all options supported by this check with their current values or default values for opti...
static const StringRef DefaultStringLikeClasses
static const StringRef CompareMessage
std::string serializeStringList(ArrayRef< StringRef > Strings)
Serialize a sequence of names that can be parsed by parseStringList.
llvm::StringMap< ClangTidyValue > OptionMap