qemu-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Qemu-devel] [PATCH 1/5] Suppress some gcc warnings with -Wtype-limi


From: malc
Subject: Re: [Qemu-devel] [PATCH 1/5] Suppress some gcc warnings with -Wtype-limits
Date: Sun, 5 Sep 2010 01:26:12 +0400 (MSD)
User-agent: Alpine 2.00 (LNX 1167 2008-08-23)

On Sat, 4 Sep 2010, Blue Swirl wrote:

> On Sat, Sep 4, 2010 at 5:57 PM, andrzej zaborowski <address@hidden> wrote:
> > On 4 September 2010 19:21, Blue Swirl <address@hidden> wrote:
> >> On Sat, Sep 4, 2010 at 4:44 PM, andrzej zaborowski <address@hidden> wrote:
> >>> On 4 September 2010 18:14, Blue Swirl <address@hidden> wrote:
> >>>> On Sat, Sep 4, 2010 at 3:40 PM, andrzej zaborowski <address@hidden> 
> >>>> wrote:
> >>>>> On 4 September 2010 16:17, Blue Swirl <address@hidden> wrote:
> >>>>>> Add various casts, adjust types etc. to make the warnings with
> >>>>>> gcc flag -Wtype-limits disappear.

[..snip..]

> The problem is that the type of an enum may be signed or unsigned,
> which affects the comparison. For example, the following program
> produces different results when it's compiled with -DUNSIGNED and
> without:
> $ cat enum.c
> #include <stdio.h>
> 
> int main(int argc, const char **argv)
> {
>     enum {
> #ifdef UNSIGNED
>         A = 0,
> #else
>         A = -1,
> #endif
>     } a;
> 
>     a = atoi(argv[1]);
>     if (a < 0) {
>         puts("1: smaller");
>     } else {
>         puts("1: not smaller");
>     }
> 
>     if ((int)a < 0) {
>         puts("2: smaller");
>     } else {
>         puts("2: not smaller");
>     }
> 
>     return 0;
> }
> $ gcc -DUNSIGNED enum.c -o enum-unsigned
> $ gcc enum.c -o enum-signed
> $ ./enum-signed 0
> 1: not smaller
> 2: not smaller
> $ ./enum-signed -1
> 1: smaller
> 2: smaller
> $ ./enum-unsigned 0
> 1: not smaller
> 2: not smaller
> $ ./enum-unsigned -1
> 1: not smaller
> 2: smaller
> 
> This is only how GCC uses enums, other compilers have other rules. So
> it's better to avoid any assumptions about signedness of enums.
> 
> In this specific case, because the enum does not have any negative
> items, it is unsigned if compiled with GCC. Unsigned items cannot be
> negative, thus the check is useless.
> 
> If the enum included also negative values (or compiled with a compiler
> using different rules), the check would succeed. Though then the check
> against 0 would be wrong because the author probably wanted to check
> against the smallest possible value.
> 
> In both cases, the cast to int makes sure that the comparison is meaningful.
> 

While enumerated types are indeed implementation defined (6.7.2.2),
enumeration constants themselves are of type int (6.4.4.3), so all one
has to do is not to declare variables with enumeration type but use
plain int instead (Which i guess goes against the desire to be gentle
with the reader of the code)

[..snip..]

-- 
mailto:address@hidden



reply via email to

[Prev in Thread] Current Thread [Next in Thread]