* [Qemu-devel] [PATCH] gtk: Fix compiler warnings with -Werror=sign-compare
@ 2013-11-04 19:51 Stefan Weil
2013-11-04 20:07 ` Peter Maydell
0 siblings, 1 reply; 6+ messages in thread
From: Stefan Weil @ 2013-11-04 19:51 UTC (permalink / raw)
To: qemu-devel; +Cc: qemu-trivial, Stefan Weil, Anthony Liguori
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]
Signed-off-by: Stefan Weil <sw@weilnetz.de>
---
ui/gtk.c | 5 +++--
1 file changed, 3 insertions(+), 2 deletions(-)
diff --git a/ui/gtk.c b/ui/gtk.c
index b5f4f0b..79efda1 100644
--- a/ui/gtk.c
+++ b/ui/gtk.c
@@ -273,7 +273,8 @@ static void gd_update_full_redraw(GtkDisplayState *s)
static void gtk_release_modifiers(GtkDisplayState *s)
{
- int i, keycode;
+ unsigned i;
+ int keycode;
if (!gd_on_vga(s)) {
return;
@@ -714,7 +715,7 @@ static gboolean gd_key_event(GtkWidget *widget, GdkEventKey *key, void *opaque)
GtkDisplayState *s = opaque;
int gdk_keycode;
int qemu_keycode;
- int i;
+ unsigned i;
gdk_keycode = key->hardware_keycode;
--
1.7.10.4
^ permalink raw reply related [flat|nested] 6+ messages in thread
* Re: [Qemu-devel] [PATCH] gtk: Fix compiler warnings with -Werror=sign-compare
2013-11-04 19:51 [Qemu-devel] [PATCH] gtk: Fix compiler warnings with -Werror=sign-compare Stefan Weil
@ 2013-11-04 20:07 ` Peter Maydell
2013-11-04 20:23 ` Stefan Weil
2013-11-04 20:38 ` Laszlo Ersek
0 siblings, 2 replies; 6+ messages in thread
From: Peter Maydell @ 2013-11-04 20:07 UTC (permalink / raw)
To: Stefan Weil; +Cc: QEMU Trivial, qemu-devel, Anthony Liguori
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++) {
...
}
then I think it is more trouble than it is worth and we
should leave it disabled.
thanks
-- PMM
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [Qemu-devel] [PATCH] gtk: Fix compiler warnings with -Werror=sign-compare
2013-11-04 20:07 ` Peter Maydell
@ 2013-11-04 20:23 ` Stefan Weil
2013-11-04 20:38 ` Laszlo Ersek
1 sibling, 0 replies; 6+ messages in thread
From: Stefan Weil @ 2013-11-04 20:23 UTC (permalink / raw)
To: Peter Maydell; +Cc: QEMU Trivial, qemu-devel, Anthony Liguori
Am 04.11.2013 21:07, schrieb Peter Maydell:
> 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++) {
> ...
> }
>
> then I think it is more trouble than it is worth and we
> should leave it disabled.
>
> thanks
> -- PMM
Yes, we should leave it disabled for simple practical reasons: there are
too many warnings of this kind, so I don't expect they will be fixed in
the near future (I have no plan to send patches to fix them all - this
one was just in my way).
And yes, we should fix code which can be improved as easily as in the
above example. ARRAY_SIZE happens to be an unsigned size_t, so comparing
it to an unsigned value is better IMHO (I'm aware that there were
already discussions about using signed or unsigned integers).
-Wsign-compare is like other compiler warnings: it can detect some
really unwanted code, but a human reviewer won't always agree on the
results.It might be a good idea to support it somewhere in the far future.
Stefan
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [Qemu-devel] [PATCH] gtk: Fix compiler warnings with -Werror=sign-compare
2013-11-04 20:07 ` Peter Maydell
2013-11-04 20:23 ` Stefan Weil
@ 2013-11-04 20:38 ` Laszlo Ersek
2013-11-04 21:00 ` Peter Maydell
2013-11-06 8:51 ` [Qemu-devel] C99 loop vars? [was: gtk: Fix compiler warnings with -Werror=sign-compare] Michael Tokarev
1 sibling, 2 replies; 6+ messages in thread
From: Laszlo Ersek @ 2013-11-04 20:38 UTC (permalink / raw)
To: Peter Maydell; +Cc: QEMU Trivial, Stefan Weil, qemu-devel, Anthony Liguori
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.)
Laszlo
/me apologizes for the raging pedantry
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [Qemu-devel] [PATCH] gtk: Fix compiler warnings with -Werror=sign-compare
2013-11-04 20:38 ` Laszlo Ersek
@ 2013-11-04 21:00 ` Peter Maydell
2013-11-06 8:51 ` [Qemu-devel] C99 loop vars? [was: gtk: Fix compiler warnings with -Werror=sign-compare] Michael Tokarev
1 sibling, 0 replies; 6+ messages in thread
From: Peter Maydell @ 2013-11-04 21:00 UTC (permalink / raw)
To: Laszlo Ersek; +Cc: QEMU Trivial, Stefan Weil, qemu-devel, Anthony Liguori
On 4 November 2013 20:38, Laszlo Ersek <lersek@redhat.com> wrote:
> On 11/04/13 21:07, Peter Maydell wrote:
>> 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.)
Making simple loop iteration variables 'unsigned' is completely
non-idiomatic. It just happens that we're calculating the upper
bound here via a macro that coincidentally produces an
unsigned result. Insisting on unsigned also runs you into
problems with loops like
for (i = ARRAY_SIZE(foo) - 1; i >=0 ; i--) {
something(foo[i]);
}
(which obviously you can avoid by contorting the code,
but why do that when leaving 'i' as signed is much clearer?)
-- PMM
^ permalink raw reply [flat|nested] 6+ messages in thread
* [Qemu-devel] C99 loop vars? [was: gtk: Fix compiler warnings with -Werror=sign-compare]
2013-11-04 20:38 ` Laszlo Ersek
2013-11-04 21:00 ` Peter Maydell
@ 2013-11-06 8:51 ` Michael Tokarev
1 sibling, 0 replies; 6+ messages in thread
From: Michael Tokarev @ 2013-11-06 8:51 UTC (permalink / raw)
To: Laszlo Ersek
Cc: QEMU Trivial, Peter Maydell, qemu-devel, Anthony Liguori,
Stefan Weil
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
^ permalink raw reply [flat|nested] 6+ messages in thread
end of thread, other threads:[~2013-11-06 8:52 UTC | newest]
Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2013-11-04 19:51 [Qemu-devel] [PATCH] gtk: Fix compiler warnings with -Werror=sign-compare Stefan Weil
2013-11-04 20:07 ` Peter Maydell
2013-11-04 20:23 ` Stefan Weil
2013-11-04 20:38 ` Laszlo Ersek
2013-11-04 21:00 ` Peter Maydell
2013-11-06 8:51 ` [Qemu-devel] C99 loop vars? [was: gtk: Fix compiler warnings with -Werror=sign-compare] Michael Tokarev
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).