[clang] [clang][bytecode] Report mutable reads when copying unions (PR #149320)
Timm Baeder via cfe-commits
cfe-commits at lists.llvm.org
Thu Jul 17 07:29:11 PDT 2025
https://github.com/tbaederr created https://github.com/llvm/llvm-project/pull/149320
None
>From 896c606f240e302f12a891aa9ddabba70f2e6364 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Timm=20B=C3=A4der?= <tbaeder at redhat.com>
Date: Thu, 17 Jul 2025 16:28:15 +0200
Subject: [PATCH] [clang][bytecode] Report mutable reads when copying unions
---
clang/lib/AST/ByteCode/InterpBuiltin.cpp | 2 ++
clang/lib/AST/ByteCode/InterpFrame.cpp | 5 +++++
clang/test/AST/ByteCode/unions.cpp | 14 ++++++++++++++
3 files changed, 21 insertions(+)
diff --git a/clang/lib/AST/ByteCode/InterpBuiltin.cpp b/clang/lib/AST/ByteCode/InterpBuiltin.cpp
index de0b97fd93c76..ace7016785069 100644
--- a/clang/lib/AST/ByteCode/InterpBuiltin.cpp
+++ b/clang/lib/AST/ByteCode/InterpBuiltin.cpp
@@ -2906,6 +2906,8 @@ static bool copyRecord(InterpState &S, CodePtr OpPC, const Pointer &Src,
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/lib/AST/ByteCode/InterpFrame.cpp b/clang/lib/AST/ByteCode/InterpFrame.cpp
index a5a4bd25fe712..d62a4f6275b50 100644
--- a/clang/lib/AST/ByteCode/InterpFrame.cpp
+++ b/clang/lib/AST/ByteCode/InterpFrame.cpp
@@ -128,6 +128,11 @@ static bool shouldSkipInBacktrace(const Function *F) {
if (FD->getDeclName().getCXXOverloadedOperator() == OO_New ||
FD->getDeclName().getCXXOverloadedOperator() == OO_Array_New)
return true;
+
+ if (const auto *MD = dyn_cast<CXXMethodDecl>(FD);
+ MD && MD->getParent()->isAnonymousStructOrUnion())
+ return true;
+
return false;
}
diff --git a/clang/test/AST/ByteCode/unions.cpp b/clang/test/AST/ByteCode/unions.cpp
index 0fa44a259a4ff..7cfd0d677a7b3 100644
--- a/clang/test/AST/ByteCode/unions.cpp
+++ b/clang/test/AST/ByteCode/unions.cpp
@@ -847,6 +847,20 @@ namespace Activation2 {
}
static_assert(change_member_indirectly() == 4);
}
+
+namespace CopyCtorMutable {
+ struct E {
+ union { // expected-note {{read of mutable member 'b'}}
+ int a;
+ mutable int b; // both-note {{here}}
+ };
+ };
+ constexpr E e1 = {{1}};
+ constexpr E e2 = e1; // both-error {{constant}} \
+ // ref-note {{read of mutable member 'b'}} \
+ // both-note {{in call}}
+}
+
#endif
namespace AddressComparison {
More information about the cfe-commits
mailing list