clang-tools 19.0.0git
AssertEquals.cpp
Go to the documentation of this file.
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
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 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
DiagnosticBuilder diag(SourceLocation Loc, StringRef Description, DiagnosticIDs::Level Level=DiagnosticIDs::Warning)
Add a diagnostic with the check's name.
void check(const ast_matchers::MatchFinder::MatchResult &Result) override
ClangTidyChecks that register ASTMatchers should do the actual work in here.
void registerMatchers(ast_matchers::MatchFinder *Finder) override
Override this to register AST matchers with Finder.
static const std::map< std::string, std::string > & NameMap()