[clang][modules] Don't prevent translation of FW_Private includes when explicitly...
[llvm-project.git] / clang-tools-extra / clang-tidy / objc / AssertEquals.cpp
blobda98e19c67412899bc123e0cf05e4831ec8d0096
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 //===----------------------------------------------------------------------===//
9 #include "AssertEquals.h"
11 #include <map>
12 #include <string>
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"},
25 return map;
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 *"))))))
39 .bind(pair.first),
40 this);
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);
61 } // namespace clang::tidy::objc