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.
> For example:
> unsigned short foo (unsigned int a)
> {
> return a;
> }
This gives a conversion warning because a short unsigned int (16-bit on
x86) is smaller than an unsigned int (32-bit on x86). So calling
foo(0x12345678) will cause truncation.
>
> 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.
>
> $ ~/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
>