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 - gcc-4.0 not c99 conforming when assigning scalar values to boolean bitfields



gcc-4.0 is not c99 conforming when assigning scalar values to boolean bitfields.
gcc-3.3 and g++4.0 are.


From: ISO/IEC 9899:1999 (E)

6.3.1.2 Boolean type

1 When any scalar value is converted to _Bool, the result is 0 if the value compares equal to 0;
otherwise, the result is 1.


So, following test fails with gcc-4.0.

struct bits {
        _Bool f2:6;
} b;
int main() {
        b.f2 = 17;
        return (b.f2 == 1 ? 0 : 1 );
}

Problem starts in c-decl.c's finish_struct routine which changes type of bitfields to a new type
which can represent width of the bitfield. Here the fact that we have a boolean bit-field is lost.
I fixed this problem by not doing this change for _Bool bitfields if size of _Bool is enough to contain
width of the bitfield. This seems logical to me as _Bool is a special case in that its precision (1) is different
that its type size. I spent some time going through
various areas of c front-end, as well as writing tests cases (attached), but do not see how this can
break the compiler. If it does, then alternative would be to save the original type in the new type and
use it where is needed (at list in two places; initialization and assignment). This is, of course a far more
intrusive change. There is of course a chance that structure lay out changes with this patch. But this
is OK because it moves us toward gcc-3.3's layout (a released gcc) and away from gcc-4.0 (an unreleased
gcc).


Is this OK if testing all went well. So far, all bitfield tests in dejagnu passed.

- fariborz (fjahanian@apple.com)



Attachment: fsf-patch-4055075.txt
Description: Text document




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