[clang] [clang][bytecode] Allow const mutation in all variable initializers (PR #195794)
Timm Baeder via cfe-commits
cfe-commits at lists.llvm.org
Wed May 6 01:18:17 PDT 2026
https://github.com/tbaederr updated https://github.com/llvm/llvm-project/pull/195794
>From 709a6c65db93d277337cf7b59c0cfd38251cb902 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Timm=20B=C3=A4der?= <tbaeder at redhat.com>
Date: Tue, 5 May 2026 07:39:17 +0200
Subject: [PATCH] [clang][bytecode]
---
clang/lib/AST/ByteCode/Compiler.cpp | 6 ++++++
clang/lib/AST/ByteCode/Interp.h | 11 +++++++++++
clang/lib/AST/ByteCode/Opcodes.td | 4 ++++
clang/test/AST/ByteCode/cxx14.cpp | 12 ++++++++++++
4 files changed, 33 insertions(+)
diff --git a/clang/lib/AST/ByteCode/Compiler.cpp b/clang/lib/AST/ByteCode/Compiler.cpp
index 11c5478a5c748..90f89c3e5af38 100644
--- a/clang/lib/AST/ByteCode/Compiler.cpp
+++ b/clang/lib/AST/ByteCode/Compiler.cpp
@@ -5290,9 +5290,15 @@ VarCreationState Compiler<Emitter>::visitVarDecl(const VarDecl *VD,
if (!this->emitGetPtrGlobal(*GlobalIndex, Init))
return false;
+ if (!this->emitStartInit(Init))
+ return false;
+
if (!visitInitializer(Init))
return false;
+ if (!this->emitEndInit(Init))
+ return false;
+
return this->emitFinishInitGlobal(Init);
}
// Local variables.
diff --git a/clang/lib/AST/ByteCode/Interp.h b/clang/lib/AST/ByteCode/Interp.h
index 4a550fdd63bfb..fe2d99901d367 100644
--- a/clang/lib/AST/ByteCode/Interp.h
+++ b/clang/lib/AST/ByteCode/Interp.h
@@ -3564,6 +3564,17 @@ inline bool StartSpeculation(InterpState &S, CodePtr OpPC) {
return true;
}
+inline bool StartInit(InterpState &S, CodePtr OpPC) {
+ const Pointer &Ptr = S.Stk.peek<Pointer>();
+ S.InitializingBlocks.push_back(Ptr.block());
+ return true;
+}
+
+inline bool EndInit(InterpState &S, CodePtr OpPC) {
+ S.InitializingBlocks.pop_back();
+ return true;
+}
+
// This is special-cased in the tablegen opcode emitter.
// Its dispatch function will NOT call InterpNext
// and instead simply return true.
diff --git a/clang/lib/AST/ByteCode/Opcodes.td b/clang/lib/AST/ByteCode/Opcodes.td
index 3fb25a5fa0884..57ed71fb6f16b 100644
--- a/clang/lib/AST/ByteCode/Opcodes.td
+++ b/clang/lib/AST/ByteCode/Opcodes.td
@@ -181,6 +181,10 @@ def Jf : JumpOpcode;
def PushIgnoreDiags : Opcode;
def PopIgnoreDiags : Opcode;
+
+def StartInit : Opcode;
+def EndInit : Opcode;
+
def StartSpeculation : Opcode;
def EndSpeculation : Opcode;
def BCP : Opcode {
diff --git a/clang/test/AST/ByteCode/cxx14.cpp b/clang/test/AST/ByteCode/cxx14.cpp
index 00a9cf5b00098..170bd09504993 100644
--- a/clang/test/AST/ByteCode/cxx14.cpp
+++ b/clang/test/AST/ByteCode/cxx14.cpp
@@ -35,3 +35,15 @@ constexpr bool f() { // both-error {{constexpr function never produces a constan
// both-warning {{array index 3 is past the end of the array}}
return true;
}
+
+namespace InitListModify {
+ struct Aggregate {
+ int x = 0;
+ int y = ++x;
+ };
+ constexpr Aggregate aggr1;
+ static_assert(aggr1.x == 1 && aggr1.y == 1, "");
+ // FIXME: This is not specified by the standard, but sanity requires it.
+ constexpr Aggregate aggr2 = {};
+ static_assert(aggr2.x == 1 && aggr2.y == 1, "");
+}
More information about the cfe-commits
mailing list