mirror-llvm-project/clang/docs
Adam Smith ec2bf56676
[Clang][Sema] Synthesize a memcpy body for defaulted union assignment (#206579)
A defaulted copy or move assignment operator for a union is synthesized
with an empty body. `DefineImplicitCopyAssignment` /
`DefineImplicitMoveAssignment` skip union members in the memberwise
loop, and the implied copy of the object representation has no AST
representation, a FIXME that has sat at that skip for a long time. The
operator ends up copying nothing.

Classic CodeGen hides this at ordinary call sites by lowering a trivial
assignment to a memcpy at the call site, so `u1 = u2` works even though
the operator body is a no-op. But when the operator is genuinely called,
through a pointer-to-member for instance, it silently copies nothing.
ClangIR calls the assignment operator at the call site rather than
eliding it, so it hits the empty body directly and drops every union
assignment. The no-op is then deleted at `-O3`. That is the MultiSource
`kc` miscompile, where a `YYSTYPE` union assignment (`*++yyvsp =
yylval`) becomes a no-op.

This implements the FIXME by mirroring the defaulted union copy
constructor (`CGClass.cpp`: "union copy constructor, we must emit a
memcpy") in the AST: when the class is a union,
`DefineImplicit{Copy,Move}Assignment` emits a single whole-object copy
through `buildMemcpyForAssignmentOp`, the helper already used for
trivially-copyable array members, instead of the skipped per-member
assignments. The operator stays trivial (triviality is fixed at
declaration time, before the body is synthesized), so constant
evaluation, which copies trivial unions through its own semantic path
and never executes the body, is unchanged.

Copying the object representation is correct even when a union member is
not trivially copyable, but the synthesized memcpy would then trip
`-Wnontrivial-memcall`. A `void*` cast would drop pointee qualifiers
such as the address space and inject an explicit cast node the source
never wrote. Instead the call keeps its typed union-pointer arguments,
and the union branch in `DefineImplicit{Copy,Move}Assignment` wraps the
build in `IgnoreAllWarningDiagRAII`, a save-and-restore around the
engine's existing ignore-all-warnings state. `CheckMemaccessArguments`
defers the warning and its note through `DiagRuntimeBehavior`, and that
queue flushes after the RAII has restored the previous ignore state, so
`DiagIfReachable` samples that state as it enqueues and drops what is
not error-class. Under `-w` the same queue is discarded before it
flushes, so nothing outside the scoped region changes.

Classic CodeGen now emits the copy when the operator is odr-used, and
ClangIR's union assignment lowers to a real memcpy. On the Sema side,
tests cover the synthesized AST for the implicit-`this` and C++23
explicit-object spellings, the `-Wnontrivial-memcall` suppression, the
preserved address space, the clean `-ast-print` output, and constexpr
active-member copy. The classic and ClangIR lowering is checked across
named, anonymous, and tail-padded unions.

#198918 added a defaulted-union `errorNYI` in ClangIR and deferred this
AST fix to a separate change. This PR makes that fix and drops the
guard, so the now-non-empty body lowers to a `cir.call @memcpy` instead
of erroring. Its own `union-copy-move-assignment.cpp` CIR test, which
expects that call, is the regression check.
2026-07-31 15:49:40 -05:00
..
analyzer Revert "[clang][docs] Update CommandLineUsage and UsersManual with some flags (#209519)" (#213092) 2026-07-30 19:37:29 +01:00
CIR [docs] Rewrite selected Clang docs to Markdown (#209283) 2026-07-15 13:54:03 -07:00
CommandGuide [docs] Rewrite selected Clang docs to Markdown (#209283) 2026-07-15 13:54:03 -07:00
DataFlowAnalysisIntroImages
HLSL [clang][docs] Rewrite 20 selected Clang docs from reST to Markdown (#208310) 2026-07-09 08:16:15 -07:00
ScalableStaticAnalysis [clang][ssaf] Integrate source-edit-generation (#208590) 2026-07-17 15:22:48 -07:00
tools [clang] Add test run of 'dump_ast_matchers.py' (#165472) 2026-06-29 18:21:29 +03:00
AddressSanitizer.md [docs] Migrate 20 clang docs from reST to Markdown (#206182) 2026-06-29 13:52:06 -07:00
AllocToken.md [docs] Rewrite selected Clang docs to Markdown (#209283) 2026-07-15 13:54:03 -07:00
AMDGPUSupport.md [docs] Rewrite selected Clang docs to Markdown (#209283) 2026-07-15 13:54:03 -07:00
APINotes.md [docs] Rewrite selected Clang docs to Markdown (#209283) 2026-07-15 13:54:03 -07:00
AutomaticReferenceCounting.rst [clang] Proofread *.rst (#168215) 2025-11-16 08:12:23 -08:00
Block-ABI-Apple.rst [clang] Proofread *.rst (#168215) 2025-11-16 08:12:23 -08:00
BlockLanguageSpec.rst [clang] Proofread *.rst (#166897) 2025-11-07 08:57:18 -08:00
BoundsSafety.md [docs] Rewrite selected Clang docs to Markdown (#209283) 2026-07-15 13:54:03 -07:00
BoundsSafetyAdoptionGuide.md [docs] Rewrite selected Clang docs to Markdown (#209283) 2026-07-15 13:54:03 -07:00
BoundsSafetyImplPlans.md [docs] Rewrite selected Clang docs to Markdown (#209283) 2026-07-15 13:54:03 -07:00
ClangCheck.rst
ClangFormat.rst [clang-format]: Fix JSON casing (#168156) 2025-11-16 16:01:06 +00:00
ClangFormatStyleOptions.rst [clang-format] Add SpacesInComments option for block comments (#204727) 2026-07-30 22:49:21 +02:00
ClangLinkerWrapper.rst [ClangLinkerWrapper] Use discrete steps in verbose mode (#204186) 2026-07-06 10:23:13 -05:00
ClangNVLinkWrapper.rst [clang] Proofread *.rst (#166897) 2025-11-07 08:57:18 -08:00
ClangOffloadBundler.rst [clang] Proofread ClangOffloadBundler.rst (#165136) 2025-10-26 13:34:07 -07:00
ClangPlugins.md [docs] Rewrite selected Clang docs to Markdown (#209283) 2026-07-15 13:54:03 -07:00
ClangRepl.rst
ClangRepl_design.png
ClangStaticAnalyzer.md [analyzer][docs] Fix invalid MyST toctree 'numbered' option after Markdown migration (#207217) 2026-07-03 14:49:33 +01:00
ClangSYCLLinker.rst [clang][SYCL] Add -fsycl-device-image-split option (#206870) 2026-07-01 16:21:45 -07:00
ClangTools.rst [clang] Proofread *.rst (#168215) 2025-11-16 08:12:23 -08:00
ClangTransformerTutorial.rst [clang] Proofread *.rst (#168215) 2025-11-16 08:12:23 -08:00
CMakeLists.txt [docs] try again to handle doxygen everywhere (#203081) 2026-06-11 08:58:36 -04:00
conf.py [docs] Migrate 20 clang docs from reST to Markdown (#206182) 2026-06-29 13:52:06 -07:00
ConstantInterpreter.rst [clang][bytecode] Update high-level documentation (#202596) 2026-06-22 10:59:13 +02:00
ControlFlowIntegrity.md [docs] Rewrite selected Clang docs to Markdown (#209283) 2026-07-15 13:54:03 -07:00
ControlFlowIntegrityDesign.rst
coro-async-task-continuations.png
coro-generator-suspended.png
coro-generator-variables.png
CrossCompilation.md [docs] Migrate 20 clang docs from reST to Markdown (#206182) 2026-06-29 13:52:06 -07:00
CXXTypeAwareAllocators.md [docs] Rewrite selected Clang docs to Markdown (#209283) 2026-07-15 13:54:03 -07:00
DataFlowAnalysisIntro.md
DataFlowSanitizer.md [docs] Migrate 20 clang docs from reST to Markdown (#206182) 2026-06-29 13:52:06 -07:00
DataFlowSanitizerDesign.rst [clang] Proofread *.rst (#168215) 2025-11-16 08:12:23 -08:00
DebuggingCoroutines.md [docs] Rewrite selected Clang docs to Markdown (#209283) 2026-07-15 13:54:03 -07:00
doxygen-mainpage.dox
doxygen.cfg.in [docs] update CI to use latest release of doxygen (#202420) 2026-06-10 10:25:33 -04:00
DriverArchitecture.png
DriverInternals.rst
ExternalClangExamples.md [docs] Rewrite selected Clang docs to Markdown (#209283) 2026-07-15 13:54:03 -07:00
FAQ.md [docs] Rewrite selected Clang docs to Markdown (#209283) 2026-07-15 13:54:03 -07:00
FunctionEffectAnalysis.md [docs] Migrate 20 clang docs from reST to Markdown (#206182) 2026-06-29 13:52:06 -07:00
HardwareAssistedAddressSanitizerDesign.rst [clang] Proofread *.rst (#166897) 2025-11-07 08:57:18 -08:00
HIPSupport.md [DOC] Update SPIR-V Support on HIPAMD ToolChain (#211542) 2026-07-23 17:12:41 +02:00
HowToSetupToolingForLLVM.rst
index.rst [clang][ssaf] Shorten directory name to ScalableStaticAnalysis (#204697) 2026-06-24 16:44:57 -07:00
InternalsManual.rst [clang][docs] Fix include path in InternalsManual (#209458) 2026-07-20 12:20:10 +00:00
IntroductionToTheClangAST.md [docs] Rewrite selected Clang docs to Markdown (#209283) 2026-07-15 13:54:03 -07:00
ItaniumMangleAbiTags.rst
JSONCompilationDatabase.rst [clang] Proofread *.rst (#168215) 2025-11-16 08:12:23 -08:00
LanguageExtensions.md [Clang][C2y] Add support for if declarations (N3356 paper) (#198244) 2026-07-14 22:16:31 +03:00
LeakSanitizer.md [docs] Migrate 20 clang docs from reST to Markdown (#206182) 2026-06-29 13:52:06 -07:00
LibASTImporter.rst [clang] Proofread *.rst (#168215) 2025-11-16 08:12:23 -08:00
LibASTMatchers.rst [clang] Proofread *.rst (#166897) 2025-11-07 08:57:18 -08:00
LibASTMatchersReference.html Revert "[clang] Reland: fix getTemplateInstantiationArgs (#208285)" (#208426) 2026-07-09 12:11:45 +00:00
LibASTMatchersTutorial.rst [clang] Proofread *.rst (#166897) 2025-11-07 08:57:18 -08:00
LibClang.md [docs] Rewrite selected Clang docs to Markdown (#209283) 2026-07-15 13:54:03 -07:00
LibFormat.md [docs] Rewrite selected Clang docs to Markdown (#209283) 2026-07-15 13:54:03 -07:00
LibTooling.md [docs] Rewrite selected Clang docs to Markdown (#209283) 2026-07-15 13:54:03 -07:00
LifetimeSafety.md [docs] Migrate 20 clang docs from reST to Markdown (#206182) 2026-06-29 13:52:06 -07:00
LLVMExceptionHandlingCodeGen.rst [clang][docs] Add documentation for EH codegen (#176236) 2026-01-29 14:51:21 -08:00
LTOVisibility.md [clang][docs] Rewrite 20 selected Clang docs from reST to Markdown (#208310) 2026-07-09 08:16:15 -07:00
Maintainers.md [clang] Convert Maintainers.md from rst to markdown 2026-02-10 23:13:56 +00:00
make.bat
MatrixTypes.rst [HLSL][Matrix] Add support for single subscript accessor (#170779) 2025-12-17 17:04:28 -05:00
MemorySanitizer.md [docs] Migrate 20 clang docs from reST to Markdown (#206182) 2026-06-29 13:52:06 -07:00
MisExpect.md [clang][docs] Rewrite 20 selected Clang docs from reST to Markdown (#208310) 2026-07-09 08:16:15 -07:00
Modules.md [clang][docs] Rewrite 20 selected Clang docs from reST to Markdown (#208310) 2026-07-09 08:16:15 -07:00
MSVCCompatibility.md [clang][docs] Rewrite 20 selected Clang docs from reST to Markdown (#208310) 2026-07-09 08:16:15 -07:00
Multilib.rst [Clang] Support multilibs on Windows (#200212) 2026-06-09 08:35:07 -05:00
ObjectiveCLiterals.rst
OffloadingDesign.rst
OpenCLSupport.md [clang][docs] Rewrite 20 selected Clang docs from reST to Markdown (#208310) 2026-07-09 08:16:15 -07:00
OpenMPSupport.md [Clang][OpenMP] Add parsing/sema for dims modifier in num_teams and thread_limit (#206412) 2026-07-19 00:13:57 -07:00
OverflowBehaviorTypes.md [clang][docs] Rewrite 20 selected Clang docs from reST to Markdown (#208310) 2026-07-09 08:16:15 -07:00
PCHInternals.rst [clang] Proofread *.rst (#168215) 2025-11-16 08:12:23 -08:00
PCHLayout.graffle
PCHLayout.png
PointerAuthentication.md [clang][docs] Rewrite 20 selected Clang docs from reST to Markdown (#208310) 2026-07-09 08:16:15 -07:00
RAVFrontendAction.rst [clang] Proofread *.rst (#168215) 2025-11-16 08:12:23 -08:00
README.txt
RealtimeSanitizer.md [docs] Migrate 20 clang docs from reST to Markdown (#206182) 2026-06-29 13:52:06 -07:00
RefactoringEngine.rst
ReleaseNotes.md [Clang][Sema] Synthesize a memcpy body for defaulted union assignment (#206579) 2026-07-31 15:49:40 -05:00
ReleaseNotesTemplate.txt [Clang] Add flags and lang option for C++2d (#203992) 2026-07-03 21:37:02 +00:00
RISCVSupport.md [docs] Rewrite selected Clang docs to Markdown (#209283) 2026-07-15 13:54:03 -07:00
SafeBuffers.md [docs] Migrate 20 clang docs from reST to Markdown (#206182) 2026-06-29 13:52:06 -07:00
SafeStack.md [clang][docs] Rewrite 20 selected Clang docs from reST to Markdown (#208310) 2026-07-09 08:16:15 -07:00
SanitizerCoverage.md [docs] Migrate 20 clang docs from reST to Markdown (#206182) 2026-06-29 13:52:06 -07:00
SanitizerSpecialCaseList.md [clang][docs] Rewrite 20 selected Clang docs from reST to Markdown (#208310) 2026-07-09 08:16:15 -07:00
SanitizerStats.md [clang][docs] Rewrite 20 selected Clang docs from reST to Markdown (#208310) 2026-07-09 08:16:15 -07:00
ShadowCallStack.md [clang][docs] Rewrite 20 selected Clang docs from reST to Markdown (#208310) 2026-07-09 08:16:15 -07:00
SourceBasedCodeCoverage.md [clang][docs] Rewrite 20 selected Clang docs from reST to Markdown (#208310) 2026-07-09 08:16:15 -07:00
StandardCPlusPlusModules.md [clang][docs] Rewrite 20 selected Clang docs from reST to Markdown (#208310) 2026-07-09 08:16:15 -07:00
StructureProtection.md [clang][docs] Rewrite 20 selected Clang docs from reST to Markdown (#208310) 2026-07-09 08:16:15 -07:00
SYCLSupport.md [clang][docs] Rewrite 20 selected Clang docs from reST to Markdown (#208310) 2026-07-09 08:16:15 -07:00
ThinLTO.md [clang][docs] Rewrite 20 selected Clang docs from reST to Markdown (#208310) 2026-07-09 08:16:15 -07:00
ThreadSafetyAnalysis.md Thread Safety Analysis: Don't treat function pointer parameters as scoped capabilities (#211885) 2026-07-28 11:53:13 -04:00
ThreadSanitizer.md [docs] Migrate 20 clang docs from reST to Markdown (#206182) 2026-06-29 13:52:06 -07:00
Toolchain.md [docs] Migrate 20 clang docs from reST to Markdown (#206182) 2026-06-29 13:52:06 -07:00
Tooling.md [docs] Rewrite selected Clang docs to Markdown (#209283) 2026-07-15 13:54:03 -07:00
TypeSanitizer.md [docs] Migrate 20 clang docs from reST to Markdown (#206182) 2026-06-29 13:52:06 -07:00
UndefinedBehaviorSanitizer.md [docs] Migrate 20 clang docs from reST to Markdown (#206182) 2026-06-29 13:52:06 -07:00
UsersManual.md Revert "[clang][docs] Update CommandLineUsage and UsersManual with some flags (#209519)" (#213092) 2026-07-30 19:37:29 +01:00
WarningSuppressionMappings.md [docs] Migrate 20 clang docs from reST to Markdown (#206182) 2026-06-29 13:52:06 -07:00

See llvm/docs/README.txt