diff mbox

[C] Don't pedwarn for C99/C11 enum bit-fields (PR c/57773)

Message ID 20140103165342.GB29237@redhat.com
State New
Headers show

Commit Message

Marek Polacek Jan. 3, 2014, 4:53 p.m. UTC
As Paul Eggert says in the PR, we shouldn't warn for enum bit-fields
in C99/C11 mode.  C11 6.7.2.1 (5) says "A bit-field shall have a type
that is a qualified or unqualified version of _Bool, signed int,
unsigned int, or some other implementation-defined type.", so ISTM
that enum bit-fields should be fine.  OTOH, I would warn in ISO C
mode.  It's true that no constraint is violated, but in C89 in 3.5.2.1
Semantics there's: "A bit-field may have type int, unsigned int, or
signed int." so it seems desirable to warn in this case.

Regtested/bootstrapped on x86_64-linux, ok for trunk?

2014-01-03  Marek Polacek  <polacek@redhat.com>

	PR c/57773
c/
	* c-decl.c (check_bitfield_type_and_width): Warn for enum bit-fields
	only in ISO C.
testsuite/
	* gcc.dg/pr57773.c: New test.


	Marek

Comments

Joseph Myers Jan. 3, 2014, 5:17 p.m. UTC | #1
On Fri, 3 Jan 2014, Marek Polacek wrote:

> As Paul Eggert says in the PR, we shouldn't warn for enum bit-fields
> in C99/C11 mode.  C11 6.7.2.1 (5) says "A bit-field shall have a type
> that is a qualified or unqualified version of _Bool, signed int,
> unsigned int, or some other implementation-defined type.", so ISTM
> that enum bit-fields should be fine.  OTOH, I would warn in ISO C
> mode.  It's true that no constraint is violated, but in C89 in 3.5.2.1
> Semantics there's: "A bit-field may have type int, unsigned int, or
> signed int." so it seems desirable to warn in this case.
> 
> Regtested/bootstrapped on x86_64-linux, ok for trunk?

Implementation-defined behavior is documented in implement-c.texi, so this 
patch is incomplete as it doesn't update that file where it says:

    No other types are permitted in strictly conforming mode.
    @c Would it be better to restrict the pedwarn for other types to C90
    @c mode and document the other types for C99/C11 mode?

(And this isn't just about enums, but other integer types as well, so the 
test should cover those.)
diff mbox

Patch

--- gcc/c/c-decl.c.mp	2014-01-03 13:50:37.041997222 +0100
+++ gcc/c/c-decl.c	2014-01-03 14:46:29.115816235 +0100
@@ -4840,7 +4840,8 @@  check_bitfield_type_and_width (tree *typ
   if (!in_system_header_at (input_location)
       && type_mv != integer_type_node
       && type_mv != unsigned_type_node
-      && type_mv != boolean_type_node)
+      && type_mv != boolean_type_node
+      && !flag_isoc99)
     pedwarn (input_location, OPT_Wpedantic,
 	     "type of bit-field %qs is a GCC extension", name);
 
--- gcc/testsuite/gcc.dg/pr57773.c.mp	2014-01-03 14:50:51.097902818 +0100
+++ gcc/testsuite/gcc.dg/pr57773.c	2014-01-03 14:49:05.727462306 +0100
@@ -0,0 +1,5 @@ 
+/* { dg-do compile } */
+/* { dg-options "-std=c99 -Wpedantic" } */
+
+enum e { zero };
+struct { enum e field: 2; } s;