From: Michael Tokarev <mjt@tls.msk.ru>
To: Laszlo Ersek <lersek@redhat.com>
Cc: QEMU Trivial <qemu-trivial@nongnu.org>,
Peter Maydell <peter.maydell@linaro.org>,
qemu-devel <qemu-devel@nongnu.org>,
Anthony Liguori <aliguori@amazon.com>,
Stefan Weil <sw@weilnetz.de>
Subject: [Qemu-trivial] C99 loop vars? [was: gtk: Fix compiler warnings with -Werror=sign-compare]
Date: Wed, 06 Nov 2013 12:51:49 +0400 [thread overview]
Message-ID: <527A0325.1020407@msgid.tls.msk.ru> (raw)
In-Reply-To: <527805D2.3000107@redhat.com>
05.11.2013 00:38, Laszlo Ersek wrote:
> On 11/04/13 21:07, Peter Maydell wrote:
>> On 4 November 2013 19:51, Stefan Weil <sw@weilnetz.de> wrote:
>>> With -Werror=sign-compare (not enabled by default), gcc shows these errors:
>>>
>>> ui/gtk.c: In function ‘gtk_release_modifiers’:
>>> ui/gtk.c:288:19: error:
>>> comparison between signed and unsigned integer expressions [-Werror=sign-compare]
>>> ui/gtk.c: In function ‘gd_key_event’:
>>> ui/gtk.c:746:19: error:
>>> comparison between signed and unsigned integer expressions [-Werror=sign-compare]
>>
>> If this warning is going to complain about entirely
>> safe and idiomatic code like
>>
>> int i;
>> static const int some_array[] = {
>> 0x2a, 0x36, 0x1d, 0x9d, 0x38, 0xb8, 0xdb, 0xdd,
>> };
>>
>> for (i = 0; i < ARRAY_SIZE(some_array); i++) {
>> ...
>> }
>
> (Entirely safe, and completely non-idiomatic: "i" should be size_t, as
> that is the type of the sizeof operator's result.)
Maybe in some places we should switch to C99 which allows to declare
a loop variable inside the loop header, like this:
for(int i = 0; i < ARRAY_SIZE(..); i++) {
}
?
This is much better than a per-unit (function-level) declaration because
by changing type in one place we don't change it for other places which
might become wrong in the result...
But this requires compiling whole thing with gcc -std=c99, which might
bit problematic.
But as it is, the original patch from Stefan, -- I don't think it's a
good idea to apply it. The code is obviously correct, ARRAY_SIZE is
a fixed value, the compiler is able to determine this and shut pretty
much up ;)
Thanks,
/mjt
WARNING: multiple messages have this Message-ID (diff)
From: Michael Tokarev <mjt@tls.msk.ru>
To: Laszlo Ersek <lersek@redhat.com>
Cc: QEMU Trivial <qemu-trivial@nongnu.org>,
Peter Maydell <peter.maydell@linaro.org>,
qemu-devel <qemu-devel@nongnu.org>,
Anthony Liguori <aliguori@amazon.com>,
Stefan Weil <sw@weilnetz.de>
Subject: [Qemu-devel] C99 loop vars? [was: gtk: Fix compiler warnings with -Werror=sign-compare]
Date: Wed, 06 Nov 2013 12:51:49 +0400 [thread overview]
Message-ID: <527A0325.1020407@msgid.tls.msk.ru> (raw)
In-Reply-To: <527805D2.3000107@redhat.com>
05.11.2013 00:38, Laszlo Ersek wrote:
> On 11/04/13 21:07, Peter Maydell wrote:
>> On 4 November 2013 19:51, Stefan Weil <sw@weilnetz.de> wrote:
>>> With -Werror=sign-compare (not enabled by default), gcc shows these errors:
>>>
>>> ui/gtk.c: In function ‘gtk_release_modifiers’:
>>> ui/gtk.c:288:19: error:
>>> comparison between signed and unsigned integer expressions [-Werror=sign-compare]
>>> ui/gtk.c: In function ‘gd_key_event’:
>>> ui/gtk.c:746:19: error:
>>> comparison between signed and unsigned integer expressions [-Werror=sign-compare]
>>
>> If this warning is going to complain about entirely
>> safe and idiomatic code like
>>
>> int i;
>> static const int some_array[] = {
>> 0x2a, 0x36, 0x1d, 0x9d, 0x38, 0xb8, 0xdb, 0xdd,
>> };
>>
>> for (i = 0; i < ARRAY_SIZE(some_array); i++) {
>> ...
>> }
>
> (Entirely safe, and completely non-idiomatic: "i" should be size_t, as
> that is the type of the sizeof operator's result.)
Maybe in some places we should switch to C99 which allows to declare
a loop variable inside the loop header, like this:
for(int i = 0; i < ARRAY_SIZE(..); i++) {
}
?
This is much better than a per-unit (function-level) declaration because
by changing type in one place we don't change it for other places which
might become wrong in the result...
But this requires compiling whole thing with gcc -std=c99, which might
bit problematic.
But as it is, the original patch from Stefan, -- I don't think it's a
good idea to apply it. The code is obviously correct, ARRAY_SIZE is
a fixed value, the compiler is able to determine this and shut pretty
much up ;)
Thanks,
/mjt
next prev parent reply other threads:[~2013-11-06 8:52 UTC|newest]
Thread overview: 12+ messages / expand[flat|nested] mbox.gz Atom feed top
2013-11-04 19:51 [Qemu-trivial] [PATCH] gtk: Fix compiler warnings with -Werror=sign-compare Stefan Weil
2013-11-04 19:51 ` [Qemu-devel] " Stefan Weil
2013-11-04 20:07 ` [Qemu-trivial] " Peter Maydell
2013-11-04 20:07 ` Peter Maydell
2013-11-04 20:23 ` [Qemu-trivial] " Stefan Weil
2013-11-04 20:23 ` Stefan Weil
2013-11-04 20:38 ` [Qemu-trivial] " Laszlo Ersek
2013-11-04 20:38 ` Laszlo Ersek
2013-11-04 21:00 ` [Qemu-trivial] " Peter Maydell
2013-11-04 21:00 ` Peter Maydell
2013-11-06 8:51 ` Michael Tokarev [this message]
2013-11-06 8:51 ` [Qemu-devel] C99 loop vars? [was: gtk: Fix compiler warnings with -Werror=sign-compare] Michael Tokarev
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=527A0325.1020407@msgid.tls.msk.ru \
--to=mjt@tls.msk.ru \
--cc=aliguori@amazon.com \
--cc=lersek@redhat.com \
--cc=peter.maydell@linaro.org \
--cc=qemu-devel@nongnu.org \
--cc=qemu-trivial@nongnu.org \
--cc=sw@weilnetz.de \
/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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.