1 //===-- StringCompareCheck.cpp - 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 9 #include "StringCompareCheck.h" 10 #include "../utils/FixItHintUtils.h" 11 #include "clang/AST/ASTContext.h" 12 #include "clang/ASTMatchers/ASTMatchFinder.h" 13 #include "clang/Tooling/FixIt.h" 14 15 using namespace clang::ast_matchers; 16 17 namespace clang { 18 namespace tidy { 19 namespace readability { 20 21 static const StringRef CompareMessage = "do not use 'compare' to test equality " 22 "of strings; use the string equality " 23 "operator instead"; 24 25 void StringCompareCheck::registerMatchers(MatchFinder *Finder) { 26 if (!getLangOpts().CPlusPlus) 27 return; 28 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"))); 35 36 // First and second case: cast str.compare(str) to boolean. 37 Finder->addMatcher(implicitCastExpr(hasImplicitDestinationType(booleanType()), 38 has(StrCompare)) 39 .bind("match1"), 40 this); 41 42 // Third and fourth case: str.compare(str) == 0 and str.compare(str) != 0. 43 Finder->addMatcher( 44 binaryOperator(anyOf(hasOperatorName("=="), hasOperatorName("!=")), 45 hasEitherOperand(StrCompare.bind("compare")), 46 hasEitherOperand(integerLiteral(equals(0)).bind("zero"))) 47 .bind("match2"), 48 this); 49 } 50 51 void StringCompareCheck::check(const MatchFinder::MatchResult &Result) { 52 if (const auto *Matched = Result.Nodes.getNodeAs<Stmt>("match1")) { 53 diag(Matched->getBeginLoc(), CompareMessage); 54 return; 55 } 56 57 if (const auto *Matched = Result.Nodes.getNodeAs<Stmt>("match2")) { 58 const ASTContext &Ctx = *Result.Context; 59 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"); 64 65 auto Diag = diag(Matched->getBeginLoc(), CompareMessage); 66 67 if (Str1->isArrow()) 68 Diag << FixItHint::CreateInsertion(Str1->getBeginLoc(), "*"); 69 70 Diag << tooling::fixit::createReplacement(*Zero, *Str2, Ctx) 71 << tooling::fixit::createReplacement(*Compare, *Str1->getBase(), 72 Ctx); 73 } 74 } 75 76 // FIXME: Add fixit to fix the code for case one and two (match1). 77 } 78 79 } // namespace readability 80 } // namespace tidy 81 } // namespace clang 82