[clang] [alpha.webkit.NoDeleteChecker] Allow no-delete default constructors (PR #201544)

Ryosuke Niwa via cfe-commits cfe-commits at lists.llvm.org
Thu Jun 4 04:33:27 PDT 2026


https://github.com/rniwa updated https://github.com/llvm/llvm-project/pull/201544

>From 6b911f5e973b2581d04e9e61d86b362b896ba2f4 Mon Sep 17 00:00:00 2001
From: Ryosuke Niwa <rniwa at webkit.org>
Date: Thu, 4 Jun 2026 03:39:34 -0700
Subject: [PATCH] [alpha.webkit.NoDeleteChecker] Allow no-delete default
 constructors

This PR fixes the bug in TrivialFunctionAnalysis that it treats a default trivial constructor
without an explicit body / definition as not "trivial". Fixed the bug by allowing the function
body to be missing when what we have is a trivial default constructor.
---
 .../Checkers/WebKit/PtrTypesSemantics.cpp     |  6 ++-
 .../Checkers/WebKit/nodelete-annotation.cpp   | 42 +++++++++++++++++++
 2 files changed, 47 insertions(+), 1 deletion(-)

diff --git a/clang/lib/StaticAnalyzer/Checkers/WebKit/PtrTypesSemantics.cpp b/clang/lib/StaticAnalyzer/Checkers/WebKit/PtrTypesSemantics.cpp
index d5ed7fc78148a..a74cd6aa8738a 100644
--- a/clang/lib/StaticAnalyzer/Checkers/WebKit/PtrTypesSemantics.cpp
+++ b/clang/lib/StaticAnalyzer/Checkers/WebKit/PtrTypesSemantics.cpp
@@ -652,7 +652,8 @@ class TrivialFunctionAnalysisVisitor
   bool IsFunctionTrivial(const Decl *D) {
     const Stmt **SavedOffendingStmt = std::exchange(OffendingStmt, nullptr);
     auto Result = WithCachedResult(D, [&]() {
-      if (auto *FnDecl = dyn_cast<FunctionDecl>(D)) {
+      auto *FnDecl = dyn_cast<FunctionDecl>(D);
+      if (FnDecl) {
         if (isNoDeleteFunction(FnDecl))
           return true;
         if (auto *MD = dyn_cast<CXXMethodDecl>(D); MD && MD->isVirtual())
@@ -667,6 +668,9 @@ class TrivialFunctionAnalysisVisitor
           if (!Visit(CtorInit->getInit()))
             return false;
         }
+        if (CtorDecl->isTrivial() && CtorDecl->isDefaultConstructor() &&
+            !CtorDecl->hasBody())
+          return true;
       }
       const Stmt *Body = D->getBody();
       if (!Body)
diff --git a/clang/test/Analysis/Checkers/WebKit/nodelete-annotation.cpp b/clang/test/Analysis/Checkers/WebKit/nodelete-annotation.cpp
index a9c50cfb1f45f..0d78fa91046ed 100644
--- a/clang/test/Analysis/Checkers/WebKit/nodelete-annotation.cpp
+++ b/clang/test/Analysis/Checkers/WebKit/nodelete-annotation.cpp
@@ -701,3 +701,45 @@ Ref<RefCountable> [[clang::annotate_type("webkit.nodelete")]] returnTypedefPrval
 
 } // namespace returned_prvalue_typedef
 
+namespace create_with_default_constructor {
+
+  struct ObjectWithDefaultConstructorWithoutMemberVariables {
+    void ref() const;
+    void deref() const;
+
+    static auto [[clang::annotate_type("webkit.nodelete")]] create() {
+      return adoptRef(*new ObjectWithDefaultConstructorWithoutMemberVariables());
+    }
+  };
+
+  struct ObjectWithDefaultConstructorWithPODMemberVariables {
+    void ref() const;
+    void deref() const;
+
+    static auto [[clang::annotate_type("webkit.nodelete")]] create() {
+      return adoptRef(*new ObjectWithDefaultConstructorWithPODMemberVariables());
+    }
+
+  private:
+    int value { 0 };
+    RefCountable* ptr { nullptr };
+  };
+
+  struct ObjectWithOpaqueCtor {
+    ObjectWithOpaqueCtor();
+  };
+
+  struct ObjectWithDefaultConstructorWithOpaqueCtorMemberVariables {
+    void ref() const;
+    void deref() const;
+
+    static auto [[clang::annotate_type("webkit.nodelete")]] create() {
+      return adoptRef(*new ObjectWithDefaultConstructorWithOpaqueCtorMemberVariables());
+      // expected-warning at -1{{A function 'create' has [[clang::annotate_type("webkit.nodelete")]] but it contains code that could destruct an object}}
+    }
+
+  private:
+    ObjectWithOpaqueCtor obj;
+  };
+
+} // namespace create_with_default_constructor



More information about the cfe-commits mailing list