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 warn about static initializers for zero length arrays


The following patch makes gcc print warnings for initializers of
zero length arrays as excess initializers.  This moves gcc toward
conforming with the C99 standard on flexible arrays (6.7.2.1).

	* extend.texi (Zero Length): State that static initializers for
	zero-length arrays are not allowed.

	* c-typeck.c (process_init_element): Flag initializers for zero
	length arrays.

	* tree.c (integer_all_onesp): Ignore TREE_INT_CST_HIGH if prec is
	exactly HOST_BITS_PERWIDE_INT.

I have also attached a file, zerolength.c, which demonstrates gcc
warning about the initializer.  Assuming that the patch is installed
the following messages should be generated:

bash$ native/gcc/xgcc -Bnative/gcc/ -c zerolength.c
zerolength.c:20: warning: excess elements in array initializer
zerolength.c:20: warning: (near initialization for `playbook.Play')


-Will Cohen
Index: tree.c
===================================================================
RCS file: /cvs/gcc/egcs/gcc/tree.c,v
retrieving revision 1.159
diff -c -2 -p -r1.159 tree.c
*** tree.c	2000/09/17 07:38:12	1.159
--- tree.c	2000/09/18 14:57:14
*************** integer_all_onesp (expr)
*** 1683,1686 ****
--- 1683,1692 ----
       actual bits, not the (arbitrary) range of the type.  */
    prec = GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (expr)));
+ 
+   /* Shifting by the host word size is undefined according to the ANSI
+      standard, so we must handle this as a special case.  */
+   if (prec == HOST_BITS_PER_WIDE_INT)
+     return TREE_INT_CST_LOW (expr) == ~ (unsigned HOST_WIDE_INT) 0;
+   
    if (prec >= HOST_BITS_PER_WIDE_INT)
      {
Index: c-typeck.c
===================================================================
RCS file: /cvs/gcc/egcs/gcc/c-typeck.c,v
retrieving revision 1.90
diff -c -2 -p -r1.90 c-typeck.c
*** c-typeck.c	2000/09/18 05:02:11	1.90
--- c-typeck.c	2000/09/18 14:57:16
*************** process_init_element (value)
*** 6461,6465 ****
  
  	  if (constructor_max_index != 0
! 	      && tree_int_cst_lt (constructor_max_index, constructor_index))
  	    {
  	      pedwarn_init ("excess elements in array initializer");
--- 6461,6466 ----
  
  	  if (constructor_max_index != 0
! 	      && (tree_int_cst_lt (constructor_max_index, constructor_index)
! 		  || integer_all_onesp (constructor_max_index)))
  	    {
  	      pedwarn_init ("excess elements in array initializer");
Index: extend.texi
===================================================================
RCS file: /cvs/gcc/egcs/gcc/extend.texi,v
retrieving revision 1.63
diff -c -2 -p -r1.63 extend.texi
*** extend.texi	2000/09/06 21:24:56	1.63
--- extend.texi	2000/09/18 14:57:18
*************** In standard C, you would have to give @c
*** 890,893 ****
--- 890,897 ----
  means either you waste space or complicate the argument to @code{malloc}.
  
+ Static initialization of the zero-length array is not allowed.  A
+ warning will be generated for each initializer attempting to initialize
+ the zero-length array.
+ 
  @node Variable Length
  @section Arrays of Variable Length
/* zerolength.c */

struct PDATA
{
    unsigned int  Dummy:32;
    const char*   PName;
};

typedef struct PDATA    P_DATA;

struct PLAYBOOK {
        const char * BookName;
        P_DATA       Play[0];
};

struct PLAYBOOK playbook  =
{
  "BookName",
  {
    { 1, "PName0" },
  }
};

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