clang-tools 19.0.0git
DeallocInCategoryCheck.cpp
Go to the documentation of this file.
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
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::tidy::objc {
17
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 objcMethodDecl(isInstanceMethod(), hasName("dealloc"),
25 hasDeclContext(objcCategoryImplDecl().bind("impl")))
26 .bind("dealloc"),
27 this);
28}
29
30void DeallocInCategoryCheck::check(const MatchFinder::MatchResult &Result) {
31 const auto *DeallocDecl = Result.Nodes.getNodeAs<ObjCMethodDecl>("dealloc");
32 const auto *CID = Result.Nodes.getNodeAs<ObjCCategoryImplDecl>("impl");
33 assert(DeallocDecl != nullptr);
34 diag(DeallocDecl->getLocation(), "category %0 should not implement -dealloc")
35 << CID;
36}
37
38} // namespace clang::tidy::objc
DiagnosticBuilder diag(SourceLocation Loc, StringRef Description, DiagnosticIDs::Level Level=DiagnosticIDs::Warning)
Add a diagnostic with the check's name.
void registerMatchers(ast_matchers::MatchFinder *Finder) override
Override this to register AST matchers with Finder.
void check(const ast_matchers::MatchFinder::MatchResult &Result) override
ClangTidyChecks that register ASTMatchers should do the actual work in here.