This is the mail archive of the gcc-patches@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Making some warnings less obnoxious


This patch causes us not to emit the "missing initializer" or
"traditional C does not accept initialization of unions" warnings when
the initializer in question uses C99 member designators.  The logic
is: if you used member designators it is more likely that omissions
are intentional, and you definitely aren't worrying about your code
compiling with a K+R compiler.  (And besides which, the member
designator itself produced a warning - which can be shut up with
__extension__, if you know what you're doing.)

In conjunction with the patch I just posted for loadmsgcat.c, stage3
of a --with-included-gettext bootstrap (i686-linux as usual) now gets
no warnings from the intl subdirectory.


zw

	* c-typeck.c (constructor_designated): New local flag.
	(struct constructor_stack): Add "designated" field to match.
	(start_init): Clear it.  
	(really_start_incremental_init, push_init_level): Push and
	clear it.
	(pop_init_level): Pop it.
	(set_designator): Set it.

	(pop_init_level): Suppress "missing initializer" warnings if
	constructor_designated is true.
	(process_init_element): Suppress warning about union
	initialization under traditional C, if constructor_designated
	is true.

	* gcc.dg/20011021-1.c: New test case.

===================================================================
Index: c-typeck.c
--- c-typeck.c	2001/10/11 03:15:23	1.141
+++ c-typeck.c	2001/10/23 04:24:51
@@ -4935,6 +4935,9 @@ static const char *constructor_asmspec;
 /* Nonzero if this is an initializer for a top-level decl.  */
 static int constructor_top_level;
 
+/* Nonzero if there were any member designators in this initializer.  */
+static int constructor_designated;
+
 /* Nesting depth of designator list.  */
 static int designator_depth;
 
@@ -4972,6 +4975,7 @@ struct constructor_stack
   char erroneous;
   char outer;
   char incremental;
+  char designated;
 };
 
 struct constructor_stack *constructor_stack;
@@ -5048,6 +5052,7 @@ start_init (decl, asmspec_tree, top_leve
   constructor_decl = decl;
   constructor_asmspec = asmspec;
   constructor_subconstants_deferred = 0;
+  constructor_designated = 0;
   constructor_top_level = top_level;
 
   if (decl != 0)
@@ -5157,6 +5162,7 @@ really_start_incremental_init (type)
   p->range_stack = 0;
   p->outer = 0;
   p->incremental = constructor_incremental;
+  p->designated = constructor_designated;
   p->next = 0;
   constructor_stack = p;
 
@@ -5167,6 +5173,7 @@ really_start_incremental_init (type)
   constructor_pending_elts = 0;
   constructor_type = type;
   constructor_incremental = 1;
+  constructor_designated = 0;
   designator_depth = 0;
   designator_errorneous = 0;
 
@@ -5268,6 +5275,7 @@ push_init_level (implicit)
   p->implicit = implicit;
   p->outer = 0;
   p->incremental = constructor_incremental;
+  p->designated = constructor_designated;
   p->next = constructor_stack;
   p->range_stack = 0;
   constructor_stack = p;
@@ -5277,6 +5285,7 @@ push_init_level (implicit)
   constructor_depth = SPELLING_DEPTH ();
   constructor_elements = 0;
   constructor_incremental = 1;
+  constructor_designated = 0;
   constructor_pending_elts = 0;
   if (!implicit)
     {
@@ -5457,7 +5466,9 @@ pop_init_level (implicit)
 		   || integer_zerop (DECL_SIZE (constructor_unfilled_fields))))
 	  constructor_unfilled_fields = TREE_CHAIN (constructor_unfilled_fields);
 
-	if (constructor_unfilled_fields)
+	/* Do not warn if this level of the initializer uses member
+	   designators; it is likely to be deliberate.  */
+	if (constructor_unfilled_fields && !constructor_designated)
 	  {
 	    push_member_name (constructor_unfilled_fields);
 	    warning_init ("missing initializer");
@@ -5523,6 +5534,7 @@ pop_init_level (implicit)
   constructor_simple = p->simple;
   constructor_erroneous = p->erroneous;
   constructor_incremental = p->incremental;
+  constructor_designated = p->designated;
   constructor_pending_elts = p->pending_elts;
   constructor_depth = p->depth;
   if (!p->implicit)
@@ -5569,6 +5581,7 @@ set_designator (array)
 	 braces.  */
       while (constructor_stack->implicit)
 	process_init_element (pop_init_level (1));
+      constructor_designated = 1;
       return 0;
     }
 
@@ -5604,6 +5617,7 @@ set_designator (array)
       return 1;
     }
 
+  constructor_designated = 1;
   push_init_level (2);
   return 0;
 }
@@ -6581,8 +6595,12 @@ process_init_element (value)
 	     under the assumption that the zero initializer in user
 	     code appears conditioned on e.g. __STDC__ to avoid
 	     "missing initializer" warnings and relies on default
-	     initialization to zero in the traditional C case.  */
-	  if (warn_traditional && !in_system_header
+	     initialization to zero in the traditional C case.
+	     We also skip the warning if the initializer is designated,
+	     again on the assumption that this must be conditional on
+	     __STDC__ anyway (and we've already complained about the
+	     member-designator already).  */
+	  if (warn_traditional && !in_system_header && !constructor_designated
 	      && !(value && (integer_zerop (value) || real_zerop (value))))
 	    warning ("traditional C rejects initialization of unions");
 
===================================================================
Index: testsuite/gcc.dg/20011021-1.c
--- testsuite/gcc.dg/20011021-1.c	Tue May  5 13:32:27 1998
+++ testsuite/gcc.dg/20011021-1.c	Mon Oct 22 21:24:52 2001
@@ -0,0 +1,43 @@
+/* Test for various initializer warnings being suppressed by use of
+   designated initializers.  */
+
+/* { dg-do compile } */
+/* { dg-options "-std=c99 -W -Wall -Wtraditional" } */
+
+
+struct t
+{
+  int a;
+  int b;
+  int c;
+};
+
+union u
+{
+  int n;
+  float i;
+};
+
+struct multilevel
+{
+   int x;
+   struct t t;
+   union u u;
+   union u v;
+   char *f;
+};
+
+struct t T0 = { 1 };		/* { dg-warning "(missing|near) init" } */
+struct t T1 = { .a = 1 };	/* { dg-bogus "(missing|near) init" } */
+
+union u U0 = { 1 };		/* { dg-warning "initialization of union" } */
+union u U1 = { .i = 1 };	/* { dg-bogus "initialization of union" } */
+
+struct multilevel M =
+{
+  12,
+  { .b = 3 },			/* { dg-bogus "(missing|near) init" } */
+  { 4 },			/* { dg-warning "initialization of union" } */
+  { .n = 9 },			/* { dg-bogus "initialization of union" } */
+  /* "string here" */
+};				/* { dg-warning "(missing|near) init" } */


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]