================
@@ -15383,6 +15383,36 @@ 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);
+
+  // Copying the object representation is correct even for a union that is not
+  // trivially copyable, so -Wnontrivial-memcall is a false positive here.
+  // Ignoring warnings rather than casting the arguments to void* keeps them
+  // typed, which preserves their address space.
+  IgnoreAllWarningDiagRAII IgnoreWarnings(S.Diags);
+  StmtResult Copy = buildMemcpyForAssignmentOp(
+      S, Loc, S.Context.getCanonicalTagType(ClassDecl), To, From);
+  if (Copy.isInvalid()) {
+    AssignOp->setInvalidDecl();
----------------
adams381 wrote:

There are two callers of this helper: `Sema::DefineImplicitCopyAssignment` and 
`Sema::DefineImplicitMoveAssignment`.  I can remove the helper, but that would 
require code duplication at both call sites.

https://github.com/llvm/llvm-project/pull/206579
_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to