[clang] 7ef636e - [clang][bytecode] Mark IndirectFieldDecl chain links as initialized (#125869)

via cfe-commits cfe-commits at lists.llvm.org
Wed Feb 5 08:50:59 PST 2025


Author: Timm Baeder
Date: 2025-02-05T17:50:55+01:00
New Revision: 7ef636e1c4c49b175833dc91c44ed338b899c29b

URL: https://github.com/llvm/llvm-project/commit/7ef636e1c4c49b175833dc91c44ed338b899c29b
DIFF: https://github.com/llvm/llvm-project/commit/7ef636e1c4c49b175833dc91c44ed338b899c29b.diff

LOG: [clang][bytecode] Mark IndirectFieldDecl chain links as initialized (#125869)

We only initialize the final field above, so make sure we're marking the
links in the chain on the way there as initialized as well.

Added: 
    

Modified: 
    clang/lib/AST/ByteCode/Compiler.cpp
    clang/test/AST/ByteCode/unions.cpp

Removed: 
    


################################################################################
diff  --git a/clang/lib/AST/ByteCode/Compiler.cpp b/clang/lib/AST/ByteCode/Compiler.cpp
index cf89cdc667acc2..1e1e96a1c4782f 100644
--- a/clang/lib/AST/ByteCode/Compiler.cpp
+++ b/clang/lib/AST/ByteCode/Compiler.cpp
@@ -5604,6 +5604,22 @@ bool Compiler<Emitter>::compileConstructor(const CXXConstructorDecl *Ctor) {
 
       if (!emitFieldInitializer(NestedField, NestedFieldOffset, InitExpr))
         return false;
+
+      // Mark all chain links as initialized.
+      unsigned InitFieldOffset = 0;
+      for (const NamedDecl *ND : IFD->chain().drop_back()) {
+        const auto *FD = cast<FieldDecl>(ND);
+        const Record *FieldRecord = this->P.getOrCreateRecord(FD->getParent());
+        assert(FieldRecord);
+        NestedField = FieldRecord->getField(FD);
+        InitFieldOffset += NestedField->Offset;
+        assert(NestedField);
+        if (!this->emitGetPtrThisField(InitFieldOffset, InitExpr))
+          return false;
+        if (!this->emitFinishInitPop(InitExpr))
+          return false;
+      }
+
     } else {
       assert(Init->isDelegatingInitializer());
       if (!this->emitThis(InitExpr))

diff  --git a/clang/test/AST/ByteCode/unions.cpp b/clang/test/AST/ByteCode/unions.cpp
index b1fbb0c4dfc06a..c6b5e34810f05d 100644
--- a/clang/test/AST/ByteCode/unions.cpp
+++ b/clang/test/AST/ByteCode/unions.cpp
@@ -463,4 +463,26 @@ namespace MoveAssign {
   }
   static_assert(f()== 12);
 }
+
+namespace IFD {
+  template <class T>
+  struct Optional {
+    struct {
+      union {
+        char null_state;
+        T val;
+      };
+    };
+    constexpr Optional() : null_state(){}
+  };
+
+  constexpr bool test()
+  {
+    Optional<int> opt{};
+    Optional<int> opt2{};
+    opt = opt2;
+    return true;
+  }
+  static_assert(test());
+}
 #endif


        


More information about the cfe-commits mailing list