|
From: | Michael Tokarev |
Subject: | [Qemu-devel] C99 loop vars? [was: gtk: Fix compiler warnings with -Werror=sign-compare] |
Date: | Wed, 06 Nov 2013 12:51:49 +0400 |
User-agent: | Mozilla/5.0 (X11; Linux x86_64; rv:17.0) Gecko/20130922 Icedove/17.0.9 |
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 <address@hidden> 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
[Prev in Thread] | Current Thread | [Next in Thread] |