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