This is the mail archive of the gcc@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]

Re: returning short-enum and truncate doesn't trigger conversion warning


On 19/03/14 15:55, Paulo Matos wrote:
> 
> 
>> -----Original Message-----
>> From: David Brown [mailto:david@westcontrol.com]
>> Sent: 19 March 2014 14:44
>> To: Paulo Matos; gcc@gcc.gnu.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
>>>
>>
>>
> 


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