10151ddc2SMichael Wyman //===--- DeallocInCategoryCheck.cpp - clang-tidy -------------------===// 20151ddc2SMichael Wyman // 30151ddc2SMichael Wyman // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 40151ddc2SMichael Wyman // See https://llvm.org/LICENSE.txt for license information. 50151ddc2SMichael Wyman // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 60151ddc2SMichael Wyman // 70151ddc2SMichael Wyman //===----------------------------------------------------------------------===// 80151ddc2SMichael Wyman 90151ddc2SMichael Wyman #include "DeallocInCategoryCheck.h" 100151ddc2SMichael Wyman #include "clang/AST/ASTContext.h" 110151ddc2SMichael Wyman #include "clang/AST/DeclObjC.h" 120151ddc2SMichael Wyman #include "clang/ASTMatchers/ASTMatchFinder.h" 130151ddc2SMichael Wyman 140151ddc2SMichael Wyman using namespace clang::ast_matchers; 150151ddc2SMichael Wyman 16*7d2ea6c4SCarlos Galvez namespace clang::tidy::objc { 170151ddc2SMichael Wyman registerMatchers(MatchFinder * Finder)180151ddc2SMichael Wymanvoid DeallocInCategoryCheck::registerMatchers(MatchFinder *Finder) { 190151ddc2SMichael Wyman // Non-NSObject/NSProxy-derived objects may not have -dealloc as a special 200151ddc2SMichael Wyman // method. However, it seems highly unrealistic to expect many false-positives 210151ddc2SMichael Wyman // by warning on -dealloc in categories on classes without one of those 220151ddc2SMichael Wyman // base classes. 230151ddc2SMichael Wyman Finder->addMatcher( 240151ddc2SMichael Wyman objcMethodDecl(isInstanceMethod(), hasName("dealloc"), 250151ddc2SMichael Wyman hasDeclContext(objcCategoryImplDecl().bind("impl"))) 260151ddc2SMichael Wyman .bind("dealloc"), 270151ddc2SMichael Wyman this); 280151ddc2SMichael Wyman } 290151ddc2SMichael Wyman check(const MatchFinder::MatchResult & Result)300151ddc2SMichael Wymanvoid DeallocInCategoryCheck::check(const MatchFinder::MatchResult &Result) { 310151ddc2SMichael Wyman const auto *DeallocDecl = Result.Nodes.getNodeAs<ObjCMethodDecl>("dealloc"); 320151ddc2SMichael Wyman const auto *CID = Result.Nodes.getNodeAs<ObjCCategoryImplDecl>("impl"); 330151ddc2SMichael Wyman assert(DeallocDecl != nullptr); 340151ddc2SMichael Wyman diag(DeallocDecl->getLocation(), "category %0 should not implement -dealloc") 350151ddc2SMichael Wyman << CID; 360151ddc2SMichael Wyman } 370151ddc2SMichael Wyman 38*7d2ea6c4SCarlos Galvez } // namespace clang::tidy::objc 39