[docs] Add LICENSE.txt to the root of the mono-repo
[llvm-project.git] / clang-tools-extra / clang-tidy / objc / DeallocInCategoryCheck.cpp
blob8c5ecb778397d3093419335bd3f79fa0648531bf
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 //===----------------------------------------------------------------------===//
9 #include "DeallocInCategoryCheck.h"
10 #include "clang/AST/ASTContext.h"
11 #include "clang/AST/DeclObjC.h"
12 #include "clang/ASTMatchers/ASTMatchFinder.h"
14 using namespace clang::ast_matchers;
16 namespace clang {
17 namespace tidy {
18 namespace objc {
20 void DeallocInCategoryCheck::registerMatchers(MatchFinder *Finder) {
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);
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;
40 } // namespace objc
41 } // namespace tidy
42 } // namespace clang