xref: /llvm-project/clang-tools-extra/clang-tidy/objc/MissingHashCheck.cpp (revision 11a411a49b62c129bba551df4587dd446fcdc660)
12f6a5281SStephane Moore //===--- MissingHashCheck.cpp - clang-tidy --------------------------------===//
22f6a5281SStephane Moore //
32f6a5281SStephane Moore // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
42f6a5281SStephane Moore // See https://llvm.org/LICENSE.txt for license information.
52f6a5281SStephane Moore // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
62f6a5281SStephane Moore //
72f6a5281SStephane Moore //===----------------------------------------------------------------------===//
82f6a5281SStephane Moore 
92f6a5281SStephane Moore #include "MissingHashCheck.h"
102f6a5281SStephane Moore #include "clang/AST/ASTContext.h"
112f6a5281SStephane Moore #include "clang/ASTMatchers/ASTMatchFinder.h"
122f6a5281SStephane Moore 
132f6a5281SStephane Moore using namespace clang::ast_matchers;
142f6a5281SStephane Moore 
157d2ea6c4SCarlos Galvez namespace clang::tidy::objc {
162f6a5281SStephane Moore 
172f6a5281SStephane Moore namespace {
182f6a5281SStephane Moore 
AST_MATCHER_P(ObjCImplementationDecl,hasInterface,ast_matchers::internal::Matcher<ObjCInterfaceDecl>,Base)19*11a411a4SPiotr Zegar AST_MATCHER_P(ObjCImplementationDecl, hasInterface,
20*11a411a4SPiotr Zegar               ast_matchers::internal::Matcher<ObjCInterfaceDecl>, Base) {
21*11a411a4SPiotr Zegar   const ObjCInterfaceDecl *InterfaceDecl = Node.getClassInterface();
22*11a411a4SPiotr Zegar   return Base.matches(*InterfaceDecl, Finder, Builder);
23*11a411a4SPiotr Zegar }
24*11a411a4SPiotr Zegar 
AST_MATCHER_P(ObjCContainerDecl,hasInstanceMethod,ast_matchers::internal::Matcher<ObjCMethodDecl>,Base)252f6a5281SStephane Moore AST_MATCHER_P(ObjCContainerDecl, hasInstanceMethod,
262f6a5281SStephane Moore               ast_matchers::internal::Matcher<ObjCMethodDecl>, Base) {
272f6a5281SStephane Moore   // Check each instance method against the provided matcher.
282f6a5281SStephane Moore   for (const auto *I : Node.instance_methods()) {
292f6a5281SStephane Moore     if (Base.matches(*I, Finder, Builder))
302f6a5281SStephane Moore       return true;
312f6a5281SStephane Moore   }
322f6a5281SStephane Moore   return false;
332f6a5281SStephane Moore }
342f6a5281SStephane Moore 
352f6a5281SStephane Moore } // namespace
362f6a5281SStephane Moore 
registerMatchers(MatchFinder * Finder)372f6a5281SStephane Moore void MissingHashCheck::registerMatchers(MatchFinder *Finder) {
382f6a5281SStephane Moore   Finder->addMatcher(
392f6a5281SStephane Moore       objcMethodDecl(
402f6a5281SStephane Moore           hasName("isEqual:"), isInstanceMethod(),
412f6a5281SStephane Moore           hasDeclContext(objcImplementationDecl(
422f6a5281SStephane Moore                              hasInterface(isDirectlyDerivedFrom("NSObject")),
432f6a5281SStephane Moore                              unless(hasInstanceMethod(hasName("hash"))))
442f6a5281SStephane Moore                              .bind("impl"))),
452f6a5281SStephane Moore       this);
462f6a5281SStephane Moore }
472f6a5281SStephane Moore 
check(const MatchFinder::MatchResult & Result)482f6a5281SStephane Moore void MissingHashCheck::check(const MatchFinder::MatchResult &Result) {
492f6a5281SStephane Moore   const auto *ID = Result.Nodes.getNodeAs<ObjCImplementationDecl>("impl");
502f6a5281SStephane Moore   diag(ID->getLocation(), "%0 implements -isEqual: without implementing -hash")
512f6a5281SStephane Moore       << ID;
522f6a5281SStephane Moore }
532f6a5281SStephane Moore 
547d2ea6c4SCarlos Galvez } // namespace clang::tidy::objc
55