Bug 19618 - Do warn if a bit-field exceeds the size of a bool type
Summary: Do warn if a bit-field exceeds the size of a bool type
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: c++ (show other bugs)
Version: 3.3.3
: P2 normal
Target Milestone: 4.9.0
Assignee: Paolo Carlini
URL:
Keywords: diagnostic
Depends on:
Blocks:
 
Reported: 2005-01-25 01:33 UTC by Michael Robinson
Modified: 2013-05-24 18:11 UTC (History)
1 user (show)

See Also:
Host: multiple platforms
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2006-01-29 17:58:00


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Michael Robinson 2005-01-25 01:33:55 UTC
The following code will compile without warning.  This isn't exactly a bug, but
it is inconsistant from how other types are treated.

struct bset{
  bool bit: 93111;
};
Comment 1 Andrew Pinski 2005-01-25 02:30:56 UTC
Confirmed, werid.

(compare_tree_int (w, TYPE_PRECISION (type)) > 0
               && TREE_CODE (type) != ENUMERAL_TYPE
               && TREE_CODE (type) != BOOLEAN_TYPE)


This changed way back in March of 1998 but since the mailing list history does not go back that far, I 
don't know why.
Comment 2 Paul Schlie 2005-01-25 03:24:45 UTC
(In reply to comment #1)

An interesting question might be how wide is a bool type, I believe C defines it as
having a rank less than char, and preumably at least as wide as an unsigned:1, so
does that imply a warning is appropriate for any bool bit-field greater than 1 bit wide?
Comment 3 Andrew Pinski 2005-04-27 00:34:08 UTC
We do error out with the C front-end.
Comment 4 Fred J. Tydeman 2009-02-04 16:16:53 UTC
/*
 * _Bool bit-fields: C99: 6.7.2.1, paragraph 3 along with Defect Report 335.
 * _Bool bit-fields of size up to CHAR_BIT must be supported.
 */
#include <limits.h>             /* CHAR_BIT */

int main(void){
  struct bits {
   int        : 0;
   _Bool      : 0;              /* force alignment */
   _Bool bbf1 : 1;              /* holds values 0 and 1 */ 
   _Bool bbf8 : CHAR_BIT;       /* must be supported */
  } bits;
  return 0;
}

The above code fails to compile in gcc 4.3.2-7
The error message is:
error: width of 'bbf8' exceeds its type

A _Bool is one storage unit, so is CHAR_BIT bits.
Therefore, _Bool bit-fields of size 0, 1, 2, ... CHAR_BIT
must be supported.
Comment 5 joseph@codesourcery.com 2009-02-04 16:30:49 UTC
Subject: Re:  Does warn if bit-fields exceed the size of bool
 types

On Wed, 4 Feb 2009, tydeman at tybor dot com wrote:

> /*
>  * _Bool bit-fields: C99: 6.7.2.1, paragraph 3 along with Defect Report 335.
>  * _Bool bit-fields of size up to CHAR_BIT must be supported.
>  */

I don't know why you think C99 is relevant to a C++ bug report.  As I have 
explained at length before and as we discussed at length at the London 
WG14 meeting, the "width" of a type is the number of value and sign bits, 
so post-TC2 a _Bool:CHAR_BIT bit-field is valid only if the implementation 
defines _Bool to have CHAR_BIT value bits.  GCC defines it to have one 
value bit with the other bits being padding bits and undefined behavior if 
you access a _Bool representation with any of the padding bits having a 
nonzero value (such representations being trap representations).  Thus the 
width of _Bool is 1 with GCC and the diagnostics are required.

As you know, TC2 changed "the number of bits in an object of the type that 
is specified if the colon and expression are omitted" to "the width of an 
object of the type that would be specified were the colon and expression 
omitted". in 6.7.2.1 paragraph 3.

I have no idea what is required by C++ and whether GCC implements the C++ 
requirements correctly or not.

Comment 6 Fred J. Tydeman 2009-02-04 17:15:40 UTC
Opps. I missed that the bug report was against C++ (I still am learning bugzilla).  So, ignore my previous comments.
Comment 7 Paolo Carlini 2013-05-24 11:47:35 UTC
Let's resolve this.
Comment 8 Paolo Carlini 2013-05-24 17:38:23 UTC
Done for 4.9.0.