This is the mail archive of the
mailing list for the GCC project.
Re: returning short-enum and truncate doesn't trigger conversion warning
- From: David Brown <david at westcontrol dot com>
- To: Paulo Matos <pmatos at broadcom dot com>, "gcc at gcc dot gnu dot org" <gcc at gcc dot gnu dot org>
- Date: Wed, 19 Mar 2014 16:46:53 +0100
- Subject: Re: returning short-enum and truncate doesn't trigger conversion warning
- Authentication-results: sourceware.org; auth=none
- References: <19EB96622A777C4AB91610E763265F46433F65 at SJEXCHMB14 dot corp dot ad dot broadcom dot com> <5329AD13 dot 4010608 at westcontrol dot com> <19EB96622A777C4AB91610E763265F46433F9B at SJEXCHMB14 dot corp dot ad dot broadcom dot com>
On 19/03/14 15:55, Paulo Matos wrote:
>> -----Original Message-----
>> From: David Brown [mailto:email@example.com]
>> Sent: 19 March 2014 14:44
>> To: Paulo Matos; firstname.lastname@example.org
>> Subject: Re: returning short-enum and truncate doesn't trigger
>> conversion warning
>> On 19/03/14 15:33, Paulo Matos wrote:
>>> Hi all,
>>> This is either a C standard subtlety or a bug in GCC.
>> This is perfectly normal behaviour for C, and not a bug. It is also a
>> topic for the gcc help list, rather than the development list.
> Apologies, but I submitted this to gcc devel because I do think it is a bug.
Usually the discovery of bugs gets discussed on the help list - fixes
for the bugs are an issue for the developers list. And it is certainly
a "help" issue until we know you have found a bug.
>>> enum xpto
>>> A = 0,
>>> B = 1,
>>> X = 512
>>> extern void print (unsigned int);
>>> unsigned char bar (enum xpto a)
>>> print (sizeof (unsigned char));
>>> print (sizeof (enum xpto));
>>> return a;
>> The sizeof operator returns an integer of type size_t (typically the
>> same as unsigned int or unsigned long, depending on the platform
>> details). But the compiler can see that the particular values in
>> question - 1 and 2 - can be converted to unsigned int without loss of
>> precision of changing the value. Therefore no warning is giving.
> 512 cannot be converted to an unsigned char without loss of precision.
> The prints are actually not relevant for the question. They were just used for me to confirm the sizes of the types.
Not only are the prints not relevant, but they hid the real issue - I
missed the point of your question!
Conversions involving enumerations are not listed in the documentation
for -Wconversion, but it would be nice to get a warning here. Filing it
as an enhancement request seems reasonable.
> enum xpto
> A = 0,
> B = 1,
> X = 512
> extern void print (unsigned int);
> unsigned char bar (enum xpto a)
> return a;
> Variable a could be 0, 1 or 512. In case of the latter there is loss of precision but still no warning.
>>> $ ~/work/tmp/GCC/builds/gcc-trunk/gcc/cc1 -O2 test.c -Wall
>>> -Wconversion --short-enums -quiet
>>> test.c: In function 'foo':
>>> test.c:3:3: warning: conversion to 'short unsigned int' from
>> 'unsigned int' may alter its value [-Wconversion]
>>> return a;
>>> I was expecting a warning for bar as well since sizeof unsigned char
>> is 1 and sizeof enum xpto is 2, therefore the value is truncated but
>> no warning is issued.
>>> Shall I open a PR?
>>> Paulo Matos