[clang] [clang][bytecode] Allow operations on volatile objects in ctors (PR #174425)
Timm Baeder via cfe-commits
cfe-commits at lists.llvm.org
Mon Jan 5 07:44:49 PST 2026
https://github.com/tbaederr created https://github.com/llvm/llvm-project/pull/174425
None
>From 7ca97b7a57638d722737d31332a9e6b7e9caef75 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Timm=20B=C3=A4der?= <tbaeder at redhat.com>
Date: Mon, 5 Jan 2026 16:42:37 +0100
Subject: [PATCH] [clang][bytecode] Allow operations on volatile objects in
ctors
---
clang/lib/AST/ByteCode/Interp.cpp | 4 ++++
clang/test/AST/ByteCode/cxx23.cpp | 20 +++++++++++++++-----
2 files changed, 19 insertions(+), 5 deletions(-)
diff --git a/clang/lib/AST/ByteCode/Interp.cpp b/clang/lib/AST/ByteCode/Interp.cpp
index 889ac1e1a9a7e..966c9d9b5bf66 100644
--- a/clang/lib/AST/ByteCode/Interp.cpp
+++ b/clang/lib/AST/ByteCode/Interp.cpp
@@ -626,6 +626,10 @@ static bool CheckVolatile(InterpState &S, CodePtr OpPC, const Pointer &Ptr,
if (!S.getLangOpts().CPlusPlus)
return Invalid(S, OpPC);
+ // Volatile object can be written-to and read if they are being constructed.
+ if (llvm::is_contained(S.InitializingBlocks, Ptr.block()))
+ return true;
+
// The reason why Ptr is volatile might be further up the hierarchy.
// Find that pointer.
Pointer P = Ptr;
diff --git a/clang/test/AST/ByteCode/cxx23.cpp b/clang/test/AST/ByteCode/cxx23.cpp
index 2a8b061d7671a..4a4ff4b48e8b5 100644
--- a/clang/test/AST/ByteCode/cxx23.cpp
+++ b/clang/test/AST/ByteCode/cxx23.cpp
@@ -395,7 +395,7 @@ namespace UnionMemberCallDiags {
}
#endif
-namespace VolatileWrites {
+namespace Volatile {
constexpr void test1() {// all20-error {{never produces a constant expression}}
int k;
volatile int &m = k;
@@ -448,10 +448,8 @@ namespace VolatileWrites {
}
static_assert(test7(12)); // all-error {{not an integral constant expression}} \
// all-note {{in call to}}
-}
-namespace VolatileReads {
- constexpr int test1(bool b) {
+ constexpr int test8(bool b) {
if (!b)
return -1;
struct C {
@@ -460,8 +458,20 @@ namespace VolatileReads {
volatile C c{12}; // all-note {{volatile object declared here}}
return ((C&)(c)).a; // all-note {{read of volatile object}}
}
- static_assert(test1(true) == 0); // all-error {{not an integral constant expression}} \
+ static_assert(test8(true) == 0); // all-error {{not an integral constant expression}} \
// all-note {{in call to}}
+
+ struct C {
+ int n;
+ constexpr C() : n(1) { n = 2; int b= n;}
+ };
+ constexpr int f(bool get) {
+ if (get)
+ return -1;
+ volatile C c;
+ return 2;
+ }
+ static_assert(f(false) == 2, "");
}
namespace AIEWithIndex0Narrows {
More information about the cfe-commits
mailing list