Wrong warning? ISO C restricts enumerator values to range of ‘int’

2008-02-06 Thread Felipe Contreras
Hi,

This is what ISO C says:

Each enumerated type shall be compatible with char, a signed integer
type, or an unsigned integer type. The choice of type is
implementation-defined,110) but shall be capable of representing the
values of all the members of the enumeration.

110) An implementation may delay the choice of which integer type
until all enumeration constants have been seen.


The interesting part here is "shall be capable of representing the
values of all the members of the enumeration", sometimes only an
"unsigned int" can do that.

So, shouldn't gcc allow this without warnings then?

typedef enum OMX_ERRORTYPE
{
  OMX_ErrorNone = 0,
  OMX_ErrorInsufficientResources = 0x80001000
} OMX_ERRORTYPE;

Best regards.

-- 
Felipe Contreras


Re: Wrong warning? ISO C restricts enumerator values to range of ‘int’

2008-02-06 Thread Felipe Contreras
On Feb 6, 2008 1:18 PM, Manuel López-Ibáñez <[EMAIL PROTECTED]> wrote:
> On 06/02/2008, Felipe Contreras <[EMAIL PROTECTED]> wrote:
> >
> > So, shouldn't gcc allow this without warnings then?
> >
> > typedef enum OMX_ERRORTYPE
> > {
> >   OMX_ErrorNone = 0,
> >   OMX_ErrorInsufficientResources = 0x80001000
> > } OMX_ERRORTYPE;
> >
>
> And what warning do you get and with which version of GCC you get it?
>
> [EMAIL PROTECTED]:~$ cat felipe.c
> typedef enum OMX_ERRORTYPE
> {
>  OMX_ErrorNone = 0,
>  OMX_ErrorInsufficientResources = 0x80001000
> } OMX_ERRORTYPE;
> [EMAIL PROTECTED]:~$ ~/personal/gcc/objdir/gcc/cc1 -Wall -Wextra
> -Wconversion ~/felipe.c
> [EMAIL PROTECTED]:~$ ~/personal/gcc/objdir/gcc/cc1 --version
> GNU C (GCC) version 4.3.0 20080122 (experimental) (x86_64-unknown-linux-gnu)
> compiled by GNU C version 4.3.0 20080122 (experimental), GMP
> version 4.2.2, MPFR version 2.3.0.
> GGC heuristics: --param ggc-min-expand=30 --param ggc-min-heapsize=4096

0x80001000u and 0x80001000UL didn't work:

gcc test.c -o test -pedantic
test.c:7: warning: ISO C restricts enumerator values to range of 'int'

This is with:
gcc (GCC) 4.1.2 20070925 (Red Hat 4.1.2-33)

-- 
Felipe Contreras


Re: Wrong warning? ISO C restricts enumerator values to range of ‘int’

2008-02-06 Thread Felipe Contreras
On Feb 6, 2008 2:47 PM, Andreas Schwab <[EMAIL PROTECTED]> wrote:
> "Felipe Contreras" <[EMAIL PROTECTED]> writes:
>
> > This is what ISO C says:
> >
> > Each enumerated type shall be compatible with char, a signed integer
> > type, or an unsigned integer type. The choice of type is
> > implementation-defined,110) but shall be capable of representing the
> > values of all the members of the enumeration.
>
> The standard also says:
>
>   The expression that defines the value of an enumeration constant shall
>   be an integer constant expression that has a value representable as an
>   int.

Ahh, I see. So even if the type of the enum is specified by the
compiler as "unsigned int"; the value assigned to it can only be an
"int".

-- 
Felipe Contreras