[clang] [clang][bytecode] Allow operations on volatile objects in ctors (PR #174425)
via cfe-commits
cfe-commits at lists.llvm.org
Mon Jan 5 07:45:18 PST 2026
llvmbot wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-clang
Author: Timm Baeder (tbaederr)
<details>
<summary>Changes</summary>
---
Full diff: https://github.com/llvm/llvm-project/pull/174425.diff
2 Files Affected:
- (modified) clang/lib/AST/ByteCode/Interp.cpp (+4)
- (modified) clang/test/AST/ByteCode/cxx23.cpp (+15-5)
``````````diff
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 {
``````````
</details>
https://github.com/llvm/llvm-project/pull/174425
More information about the cfe-commits
mailing list