Hi,
I think we can resolve this very old issue too: we don't warn at all for
bitfields of size exceeding the type when it's bool or enum. I have no
idea why historically we decided to not do that, but certainly all the
modern compilers I have at hand do warn, by default, thus it seems safe
to at least pedwarn, or even simply warn, if you like.
Patch booted and tested x86_64-linux.
Thanks,
Paolo.
//////////////////////////
/cp
2013-05-24 Paolo Carlini <paolo.carl...@oracle.com>
PR c++/19618
* class.c (check_bitfield_decl): Pedwarn for bool and enum bitfields
with width exceeding the type.
/testsuite
2013-05-24 Paolo Carlini <paolo.carl...@oracle.com>
PR c++/19618
* g++.dg/expr/bitfield12.C: New.
* g++.dg/expr/bitfield1.C: Adjust.
* g++.dg/expr/bitfield3.C: Likewise.
* g++.dg/expr/bitfield4.C: Likewise.
* g++.dg/expr/bitfield5.C: Likewise.
* g++.dg/expr/bitfield6.C: Likewise.
* g++.old-deja/g++.jason/bool2.C: Likewise.
Index: cp/class.c
===================================================================
--- cp/class.c (revision 199287)
+++ cp/class.c (working copy)
@@ -3140,10 +3140,15 @@ check_bitfield_decl (tree field)
error ("zero width for bit-field %q+D", field);
w = error_mark_node;
}
- else if (compare_tree_int (w, TYPE_PRECISION (type)) > 0
- && TREE_CODE (type) != ENUMERAL_TYPE
- && TREE_CODE (type) != BOOLEAN_TYPE)
- warning (0, "width of %q+D exceeds its type", field);
+ else if (compare_tree_int (w, TYPE_PRECISION (type)) > 0)
+ {
+ if (TREE_CODE (type) == ENUMERAL_TYPE
+ || TREE_CODE (type) == BOOLEAN_TYPE)
+ pedwarn (input_location, OPT_Wpedantic,
+ "width of %q+D exceeds its type", field);
+ else
+ warning (0, "width of %q+D exceeds its type", field);
+ }
else if (TREE_CODE (type) == ENUMERAL_TYPE
&& (0 > (compare_tree_int
(w, TYPE_PRECISION (ENUM_UNDERLYING_TYPE (type))))))
Index: testsuite/g++.dg/expr/bitfield1.C
===================================================================
--- testsuite/g++.dg/expr/bitfield1.C (revision 199288)
+++ testsuite/g++.dg/expr/bitfield1.C (working copy)
@@ -1,4 +1,5 @@
// PR c++/27505
+// { dg-options "" }
struct s {
bool field:8;
Index: testsuite/g++.dg/expr/bitfield12.C
===================================================================
--- testsuite/g++.dg/expr/bitfield12.C (revision 0)
+++ testsuite/g++.dg/expr/bitfield12.C (working copy)
@@ -0,0 +1,11 @@
+// PR c++/19618
+
+struct bset1 {
+ bool bit : 93111; // { dg-error "exceeds" }
+};
+
+enum E {};
+
+struct bset2 {
+ E bit : 93111; // { dg-error "exceeds" }
+};
Index: testsuite/g++.dg/expr/bitfield3.C
===================================================================
--- testsuite/g++.dg/expr/bitfield3.C (revision 199288)
+++ testsuite/g++.dg/expr/bitfield3.C (working copy)
@@ -1,4 +1,5 @@
// PR c++/30274
+// { dg-options "" }
struct S {
bool x : 4;
Index: testsuite/g++.dg/expr/bitfield4.C
===================================================================
--- testsuite/g++.dg/expr/bitfield4.C (revision 199288)
+++ testsuite/g++.dg/expr/bitfield4.C (working copy)
@@ -1,5 +1,6 @@
// PR c++/30274
// { dg-do link }
+// { dg-options "" }
struct S {
bool x : 4;
Index: testsuite/g++.dg/expr/bitfield5.C
===================================================================
--- testsuite/g++.dg/expr/bitfield5.C (revision 199288)
+++ testsuite/g++.dg/expr/bitfield5.C (working copy)
@@ -1,5 +1,6 @@
// PR c++/30274
// { dg-do run }
+// { dg-options "" }
struct S {
bool x : 4;
Index: testsuite/g++.dg/expr/bitfield6.C
===================================================================
--- testsuite/g++.dg/expr/bitfield6.C (revision 199288)
+++ testsuite/g++.dg/expr/bitfield6.C (working copy)
@@ -1,4 +1,5 @@
// PR c++/30274
+// { dg-options "" }
struct S {
bool x : 4;
Index: testsuite/g++.old-deja/g++.jason/bool2.C
===================================================================
--- testsuite/g++.old-deja/g++.jason/bool2.C (revision 199287)
+++ testsuite/g++.old-deja/g++.jason/bool2.C (working copy)
@@ -1,4 +1,5 @@
// { dg-do run }
+// { dg-options "" }
// Make sure that bool bitfields promote to int properly.
struct F {