[clang] [Clang][Sema] Synthesize a memcpy body for defaulted union assignment (PR #206579)
Adam Smith via cfe-commits
cfe-commits at lists.llvm.org
Wed Jul 29 08:54:50 PDT 2026
================
@@ -15383,6 +15401,31 @@ static void diagnoseDeprecatedCopyOperation(Sema &S, CXXMethodDecl *CopyOp) {
}
}
+// A defaulted copy or move assignment operator for a union copies the object
+// representation as if by a memcpy, the same way the defaulted union copy
+// constructor does. The memberwise loops in DefineImplicitCopyAssignment and
+// DefineImplicitMoveAssignment skip union members, so the whole-object copy is
+// emitted here instead. Marks AssignOp invalid and returns false on failure.
+static bool buildUnionAssignmentCopy(Sema &S, SourceLocation Loc,
+ CXXRecordDecl *ClassDecl,
+ std::optional<RefBuilder> &ExplicitObject,
+ std::optional<DerefBuilder> &DerefThis,
+ const ExprBuilder &From,
+ CXXMethodDecl *AssignOp,
+ SmallVectorImpl<Stmt *> &Statements) {
+ ExprBuilder &To = ExplicitObject ? static_cast<ExprBuilder &>(*ExplicitObject)
+ : static_cast<ExprBuilder &>(*DerefThis);
+ StmtResult Copy = buildMemcpyForAssignmentOp(
----------------
adams381 wrote:
I traced the same path and land on the same `DiagIfReachable` fix. The `warn_cxxstruct_memaccess` from the synthesized `__builtin_memcpy` runs through `DiagRuntimeBehavior`, gets parked in the function scope's `PossiblyUnreachableDiags`, and is flushed later by `~SynthesizedFunctionScope`. Because `setIgnoreAllWarnings` is a global flag read at emit time with no source location attached, a narrow save/restore around the call has already been undone by the time the queue flushes, so the warning still comes out.
`CheckMemaccessArguments` also queues the `(void*)` fixit note right after the warning, as its own deferred diagnostic. In normal immediate emission the engine already drops a note whose parent warning was ignored (Diagnostic.cpp:676). If the early-return only covers the warning, that note still lands in the queue and can flush on its own with no parent, so I will have the skip drop the trailing note too when all warnings are ignored, matching the immediate path.
With that in place I will remove the `SuppressMemaccessCheck` flag and add the release note.
https://github.com/llvm/llvm-project/pull/206579
More information about the cfe-commits
mailing list