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 
registerMatchers(MatchFinder * Finder)25 void StringCompareCheck::registerMatchers(MatchFinder *Finder) {
26   const auto StrCompare = cxxMemberCallExpr(
27       callee(cxxMethodDecl(hasName("compare"),
28                            ofClass(classTemplateSpecializationDecl(
29                                hasName("::std::basic_string"))))),
30       hasArgument(0, expr().bind("str2")), argumentCountIs(1),
31       callee(memberExpr().bind("str1")));
32 
33   // First and second case: cast str.compare(str) to boolean.
34   Finder->addMatcher(
35       traverse(ast_type_traits::TK_AsIs,
36                implicitCastExpr(hasImplicitDestinationType(booleanType()),
37                                 has(StrCompare))
38                    .bind("match1")),
39       this);
40 
41   // Third and fourth case: str.compare(str) == 0 and str.compare(str) != 0.
42   Finder->addMatcher(
43       binaryOperator(hasAnyOperatorName("==", "!="),
44                      hasOperands(StrCompare.bind("compare"),
45                                  integerLiteral(equals(0)).bind("zero")))
46           .bind("match2"),
47       this);
48 }
49 
check(const MatchFinder::MatchResult & Result)50 void StringCompareCheck::check(const MatchFinder::MatchResult &Result) {
51   if (const auto *Matched = Result.Nodes.getNodeAs<Stmt>("match1")) {
52     diag(Matched->getBeginLoc(), CompareMessage);
53     return;
54   }
55 
56   if (const auto *Matched = Result.Nodes.getNodeAs<Stmt>("match2")) {
57     const ASTContext &Ctx = *Result.Context;
58 
59     if (const auto *Zero = Result.Nodes.getNodeAs<Stmt>("zero")) {
60       const auto *Str1 = Result.Nodes.getNodeAs<MemberExpr>("str1");
61       const auto *Str2 = Result.Nodes.getNodeAs<Stmt>("str2");
62       const auto *Compare = Result.Nodes.getNodeAs<Stmt>("compare");
63 
64       auto Diag = diag(Matched->getBeginLoc(), CompareMessage);
65 
66       if (Str1->isArrow())
67         Diag << FixItHint::CreateInsertion(Str1->getBeginLoc(), "*");
68 
69       Diag << tooling::fixit::createReplacement(*Zero, *Str2, Ctx)
70            << tooling::fixit::createReplacement(*Compare, *Str1->getBase(),
71                                                 Ctx);
72     }
73   }
74 
75   // FIXME: Add fixit to fix the code for case one and two (match1).
76 }
77 
78 } // namespace readability
79 } // namespace tidy
80 } // namespace clang
81