c-format: double vs float
Jakub Jelinek
jakub@redhat.com
Fri Jan 8 20:03:00 GMT 2010
On Fri, Jan 08, 2010 at 02:52:45PM -0500, DJ Delorie wrote:
>
> This one is a worry... when we pass float to printf() on a target
> where doubles and floats are the same size (h8300-elf for example),
> the normal float->double type promotion doesn't happen (nor does it
> need to) but c-format now complains that a float is passed where a
> double is expected (which is true). Is the code technically correct,
> but still wrong? Is gcc trying too hard to be pedantic in this case?
> Is the test I wrote sufficient?
>
> float x;
> printf("%f", x);
As we warn even for say:
void foo (long long x)
{
printf ("%lx\n", x);
}
when sizeof (long) == sizeof (long long), we should warn about the above
as well. So IMHO the patch is wrong. The if stmt before this one is just
used for M$ I32/I64 formats, nothing else.
>
> * c-format.c (check_format_types): As a special case, check for
> targets where sizeof(double) == sizeof(float) and thus no
> promotion happens.
>
> Index: c-format.c
> ===================================================================
> --- c-format.c (revision 155741)
> +++ c-format.c (working copy)
> @@ -2272,12 +2272,18 @@ check_format_types (format_wanted_type *
> if (types->scalar_identity_flag
> && (TREE_CODE (cur_type) == TREE_CODE (wanted_type)
> || (INTEGRAL_TYPE_P (cur_type)
> && INTEGRAL_TYPE_P (wanted_type)))
> && TYPE_PRECISION (cur_type) == TYPE_PRECISION (wanted_type))
> continue;
> + /* For targets where sizeof(float) == sizeof(double), the usual
> + float->double type promotion doesn't happen, but the user's
> + code is technically correct. */
> + if (cur_type == float_type_node && wanted_type == double_type_node
> + && TYPE_PRECISION (cur_type) == TYPE_PRECISION (wanted_type))
> + continue;
> /* Now we have a type mismatch. */
> format_type_warning (types->name, format_start, format_length,
> wanted_type, types->pointer_count,
> types->wanted_type_name, orig_cur_type, arg_num);
> }
> }
Jakub
More information about the Gcc-patches
mailing list