diff --git a/clang-tools-extra/clang-tidy/objc/CMakeLists.txt b/clang-tools-extra/clang-tidy/objc/CMakeLists.txt --- a/clang-tools-extra/clang-tidy/objc/CMakeLists.txt +++ b/clang-tools-extra/clang-tidy/objc/CMakeLists.txt @@ -2,6 +2,7 @@ add_clang_library(clangTidyObjCModule AvoidNSErrorInitCheck.cpp + DeallocInCategoryCheck.cpp ForbiddenSubclassingCheck.cpp MissingHashCheck.cpp ObjCTidyModule.cpp diff --git a/clang-tools-extra/clang-tidy/objc/DeallocInCategoryCheck.h b/clang-tools-extra/clang-tidy/objc/DeallocInCategoryCheck.h new file mode 100644 --- /dev/null +++ b/clang-tools-extra/clang-tidy/objc/DeallocInCategoryCheck.h @@ -0,0 +1,36 @@ +//===--- DeallocInCategoryCheck.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_DEALLOCINCATEGORYCHECK_H +#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_OBJC_DEALLOCINCATEGORYCHECK_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-category.html +class DeallocInCategoryCheck : public ClangTidyCheck { +public: + DeallocInCategoryCheck(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_DEALLOCINCATEGORYCHECK_H diff --git a/clang-tools-extra/clang-tidy/objc/DeallocInCategoryCheck.cpp b/clang-tools-extra/clang-tidy/objc/DeallocInCategoryCheck.cpp new file mode 100644 --- /dev/null +++ b/clang-tools-extra/clang-tidy/objc/DeallocInCategoryCheck.cpp @@ -0,0 +1,46 @@ +//===--- DeallocInCategoryCheck.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 "DeallocInCategoryCheck.h" +#include "clang/AST/ASTContext.h" +#include "clang/AST/DeclObjC.h" +#include "clang/ASTMatchers/ASTMatchFinder.h" + +using namespace clang::ast_matchers; + +namespace clang { +namespace tidy { +namespace objc { + +void DeallocInCategoryCheck::registerMatchers(MatchFinder *Finder) { + // This check should only be applied to Objective-C sources. + if (!getLangOpts().ObjC) + return; + + // Non-NSObject/NSProxy-derived objects may not have -dealloc as a special + // method. However, it seems highly unrealistic to expect many false-positives + // by warning on -dealloc in categories on classes without one of those + // base classes. + Finder->addMatcher( + objcMethodDecl(isInstanceMethod(), hasName("dealloc"), + hasDeclContext(objcCategoryImplDecl().bind("impl"))) + .bind("dealloc"), + this); +} + +void DeallocInCategoryCheck::check(const MatchFinder::MatchResult &Result) { + const auto *DeallocDecl = Result.Nodes.getNodeAs("dealloc"); + const auto *CID = Result.Nodes.getNodeAs("impl"); + assert(DeallocDecl != nullptr); + diag(DeallocDecl->getLocation(), "category %0 should not implement -dealloc") + << CID; +} + +} // namespace objc +} // namespace tidy +} // namespace clang diff --git a/clang-tools-extra/clang-tidy/objc/ObjCTidyModule.cpp b/clang-tools-extra/clang-tidy/objc/ObjCTidyModule.cpp --- a/clang-tools-extra/clang-tidy/objc/ObjCTidyModule.cpp +++ b/clang-tools-extra/clang-tidy/objc/ObjCTidyModule.cpp @@ -10,6 +10,7 @@ #include "../ClangTidyModule.h" #include "../ClangTidyModuleRegistry.h" #include "AvoidNSErrorInitCheck.h" +#include "DeallocInCategoryCheck.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-category"); CheckFactories.registerCheck( "objc-forbidden-subclassing"); CheckFactories.registerCheck( diff --git a/clang-tools-extra/docs/ReleaseNotes.rst b/clang-tools-extra/docs/ReleaseNotes.rst --- a/clang-tools-extra/docs/ReleaseNotes.rst +++ b/clang-tools-extra/docs/ReleaseNotes.rst @@ -81,13 +81,18 @@ ` check. Checks for usages of identifiers reserved for use by the implementation. - + - New :doc:`cert-oop57-cpp ` check. - + Flags use of the `C` standard library functions ``memset``, ``memcpy`` and ``memcmp`` and similar derivatives on non-trivial types. +- New :doc:`objc-dealloc-in-category + ` check. + + Finds implementations of -dealloc in Objective-C categories. + New check aliases ^^^^^^^^^^^^^^^^^ @@ -111,8 +116,8 @@ - Improved :doc:`readability-redundant-string-init ` check now supports a - `StringNames` option enabling its application to custom string classes. The - check now detects in class initializers and constructor initializers which + `StringNames` option enabling its application to custom string classes. The + check now detects in class initializers and constructor initializers which are deemed to be redundant. Renamed checks diff --git a/clang-tools-extra/docs/clang-tidy/checks/list.rst b/clang-tools-extra/docs/clang-tidy/checks/list.rst --- a/clang-tools-extra/docs/clang-tidy/checks/list.rst +++ b/clang-tools-extra/docs/clang-tidy/checks/list.rst @@ -232,6 +232,7 @@ `mpi-buffer-deref `_, "Yes" `mpi-type-mismatch `_, "Yes" `objc-avoid-nserror-init `_, + `objc-dealloc-in-category `_, `objc-forbidden-subclassing `_, `objc-missing-hash `_, `objc-property-declaration `_, "Yes" diff --git a/clang-tools-extra/docs/clang-tidy/checks/objc-dealloc-in-category.rst b/clang-tools-extra/docs/clang-tidy/checks/objc-dealloc-in-category.rst new file mode 100644 --- /dev/null +++ b/clang-tools-extra/docs/clang-tidy/checks/objc-dealloc-in-category.rst @@ -0,0 +1,13 @@ +.. title:: clang-tidy - objc-dealloc-in-category + +objc-dealloc-in-category +======================== + +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 to deallocate +an object. If a category on the class implements ``-dealloc``, it will +override the class's implementation and unexpected deallocation behavior +may occur. diff --git a/clang-tools-extra/test/clang-tidy/checkers/objc-dealloc-in-category.m b/clang-tools-extra/test/clang-tidy/checkers/objc-dealloc-in-category.m new file mode 100644 --- /dev/null +++ b/clang-tools-extra/test/clang-tidy/checkers/objc-dealloc-in-category.m @@ -0,0 +1,47 @@ +// RUN: %check_clang_tidy %s objc-dealloc-in-category %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 (BarCategory) +@end + +@implementation Bar (BarCategory) ++ (void)dealloc { + // Should not trigger on class methods. +} + +- (void)dealloc { + // CHECK-MESSAGES: :[[@LINE-1]]:1: warning: category 'BarCategory' should not implement -dealloc [objc-dealloc-in-category] +} +@end + +@interface Baz : NSObject +@end + +@implementation Baz +- (void)dealloc { + // Should not trigger on implementation in the class itself, even with + // it declared in the category (below). +} +@end + +@interface Baz (BazCategory) +// A declaration in a category @interface does not by itself provide an +// overriding implementation, and should not generate a warning. +- (void)dealloc; +@end