m4tx created this revision.
m4tx added reviewers: alexfh, hokein.
Herald added subscribers: cfe-commits, xazax.hun, mgorny.

Add a bugprone check to clang-tidy that detects the usages of `delete this`. 
The warning is shown even when `delete this` is the last line in the method (as 
there is no guarantee that it won't be called from another method, or the 
pointer won't be used in any way afterwards).

https://bugs.llvm.org/show_bug.cgi?id=38741


Repository:
  rCTE Clang Tools Extra

https://reviews.llvm.org/D54262

Files:
  clang-tidy/bugprone/BugproneTidyModule.cpp
  clang-tidy/bugprone/CMakeLists.txt
  clang-tidy/bugprone/DeleteThisCheck.cpp
  clang-tidy/bugprone/DeleteThisCheck.h
  docs/clang-tidy/checks/bugprone-delete-this.rst

Index: docs/clang-tidy/checks/bugprone-delete-this.rst
===================================================================
--- /dev/null
+++ docs/clang-tidy/checks/bugprone-delete-this.rst
@@ -0,0 +1,21 @@
+.. title:: clang-tidy - bugprone-delete-this
+
+bugprone-delete-this
+====================
+
+Detects the usages of ``delete this``.
+
+Said statement generates multiple problems, such as enforcing allocating the object via ``new``, or not allowing any
+usage of instance members (neither fields nor methods) after the execution of it, nor touching ``this`` pointer
+in any way.
+
+Example:
+
+.. code-block:: c++
+
+  class A {
+    void foo() {
+      delete this;
+      // warning: usage of 'delete this' is suspicious [bugprone-delete-this]
+    }
+  }
Index: clang-tidy/bugprone/DeleteThisCheck.h
===================================================================
--- /dev/null
+++ clang-tidy/bugprone/DeleteThisCheck.h
@@ -0,0 +1,35 @@
+//===--- DeleteThisCheck.h - clang-tidy--------------------------*- C++ -*-===//
+//
+//                     The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_BUGPRONE_DELETE_THIS_H
+#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_BUGPRONE_DELETE_THIS_H
+
+#include "../ClangTidy.h"
+
+namespace clang {
+namespace tidy {
+namespace bugprone {
+
+/// Detects the usages of `delete this`.
+///
+/// For the user-facing documentation see:
+/// http://clang.llvm.org/extra/clang-tidy/checks/bugprone-delete-this.html
+class DeleteThisCheck : public ClangTidyCheck {
+public:
+  DeleteThisCheck(StringRef Name, ClangTidyContext *Context)
+      : ClangTidyCheck(Name, Context) {}
+  void registerMatchers(ast_matchers::MatchFinder *Finder) override;
+  void check(const ast_matchers::MatchFinder::MatchResult &Result) override;
+};
+
+} // namespace bugprone
+} // namespace tidy
+} // namespace clang
+
+#endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_BUGPRONE_DELETE_THIS_H
Index: clang-tidy/bugprone/DeleteThisCheck.cpp
===================================================================
--- /dev/null
+++ clang-tidy/bugprone/DeleteThisCheck.cpp
@@ -0,0 +1,35 @@
+//===--- DeleteThisCheck.cpp - clang-tidy----------------------------------===//
+//
+//                     The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+
+#include "DeleteThisCheck.h"
+#include "clang/AST/ASTContext.h"
+#include "clang/ASTMatchers/ASTMatchFinder.h"
+
+using namespace clang::ast_matchers;
+
+namespace clang {
+namespace tidy {
+namespace bugprone {
+
+void DeleteThisCheck::registerMatchers(MatchFinder *Finder) {
+  Finder->addMatcher(
+      cxxDeleteExpr(
+          has(ignoringParenImpCasts(cxxThisExpr())))
+          .bind("DeleteThis"),
+      this);
+}
+
+void DeleteThisCheck::check(const MatchFinder::MatchResult &Result) {
+  const auto *E = Result.Nodes.getNodeAs<Expr>("DeleteThis");
+  diag(E->getBeginLoc(), "usage of 'delete this' is suspicious");
+}
+
+} // namespace bugprone
+} // namespace tidy
+} // namespace clang
Index: clang-tidy/bugprone/CMakeLists.txt
===================================================================
--- clang-tidy/bugprone/CMakeLists.txt
+++ clang-tidy/bugprone/CMakeLists.txt
@@ -7,6 +7,7 @@
   BugproneTidyModule.cpp
   CopyConstructorInitCheck.cpp
   DanglingHandleCheck.cpp
+  DeleteThisCheck.cpp
   ExceptionEscapeCheck.cpp
   FoldInitTypeCheck.cpp
   ForwardDeclarationNamespaceCheck.cpp
Index: clang-tidy/bugprone/BugproneTidyModule.cpp
===================================================================
--- clang-tidy/bugprone/BugproneTidyModule.cpp
+++ clang-tidy/bugprone/BugproneTidyModule.cpp
@@ -16,6 +16,7 @@
 #include "BoolPointerImplicitConversionCheck.h"
 #include "CopyConstructorInitCheck.h"
 #include "DanglingHandleCheck.h"
+#include "DeleteThisCheck.h"
 #include "ExceptionEscapeCheck.h"
 #include "FoldInitTypeCheck.h"
 #include "ForwardDeclarationNamespaceCheck.h"
@@ -68,6 +69,8 @@
         "bugprone-copy-constructor-init");
     CheckFactories.registerCheck<DanglingHandleCheck>(
         "bugprone-dangling-handle");
+    CheckFactories.registerCheck<DeleteThisCheck>(
+        "bugprone-delete-this");
     CheckFactories.registerCheck<ExceptionEscapeCheck>(
         "bugprone-exception-escape");
     CheckFactories.registerCheck<FoldInitTypeCheck>(
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to