Author: Timm Baeder Date: 2025-07-18T11:10:57+02:00 New Revision: 3bb4355bb83692d9c859043076db16baa86431e1
URL: https://github.com/llvm/llvm-project/commit/3bb4355bb83692d9c859043076db16baa86431e1 DIFF: https://github.com/llvm/llvm-project/commit/3bb4355bb83692d9c859043076db16baa86431e1.diff LOG: [clang][bytecode] Report mutable reads when copying unions (#149320) Added: Modified: clang/lib/AST/ByteCode/InterpBuiltin.cpp clang/lib/AST/ByteCode/InterpFrame.cpp clang/test/AST/ByteCode/unions.cpp Removed: ################################################################################ diff --git a/clang/lib/AST/ByteCode/InterpBuiltin.cpp b/clang/lib/AST/ByteCode/InterpBuiltin.cpp index 9ce1e380bff2c..462b9a11e0a5c 100644 --- a/clang/lib/AST/ByteCode/InterpBuiltin.cpp +++ b/clang/lib/AST/ByteCode/InterpBuiltin.cpp @@ -2905,6 +2905,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 { _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits