Michael Wyman | 0151ddc | 2020-02-07 16:08:17 -0700 | [diff] [blame] | 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 { |
| 17 | namespace tidy { |
| 18 | namespace objc { |
| 19 | |
| 20 | void DeallocInCategoryCheck::registerMatchers(MatchFinder *Finder) { |
Michael Wyman | 0151ddc | 2020-02-07 16:08:17 -0700 | [diff] [blame] | 21 | // Non-NSObject/NSProxy-derived objects may not have -dealloc as a special |
| 22 | // method. However, it seems highly unrealistic to expect many false-positives |
| 23 | // by warning on -dealloc in categories on classes without one of those |
| 24 | // base classes. |
| 25 | Finder->addMatcher( |
| 26 | objcMethodDecl(isInstanceMethod(), hasName("dealloc"), |
| 27 | hasDeclContext(objcCategoryImplDecl().bind("impl"))) |
| 28 | .bind("dealloc"), |
| 29 | this); |
| 30 | } |
| 31 | |
| 32 | void DeallocInCategoryCheck::check(const MatchFinder::MatchResult &Result) { |
| 33 | const auto *DeallocDecl = Result.Nodes.getNodeAs<ObjCMethodDecl>("dealloc"); |
| 34 | const auto *CID = Result.Nodes.getNodeAs<ObjCCategoryImplDecl>("impl"); |
| 35 | assert(DeallocDecl != nullptr); |
| 36 | diag(DeallocDecl->getLocation(), "category %0 should not implement -dealloc") |
| 37 | << CID; |
| 38 | } |
| 39 | |
| 40 | } // namespace objc |
| 41 | } // namespace tidy |
| 42 | } // namespace clang |