c/10040: Incorrect warning message for bit fields
Zack Weinberg
zack@codesourcery.com
Thu Mar 13 21:33:00 GMT 2003
James L Peterson <peterson@austin.ibm.com> writes:
> Falk Hueffner wrote:
>
>> On 12 Mar 2003 peterson@austin.ibm.com wrote:
>>
>> > int _v: 1;
>> > if ((*tlb)._hi._v == 1)
>> >
>> > warning: comparison is always 0 due to width of bit-field
>>
>> The warning seems 100% correct to me. The comparison *is* always 0.
>>
>> > In trying to explain the problem, I'm beginning to see that this error
>> > message is a result of the bit field being an "int" (signed) rather
>> > than "unsigned int". In which case, maybe the problem is best
>> > addressed by a different warning message -- "bit field too small for
>> > value and sign bit; consider unsigned type for field"
>>
>> That's too much guessing for my taste. The declaration might be just what
>> was intended, and only the comparison is bogus. At most I'd consider
>> "warning: comparison is always 0 due to width and signedness of
>> bit-field".
>
> I've gone back over the C spec and I understand (now) what gcc is
> doing. I would really suggest that the warning message be changed
> to indicate that the compiler has found a signed field, and since one
> bit is kept as a sign, there is not room for the value.
>
> I've talked to a number of experienced C programmers, and even some
> friends who teach C (and C++ and Java) at the University. Everyone
> agrees that the expectation is that a bit field of size 1 will hold only
> two values: 0 and 1. It's only after the signed/unsigned aspect is
> explained that they understand the reason for the error message.
I agree; this nuance surprised me the first time I ran into it, too.
Would you care to submit a patch with better wording?
zw
More information about the Gcc-bugs
mailing list