1 //===--- AssertEquals.cpp - clang-tidy --------------------------*- C++ -*-===//
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
7 //===----------------------------------------------------------------------===//
9 #include "AssertEquals.h"
14 using namespace clang::ast_matchers
;
16 namespace clang::tidy::objc
{
18 // Mapping from `XCTAssert*Equal` to `XCTAssert*EqualObjects` name.
19 static const std::map
<std::string
, std::string
> &NameMap() {
20 static std::map
<std::string
, std::string
> map
{
21 {"XCTAssertEqual", "XCTAssertEqualObjects"},
22 {"XCTAssertNotEqual", "XCTAssertNotEqualObjects"},
28 void AssertEquals::registerMatchers(MatchFinder
*finder
) {
29 for (const auto &pair
: NameMap()) {
31 binaryOperator(anyOf(hasOperatorName("!="), hasOperatorName("==")),
32 isExpandedFromMacro(pair
.first
),
33 anyOf(hasLHS(hasType(qualType(
34 hasCanonicalType(asString("NSString *"))))),
35 hasRHS(hasType(qualType(
36 hasCanonicalType(asString("NSString *"))))))
44 void AssertEquals::check(const ast_matchers::MatchFinder::MatchResult
&result
) {
45 for (const auto &pair
: NameMap()) {
46 if (const auto *root
= result
.Nodes
.getNodeAs
<BinaryOperator
>(pair
.first
)) {
47 SourceManager
*sm
= result
.SourceManager
;
48 // The macros are nested two levels, so going up twice.
49 auto macro_callsite
= sm
->getImmediateMacroCallerLoc(
50 sm
->getImmediateMacroCallerLoc(root
->getBeginLoc()));
51 diag(macro_callsite
, "use " + pair
.second
+ " for comparing objects")
52 << FixItHint::CreateReplacement(
53 clang::CharSourceRange::getCharRange(
55 macro_callsite
.getLocWithOffset(pair
.first
.length())),
61 } // namespace clang::tidy::objc