[clang] 954a729 - [clang][bytecode] Diagnose reading mutable fields in Memcpy op (#197197)
via cfe-commits
cfe-commits at lists.llvm.org
Fri May 15 01:29:33 PDT 2026
Author: Timm Baeder
Date: 2026-05-15T10:29:28+02:00
New Revision: 954a729039953b42af644a20f44707e86978b977
URL: https://github.com/llvm/llvm-project/commit/954a729039953b42af644a20f44707e86978b977
DIFF: https://github.com/llvm/llvm-project/commit/954a729039953b42af644a20f44707e86978b977.diff
LOG: [clang][bytecode] Diagnose reading mutable fields in Memcpy op (#197197)
Added:
Modified:
clang/lib/AST/ByteCode/InterpBuiltin.cpp
clang/test/AST/ByteCode/records.cpp
Removed:
################################################################################
diff --git a/clang/lib/AST/ByteCode/InterpBuiltin.cpp b/clang/lib/AST/ByteCode/InterpBuiltin.cpp
index e05c9aed39b14..3e9ce902427eb 100644
--- a/clang/lib/AST/ByteCode/InterpBuiltin.cpp
+++ b/clang/lib/AST/ByteCode/InterpBuiltin.cpp
@@ -6664,15 +6664,17 @@ static bool copyRecord(InterpState &S, CodePtr OpPC, const Pointer &Src,
assert(SrcDesc->ElemRecord == DestDesc->ElemRecord);
const Record *R = DestDesc->ElemRecord;
for (const Record::Field &F : R->fields()) {
+ Pointer FP = Src.atField(F.Offset);
+
+ if (!CheckMutable(S, OpPC, FP))
+ return false;
+
if (R->isUnion()) {
// For unions, only copy the active field. Zero all others.
- const Pointer &SrcField = Src.atField(F.Offset);
- if (SrcField.isActive()) {
+ if (FP.isActive()) {
if (!copyField(F, /*Activate=*/true))
return false;
} else {
- if (!CheckMutable(S, OpPC, Src.atField(F.Offset)))
- return false;
Pointer DestField = Dest.atField(F.Offset);
zeroAll(DestField);
}
diff --git a/clang/test/AST/ByteCode/records.cpp b/clang/test/AST/ByteCode/records.cpp
index 20ead8cffdd99..b3f07c3cf007e 100644
--- a/clang/test/AST/ByteCode/records.cpp
+++ b/clang/test/AST/ByteCode/records.cpp
@@ -2011,3 +2011,13 @@ namespace RVOPtrIsExtern {
}
static_assert(test_all(), "");
}
+
+namespace MutableInMemcpy {
+ union H {
+ mutable struct {} gx; // both-note {{declared here}}
+ };
+ constexpr H h1 = {};
+ constexpr H h2 = h1; // both-error {{must be initialized by a constant expression}} \
+ // both-note {{read of mutable member 'gx' is not allowed in a constant expression}} \
+ // both-note {{in call}}
+}
More information about the cfe-commits
mailing list