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]

patch to supress trailing missing initializer warnings


Hi,
egcs currently warns when an aggregate initializer does not initialize
all the members. I found this annoying (and did report it as a bug
http://www.cygnus.com/ml/egcs-bugs/1998-Jul/0031.html). There was
previously some discussion on the patches list about the patch which
introduced the check
(http://www.cygnus.com/ml/egcs-patches/1998-Jun/0421.html). The
suggestion was made to inhibit the warning, if the last initialized
member was zero, although some reservations were expressed about that.

This patch only warns when the last supplied initializer is not a
constant expression of value zero (integral or null pointer), or
-Wuninitialized is also given. Thus you get the current egcs behaviour
by supplying -W -Wuninitialized, rather than just -W. Even though
implicit initialization to zero is different to uninitialized, using
-Wuninitialized to trigger the picky behaviour seems a reasonable thing
to do in the abscense of adding a completely separate warning flag
(opinions may vary ...).

Files altered gcc/cp/ChangeLog, gcc/cp/typeck2.c, gcc/invoke.texi

Here is a test source file you can use to show the behaviours,
--begin missinit.ii
struct S1 { int a; int b; int c;};
struct S2 { void *a; int b; int c;};
struct S3 { S1 a; int b; int c;};
static S1 s1a = {0};
static S1 s1b = {1};
static S2 s2a = {0};
static S2 s2b = {__null};
static S2 s2c = {&s2a};
static S3 s3a = {{0}};
static S3 s3b = {{0}, 0};
--end missinit.ii

Again I assume this is a small enough change to not require a copyright
assignment.

nathan
-- 
Dr Nathan Sidwell :: Computer Science Department :: Bristol University
      You can up the bandwidth, but you can't up the speed of light      
nathan@acm.org  http://www.cs.bris.ac.uk/~nathan/  nathan@cs.bris.ac.uk
Index: gcc/cp/ChangeLog
===================================================================
RCS file: /egcs/carton/cvsfiles/egcs/gcc/cp/ChangeLog,v
retrieving revision 1.535.2.15
diff -b -c -3 -p -r1.535.2.15 ChangeLog
*** ChangeLog	1998/07/29 14:56:21	1.535.2.15
--- ChangeLog	1998/08/04 10:09:04
***************
*** 1,3 ****
--- 1,9 ----
+ Tue Aug  4 11:05:48 BST 1998  Nathan Sidwell  <nathan@acm.org>
+ 
+ 	* typeck2.c (process_init_constructor): Only warn about missing
+ 	initializers if the final one is not zero or -Wuninitialized was
+ 	given as well.
+ 
  1998-07-29  Jason Merrill  <jason@yorick.cygnus.com>
  
  	* decl.c (push_overloaded_decl): Use current_namespace instead of
Index: gcc/cp/typeck2.c
===================================================================
RCS file: /egcs/carton/cvsfiles/egcs/gcc/cp/typeck2.c,v
retrieving revision 1.26.2.1
diff -b -c -3 -p -r1.26.2.1 typeck2.c
*** typeck2.c	1998/07/28 14:00:25	1.26.2.1
--- typeck2.c	1998/08/04 10:09:05
*************** process_init_constructor (type, init, el
*** 1018,1023 ****
--- 1018,1024 ----
    if (TREE_CODE (type) == RECORD_TYPE)
      {
        register tree field;
+       int prev_was_zero = 0;
  
        if (tail)
  	{
*************** process_init_constructor (type, init, el
*** 1075,1086 ****
--- 1076,1092 ----
  	      tail = TREE_CHAIN (tail);
  	    }
  
+ 	  prev_was_zero = 0;
  	  if (next1 == error_mark_node)
  	    erroneous = 1;
  	  else if (!TREE_CONSTANT (next1))
  	    allconstant = 0;
  	  else if (! initializer_constant_valid_p (next1, TREE_TYPE (next1)))
  	    allsimple = 0;
+ 	  else if (TREE_CODE (next1) == INTEGER_CST
+ 		    && ! TREE_INT_CST_LOW (next1)
+ 		    && ! TREE_INT_CST_HIGH (next1))
+ 	    prev_was_zero = 1;
  	  members = expr_tree_cons (field, next1, members);
  	}
        for (; field; field = TREE_CHAIN (field))
*************** process_init_constructor (type, init, el
*** 1112,1118 ****
  		   IDENTIFIER_POINTER (DECL_NAME (field)));
  	  /* Warn when some struct elements are implicitly initialized
  	      to zero.  */
! 	  else if (extra_warnings)
  	    warning ("missing initializer for member `%s'",
  		     IDENTIFIER_POINTER (DECL_NAME (field)));
  	}
--- 1118,1125 ----
  		   IDENTIFIER_POINTER (DECL_NAME (field)));
  	  /* Warn when some struct elements are implicitly initialized
  	      to zero.  */
! 	  else if (extra_warnings
! 		  && (!prev_was_zero || warn_uninitialized == 1))
  	    warning ("missing initializer for member `%s'",
  		     IDENTIFIER_POINTER (DECL_NAME (field)));
  	}
Index: gcc/invoke.texi
===================================================================
RCS file: /egcs/carton/cvsfiles/egcs/gcc/invoke.texi,v
retrieving revision 1.56
diff -b -c -3 -p -r1.56 invoke.texi
*** invoke.texi	1998/07/13 12:39:33	1.56
--- invoke.texi	1998/08/04 10:09:07
*************** struct t x = @{ 1, 2, 3 @};
*** 1547,1553 ****
  @end smallexample
  
  @item
! An aggregate has an initializer which does not initialize all members.
  For example, the following code would cause such a warning, because
  @code{x.h} would be implicitly initialized to zero:
  
--- 1547,1555 ----
  @end smallexample
  
  @item
! An aggregate has an initializer which does not initialize all members
! and the final initializer member is not zero. If -Wuninitialized is
! also given, the warning is given even if the final initializer is zero.
  For example, the following code would cause such a warning, because
  @code{x.h} would be implicitly initialized to zero:
  

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