mwyman updated this revision to Diff 241573. mwyman marked 9 inline comments as done. mwyman added a comment.
Implemented review feedback. Updated diagnostic message to mention the category name. Repository: rG LLVM Github Monorepo CHANGES SINCE LAST ACTION https://reviews.llvm.org/D72876/new/ https://reviews.llvm.org/D72876 Files: clang-tools-extra/clang-tidy/objc/CMakeLists.txt clang-tools-extra/clang-tidy/objc/DeallocInCategoryCheck.cpp clang-tools-extra/clang-tidy/objc/DeallocInCategoryCheck.h clang-tools-extra/clang-tidy/objc/ObjCTidyModule.cpp clang-tools-extra/docs/ReleaseNotes.rst clang-tools-extra/docs/clang-tidy/checks/list.rst clang-tools-extra/docs/clang-tidy/checks/objc-dealloc-in-category.rst clang-tools-extra/test/clang-tidy/checkers/objc-dealloc-in-category.m
Index: clang-tools-extra/test/clang-tidy/checkers/objc-dealloc-in-category.m =================================================================== --- /dev/null +++ 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 Index: clang-tools-extra/docs/clang-tidy/checks/objc-dealloc-in-category.rst =================================================================== --- /dev/null +++ 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. 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 @@ -232,6 +232,7 @@ `mpi-buffer-deref <mpi-buffer-deref.html>`_, "Yes" `mpi-type-mismatch <mpi-type-mismatch.html>`_, "Yes" `objc-avoid-nserror-init <objc-avoid-nserror-init.html>`_, + `objc-dealloc-in-category <objc-dealloc-in-category.html>`_, `objc-forbidden-subclassing <objc-forbidden-subclassing.html>`_, `objc-missing-hash <objc-missing-hash.html>`_, `objc-property-declaration <objc-property-declaration.html>`_, "Yes" @@ -280,7 +281,7 @@ `readability-redundant-member-init <readability-redundant-member-init.html>`_, "Yes" `readability-redundant-preprocessor <readability-redundant-preprocessor.html>`_, `readability-redundant-smartptr-get <readability-redundant-smartptr-get.html>`_, "Yes" - `readability-redundant-string-cstr <readability-redundant-string-cstr.html>`_, + `readability-redundant-string-cstr <readability-redundant-string-cstr.html>`_, "Yes" `readability-redundant-string-init <readability-redundant-string-init.html>`_, "Yes" `readability-simplify-boolean-expr <readability-simplify-boolean-expr.html>`_, "Yes" `readability-simplify-subscript-expr <readability-simplify-subscript-expr.html>`_, "Yes" Index: clang-tools-extra/docs/ReleaseNotes.rst =================================================================== --- clang-tools-extra/docs/ReleaseNotes.rst +++ clang-tools-extra/docs/ReleaseNotes.rst @@ -81,13 +81,18 @@ <clang-tidy/checks/bugprone-reserved-identifier>` check. Checks for usages of identifiers reserved for use by the implementation. - + - New :doc:`cert-oop57-cpp <clang-tidy/checks/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 + <clang-tidy/checks/objc-dealloc-in-category>` check. + + Finds implementations of -dealloc in Objective-C categories. + New check aliases ^^^^^^^^^^^^^^^^^ @@ -106,8 +111,8 @@ - Improved :doc:`readability-redundant-string-init <clang-tidy/checks/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 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 "DeallocInCategoryCheck.h" #include "ForbiddenSubclassingCheck.h" #include "MissingHashCheck.h" #include "PropertyDeclarationCheck.h" @@ -26,6 +27,8 @@ void addCheckFactories(ClangTidyCheckFactories &CheckFactories) override { CheckFactories.registerCheck<AvoidNSErrorInitCheck>( "objc-avoid-nserror-init"); + CheckFactories.registerCheck<DeallocInCategoryCheck>( + "objc-dealloc-in-category"); CheckFactories.registerCheck<ForbiddenSubclassingCheck>( "objc-forbidden-subclassing"); CheckFactories.registerCheck<MissingHashCheck>( Index: clang-tools-extra/clang-tidy/objc/DeallocInCategoryCheck.h =================================================================== --- /dev/null +++ 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 Index: clang-tools-extra/clang-tidy/objc/DeallocInCategoryCheck.cpp =================================================================== --- /dev/null +++ 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<ObjCMethodDecl>("dealloc"); + const auto *CID = Result.Nodes.getNodeAs<ObjCCategoryImplDecl>("impl"); + assert(DeallocDecl != nullptr); + diag(DeallocDecl->getLocation(), "category %0 should not implement -dealloc") + << CID; +} + +} // namespace objc +} // namespace tidy +} // namespace clang 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 + DeallocInCategoryCheck.cpp ForbiddenSubclassingCheck.cpp MissingHashCheck.cpp ObjCTidyModule.cpp
_______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits