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

Erich Keane via cfe-commits cfe-commits at lists.llvm.org
Wed Jul 29 07:45:06 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(
----------------
erichkeane wrote:

> > No, I don't think we should be messing with the 'CheckMemaccessArguments' bit either. I suspect the solution is for `DiagRuntimeBehavior ` to be taught to pay attention to whether the warning it is trying to emit is ignored. WDYT @AaronBallman ?
> 
> I think more debugging is needed -- the diagnostic engine is what decides to skip rendering an ignored diagnostic:
> 
> https://github.com/llvm/llvm-project/blob/01891953eea17544f6181bb51c973711a382702b/clang/lib/Basic/Diagnostic.cpp#L676
> so why is that logic not working?
> 
> > ... which in a potentially-evaluated context defers it via DiagIfReachable into the function scope's PossiblyUnreachableDiags. Those flush only when ~SynthesizedFunctionScope runs PopFunctionScopeInfo at the end of the synthesized body. The ignore state is read at that flush rather than at the call. S
> 
> It makes sense that we process those when popping the function scope, but why is the location of the diagnostics incorrect? I can't seem to induce a failure through using the pragma directly: https://godbolt.org/z/T31b1T8f1

`SetIgnoreAllWarnings` doesn't actually modify the 'loc' info of the diagnostic being enabled.  It is just a little bit of a side-channel disable for warnings.  So it only applys at the actual 'When emitted' time of the compiler, not of the location in source.  So it is a bit 'hacked' in like I said above.

To do the 'source based' bit, we'd have to do the `setSeverity` type calls, and doing that for all warnings every time we do a union seems like a 'heavy' lift?  

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


More information about the cfe-commits mailing list