llvm-project
54 строки · 1.8 Кб
1//===--- MissingHashCheck.cpp - clang-tidy --------------------------------===//
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 "MissingHashCheck.h"
10#include "clang/AST/ASTContext.h"
11#include "clang/ASTMatchers/ASTMatchFinder.h"
12
13using namespace clang::ast_matchers;
14
15namespace clang::tidy::objc {
16
17namespace {
18
19AST_MATCHER_P(ObjCImplementationDecl, hasInterface,
20ast_matchers::internal::Matcher<ObjCInterfaceDecl>, Base) {
21const ObjCInterfaceDecl *InterfaceDecl = Node.getClassInterface();
22return Base.matches(*InterfaceDecl, Finder, Builder);
23}
24
25AST_MATCHER_P(ObjCContainerDecl, hasInstanceMethod,
26ast_matchers::internal::Matcher<ObjCMethodDecl>, Base) {
27// Check each instance method against the provided matcher.
28for (const auto *I : Node.instance_methods()) {
29if (Base.matches(*I, Finder, Builder))
30return true;
31}
32return false;
33}
34
35} // namespace
36
37void MissingHashCheck::registerMatchers(MatchFinder *Finder) {
38Finder->addMatcher(
39objcMethodDecl(
40hasName("isEqual:"), isInstanceMethod(),
41hasDeclContext(objcImplementationDecl(
42hasInterface(isDirectlyDerivedFrom("NSObject")),
43unless(hasInstanceMethod(hasName("hash"))))
44.bind("impl"))),
45this);
46}
47
48void MissingHashCheck::check(const MatchFinder::MatchResult &Result) {
49const auto *ID = Result.Nodes.getNodeAs<ObjCImplementationDecl>("impl");
50diag(ID->getLocation(), "%0 implements -isEqual: without implementing -hash")
51<< ID;
52}
53
54} // namespace clang::tidy::objc
55