clang-tools 22.0.0git
AssertEquals.cpp
Go to the documentation of this file.
1//===----------------------------------------------------------------------===//
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
14using namespace clang::ast_matchers;
15
17
18// Mapping from `XCTAssert*Equal` to `XCTAssert*EqualObjects` name.
19static 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
28void 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
44void 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 MacroCallsite = Sm->getImmediateMacroCallerLoc(
50 Sm->getImmediateMacroCallerLoc(Root->getBeginLoc()));
51 diag(MacroCallsite, "use " + Pair.second + " for comparing objects")
52 << FixItHint::CreateReplacement(
53 clang::CharSourceRange::getCharRange(
54 MacroCallsite,
55 MacroCallsite.getLocWithOffset(Pair.first.length())),
56 Pair.second);
57 }
58 }
59}
60
61} // namespace clang::tidy::objc
void check(const ast_matchers::MatchFinder::MatchResult &Result) override
void registerMatchers(ast_matchers::MatchFinder *Finder) override
static const std::map< std::string, std::string > & nameMap()