clang-tools 10.0.0

MissingHashCheck.cpp
Go to the documentation of this file.
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
13 using namespace clang::ast_matchers;
14
15 namespace clang {
16 namespace tidy {
17 namespace objc {
18
19 namespace {
20
21 AST_MATCHER_P(ObjCImplementationDecl, hasInterface,
22 ast_matchers::internal::Matcher<ObjCInterfaceDecl>, Base) {
23 const ObjCInterfaceDecl *InterfaceDecl = Node.getClassInterface();
24 return Base.matches(*InterfaceDecl, Finder, Builder);
25 }
26
27 AST_MATCHER_P(ObjCContainerDecl, hasInstanceMethod,
28 ast_matchers::internal::Matcher<ObjCMethodDecl>, Base) {
29 // Check each instance method against the provided matcher.
30 for (const auto *I : Node.instance_methods()) {
31 if (Base.matches(*I, Finder, Builder))
32 return true;
33 }
34 return false;
35 }
36
37 } // namespace
38
39 void MissingHashCheck::registerMatchers(MatchFinder *Finder) {
40 // This check should only be applied to Objective-C sources.
41 if (!getLangOpts().ObjC)
42 return;
43
44 Finder->addMatcher(
45 objcMethodDecl(
46 hasName("isEqual:"), isInstanceMethod(),
47 hasDeclContext(objcImplementationDecl(
48 hasInterface(isDirectlyDerivedFrom("NSObject")),
49 unless(hasInstanceMethod(hasName("hash"))))
50 .bind("impl"))),
51 this);
52 }
53
54 void MissingHashCheck::check(const MatchFinder::MatchResult &Result) {
55 const auto *ID = Result.Nodes.getNodeAs<ObjCImplementationDecl>("impl");
56 diag(ID->getLocation(), "%0 implements -isEqual: without implementing -hash")
57 << ID;
58 }
59
60 } // namespace objc
61 } // namespace tidy
62 } // namespace clang
clang::ast_matchers
Definition: AbseilMatcher.h:14
MissingHashCheck.h
Builder
CodeCompletionBuilder Builder
Definition: CodeCompletionStringsTests.cpp:35
clang
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
Definition: ApplyReplacements.h:27
Base
std::unique_ptr< GlobalCompilationDatabase > Base
Definition: GlobalCompilationDatabaseTests.cpp:83
clang::tidy::readability::AST_MATCHER_P
AST_MATCHER_P(CXXMethodDecl, hasCanonicalDecl, ast_matchers::internal::Matcher< CXXMethodDecl >, InnerMatcher)
Definition: ConvertMemberFunctionsToStatic.cpp:52