[clang] [clang][bytecode] Mark IndirectFieldDecl chain links as initialized (PR #125869)
Timm Baeder via cfe-commits
cfe-commits at lists.llvm.org
Wed Feb 5 07:20:02 PST 2025
https://github.com/tbaederr created https://github.com/llvm/llvm-project/pull/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.
>From 38c40cb30b5e71260ece1c5aa42748513822fe2a Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Timm=20B=C3=A4der?= <tbaeder at redhat.com>
Date: Wed, 5 Feb 2025 16:14:46 +0100
Subject: [PATCH] [clang][bytecode] Mark IndirectFieldDecl chain links as
initialized
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.
---
clang/lib/AST/ByteCode/Compiler.cpp | 16 ++++++++++++++++
clang/test/AST/ByteCode/unions.cpp | 22 ++++++++++++++++++++++
2 files changed, 38 insertions(+)
diff --git a/clang/lib/AST/ByteCode/Compiler.cpp b/clang/lib/AST/ByteCode/Compiler.cpp
index cf89cdc667acc29..1e1e96a1c4782f4 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 b1fbb0c4dfc06a4..c6b5e34810f05d8 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