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

Aaron Ballman via cfe-commits cfe-commits at lists.llvm.org
Thu Jul 23 12:03:46 PDT 2026


================
@@ -14980,6 +14988,22 @@ buildMemcpyForAssignmentOp(Sema &S, SourceLocation Loc, QualType T,
       S.Context, To, UO_AddrOf, S.Context.getPointerType(To->getType()),
       VK_PRValue, OK_Ordinary, Loc, false, S.CurFPFeatureOverrides());
 
+  if (SuppressMemaccessWarning) {
+    // An explicit cast to void* survives IgnoreParenImpCasts, so the memaccess
+    // check sees a void pointee and skips the non-trivial-copy warning.
+    QualType ConstVoidPtr =
+        S.Context.getPointerType(S.Context.VoidTy.withConst());
+    ExprResult FromCast = S.BuildCStyleCastExpr(
+        Loc, S.Context.getTrivialTypeSourceInfo(ConstVoidPtr, Loc), Loc, From);
+    ExprResult ToCast = S.BuildCStyleCastExpr(
----------------
AaronBallman wrote:

> I feel like addings casts to silence warnings is not something we should do (even if that's what people would do in source code)

I tend to agree. For example, those casts would likely be a bit of a surprise to tooling using AST matchers. (But then again, maybe the optimization where we call memcpy will already be a surprise to them and the casts are just more surprise.) And because they're not explicit, will it be possible for them to show up in `-ast-print` output?

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


More information about the cfe-commits mailing list