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/OptionsUtils.h" |
11 | #include "clang/AST/ASTContext.h" |
12 | #include "clang/ASTMatchers/ASTMatchFinder.h" |
13 | #include "clang/ASTMatchers/ASTMatchers.h" |
14 | #include "clang/Tooling/FixIt.h" |
15 | #include "llvm/ADT/StringRef.h" |
16 | |
17 | using namespace clang::ast_matchers; |
18 | namespace optutils = clang::tidy::utils::options; |
19 | |
20 | namespace clang::tidy::readability { |
21 | |
22 | static const StringRef CompareMessage = "do not use 'compare' to test equality " |
23 | "of strings; use the string equality " |
24 | "operator instead" ; |
25 | |
26 | static const StringRef DefaultStringLikeClasses = "::std::basic_string;" |
27 | "::std::basic_string_view" ; |
28 | |
29 | StringCompareCheck::StringCompareCheck(StringRef Name, |
30 | ClangTidyContext *Context) |
31 | : ClangTidyCheck(Name, Context), |
32 | StringLikeClasses(optutils::parseStringList( |
33 | Option: Options.get(LocalName: "StringLikeClasses" , Default: DefaultStringLikeClasses))) {} |
34 | |
35 | void StringCompareCheck::storeOptions(ClangTidyOptions::OptionMap &Opts) { |
36 | Options.store(Options&: Opts, LocalName: "StringLikeClasses" , |
37 | Value: optutils::serializeStringList(Strings: StringLikeClasses)); |
38 | } |
39 | |
40 | void StringCompareCheck::registerMatchers(MatchFinder *Finder) { |
41 | if (StringLikeClasses.empty()) { |
42 | return; |
43 | } |
44 | const auto StrCompare = cxxMemberCallExpr( |
45 | callee(InnerMatcher: cxxMethodDecl(hasName(Name: "compare" ), ofClass(InnerMatcher: cxxRecordDecl(hasAnyName( |
46 | StringLikeClasses))))), |
47 | hasArgument(N: 0, InnerMatcher: expr().bind(ID: "str2" )), argumentCountIs(N: 1), |
48 | callee(InnerMatcher: memberExpr().bind(ID: "str1" ))); |
49 | |
50 | // First and second case: cast str.compare(str) to boolean. |
51 | Finder->addMatcher( |
52 | NodeMatch: traverse(TK: TK_AsIs, |
53 | InnerMatcher: implicitCastExpr(hasImplicitDestinationType(InnerMatcher: booleanType()), |
54 | has(StrCompare)) |
55 | .bind(ID: "match1" )), |
56 | Action: this); |
57 | |
58 | // Third and fourth case: str.compare(str) == 0 and str.compare(str) != 0. |
59 | Finder->addMatcher( |
60 | NodeMatch: binaryOperator(hasAnyOperatorName("==" , "!=" ), |
61 | hasOperands(Matcher1: StrCompare.bind(ID: "compare" ), |
62 | Matcher2: integerLiteral(equals(Value: 0)).bind(ID: "zero" ))) |
63 | .bind(ID: "match2" ), |
64 | Action: this); |
65 | } |
66 | |
67 | void StringCompareCheck::check(const MatchFinder::MatchResult &Result) { |
68 | if (const auto *Matched = Result.Nodes.getNodeAs<Stmt>(ID: "match1" )) { |
69 | diag(Loc: Matched->getBeginLoc(), Description: CompareMessage); |
70 | return; |
71 | } |
72 | |
73 | if (const auto *Matched = Result.Nodes.getNodeAs<Stmt>(ID: "match2" )) { |
74 | const ASTContext &Ctx = *Result.Context; |
75 | |
76 | if (const auto *Zero = Result.Nodes.getNodeAs<Stmt>(ID: "zero" )) { |
77 | const auto *Str1 = Result.Nodes.getNodeAs<MemberExpr>(ID: "str1" ); |
78 | const auto *Str2 = Result.Nodes.getNodeAs<Stmt>(ID: "str2" ); |
79 | const auto *Compare = Result.Nodes.getNodeAs<Stmt>(ID: "compare" ); |
80 | |
81 | auto Diag = diag(Loc: Matched->getBeginLoc(), Description: CompareMessage); |
82 | |
83 | if (Str1->isArrow()) |
84 | Diag << FixItHint::CreateInsertion(InsertionLoc: Str1->getBeginLoc(), Code: "*" ); |
85 | |
86 | Diag << tooling::fixit::createReplacement(Destination: *Zero, Source: *Str2, Context: Ctx) |
87 | << tooling::fixit::createReplacement(Destination: *Compare, Source: *Str1->getBase(), |
88 | Context: Ctx); |
89 | } |
90 | } |
91 | |
92 | // FIXME: Add fixit to fix the code for case one and two (match1). |
93 | } |
94 | |
95 | } // namespace clang::tidy::readability |
96 | |