blob: 1064c3a8ea4a355854c5df9abaac37a28e9ae50c [file] [log] [blame]
Fangrui Songffe9f002019-03-01 09:52:53 +00001//===-- StringCompareCheck.cpp - clang-tidy--------------------------------===//
Mads Ravn72bcc042016-12-30 10:09:46 +00002//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// 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 Ravn72bcc042016-12-30 10:09:46 +00006//
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
15using namespace clang::ast_matchers;
16
17namespace clang {
18namespace tidy {
Alexander Kornienko5c9c0422018-01-30 14:55:50 +000019namespace readability {
Mads Ravn72bcc042016-12-30 10:09:46 +000020
21static const StringRef CompareMessage = "do not use 'compare' to test equality "
22 "of strings; use the string equality "
23 "operator instead";
24
25void 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
51void StringCompareCheck::check(const MatchFinder::MatchResult &Result) {
52 if (const auto *Matched = Result.Nodes.getNodeAs<Stmt>("match1")) {
Stephen Kelly43465bf2018-08-09 22:42:26 +000053 diag(Matched->getBeginLoc(), CompareMessage);
Mads Ravn72bcc042016-12-30 10:09:46 +000054 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 Kelly43465bf2018-08-09 22:42:26 +000065 auto Diag = diag(Matched->getBeginLoc(), CompareMessage);
Mads Ravn72bcc042016-12-30 10:09:46 +000066
67 if (Str1->isArrow())
Stephen Kelly43465bf2018-08-09 22:42:26 +000068 Diag << FixItHint::CreateInsertion(Str1->getBeginLoc(), "*");
Mads Ravn72bcc042016-12-30 10:09:46 +000069
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 Kornienko5c9c0422018-01-30 14:55:50 +000079} // namespace readability
Mads Ravn72bcc042016-12-30 10:09:46 +000080} // namespace tidy
81} // namespace clang