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