11 #include "../utils/FixItHintUtils.h" 12 #include "clang/AST/ASTContext.h" 13 #include "clang/ASTMatchers/ASTMatchFinder.h" 14 #include "clang/Tooling/FixIt.h" 20 namespace readability {
22 static const StringRef
CompareMessage =
"do not use 'compare' to test equality " 23 "of strings; use the string equality " 26 void StringCompareCheck::registerMatchers(MatchFinder *Finder) {
27 if (!getLangOpts().CPlusPlus)
30 const auto StrCompare = cxxMemberCallExpr(
31 callee(cxxMethodDecl(hasName(
"compare"),
32 ofClass(classTemplateSpecializationDecl(
33 hasName(
"::std::basic_string"))))),
34 hasArgument(0, expr().bind(
"str2")), argumentCountIs(1),
35 callee(memberExpr().bind(
"str1")));
38 Finder->addMatcher(implicitCastExpr(hasImplicitDestinationType(booleanType()),
45 binaryOperator(anyOf(hasOperatorName(
"=="), hasOperatorName(
"!=")),
46 hasEitherOperand(StrCompare.bind(
"compare")),
47 hasEitherOperand(integerLiteral(equals(0)).bind(
"zero")))
52 void StringCompareCheck::check(
const MatchFinder::MatchResult &Result) {
53 if (
const auto *Matched = Result.Nodes.getNodeAs<Stmt>(
"match1")) {
58 if (
const auto *Matched = Result.Nodes.getNodeAs<Stmt>(
"match2")) {
59 const ASTContext &
Ctx = *Result.Context;
61 if (
const auto *Zero = Result.Nodes.getNodeAs<Stmt>(
"zero")) {
62 const auto *Str1 = Result.Nodes.getNodeAs<MemberExpr>(
"str1");
63 const auto *Str2 = Result.Nodes.getNodeAs<Stmt>(
"str2");
64 const auto *Compare = Result.Nodes.getNodeAs<Stmt>(
"compare");
69 Diag << FixItHint::CreateInsertion(Str1->getLocStart(),
"*");
71 Diag << tooling::fixit::createReplacement(*Zero, *Str2, Ctx)
72 << tooling::fixit::createReplacement(*Compare, *Str1->getBase(),
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
tooling::ExecutionContext * Ctx
static const StringRef CompareMessage