On 11/09/2014 02:28 AM, Manuel López-Ibáñez wrote:
We had a request from a customer to add a warning to the C front end to
diagnose cases where bit-fields larger than an int are used in shift
expressions; confusingly, the operation is done in the precision of the
bit-field size rather than its declared type. This is a symptom of a larger
problem, of course, as it affects bit-field uses in other expressions
besides shifts, too.
The C standard specifies that bit-fields have an integral type of the
specified number of bits. On the other hand, in C++, bit-fields do have
their declared type, so it seems appropriate to tie the new proposed warning
to -Wc++-compat.
The message:
+ warning_at (EXPR_LOCATION (exp), OPT_Wbitfield_conversion,
+ "suggest casting bit-field to its declared type");
is not very clear without the context of your email. Could it explain
what is (or could) be wrong? For example, "ISO C specifies that this
bit-field is implicitly converted to type %qT rather than its declared
type (%qT)". And a note at lhs location could say "use an explicit
cast to silence this warning".
Yes, I know the text of the warning in my patch needed improvement. But
I don't think your suggested improvement is quite correct, either; e.g.
in the case of sp->b, ISO C specifies that the bit-field type is 40-bit
unsigned integer, and that it is *not* converted implicitly to anything
else. Maybe
"ISO C specifies that the promoted type of this bit-field differs from
that of its declared type (%qT)"
??
Nonetheless, perhaps I'm missing something but Wconversion seems to
think that the promotion of sp->a and sp->c is always to 'int'.
Hrmmm. In C, sp->a has type "3-bit unsigned integer" which is promoted
to int by the integral promotions since all values are representable as
int. sp->c has type "5-bit unsigned integer" which is likewise
promoted to int. In C++, sp->a has type "unsigned char" and sp->c has
type "unsigned long long". By the C++ conversion rules, "unsigned char"
is promoted to int (again since all values are representable), and
"unsigned long long" is not promoted to anything else.
Now I'm worried, though, that I didn't account for signed-ness properly
in the test about whether or not to give a warning. Maybe a better test
would be to actually do the promotions of both types and warn if they
don't match in signedness as well as size?
-Sandra