1 //===--- AssertEquals.cpp - clang-tidy --------------------------*- C++ -*-===//
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 "AssertEquals.h"
10
11 #include <map>
12 #include <string>
13
14 using namespace clang::ast_matchers;
15
16 namespace clang::tidy::objc {
17
18 // Mapping from `XCTAssert*Equal` to `XCTAssert*EqualObjects` name.
NameMap()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"},
23
24 };
25 return map;
26 }
27
registerMatchers(MatchFinder * finder)28 void AssertEquals::registerMatchers(MatchFinder *finder) {
29 for (const auto &pair : NameMap()) {
30 finder->addMatcher(
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 *"))))))
37
38 )
39 .bind(pair.first),
40 this);
41 }
42 }
43
check(const ast_matchers::MatchFinder::MatchResult & result)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(
54 macro_callsite,
55 macro_callsite.getLocWithOffset(pair.first.length())),
56 pair.second);
57 }
58 }
59 }
60
61 } // namespace clang::tidy::objc
62