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]
Other format: [Raw text]

Patch to check designators have integer type


This patch fixes a bug I noticed in the course of working on bug
11250: designated initializers were being allowed with array indices
not of integer type.  (The stripping of conversions present is still
dodgy, but I think that is better addressed as part of implementing
constant expressions properly.)

Bootstrapped with no regressions on i686-pc-linux-gnu.  Applied to
mainline.

-- 
Joseph S. Myers               http://www.srcf.ucam.org/~jsm28/gcc/
    jsm@polyomino.org.uk (personal mail)
    jsm28@gcc.gnu.org (Bugzilla assignments and CCs)

2004-07-22  Joseph S. Myers  <jsm@polyomino.org.uk>

	* c-typeck.c (set_init_index): Require designator to be of integer
	type.

testsuite:
2004-07-22  Joseph S. Myers  <jsm@polyomino.org.uk>

	* gcc.dg/c99-init-3.c, gcc.dg/gnu99-init-2.c: New tests.
	* gcc.dg/noncompile/921102-1.c: Update expected error message.

diff -rupN GCC.orig/gcc/c-typeck.c GCC/gcc/c-typeck.c
--- GCC.orig/gcc/c-typeck.c	2004-07-20 20:58:41.000000000 +0000
+++ GCC/gcc/c-typeck.c	2004-07-20 21:39:02.000000000 +0000
@@ -4956,6 +4956,13 @@ set_init_index (tree first, tree last)
 
   designator_errorneous = 1;
 
+  if (!INTEGRAL_TYPE_P (TREE_TYPE (first))
+      || (last && !INTEGRAL_TYPE_P (TREE_TYPE (last))))
+    {
+      error_init ("array index in initializer not of integer type");
+      return;
+    }
+
   while ((TREE_CODE (first) == NOP_EXPR
 	  || TREE_CODE (first) == CONVERT_EXPR
 	  || TREE_CODE (first) == NON_LVALUE_EXPR)
diff -rupN GCC.orig/gcc/testsuite/gcc.dg/c99-init-3.c GCC/gcc/testsuite/gcc.dg/c99-init-3.c
--- GCC.orig/gcc/testsuite/gcc.dg/c99-init-3.c	1970-01-01 00:00:00.000000000 +0000
+++ GCC/gcc/testsuite/gcc.dg/c99-init-3.c	2004-07-21 12:15:54.000000000 +0000
@@ -0,0 +1,8 @@
+/* Test for designated initializers: array designators must be of
+   integer type.  */
+/* Origin: Joseph Myers <jsm@polyomino.org.uk> */
+/* { dg-do compile } */
+/* { dg-options "-std=iso9899:1999 -pedantic-errors" } */
+
+int a[] = { [(void *)0] = 1 }; /* { dg-bogus "warning" "warning in place of error" } */
+/* { dg-error "integer type|near init" "pointer designator"  { target *-*-* } 7 } */
diff -rupN GCC.orig/gcc/testsuite/gcc.dg/gnu99-init-2.c GCC/gcc/testsuite/gcc.dg/gnu99-init-2.c
--- GCC.orig/gcc/testsuite/gcc.dg/gnu99-init-2.c	1970-01-01 00:00:00.000000000 +0000
+++ GCC/gcc/testsuite/gcc.dg/gnu99-init-2.c	2004-07-21 12:16:23.000000000 +0000
@@ -0,0 +1,11 @@
+/* Test for designated initializers: array designators must be of
+   integer type.  Test for index ranges (GNU extension).  */
+/* Origin: Joseph Myers <jsm@polyomino.org.uk> */
+/* { dg-do compile } */
+/* { dg-options "-std=gnu99" } */
+
+int a[] = { [(void *)0 ... 0] = 1 }; /* { dg-bogus "warning" "warning in place of error" } */
+/* { dg-error "integer type|near init" "pointer designator"  { target *-*-* } 7 } */
+
+int b[] = { [0 ... (void *)0] = 1 }; /* { dg-bogus "warning" "warning in place of error" } */
+/* { dg-error "integer type|near init" "pointer designator"  { target *-*-* } 10 } */
diff -rupN GCC.orig/gcc/testsuite/gcc.dg/noncompile/921102-1.c GCC/gcc/testsuite/gcc.dg/noncompile/921102-1.c
--- GCC.orig/gcc/testsuite/gcc.dg/noncompile/921102-1.c	2000-06-29 19:55:26.000000000 +0000
+++ GCC/gcc/testsuite/gcc.dg/noncompile/921102-1.c	2004-07-21 16:26:59.000000000 +0000
@@ -1 +1 @@
-int x[]={[0.3 ... 4.6]9}; /* { dg-error "nonconstant array|near init" } */
+int x[]={[0.3 ... 4.6]9}; /* { dg-error "not of integer type|near init" } */


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