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 bit-field widths and enumerators are integral


GCC has INTEGER_CSTs of pointer type.  When you need an integer
constant expression, you should check for being of integer type, not
just for being an INTEGER_CST.  I fixed a bug with the lack of such
tests for array designators a while back
<http://gcc.gnu.org/ml/gcc-patches/2004-07/msg02083.html> but didn't
notice there was such a problem for bit-field widths and enumerators
as well.

This patch fixes that problem.  Bootstrapped with no regressions on
x86_64-unknown-linux-gnu.  Applied to mainline and 4.0 branch.

Similar problems apply for many attribute handlers and will be dealt
with separately.  In addition, there is no need to strip NOPs when
requiring a constant, as there won't be any; the expression will have
been properly folded; this will also be dealt with in a separate
patch.  And, STRIP_MAIN_TYPE_NOPS is an entirely unused macro.

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

2005-03-20  Joseph S. Myers  <joseph@codesourcery.com>

	* c-decl.c (check_bitfield_type_and_width): Require bit-field
	width to have integer type.
	(build_enumerator): Require enumerator value to have integer type.

testsuite:
2005-03-20  Joseph S. Myers  <joseph@codesourcery.com>

	* gcc.dg/bitfld-14.c, gcc.dg/enum3.c: New tests.

diff -rupN GCC.orig/gcc/c-decl.c GCC/gcc/c-decl.c
--- GCC.orig/gcc/c-decl.c	2005-03-18 20:45:08.000000000 +0000
+++ GCC/gcc/c-decl.c	2005-03-20 00:11:49.000000000 +0000
@@ -3642,7 +3642,8 @@ check_bitfield_type_and_width (tree *typ
 
   /* Detect and ignore out of range field width and process valid
      field widths.  */
-  if (TREE_CODE (*width) != INTEGER_CST)
+  if (!INTEGRAL_TYPE_P (TREE_TYPE (*width))
+      || TREE_CODE (*width) != INTEGER_CST)
     {
       error ("bit-field %qs width not an integer constant", name);
       *width = integer_one_node;
@@ -5633,7 +5634,8 @@ build_enumerator (tree name, tree value)
 	 undeclared identifier) - just ignore the value expression.  */
       if (value == error_mark_node)
 	value = 0;
-      else if (TREE_CODE (value) != INTEGER_CST)
+      else if (!INTEGRAL_TYPE_P (TREE_TYPE (value))
+	       || TREE_CODE (value) != INTEGER_CST)
 	{
 	  error ("enumerator value for %qE is not an integer constant", name);
 	  value = 0;
diff -rupN GCC.orig/gcc/testsuite/gcc.dg/bitfld-14.c GCC/gcc/testsuite/gcc.dg/bitfld-14.c
--- GCC.orig/gcc/testsuite/gcc.dg/bitfld-14.c	1970-01-01 00:00:00.000000000 +0000
+++ GCC/gcc/testsuite/gcc.dg/bitfld-14.c	2005-03-20 00:08:10.000000000 +0000
@@ -0,0 +1,11 @@
+/* Test for non-integer bit-field widths.  */
+/* Origin: Joseph Myers <joseph@codesourcery.com> */
+/* { dg-do compile } */
+/* { dg-options "" } */
+
+enum e { E, F };
+struct s {
+  int a : (void *)4; /* { dg-error "error: bit-field 'a' width not an integer constant" } */
+  int b : (enum e)F;
+  int c : (_Bool)1;
+};
diff -rupN GCC.orig/gcc/testsuite/gcc.dg/enum3.c GCC/gcc/testsuite/gcc.dg/enum3.c
--- GCC.orig/gcc/testsuite/gcc.dg/enum3.c	1970-01-01 00:00:00.000000000 +0000
+++ GCC/gcc/testsuite/gcc.dg/enum3.c	2005-03-20 00:09:57.000000000 +0000
@@ -0,0 +1,11 @@
+/* Test for non-integer enum values.  */
+/* Origin: Joseph Myers <joseph@codesourcery.com> */
+/* { dg-do compile } */
+/* { dg-options "" } */
+
+enum e { E, F };
+enum e2 {
+  E1 = (void *)4, /* { dg-error "error: enumerator value for 'E1' is not an integer constant" } */
+  E2 = (enum e)F,
+  E3 = (_Bool)1
+};


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