blob: 8c5ecb778397d3093419335bd3f79fa0648531bf [file] [log] [blame]
Michael Wyman0151ddc2020-02-07 16:08:17 -07001//===--- 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
14using namespace clang::ast_matchers;
15
16namespace clang {
17namespace tidy {
18namespace objc {
19
20void DeallocInCategoryCheck::registerMatchers(MatchFinder *Finder) {
Michael Wyman0151ddc2020-02-07 16:08:17 -070021 // 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
32void 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