[PATCH] Add -Wuniversal-initializer to not suppress warnings about { 0 }.

Asher Gordon AsDaGo@posteo.net
Sun Jun 7 17:50:12 GMT 2020


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.
---
Asher Gordon <AsDaGo@posteo.net> writes:

> Also note that this patch does not implement a
> [-Wuniversal-initializer] flag, but that shouldn't be too hard to
> implement.

This patch now implements that.

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

diff --git a/gcc/ChangeLog b/gcc/ChangeLog
index d569416c02f..15d520d3656 100644
--- a/gcc/ChangeLog
+++ b/gcc/ChangeLog
@@ -1,3 +1,7 @@
+2020-06-07  Asher Gordon  <AsDaGo@posteo.net>
+
+	* doc/invoke.texi: Document -Wuniversal-initializer.
+
 2020-06-06  Jan Hubicka  <hubicka@ucw.cz>
 
 	PR lto/95548
diff --git a/gcc/c-family/ChangeLog b/gcc/c-family/ChangeLog
index 8fac84d3b02..6824d7ea130 100644
--- a/gcc/c-family/ChangeLog
+++ b/gcc/c-family/ChangeLog
@@ -1,3 +1,7 @@
+2020-06-07  Asher Gordon  <AsDaGo@posteo.net>
+
+	* c.opt: Add -Wuniversal-initializer
+
 2020-06-05  Jason Merrill  <jason@redhat.com>
 
 	* c-pretty-print.c (pp_c_additive_expression): Handle negative
diff --git a/gcc/c-family/c.opt b/gcc/c-family/c.opt
index 89a58282b3f..15bbb8e69f7 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(1) 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/ChangeLog b/gcc/c/ChangeLog
index 50fb1e87ad6..3ade67830e8 100644
--- a/gcc/c/ChangeLog
+++ b/gcc/c/ChangeLog
@@ -1,3 +1,8 @@
+2020-06-07  Asher Gordon  <AsDaGo@posteo.net>
+
+	* c-typeck.c (pop_init_level): Don't suppress warnings about { 0 }
+	if warn_zero_init.
+
 2020-06-05  Mark Wielaard  <mark@klomp.org>
 
 	* c-decl.c (implicit_decl_warning): When warned and olddecl is
diff --git a/gcc/c/c-typeck.c b/gcc/c/c-typeck.c
index 2d04f70f0cf..e1071472301 100644
--- a/gcc/c/c-typeck.c
+++ b/gcc/c/c-typeck.c
@@ -8724,7 +8724,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,
@@ -8748,8 +8748,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",
@@ -8766,7 +8767,7 @@ pop_init_level (location_t loc, int implicit,
     {
       struct location_list *loc = initializer_stack->positional_init_locs;
 
-      if (!constructor_zeroinit)
+      if (!constructor_zeroinit || warn_zero_init)
 	warning_init (loc->loc,
 		      OPT_Wdesignated_init,
 		      "positional initialization of field in %<struct%> "
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/ChangeLog b/gcc/testsuite/ChangeLog
index 825f60b9d5b..65bd6fd6028 100644
--- a/gcc/testsuite/ChangeLog
+++ b/gcc/testsuite/ChangeLog
@@ -1,3 +1,7 @@
+2020-06-07  Asher Gordon  <AsDaGo@posteo.net>
+
+	* gcc.dg/Wuniversal-initializer.c: New test.
+
 2020-06-06  Jan Hubicka  <hubicka@ucw.cz>
 
 	* g++.dg/torture/pr95548.C: New test.
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



More information about the Gcc-patches mailing list