Fangrui Song | ffe9f00 | 2019-03-01 09:52:53 +0000 | [diff] [blame] | 1 | //===-- StringCompareCheck.cpp - clang-tidy--------------------------------===// |
Mads Ravn | 72bcc04 | 2016-12-30 10:09:46 +0000 | [diff] [blame] | 2 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 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 |
Mads Ravn | 72bcc04 | 2016-12-30 10:09:46 +0000 | [diff] [blame] | 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 { |
Alexander Kornienko | 5c9c042 | 2018-01-30 14:55:50 +0000 | [diff] [blame] | 19 | namespace readability { |
Mads Ravn | 72bcc04 | 2016-12-30 10:09:46 +0000 | [diff] [blame] | 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")) { |
Stephen Kelly | 43465bf | 2018-08-09 22:42:26 +0000 | [diff] [blame] | 53 | diag(Matched->getBeginLoc(), CompareMessage); |
Mads Ravn | 72bcc04 | 2016-12-30 10:09:46 +0000 | [diff] [blame] | 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 | |
Stephen Kelly | 43465bf | 2018-08-09 22:42:26 +0000 | [diff] [blame] | 65 | auto Diag = diag(Matched->getBeginLoc(), CompareMessage); |
Mads Ravn | 72bcc04 | 2016-12-30 10:09:46 +0000 | [diff] [blame] | 66 | |
| 67 | if (Str1->isArrow()) |
Stephen Kelly | 43465bf | 2018-08-09 22:42:26 +0000 | [diff] [blame] | 68 | Diag << FixItHint::CreateInsertion(Str1->getBeginLoc(), "*"); |
Mads Ravn | 72bcc04 | 2016-12-30 10:09:46 +0000 | [diff] [blame] | 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 | |
Alexander Kornienko | 5c9c042 | 2018-01-30 14:55:50 +0000 | [diff] [blame] | 79 | } // namespace readability |
Mads Ravn | 72bcc04 | 2016-12-30 10:09:46 +0000 | [diff] [blame] | 80 | } // namespace tidy |
| 81 | } // namespace clang |