| 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 | |
| 18 | void 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 | NodeMatch: objcMethodDecl(isInstanceMethod(), hasName(Name: "dealloc"), |
| 25 | hasDeclContext(InnerMatcher: objcCategoryImplDecl().bind(ID: "impl"))) |
| 26 | .bind(ID: "dealloc"), |
| 27 | Action: this); |
| 28 | } |
| 29 | |
| 30 | void DeallocInCategoryCheck::check(const MatchFinder::MatchResult &Result) { |
| 31 | const auto *DeallocDecl = Result.Nodes.getNodeAs<ObjCMethodDecl>(ID: "dealloc"); |
| 32 | const auto *CID = Result.Nodes.getNodeAs<ObjCCategoryImplDecl>(ID: "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 |
