[clang] [clang][bytecode] Add Record::HasNothrowDtor flag (PR #247165)

Timm Baeder via cfe-commits cfe-commits at lists.llvm.org
Fri Dec 4 02:03:02 PST 2026


https://github.com/tbaederr updated https://github.com/llvm/llvm-project/pull/247165

>From 5a053a5a01bbb3063921c73ad7eff8d47bd89735 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Timm=20B=C3=A4der?= <tbaeder at redhat.com>
Date: Sat, 28 Nov 2026 09:08:37 +0100
Subject: [PATCH] [clang][bytecode] Add Record::HasNothrowDtor flag

---
 clang/lib/AST/ByteCode/Descriptor.cpp | 15 +++++++++++++++
 clang/lib/AST/ByteCode/Descriptor.h   |  1 +
 clang/lib/AST/ByteCode/Record.cpp     | 19 ++++++++++++-------
 clang/lib/AST/ByteCode/Record.h       |  5 ++++-
 4 files changed, 32 insertions(+), 8 deletions(-)

diff --git a/clang/lib/AST/ByteCode/Descriptor.cpp b/clang/lib/AST/ByteCode/Descriptor.cpp
index 67e09cb004a88a..58c4c52c631601 100644
--- a/clang/lib/AST/ByteCode/Descriptor.cpp
+++ b/clang/lib/AST/ByteCode/Descriptor.cpp
@@ -484,6 +484,21 @@ bool Descriptor::hasTrivialDtor() const {
   return ElemDesc->hasTrivialDtor();
 }
 
+bool Descriptor::hasTrivialDtor() const {
+  if (isPrimitive() || isPrimitiveArray())
+    return true;
+
+  if (isRecord()) {
+    assert(ElemRecord);
+    return ElemRecord->hasNothrowDtor();
+  }
+
+  if (!ElemDesc)
+    return true;
+  // Composite arrays.
+  return ElemDesc->hasNothrowDtor();
+}
+
 bool Descriptor::isUnion() const { return isRecord() && ElemRecord->isUnion(); }
 
 unsigned Descriptor::getElemDataSize() const {
diff --git a/clang/lib/AST/ByteCode/Descriptor.h b/clang/lib/AST/ByteCode/Descriptor.h
index 564ce64e6f40c5..4bb596f0c0282b 100644
--- a/clang/lib/AST/ByteCode/Descriptor.h
+++ b/clang/lib/AST/ByteCode/Descriptor.h
@@ -271,6 +271,7 @@ struct Descriptor final {
 
   /// Whether variables of this descriptor need their destructor called or not.
   bool hasTrivialDtor() const;
+  bool hasNothrowDtor() const;
 
   /// Returns the kind of dynamic allocation source of this block.
   static DynAllocKind getDynAllocKindForExpr(const Expr *E);
diff --git a/clang/lib/AST/ByteCode/Record.cpp b/clang/lib/AST/ByteCode/Record.cpp
index 81ec4e8afe6c4b..2d7ec700c53a78 100644
--- a/clang/lib/AST/ByteCode/Record.cpp
+++ b/clang/lib/AST/ByteCode/Record.cpp
@@ -8,6 +8,7 @@
 
 #include "Record.h"
 #include "clang/AST/ASTContext.h"
+#include "clang/AST/Attr.h"
 
 using namespace clang;
 using namespace clang::interp;
@@ -21,6 +22,17 @@ Record::Record(const RecordDecl *Decl, ArrayRef<Base> Bases,
       HasPtrField(HasPtrField) {
   for (const Base &B : this->Bases)
     BaseMap[cast<RecordDecl>(B.Decl->getFirstDecl())] = &B;
+
+  if (isAnonymousUnion()) {
+    HasTrivialDtor = true;
+    HasNothrowDtor = true;
+  } else if (const CXXDestructorDecl *Dtor = getDestructor()) {
+    HasTrivialDtor = Dtor->isTrivial();
+    HasNothrowDtor = Dtor->isNothrow();
+  } else {
+    HasTrivialDtor = true;
+    HasNothrowDtor = true;
+  }
 }
 
 std::string Record::getName() const {
@@ -31,13 +43,6 @@ std::string Record::getName() const {
   return Ret;
 }
 
-bool Record::hasTrivialDtor() const {
-  if (isAnonymousUnion())
-    return true;
-  const CXXDestructorDecl *Dtor = getDestructor();
-  return !Dtor || Dtor->isTrivial();
-}
-
 const Record::Field *Record::findField(unsigned Offset) const {
   if (const auto *It = llvm::find_if(
           Fields,
diff --git a/clang/lib/AST/ByteCode/Record.h b/clang/lib/AST/ByteCode/Record.h
index 6984c4957b0a25..6ac7bebf065586 100644
--- a/clang/lib/AST/ByteCode/Record.h
+++ b/clang/lib/AST/ByteCode/Record.h
@@ -89,7 +89,8 @@ class Record final {
   }
   /// Returns true for anonymous unions and records
   /// with no destructor or for those with a trivial destructor.
-  bool hasTrivialDtor() const;
+  bool hasTrivialDtor() const { return HasTrivialDtor; }
+  bool hasNothrowDtor() const { return HasNothrowDtor; }
 
   using const_field_iter = ArrayRef<Field>::const_iterator;
   llvm::iterator_range<const_field_iter> fields() const {
@@ -166,6 +167,8 @@ class Record final {
   bool IsAnonymousUnion;
   /// If any of the fields are pointers (or references).
   bool HasPtrField = false;
+  bool HasTrivialDtor;
+  bool HasNothrowDtor;
 };
 
 } // namespace interp



More information about the cfe-commits mailing list