|
| 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 { |
| 17 | +namespace tidy { |
| 18 | +namespace objc { |
| 19 | + |
| 20 | +// Mapping from `XCTAssert*Equal` to `XCTAssert*EqualObjects` name. |
| 21 | +static const std::map<std::string, std::string> &NameMap() { |
| 22 | + static std::map<std::string, std::string> map{ |
| 23 | + {"XCTAssertEqual", "XCTAssertEqualObjects"}, |
| 24 | + {"XCTAssertNotEqual", "XCTAssertNotEqualObjects"}, |
| 25 | + |
| 26 | + }; |
| 27 | + return map; |
| 28 | +} |
| 29 | + |
| 30 | +void AssertEquals::registerMatchers(MatchFinder *finder) { |
| 31 | + for (const auto &pair : NameMap()) { |
| 32 | + finder->addMatcher( |
| 33 | + binaryOperator(anyOf(hasOperatorName("!="), hasOperatorName("==")), |
| 34 | + isExpandedFromMacro(pair.first), |
| 35 | + anyOf(hasLHS(hasType(qualType( |
| 36 | + hasCanonicalType(asString("NSString *"))))), |
| 37 | + hasRHS(hasType(qualType( |
| 38 | + hasCanonicalType(asString("NSString *")))))) |
| 39 | + |
| 40 | + ) |
| 41 | + .bind(pair.first), |
| 42 | + this); |
| 43 | + } |
| 44 | +} |
| 45 | + |
| 46 | +void AssertEquals::check(const ast_matchers::MatchFinder::MatchResult &result) { |
| 47 | + for (const auto &pair : NameMap()) { |
| 48 | + if (const auto *root = result.Nodes.getNodeAs<BinaryOperator>(pair.first)) { |
| 49 | + SourceManager *sm = result.SourceManager; |
| 50 | + // The macros are nested two levels, so going up twice. |
| 51 | + auto macro_callsite = sm->getImmediateMacroCallerLoc( |
| 52 | + sm->getImmediateMacroCallerLoc(root->getBeginLoc())); |
| 53 | + diag(macro_callsite, "use " + pair.second + " for comparing objects") |
| 54 | + << FixItHint::CreateReplacement( |
| 55 | + clang::CharSourceRange::getCharRange( |
| 56 | + macro_callsite, |
| 57 | + macro_callsite.getLocWithOffset(pair.first.length())), |
| 58 | + pair.second); |
| 59 | + } |
| 60 | + } |
| 61 | +} |
| 62 | + |
| 63 | +} // namespace objc |
| 64 | +} // namespace tidy |
| 65 | +} // namespace clang |
0 commit comments