From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from [140.186.70.92] (port=46253 helo=eggs.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1Os0Fk-0004XW-De for qemu-devel@nongnu.org; Sat, 04 Sep 2010 17:26:25 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.69) (envelope-from ) id 1Os0Fj-0003wB-5w for qemu-devel@nongnu.org; Sat, 04 Sep 2010 17:26:24 -0400 Received: from fe02x03-cgp.akado.ru ([77.232.31.165]:54713 helo=akado.ru) by eggs.gnu.org with esmtp (Exim 4.69) (envelope-from ) id 1Os0Fi-0003vI-VO for qemu-devel@nongnu.org; Sat, 04 Sep 2010 17:26:23 -0400 Date: Sun, 5 Sep 2010 01:26:12 +0400 (MSD) From: malc Subject: Re: [Qemu-devel] [PATCH 1/5] Suppress some gcc warnings with -Wtype-limits In-Reply-To: Message-ID: References: MIME-Version: 1.0 Content-Type: TEXT/PLAIN; charset=US-ASCII List-Id: qemu-devel.nongnu.org List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: Blue Swirl Cc: qemu-devel On Sat, 4 Sep 2010, Blue Swirl wrote: > On Sat, Sep 4, 2010 at 5:57 PM, andrzej zaborowski wrote: > > On 4 September 2010 19:21, Blue Swirl wrote: > >> On Sat, Sep 4, 2010 at 4:44 PM, andrzej zaborowski wrote: > >>> On 4 September 2010 18:14, Blue Swirl wrote: > >>>> On Sat, Sep 4, 2010 at 3:40 PM, andrzej zaborowski wrote: > >>>>> On 4 September 2010 16:17, Blue Swirl 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 > > 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:av1474@comtv.ru