clang-tools 22.0.0git
MissingHashCheck.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 "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,
20 ast_matchers::internal::Matcher<ObjCInterfaceDecl>, Base) {
21 const ObjCInterfaceDecl *InterfaceDecl = Node.getClassInterface();
22 return Base.matches(*InterfaceDecl, Finder, Builder);
23}
24
25AST_MATCHER_P(ObjCContainerDecl, hasInstanceMethod,
26 ast_matchers::internal::Matcher<ObjCMethodDecl>, Base) {
27 // Check each instance method against the provided matcher.
28 return llvm::any_of(Node.instance_methods(), [&](const ObjCMethodDecl *I) {
29 return Base.matches(*I, Finder, Builder);
30 });
31}
32
33} // namespace
34
35void MissingHashCheck::registerMatchers(MatchFinder *Finder) {
36 Finder->addMatcher(
37 objcMethodDecl(
38 hasName("isEqual:"), isInstanceMethod(),
39 hasDeclContext(objcImplementationDecl(
40 hasInterface(isDirectlyDerivedFrom("NSObject")),
41 unless(hasInstanceMethod(hasName("hash"))))
42 .bind("impl"))),
43 this);
44}
45
46void MissingHashCheck::check(const MatchFinder::MatchResult &Result) {
47 const auto *ID = Result.Nodes.getNodeAs<ObjCImplementationDecl>("impl");
48 diag(ID->getLocation(), "%0 implements -isEqual: without implementing -hash")
49 << ID;
50}
51
52} // namespace clang::tidy::objc
void registerMatchers(ast_matchers::MatchFinder *Finder) override
void check(const ast_matchers::MatchFinder::MatchResult &Result) override
AST_MATCHER_P(Stmt, isStatementIdenticalToBoundNode, std::string, ID)