From: malc <av1474@comtv.ru>
To: Blue Swirl <blauwirbel@gmail.com>
Cc: qemu-devel <qemu-devel@nongnu.org>
Subject: Re: [Qemu-devel] [PATCH 1/5] Suppress some gcc warnings with -Wtype-limits
Date: Sun, 5 Sep 2010 01:26:12 +0400 (MSD) [thread overview]
Message-ID: <alpine.LNX.2.00.1009050120070.3059@linmac> (raw)
In-Reply-To: <AANLkTikhfC2U17B9jHuR6v6_qOAW3CPbjncoA6WCraHX@mail.gmail.com>
On Sat, 4 Sep 2010, Blue Swirl wrote:
> On Sat, Sep 4, 2010 at 5:57 PM, andrzej zaborowski <balrogg@gmail.com> wrote:
> > On 4 September 2010 19:21, Blue Swirl <blauwirbel@gmail.com> wrote:
> >> On Sat, Sep 4, 2010 at 4:44 PM, andrzej zaborowski <balrogg@gmail.com> wrote:
> >>> On 4 September 2010 18:14, Blue Swirl <blauwirbel@gmail.com> wrote:
> >>>> On Sat, Sep 4, 2010 at 3:40 PM, andrzej zaborowski <balrogg@gmail.com> wrote:
> >>>>> On 4 September 2010 16:17, Blue Swirl <blauwirbel@gmail.com> 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:av1474@comtv.ru
next prev parent reply other threads:[~2010-09-04 21:26 UTC|newest]
Thread overview: 26+ messages / expand[flat|nested] mbox.gz Atom feed top
2010-09-04 14:17 [Qemu-devel] [PATCH 1/5] Suppress some gcc warnings with -Wtype-limits Blue Swirl
2010-09-04 15:40 ` andrzej zaborowski
2010-09-04 16:14 ` Blue Swirl
2010-09-04 16:44 ` andrzej zaborowski
2010-09-04 17:21 ` Blue Swirl
2010-09-04 17:57 ` andrzej zaborowski
2010-09-04 19:45 ` Blue Swirl
2010-09-04 20:30 ` andrzej zaborowski
2010-09-04 21:07 ` Blue Swirl
2010-09-06 13:04 ` Kevin Wolf
2010-09-06 19:21 ` Blue Swirl
2010-09-04 21:26 ` malc [this message]
2010-09-05 7:54 ` [Qemu-devel] " Michael S. Tsirkin
2010-09-05 9:06 ` Blue Swirl
2010-09-05 9:26 ` Michael S. Tsirkin
2010-09-05 9:44 ` Blue Swirl
2010-09-05 10:35 ` Michael S. Tsirkin
2010-09-05 15:26 ` andrzej zaborowski
2010-09-05 16:15 ` Blue Swirl
2010-09-05 17:02 ` andrzej zaborowski
2010-09-05 17:09 ` andrzej zaborowski
2010-09-05 19:16 ` Blue Swirl
2010-09-05 20:32 ` andrzej zaborowski
2010-09-05 21:44 ` Blue Swirl
2010-09-05 22:33 ` andrzej zaborowski
2010-09-06 19:08 ` Blue Swirl
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=alpine.LNX.2.00.1009050120070.3059@linmac \
--to=av1474@comtv.ru \
--cc=blauwirbel@gmail.com \
--cc=qemu-devel@nongnu.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).