[clang] [clang][bytecode] Report mutable reads when copying unions (PR #149320)
via cfe-commits
cfe-commits at lists.llvm.org
Thu Jul 17 07:29:42 PDT 2025
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/149320.diff
3 Files Affected:
- (modified) clang/lib/AST/ByteCode/InterpBuiltin.cpp (+2)
- (modified) clang/lib/AST/ByteCode/InterpFrame.cpp (+5)
- (modified) clang/test/AST/ByteCode/unions.cpp (+14)
``````````diff
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 {
``````````
</details>
https://github.com/llvm/llvm-project/pull/149320
More information about the cfe-commits
mailing list