[clang] [Clang][Sema] Synthesize a memcpy body for defaulted union assignment (PR #206579)

Aaron Ballman via cfe-commits cfe-commits at lists.llvm.org
Wed Jul 29 07:54:09 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(
----------------
AaronBallman wrote:

Good point, I thought we were leaning back towards using the existing machinery to suppress diagnostics. (Huh, it turns out that the pragma to ignore warnings in Clang is not hooked up to `setIgnoreAllWarnings()` which is how I started going down this rabbit hole in the first place, lol)

Changing `DiagIfReachable` to inspect whether all warnings are ignored seems reasonable though.

https://github.com/llvm/llvm-project/pull/206579


More information about the cfe-commits mailing list