MissingHashCheck.cpp 1.8 KB

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