1 //===--- DeallocInCategoryCheck.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 "DeallocInCategoryCheck.h" 10 #include "clang/AST/ASTContext.h" 11 #include "clang/AST/DeclObjC.h" 12 #include "clang/ASTMatchers/ASTMatchFinder.h" 13 14 using namespace clang::ast_matchers; 15 16 namespace clang::tidy::objc { 17 registerMatchers(MatchFinder * Finder)18void DeallocInCategoryCheck::registerMatchers(MatchFinder *Finder) { 19 // Non-NSObject/NSProxy-derived objects may not have -dealloc as a special 20 // method. However, it seems highly unrealistic to expect many false-positives 21 // by warning on -dealloc in categories on classes without one of those 22 // base classes. 23 Finder->addMatcher( 24 objcMethodDecl(isInstanceMethod(), hasName("dealloc"), 25 hasDeclContext(objcCategoryImplDecl().bind("impl"))) 26 .bind("dealloc"), 27 this); 28 } 29 check(const MatchFinder::MatchResult & Result)30void DeallocInCategoryCheck::check(const MatchFinder::MatchResult &Result) { 31 const auto *DeallocDecl = Result.Nodes.getNodeAs<ObjCMethodDecl>("dealloc"); 32 const auto *CID = Result.Nodes.getNodeAs<ObjCCategoryImplDecl>("impl"); 33 assert(DeallocDecl != nullptr); 34 diag(DeallocDecl->getLocation(), "category %0 should not implement -dealloc") 35 << CID; 36 } 37 38 } // namespace clang::tidy::objc 39