Index: clang-tools-extra/clang-tidy/objc/CMakeLists.txt =================================================================== --- clang-tools-extra/clang-tidy/objc/CMakeLists.txt +++ clang-tools-extra/clang-tidy/objc/CMakeLists.txt @@ -2,6 +2,7 @@ add_clang_library(clangTidyObjCModule AvoidNSErrorInitCheck.cpp + DeallocInCategoriesCheck.cpp ForbiddenSubclassingCheck.cpp MissingHashCheck.cpp ObjCTidyModule.cpp Index: clang-tools-extra/clang-tidy/objc/DeallocInCategoriesCheck.h =================================================================== --- /dev/null +++ clang-tools-extra/clang-tidy/objc/DeallocInCategoriesCheck.h @@ -0,0 +1,36 @@ +//===--- DeallocInCategoriesCheck.h - clang-tidy ----------------*- C++ -*-===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_OBJC_DEALLOCINCATEGORIESCHECK_H +#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_OBJC_DEALLOCINCATEGORIESCHECK_H + +#include "../ClangTidyCheck.h" + +namespace clang { +namespace tidy { +namespace objc { + +/// Finds implementations of -dealloc in Objective-C categories. The category +/// implementation will override any dealloc in the class implementation, +/// potentially causing issues. +/// +/// For the user-facing documentation see: +/// http://clang.llvm.org/extra/clang-tidy/checks/objc-dealloc-in-categories.html +class DeallocInCategoriesCheck : public ClangTidyCheck { +public: + DeallocInCategoriesCheck(StringRef Name, ClangTidyContext *Context) + : ClangTidyCheck(Name, Context) {} + void registerMatchers(ast_matchers::MatchFinder *Finder) override; + void check(const ast_matchers::MatchFinder::MatchResult &Result) override; +}; + +} // namespace objc +} // namespace tidy +} // namespace clang + +#endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_OBJC_DEALLOCINCATEGORIESCHECK_H Index: clang-tools-extra/clang-tidy/objc/DeallocInCategoriesCheck.cpp =================================================================== --- /dev/null +++ clang-tools-extra/clang-tidy/objc/DeallocInCategoriesCheck.cpp @@ -0,0 +1,37 @@ +//===--- DeallocInCategoriesCheck.cpp - clang-tidy -------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +#include "DeallocInCategoriesCheck.h" +#include "clang/AST/ASTContext.h" +#include "clang/ASTMatchers/ASTMatchFinder.h" + +using namespace clang::ast_matchers; + +namespace clang { +namespace tidy { +namespace objc { + +void DeallocInCategoriesCheck::registerMatchers(MatchFinder *Finder) { + Finder->addMatcher( + objcMethodDecl(hasName("dealloc"), hasDeclContext(objcCategoryImplDecl())) + .bind("dealloc"), + this); +} + +void DeallocInCategoriesCheck::check( + const MatchFinder::MatchResult &Result) { + const auto *MatchedDecl = Result.Nodes.getNodeAs("dealloc"); + if (MatchedDecl) { + diag(MatchedDecl->getLocation(), + "method -dealloc should not be implemented in a category"); + } +} + +} // namespace objc +} // namespace tidy +} // namespace clang Index: clang-tools-extra/clang-tidy/objc/ObjCTidyModule.cpp =================================================================== --- clang-tools-extra/clang-tidy/objc/ObjCTidyModule.cpp +++ clang-tools-extra/clang-tidy/objc/ObjCTidyModule.cpp @@ -10,6 +10,7 @@ #include "../ClangTidyModule.h" #include "../ClangTidyModuleRegistry.h" #include "AvoidNSErrorInitCheck.h" +#include "DeallocInCategoriesCheck.h" #include "ForbiddenSubclassingCheck.h" #include "MissingHashCheck.h" #include "PropertyDeclarationCheck.h" @@ -26,6 +27,8 @@ void addCheckFactories(ClangTidyCheckFactories &CheckFactories) override { CheckFactories.registerCheck( "objc-avoid-nserror-init"); + CheckFactories.registerCheck( + "objc-dealloc-in-categories"); CheckFactories.registerCheck( "objc-forbidden-subclassing"); CheckFactories.registerCheck( Index: clang-tools-extra/docs/ReleaseNotes.rst =================================================================== --- clang-tools-extra/docs/ReleaseNotes.rst +++ clang-tools-extra/docs/ReleaseNotes.rst @@ -70,6 +70,10 @@ New checks ^^^^^^^^^^ +- New :doc:`objc-dealloc-in-categories + ` check. + + Finds implementations of -dealloc in Objective-C categories. New aliases ^^^^^^^^^^^ Index: clang-tools-extra/docs/clang-tidy/checks/list.rst =================================================================== --- clang-tools-extra/docs/clang-tidy/checks/list.rst +++ clang-tools-extra/docs/clang-tidy/checks/list.rst @@ -230,6 +230,7 @@ `mpi-buffer-deref `_, "Yes" `mpi-type-mismatch `_, "Yes" `objc-avoid-nserror-init `_, + `objc-dealloc-in-categories `_, `objc-forbidden-subclassing `_, `objc-missing-hash `_, `objc-property-declaration `_, "Yes" Index: clang-tools-extra/docs/clang-tidy/checks/objc-dealloc-in-categories.rst =================================================================== --- /dev/null +++ clang-tools-extra/docs/clang-tidy/checks/objc-dealloc-in-categories.rst @@ -0,0 +1,13 @@ +.. title:: clang-tidy - objc-dealloc-in-categories + +objc-dealloc-in-categories +========================== + +Finds implementations of ``-dealloc`` in Objective-C categories. The category +implementation will override any dealloc in the class implementation, +potentially causing issues. + +Classes implement ``-dealloc`` to perform important actions just before an +object is deallocated, but if a category on the class implements ``-dealloc`` +it will override the class's implementation and those important actions may +not be handled by the overriding ``-dealloc``. Index: clang-tools-extra/test/clang-tidy/checkers/objc-dealloc-in-categories.m =================================================================== --- /dev/null +++ clang-tools-extra/test/clang-tidy/checkers/objc-dealloc-in-categories.m @@ -0,0 +1,36 @@ +// RUN: %check_clang_tidy %s objc-dealloc-in-categories %t + +@interface NSObject +// Used to quash warning about missing base class. +- (void)dealloc; +@end + +@interface Foo : NSObject +@end + +@implementation Foo +- (void)dealloc { + // No warning should be generated here. +} +@end + +@interface Bar : NSObject +@end + +@interface Bar (Category) +@end + +@implementation Bar (Category) +- (void)dealloc { + // CHECK-MESSAGES: :[[@LINE-1]]:1: warning: method -dealloc should not be implemented in a category [objc-dealloc-in-categories] +} +@end + +@interface Baz : NSObject +@end + +@interface Baz (Category) +// A declaration in a category @interface does not by itself provide an +// overriding implementation, and should not generate a warning. +- (void)dealloc; +@end