> From: Christopher Bazley <[email protected]> > Date: Tue, 10 Feb 2026 15:33:07 +0000
The following patch, archived as https://gcc.gnu.org/pipermail/gcc-patches/2026-February/708111.html seems to have received no comments nor has it been pinged AFAICT. I'd like to ping it on behalf of the author, and FWIW endorse it. If the author has lost interest (I hope not), I offer to adopt it. > Commit 0547dbb725b reduced the number of cases in which > union padding bits are zeroed when the relevant language > standard does not strictly require it, unless gcc was > invoked with -fzero-init-padding-bits=unions or > -fzero-init-padding-bits=all in order to explicitly > request zeroing of padding bits. > > This commit adds a closely related warning, > -Wzero-init-padding-bits=, which is intended to help > programmers to find code that might now need to be > rewritten or recompiled with > -fzero-init-padding-bits=unions or > -fzero-init-padding-bits=all in order to replicate > the behaviour that it had when compiled by older > versions of GCC. It can also be used to find struct > padding that was never previously guaranteed to be > zero initialized and still isn't unless GCC is > invoked with -fzero-init-padding-bits=all. > > The new warning can be set to the same three states > as -fzero-init-padding-bits ('standard', 'unions' > or 'all') and has the same default value ('standard'). > > The two options interact as follows: > > f: standard f: unions f: all > w: standard X X X > w: unions U X X > w: all A S X > > X = No warnings about padding > U = Warnings about padding of unions. > S = Warnings about padding of structs. > A = Warnings about padding of structs and unions. > > The level of optimisation and whether or not the > entire initializer is dropped to memory can both > affect whether warnings are produced when compiling > a given program. This is intentional, since tying > the warnings more closely to the relevant language > standard would require a very different approach > that would still be target-dependent, might impose > an unacceptable burden on programmers, and would > risk not satisfying the intended use-case (which > is closely tied to a specific optimisation). > > gcc/ChangeLog: > > * common.opt: Add Wzero-init-padding-bits=. > * doc/invoke.texi: Document Wzero-init-padding-bits=. > * expr.cc (categorize_ctor_elements_1): Update new struct type > ctor_completeness instead of an integer to indicate presence of > padding or missing fields in a constructor. Instead of setting -1 > upon discovery of padding bits in both structs and unions, > set separate flags to indicate the type of padding bits. > (categorize_ctor_elements): Update the type and documentation of > the p_complete parameter. > (mostly_zeros_p): Use new struct type ctor_completeness when > calling categorize_ctor_elements. > (all_zeros_p): Use new struct type ctor_completeness when > calling categorize_ctor_elements. > * expr.h (struct ctor_completeness): New struct type to replace an > an integer that could take the value -1 ('all fields are > initialized, but there's padding'), 0 ('fields are missing') or > 1 ('all fields are initialized, and there's no padding'). Named > bool members make the code easier to understand and make room to > disambiguate struct padding bits from union padding bits. > (categorize_ctor_elements): Update the function declaration to use > the new struct type in the last parameter declaration. > * gimplify.cc (gimplify_init_constructor): Replace use of > complete_p != 0 ('all fields are initialized') with !sparse, > replace use of complete == 0 ('fields are missing') with sparse, and > replace use of complete <= 0 ('fields are missing' or 'all fields are > initialized, but there's padding') with sparse || padded_union or > padded_non_union. Trigger new warnings if storage for the object > is not zeroed but padded_union or padded_non_union is set > (because this combination implies possible non-zero padding bits). > > gcc/testsuite/ChangeLog: > > * gcc.dg/c23-empty-init-warn-1.c: New test. > * gcc.dg/c23-empty-init-warn-10.c: New test. > * gcc.dg/c23-empty-init-warn-11.c: New test. > * gcc.dg/c23-empty-init-warn-12.c: New test. > * gcc.dg/c23-empty-init-warn-13.c: New test. > * gcc.dg/c23-empty-init-warn-14.c: New test. > * gcc.dg/c23-empty-init-warn-15.c: New test. > * gcc.dg/c23-empty-init-warn-16.c: New test. > * gcc.dg/c23-empty-init-warn-17.c: New test. > * gcc.dg/c23-empty-init-warn-2.c: New test. > * gcc.dg/c23-empty-init-warn-3.c: New test. > * gcc.dg/c23-empty-init-warn-4.c: New test. > * gcc.dg/c23-empty-init-warn-5.c: New test. > * gcc.dg/c23-empty-init-warn-6.c: New test. > * gcc.dg/c23-empty-init-warn-7.c: New test. > * gcc.dg/c23-empty-init-warn-8.c: New test. > * gcc.dg/c23-empty-init-warn-9.c: New test. > * gcc.dg/gnu11-empty-init-warn-1.c: New test. > * gcc.dg/gnu11-empty-init-warn-10.c: New test. > * gcc.dg/gnu11-empty-init-warn-11.c: New test. > * gcc.dg/gnu11-empty-init-warn-12.c: New test. > * gcc.dg/gnu11-empty-init-warn-13.c: New test. > * gcc.dg/gnu11-empty-init-warn-14.c: New test. > * gcc.dg/gnu11-empty-init-warn-15.c: New test. > * gcc.dg/gnu11-empty-init-warn-16.c: New test. > * gcc.dg/gnu11-empty-init-warn-17.c: New test. > * gcc.dg/gnu11-empty-init-warn-2.c: New test. > * gcc.dg/gnu11-empty-init-warn-3.c: New test. > * gcc.dg/gnu11-empty-init-warn-4.c: New test. > * gcc.dg/gnu11-empty-init-warn-5.c: New test. > * gcc.dg/gnu11-empty-init-warn-6.c: New test. > * gcc.dg/gnu11-empty-init-warn-7.c: New test. > * gcc.dg/gnu11-empty-init-warn-8.c: New test. > * gcc.dg/gnu11-empty-init-warn-9.c: New test. [pruned]
