https://github.com/rniwa created 
https://github.com/llvm/llvm-project/pull/242238

The checker reported a constructor annotated with webkit.nodelete when any 
field of its class, or of any of its base classes, has a default constructor 
that is not known to be trivial. Neither base class fields nor fields of a 
delegating constructor are constructed by the constructor being checked:

  struct Observer {
      [[clang::annotate("webkit.nodelete")]] explicit Observer(int*);
      WeakPtr<Context> m_context;
  };

  struct ActiveObject : Observer {
      [[clang::annotate("webkit.nodelete")]] explicit ActiveObject(int* p)
          : ActiveObject(p, Checked) { }
      ...
      [[clang::annotate("webkit.nodelete")]] ActiveObject(int* p, CheckedType)
          : Observer(p) { }
  };

Both ActiveObject constructors were reported as constructing m_context. That 
field is built by Observer's constructor, which appears in inits() and is 
already checked there; here it is itself nodelete. The delegating constructor 
builds no field at all; its call to the target constructor is likewise in 
inits(). This showed up as four false positives on WebKit's ActiveDOMObject 
constructors once they were annotated.

Stop walking base classes when looking for a field with a non-trivial 
constructor, and skip the field check for delegating constructors. A base class 
whose constructor could destruct an object is still reported through its 
initializer, whether it is written explicitly or synthesized for an implicit 
default construction. The destructor check keeps walking base class fields, 
since base class destructors run implicitly and do not appear as initializers.

Co-authored with Claude.

rdar://192678282

>From 09b20169551ff89c2f6fdfe187b21934d53ae365 Mon Sep 17 00:00:00 2001
From: Ryosuke Niwa <[email protected]>
Date: Fri, 13 Nov 2026 02:19:35 -0800
Subject: [PATCH] [alpha.webkit.NoDeleteChecker] Don't check base class fields
 in constructors

The checker reported a constructor annotated with webkit.nodelete when
any field of its class, or of any of its base classes, has a default
constructor that is not known to be trivial. Neither base class fields
nor fields of a delegating constructor are constructed by the
constructor being checked:

  struct Observer {
      [[clang::annotate("webkit.nodelete")]] explicit Observer(int*);
      WeakPtr<Context> m_context;
  };

  struct ActiveObject : Observer {
      [[clang::annotate("webkit.nodelete")]] explicit ActiveObject(int* p)
          : ActiveObject(p, Checked) { }
      ...
      [[clang::annotate("webkit.nodelete")]] ActiveObject(int* p, CheckedType)
          : Observer(p) { }
  };

Both ActiveObject constructors were reported as constructing
m_context. That field is built by Observer's constructor, which
appears in inits() and is already checked there; here it is itself
nodelete. The delegating constructor builds no field at all; its call
to the target constructor is likewise in inits(). This showed up as four
false positives on WebKit's ActiveDOMObject constructors once they were
annotated.

Stop walking base classes when looking for a field with a non-trivial
constructor, and skip the field check for delegating constructors.
A base class whose constructor could destruct an object is still
reported through its initializer, whether it is written explicitly or
synthesized for an implicit default construction. The destructor check
keeps walking base class fields, since base class destructors run
implicitly and do not appear as initializers.

Co-authored with Claude.

rdar://192678282
---
 .../Checkers/WebKit/NoDeleteChecker.cpp       |  5 +-
 .../Checkers/WebKit/PtrTypesSemantics.cpp     | 11 ++--
 .../Checkers/WebKit/nodelete-annotation.cpp   | 61 +++++++++++++++++++
 3 files changed, 72 insertions(+), 5 deletions(-)

diff --git a/clang/lib/StaticAnalyzer/Checkers/WebKit/NoDeleteChecker.cpp 
b/clang/lib/StaticAnalyzer/Checkers/WebKit/NoDeleteChecker.cpp
index 23f669c28f664..682407b7190d1 100644
--- a/clang/lib/StaticAnalyzer/Checkers/WebKit/NoDeleteChecker.cpp
+++ b/clang/lib/StaticAnalyzer/Checkers/WebKit/NoDeleteChecker.cpp
@@ -91,7 +91,10 @@ class NoDeleteChecker : public 
Checker<check::ASTDecl<TranslationUnitDecl>> {
     bool IsDtor = false;
     if (auto *Ctor = dyn_cast<CXXConstructorDecl>(FD)) {
       IsCtor = true;
-      Field = TFA.fieldWithNonTrivialCtor(Ctor->getParent());
+      // A delegating constructor doesn't construct any member variable itself;
+      // the target constructor does, and the call to it is in inits().
+      if (!Ctor->isDelegatingConstructor())
+        Field = TFA.fieldWithNonTrivialCtor(Ctor->getParent());
       if (!Field) {
         for (auto *CtorInit : Ctor->inits()) {
           auto *Init = CtorInit->getInit();
diff --git a/clang/lib/StaticAnalyzer/Checkers/WebKit/PtrTypesSemantics.cpp 
b/clang/lib/StaticAnalyzer/Checkers/WebKit/PtrTypesSemantics.cpp
index 9cc782985c834..e58e14ca61c22 100644
--- a/clang/lib/StaticAnalyzer/Checkers/WebKit/PtrTypesSemantics.cpp
+++ b/clang/lib/StaticAnalyzer/Checkers/WebKit/PtrTypesSemantics.cpp
@@ -881,7 +881,7 @@ class TrivialFunctionAnalysisVisitor
   template <typename CacheTy, typename IsTrivialTypeFn>
   bool hasNonTrivialField(const CXXRecordDecl *Cls,
                           const FieldDecl **OffendingField, CacheTy &Cache,
-                          IsTrivialTypeFn IsTrivialType) {
+                          bool IncludeBases, IsTrivialTypeFn IsTrivialType) {
     auto CacheIt = Cache.find(Cls);
     if (CacheIt != Cache.end() && !OffendingField)
       return CacheIt->second;
@@ -901,7 +901,7 @@ class TrivialFunctionAnalysisVisitor
       if (HasNonTrivialField(Cls))
         return true;
 
-      if (!Cls->hasDefinition())
+      if (!IncludeBases || !Cls->hasDefinition())
         return false;
 
       CXXBasePaths Paths;
@@ -1034,8 +1034,11 @@ class TrivialFunctionAnalysisVisitor
 
   const FieldDecl *FieldWithNonTrivialCtor(const CXXRecordDecl *Cls) {
     const FieldDecl *OffendingField = nullptr;
+    // Base class subobjects are constructed by the base class constructors,
+    // which appear in the constructor's inits() and are checked there, so
+    // only look at the fields declared in Cls itself.
     hasNonTrivialField(
-        Cls, &OffendingField, FieldCtorCache,
+        Cls, &OffendingField, FieldCtorCache, /*IncludeBases=*/false,
         [&](const QualType Ty) { return CanTriviallyConstruct(Ty); });
     return OffendingField;
   }
@@ -1043,7 +1046,7 @@ class TrivialFunctionAnalysisVisitor
   const FieldDecl *FieldWithNonTrivialDtor(const CXXRecordDecl *Cls) {
     const FieldDecl *OffendingField = nullptr;
     hasNonTrivialField(
-        Cls, &OffendingField, FieldDtorCache,
+        Cls, &OffendingField, FieldDtorCache, /*IncludeBases=*/true,
         [&](const QualType Ty) { return CanTriviallyDestruct(Ty); });
     return OffendingField;
   }
diff --git a/clang/test/Analysis/Checkers/WebKit/nodelete-annotation.cpp 
b/clang/test/Analysis/Checkers/WebKit/nodelete-annotation.cpp
index bc5ecf2803cea..9c64155a99541 100644
--- a/clang/test/Analysis/Checkers/WebKit/nodelete-annotation.cpp
+++ b/clang/test/Analysis/Checkers/WebKit/nodelete-annotation.cpp
@@ -1069,3 +1069,64 @@ void [[clang::annotate_type("webkit.nodelete")]] 
deletingCaptureByValue() {
 }
 
 } // namespace implicit_destructor
+
+namespace nodelete_ctor_base_fields {
+
+struct OpaqueMember {
+  OpaqueMember();
+  ~OpaqueMember();
+};
+
+struct Observer {
+  [[clang::annotate("webkit.nodelete")]] explicit Observer(int*);
+  virtual ~Observer();
+  OpaqueMember member;
+};
+
+// The base class fields are constructed by Observer's constructor, and
+// delegating constructors don't construct any field at all.
+struct ActiveObject : Observer {
+  [[clang::annotate("webkit.nodelete")]] explicit ActiveObject(int* p) : 
ActiveObject(p, Checked) { }
+  [[clang::annotate("webkit.nodelete")]] explicit ActiveObject(int& p) : 
ActiveObject(&p, Checked) { }
+
+private:
+  enum CheckedType { Checked };
+  [[clang::annotate("webkit.nodelete")]] ActiveObject(int* p, CheckedType) : 
Observer(p) { }
+
+  int count { 0 };
+};
+
+struct DelegatesToOpaqueCtor {
+  [[clang::annotate("webkit.nodelete")]] DelegatesToOpaqueCtor() : 
DelegatesToOpaqueCtor(nullptr) { }
+  // expected-warning@-1{{A constructor 'DelegatesToOpaqueCtor' has 
[[clang::annotate_type("webkit.nodelete")]] but it contains code that could 
destruct an object}}
+  explicit DelegatesToOpaqueCtor(int*);
+  OpaqueMember member;
+};
+
+struct ObserverWithOpaqueCtor {
+  explicit ObserverWithOpaqueCtor(int*);
+  OpaqueMember member;
+};
+
+struct DerivedFromOpaqueCtor : ObserverWithOpaqueCtor {
+  [[clang::annotate("webkit.nodelete")]] explicit DerivedFromOpaqueCtor(int* 
p) : ObserverWithOpaqueCtor(p) { }
+  // expected-warning@-1{{A constructor 'DerivedFromOpaqueCtor' has 
[[clang::annotate_type("webkit.nodelete")]] but it contains code that could 
destruct an object}}
+};
+
+struct ObserverWithOpaqueDefaultCtor {
+  ObserverWithOpaqueDefaultCtor();
+  OpaqueMember member;
+};
+
+struct DerivedFromOpaqueDefaultCtor : ObserverWithOpaqueDefaultCtor {
+  [[clang::annotate("webkit.nodelete")]] DerivedFromOpaqueDefaultCtor() { }
+  // expected-warning@-1{{A constructor 'DerivedFromOpaqueDefaultCtor' has 
[[clang::annotate_type("webkit.nodelete")]] but it contains code that could 
destruct an object}}
+};
+
+struct DerivedWithOpaqueMember : Observer {
+  [[clang::annotate("webkit.nodelete")]] explicit DerivedWithOpaqueMember(int* 
p) : Observer(p) { }
+  // expected-warning@-1{{A constructor 'DerivedWithOpaqueMember' has 
[[clang::annotate_type("webkit.nodelete")]] but it constructs a member variable 
'ownMember' that could destruct an object}}
+  OpaqueMember ownMember;
+};
+
+} // namespace nodelete_ctor_base_fields

_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to