Patch for PR 42439 (non-constant-expression bit-field widths)

Joseph S. Myers joseph@codesourcery.com
Wed Dec 30 22:11:00 GMT 2009


This patch extends the laxity regarding integer constant expressions
in places where C requires them (using a pedwarn-if-pedantic in
various syntactic contexts where something other than an integer
constant expression is invalid, if it folds to an integer constant) to
bit-field widths, as per PR 42439.  The code in that PR is doubtful,
but it seems reasonable to accept the same cases here in the same
circumstances as is done for static array sizes, case labels, enum
values and array designators.

Bootstrapped with no regressions on x86_64-unknown-linux-gnu.  Applied
to mainline.

2009-12-30  Joseph Myers  <joseph@codesourcery.com>

	PR c/42439
	* c-decl.c (check_bitfield_type_and_width): Only pedwarn if
	pedantic for bit-field width not an integer constant expression
	but folding to one.

testsuite:
2009-12-30  Joseph Myers  <joseph@codesourcery.com>

	PR c/42439
	* gcc.dg/bitfld-19.c, gcc.dg/bitfld-20.c, gcc.dg/bitfld-21.c: New
	tests.

Index: testsuite/gcc.dg/bitfld-21.c
===================================================================
--- testsuite/gcc.dg/bitfld-21.c	(revision 0)
+++ testsuite/gcc.dg/bitfld-21.c	(revision 0)
@@ -0,0 +1,11 @@
+/* Test for bit-field widths not integer constant expressions but
+   folding to integer constants: PR 42439.  */
+/* { dg-do compile } */
+/* { dg-options "-O2 -pedantic-errors" } */
+
+void
+f (void)
+{
+  const int m = 1;
+  ((void)(sizeof(struct { int i:!!m; }))); /* { dg-error "not an integer constant expression" } */
+}
Index: testsuite/gcc.dg/bitfld-19.c
===================================================================
--- testsuite/gcc.dg/bitfld-19.c	(revision 0)
+++ testsuite/gcc.dg/bitfld-19.c	(revision 0)
@@ -0,0 +1,11 @@
+/* Test for bit-field widths not integer constant expressions but
+   folding to integer constants: PR 42439.  */
+/* { dg-do compile } */
+/* { dg-options "-O2" } */
+
+void
+f (void)
+{
+  const int m = 1;
+  ((void)(sizeof(struct { int i:!!m; })));
+}
Index: testsuite/gcc.dg/bitfld-20.c
===================================================================
--- testsuite/gcc.dg/bitfld-20.c	(revision 0)
+++ testsuite/gcc.dg/bitfld-20.c	(revision 0)
@@ -0,0 +1,11 @@
+/* Test for bit-field widths not integer constant expressions but
+   folding to integer constants: PR 42439.  */
+/* { dg-do compile } */
+/* { dg-options "-O2 -pedantic" } */
+
+void
+f (void)
+{
+  const int m = 1;
+  ((void)(sizeof(struct { int i:!!m; }))); /* { dg-warning "not an integer constant expression" } */
+}
Index: c-decl.c
===================================================================
--- c-decl.c	(revision 155523)
+++ c-decl.c	(working copy)
@@ -4570,14 +4570,26 @@ check_bitfield_type_and_width (tree *typ
 
   /* Detect and ignore out of range field width and process valid
      field widths.  */
-  if (!INTEGRAL_TYPE_P (TREE_TYPE (*width))
-      || TREE_CODE (*width) != INTEGER_CST)
+  if (!INTEGRAL_TYPE_P (TREE_TYPE (*width)))
     {
       error ("bit-field %qs width not an integer constant", name);
       *width = integer_one_node;
     }
   else
     {
+      if (TREE_CODE (*width) != INTEGER_CST)
+	{
+	  *width = c_fully_fold (*width, false, NULL);
+	  if (TREE_CODE (*width) == INTEGER_CST)
+	    pedwarn (input_location, OPT_pedantic,
+		     "bit-field %qs width not an integer constant expression",
+		     name);
+	}
+      if (TREE_CODE (*width) != INTEGER_CST)
+	{
+	  error ("bit-field %qs width not an integer constant", name);
+	  *width = integer_one_node;
+	}
       constant_expression_warning (*width);
       if (tree_int_cst_sgn (*width) < 0)
 	{

-- 
Joseph S. Myers
joseph@codesourcery.com



More information about the Gcc-patches mailing list