mwyman created this revision.
mwyman added reviewers: stephanemoore, benhamilton.
mwyman added projects: clang-tools-extra, clang.
Herald added subscribers: cfe-commits, mgehre, mgorny.

Such implementations may override the class's own implementation, and even be a 
danger in case someone later comes and adds one to the class itself. Most times 
this has been encountered have been a mistake.


Repository:
  rCTE Clang Tools Extra

https://reviews.llvm.org/D72876

Files:
  clang-tools-extra/clang-tidy/objc/CMakeLists.txt
  clang-tools-extra/clang-tidy/objc/DeallocInCategoriesCheck.cpp
  clang-tools-extra/clang-tidy/objc/DeallocInCategoriesCheck.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-categories.rst
  clang-tools-extra/test/clang-tidy/checkers/objc-dealloc-in-categories.m

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
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/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 <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-categories <objc-dealloc-in-categories.html>`_,
    `objc-forbidden-subclassing <objc-forbidden-subclassing.html>`_,
    `objc-missing-hash <objc-missing-hash.html>`_,
    `objc-property-declaration <objc-property-declaration.html>`_, "Yes"
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
+  <clang-tidy/checks/objc-dealloc-in-categories>` check.
+
+  Finds implementations of -dealloc in Objective-C categories.
 
 New aliases
 ^^^^^^^^^^^
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<AvoidNSErrorInitCheck>(
         "objc-avoid-nserror-init");
+    CheckFactories.registerCheck<DeallocInCategoriesCheck>(
+        "objc-dealloc-in-categories");
     CheckFactories.registerCheck<ForbiddenSubclassingCheck>(
         "objc-forbidden-subclassing");
     CheckFactories.registerCheck<MissingHashCheck>(
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<ObjCMethodDecl>("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/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
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to