As Martin Sebor points out, the ChangeLog entries should not be included
in the patch. So I have removed those. I also made some changes to this
patch that were necessary because of changes to the other patch.

See the patch below:
From 676b6436835434f89c8511cd68e89947c32f11c6 Mon Sep 17 00:00:00 2001
From: Asher Gordon <asd...@posteo.net>
Date: Sun, 7 Jun 2020 13:37:27 -0400
Subject: [PATCH] Add -Wuniversal-initializer to not suppress warnings about {
 0 }.

gcc/ChangeLog:

	* doc/invoke.texi: Document -Wuniversal-initializer.

gcc/c-family/ChangeLog:

	* c.opt: Add -Wuniversal-initializer

gcc/c/ChangeLog:

	* c-typeck.c (pop_init_level): Don't suppress warnings about { 0 }
	if warn_zero_init.

gcc/testsuite/ChangeLog:

	* gcc.dg/Wuniversal-initializer.c: New test.
---
 gcc/c-family/c.opt                            |  4 ++++
 gcc/c/c-typeck.c                              |  9 ++++----
 gcc/doc/invoke.texi                           | 22 ++++++++++++++++++-
 gcc/testsuite/gcc.dg/Wuniversal-initializer.c | 20 +++++++++++++++++
 4 files changed, 50 insertions(+), 5 deletions(-)
 create mode 100644 gcc/testsuite/gcc.dg/Wuniversal-initializer.c

diff --git a/gcc/c-family/c.opt b/gcc/c-family/c.opt
index 89a58282b3f..8bfa28e5f6c 100644
--- a/gcc/c-family/c.opt
+++ b/gcc/c-family/c.opt
@@ -1191,6 +1191,10 @@ Wuninitialized
 C ObjC C++ ObjC++ LTO LangEnabledBy(C ObjC C++ ObjC++ LTO,Wall)
 ;
 
+Wuniversal-initializer
+C ObjC C++ ObjC++ Var(warn_zero_init) Init(0) Warning
+Don't suppress warnings about { 0 }.
+
 Wmaybe-uninitialized
 C ObjC C++ ObjC++ LTO LangEnabledBy(C ObjC C++ ObjC++ LTO,Wall)
 ;
diff --git a/gcc/c/c-typeck.c b/gcc/c/c-typeck.c
index 1bae124c6dc..72efb90c58b 100644
--- a/gcc/c/c-typeck.c
+++ b/gcc/c/c-typeck.c
@@ -8731,7 +8731,7 @@ pop_init_level (location_t loc, int implicit,
 
   /* Warn when some structs are initialized with direct aggregation.  */
   if (!implicit && found_missing_braces && warn_missing_braces
-      && !constructor_zeroinit)
+      && (!constructor_zeroinit || warn_zero_init))
     {
       gcc_assert (initializer_stack->missing_brace_richloc);
       warning_at (initializer_stack->missing_brace_richloc,
@@ -8755,8 +8755,9 @@ pop_init_level (location_t loc, int implicit,
 	    /* Do not warn if this level of the initializer uses member
 	       designators; it is likely to be deliberate.  */
 	    && !constructor_designated
-	    /* Do not warn about initializing with { 0 } or with { }.  */
-	    && !constructor_zeroinit)
+	    /* Do not warn about initializing with { 0 } or with { }
+	       unless warn_zero_init.  */
+	    && (!constructor_zeroinit || warn_zero_init))
 	  {
 	    if (warning_at (input_location, OPT_Wmissing_field_initializers,
 			    "missing initializer for field %qD of %qT",
@@ -8769,7 +8770,7 @@ pop_init_level (location_t loc, int implicit,
 
   /* Warn when positional initializers are used for a structure with
      the designated_init attribute, but make an exception for { 0 }.  */
-  if (!constructor_zeroinit
+  if ((!constructor_zeroinit || warn_zero_init)
       && vec_safe_length (initializer_stack->positional_init_info))
     {
       struct positional_init_info info;
diff --git a/gcc/doc/invoke.texi b/gcc/doc/invoke.texi
index 06a04e3d7dd..f0893c250e7 100644
--- a/gcc/doc/invoke.texi
+++ b/gcc/doc/invoke.texi
@@ -369,7 +369,7 @@ Objective-C and Objective-C++ Dialects}.
 -Wno-switch-outside-range  -Wno-switch-unreachable  -Wsync-nand @gol
 -Wsystem-headers  -Wtautological-compare  -Wtrampolines  -Wtrigraphs @gol
 -Wtype-limits  -Wundef @gol
--Wuninitialized  -Wunknown-pragmas @gol
+-Wuninitialized  -Wno-universal-initializer  -Wunknown-pragmas @gol
 -Wunsuffixed-float-constants  -Wunused @gol
 -Wunused-but-set-parameter  -Wunused-but-set-variable @gol
 -Wunused-const-variable  -Wunused-const-variable=@var{n} @gol
@@ -6453,6 +6453,26 @@ to compute a value that itself is never used, because such
 computations may be deleted by data flow analysis before the warnings
 are printed.
 
+@item -Wuniversal-initializer
+@opindex Wuniversal-initializer
+@opindex Wno-universal-initializer
+Do not suppress warnings about the universal zero initializer,
+@code{@{ 0 @}}, where a warning would otherwise be produced.  For
+example, even with @option{-Wmissing-braces}, the following would not
+warn, because an exception is made for the universal initializer:
+
+@smallexample
+int a[1][1] = @{ 0 @};
+@end smallexample
+
+However, if @code{-Wuniversal-initializer} is used, that code would
+produce a warning (caused by @option{-Wmissing-braces}).
+
+This warning is not enabled by default, nor is it enabled by any other
+options.  If you don't want to suppress warnings about the universal
+zero initializer, you must specify @option{-Wuniversal-initializer}
+explicitly.
+
 @item -Wno-invalid-memory-model
 @opindex Winvalid-memory-model
 @opindex Wno-invalid-memory-model
diff --git a/gcc/testsuite/gcc.dg/Wuniversal-initializer.c b/gcc/testsuite/gcc.dg/Wuniversal-initializer.c
new file mode 100644
index 00000000000..803dfd10e1c
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/Wuniversal-initializer.c
@@ -0,0 +1,20 @@
+/* { dg-do compile } */
+/* { dg-options "-Wall -Wmissing-field-initializers -Wuniversal-initializer" } */
+
+struct Pok {
+  int x;
+  int y;
+};
+
+struct Nest {
+  struct Pok p;
+};
+
+struct Des {
+  int a;
+} __attribute__((designated_init));
+
+struct Pok p = { 0 }; /* { dg-warning "missing initializer for field" } */
+struct Nest n = { 0 }; /* { dg-warning "missing braces around initializer" } */
+struct Des d = { 0 }; /* { dg-warning "(positional|near initialization)" } */
+int a[1][1] = { 0 };  /* { dg-warning "missing braces around initializer" } */
-- 
2.26.2

Thanks,
Asher

-- 
The marvels of today's modern technology include the development of a
soda can, when discarded will last forever ... and a $7,000 car which
when properly cared for will rust out in two or three years.
                               --------
I prefer to send and receive mail encrypted. Please send me your
public key, and if you do not have my public key, please let me
know. Thanks.

GPG fingerprint: 38F3 975C D173 4037 B397  8095 D4C9 C4FC 5460 8E68

Attachment: signature.asc
Description: PGP signature

Reply via email to