This is the mail archive of the gcc@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]

Re: host_integerp vs [] decls



> However, please follow the directions of the comment and remove the
> special case from pop_level_1.  Also update the documentation such
> that "deprecated" is now "illegal".

It took a while to get the right warnings for flexible vs zero-length
arrays, and I had to adjust some of the test cases to detect the right
warnings, but here it all is.  No regressions on x86 linux.

2001-09-19  DJ Delorie  <dj@redhat.com>

	* c-typeck.c (really_start_incremental_init): Discriminate
	between zero-length arrays and flexible arrays.
	(push_init_level): Detect zero-length arrays and handle them
	like fixed-sized arrays.
	* expr.c (store_constructor): Handle zero-length arrays and
	flexible arrays correctly.
	* doc/extend.texi: Update zero-length array notes.

2001-09-19  DJ Delorie  <dj@redhat.com>

	* gcc.dg/20000926-1.c: Update expected warning messages.
	* gcc.dg/array-2.c: Likewise, and test for warnings too.
	* gcc.dg/array-4.c: Likewise, and don't verify the zero-length
	array.

Index: c-typeck.c
===================================================================
RCS file: /cvs/gcc/gcc/gcc/c-typeck.c,v
retrieving revision 1.137
diff -p -3 -r1.137 c-typeck.c
*** c-typeck.c	2001/09/16 00:48:52	1.137
--- c-typeck.c	2001/09/20 00:58:55
*************** really_start_incremental_init (type)
*** 5190,5196 ****
  	    = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type));
  
  	  /* Detect non-empty initializations of zero-length arrays.  */
! 	  if (constructor_max_index == NULL_TREE)
  	    constructor_max_index = build_int_2 (-1, -1);
  
  	  constructor_index
--- 5190,5197 ----
  	    = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type));
  
  	  /* Detect non-empty initializations of zero-length arrays.  */
! 	  if (constructor_max_index == NULL_TREE
! 	      && TYPE_SIZE (constructor_type))
  	    constructor_max_index = build_int_2 (-1, -1);
  
  	  constructor_index
*************** push_init_level (implicit)
*** 5352,5365 ****
  	{
  	  constructor_max_index
  	    = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type));
  	  constructor_index
  	    = convert (bitsizetype, 
  		       TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
- 
- 	  /* ??? For GCC 3.1, remove special case initialization of
- 	     zero-length array members from pop_init_level and set
- 	     constructor_max_index such that we get the normal
- 	     "excess elements" warning.  */
  	}
        else
  	constructor_index = bitsize_zero_node;
--- 5353,5367 ----
  	{
  	  constructor_max_index
  	    = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type));
+ 
+ 	  /* Detect non-empty initializations of zero-length arrays.  */
+ 	  if (constructor_max_index == NULL_TREE
+ 	      && TYPE_SIZE (constructor_type))
+ 	    constructor_max_index = build_int_2 (-1, -1);
+ 
  	  constructor_index
  	    = convert (bitsizetype, 
  		       TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
  	}
        else
  	constructor_index = bitsize_zero_node;
*************** pop_init_level (implicit)
*** 5438,5456 ****
  	    constructor_type = NULL_TREE;
  	}
        else
! 	{
! 	  warning_init ("deprecated initialization of zero-length array");
! 
! 	  /* We must be initializing the last member of a top-level struct.  */
! 	  if (TREE_CHAIN (constructor_fields) != NULL_TREE)
! 	    {
! 	      error_init ("initialization of zero-length array before end of structure");
! 	      /* Discard the initializer so that we do not abort later.  */
! 	      constructor_type = NULL_TREE;
! 	    }
! 	  else if (constructor_depth > 2)
! 	    error_init ("initialization of zero-length array inside a nested context");
! 	}
      }
  
    /* Warn when some struct elements are implicitly initialized to zero.  */
--- 5440,5448 ----
  	    constructor_type = NULL_TREE;
  	}
        else
! 	/* Zero-length arrays are no longer special, so we should no longer
! 	   get here.  */
! 	abort();
      }
  
    /* Warn when some struct elements are implicitly initialized to zero.  */
Index: expr.c
===================================================================
RCS file: /cvs/gcc/gcc/gcc/expr.c,v
retrieving revision 1.351
diff -p -3 -r1.351 expr.c
*** expr.c	2001/09/06 08:59:36	1.351
--- expr.c	2001/09/20 00:59:23
*************** store_constructor (exp, target, align, c
*** 4710,4716 ****
        int need_to_clear;
        tree domain = TYPE_DOMAIN (type);
        tree elttype = TREE_TYPE (type);
!       int const_bounds_p = (host_integerp (TYPE_MIN_VALUE (domain), 0)
  			    && host_integerp (TYPE_MAX_VALUE (domain), 0));
        HOST_WIDE_INT minelt = 0;
        HOST_WIDE_INT maxelt = 0;
--- 4710,4718 ----
        int need_to_clear;
        tree domain = TYPE_DOMAIN (type);
        tree elttype = TREE_TYPE (type);
!       int const_bounds_p = (TYPE_MIN_VALUE (domain)
! 			    && TYPE_MAX_VALUE (domain)
! 			    && host_integerp (TYPE_MIN_VALUE (domain), 0)
  			    && host_integerp (TYPE_MAX_VALUE (domain), 0));
        HOST_WIDE_INT minelt = 0;
        HOST_WIDE_INT maxelt = 0;
Index: doc/extend.texi
===================================================================
RCS file: /cvs/gcc/gcc/gcc/doc/extend.texi,v
retrieving revision 1.23
diff -p -3 -r1.23 extend.texi
*** extend.texi	2001/08/18 21:02:43	1.23
--- extend.texi	2001/09/20 00:59:42
*************** of zero-length arrays, @code{sizeof} eva
*** 1303,1319 ****
  
  @item
  Flexible array members may only appear as the last member of a
! @code{struct} that is otherwise non-empty.  GCC currently allows
! zero-length arrays anywhere.  You may encounter problems, however,
! defining structures containing only a zero-length array.  Such usage
! is deprecated, and we recommend using zero-length arrays only in
! places in which flexible array members would be allowed.
  @end itemize
  
  GCC versions before 3.0 allowed zero-length arrays to be statically
! initialized.  In addition to those cases that were useful, it also
! allowed initializations in situations that would corrupt later data.
! Non-empty initialization of zero-length arrays is now deprecated.
  
  Instead GCC allows static initialization of flexible array members.
  This is equivalent to defining a new structure containing the original
--- 1303,1319 ----
  
  @item
  Flexible array members may only appear as the last member of a
! @code{struct} that is otherwise non-empty.
  @end itemize
  
  GCC versions before 3.0 allowed zero-length arrays to be statically
! initialized, as if they were flexible arrays.  In addition to those
! cases that were useful, it also allowed initializations in situations
! that would corrupt later data.  Non-empty initialization of zero-length
! arrays is now treated like any case where there are more initializer
! elements than the array holds, in that a suitable warning about "excess
! elements in array" is given, and the excess elements (all of them, in
! this case) are ignored.
  
  Instead GCC allows static initialization of flexible array members.
  This is equivalent to defining a new structure containing the original
Index: testsuite/gcc.dg/20000926-1.c
===================================================================
RCS file: /cvs/gcc/gcc/gcc/testsuite/gcc.dg/20000926-1.c,v
retrieving revision 1.4
diff -p -3 -r1.4 20000926-1.c
*** 20000926-1.c	2001/01/10 23:57:55	1.4
--- 20000926-1.c	2001/09/20 00:59:54
*************** struct PLAYBOOK playbook  =
*** 22,27 ****
  {
    "BookName",
    {
!     { 1, "PName0" },
!   } /* { dg-warning "(deprecated initialization)|(near initialization)" "" } */
  };
--- 22,27 ----
  {
    "BookName",
    {
!     { 1, "PName0" }, /* { dg-warning "(excess elements)|(near initialization)" "" } */
!   }
  };
Index: testsuite/gcc.dg/array-2.c
===================================================================
RCS file: /cvs/gcc/gcc/gcc/testsuite/gcc.dg/array-2.c,v
retrieving revision 1.2
diff -p -3 -r1.2 array-2.c
*** array-2.c	2001/01/05 05:56:00	1.2
--- array-2.c	2001/09/20 00:59:54
***************
*** 1,5 ****
  /* { dg-do compile } */
! /* { dg-options "-w" } */
  
  /* Verify that we can't do things to get ourselves in trouble
     with GCC's initialized flexible array member extension.  */
--- 1,5 ----
  /* { dg-do compile } */
! /* { dg-options "" } */
  
  /* Verify that we can't do things to get ourselves in trouble
     with GCC's initialized flexible array member extension.  */
*************** struct g g1 = { { 0, { } } };
*** 10,13 ****
  struct g g2 = { { 0, { 1 } } }; /* { dg-error "(nested context)|(near initialization)" "nested" } */
  
  struct h { int x[0]; int y; };
! struct h h1 = { { 0 }, 1 }; /* { dg-error "(before end)|(near initialization)" "before end" } */
--- 10,13 ----
  struct g g2 = { { 0, { 1 } } }; /* { dg-error "(nested context)|(near initialization)" "nested" } */
  
  struct h { int x[0]; int y; };
! struct h h1 = { { 0 }, 1 }; /* { dg-error "(excess elements)|(near initialization)" "before end" } */
Index: testsuite/gcc.dg/array-4.c
===================================================================
RCS file: /cvs/gcc/gcc/gcc/testsuite/gcc.dg/array-4.c,v
retrieving revision 1.2
diff -p -3 -r1.2 array-4.c
*** array-4.c	2001/01/05 05:56:00	1.2
--- array-4.c	2001/09/20 00:59:54
*************** struct g { int w; int x[0]; };
*** 12,18 ****
  
  static struct f f = { 4, { 0, 1, 2, 3 } };
  static int junk1[] = { -1, -1, -1, -1 };
! static struct g g = { 4, { 0, 1, 2, 3 } }; /* { dg-warning "(deprecated initialization)|(near initialization)" "" } */
  static int junk2[] = { -1, -1, -1, -1 };
  
  int main()
--- 12,18 ----
  
  static struct f f = { 4, { 0, 1, 2, 3 } };
  static int junk1[] = { -1, -1, -1, -1 };
! static struct g g = { 4, { 0, 1, 2, 3 } }; /* { dg-warning "(excess elements)|(near initialization)" "" } */
  static int junk2[] = { -1, -1, -1, -1 };
  
  int main()
*************** int main()
*** 21,28 ****
--- 21,30 ----
    for (i = 0; i < f.w; ++i)
      if (f.x[i] != i)
        abort ();
+   /* zero length arrays are no longer initialized to non-zero length.
    for (i = 0; i < g.w; ++i)
      if (g.x[i] != i)
        abort ();
+   */
    exit(0);
  }


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