[clang][bytecode] Report mutable reads when copying unions#149320
Merged
[clang][bytecode] Report mutable reads when copying unions#149320
Conversation
Member
|
@llvm/pr-subscribers-clang Author: Timm Baeder (tbaederr) ChangesFull diff: https://github.com/llvm/llvm-project/pull/149320.diff 3 Files Affected:
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 {
|
This was referenced Jul 23, 2025
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
No description provided.