From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from list by lists.gnu.org with archive (Exim 4.71) id 1VdyqT-0004jW-Ss for mharc-qemu-trivial@gnu.org; Wed, 06 Nov 2013 03:52:13 -0500 Received: from eggs.gnu.org ([2001:4830:134:3::10]:34545) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1VdyqM-0004Vw-M4 for qemu-trivial@nongnu.org; Wed, 06 Nov 2013 03:52:11 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1VdyqH-0002S9-QC for qemu-trivial@nongnu.org; Wed, 06 Nov 2013 03:52:06 -0500 Received: from isrv.corpit.ru ([86.62.121.231]:40284) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Vdyq6-0002PN-K4; Wed, 06 Nov 2013 03:51:50 -0500 Received: from [192.168.88.2] (mjt.vpn.tls.msk.ru [192.168.177.99]) by isrv.corpit.ru (Postfix) with ESMTP id 1AEF44085D; Wed, 6 Nov 2013 12:51:49 +0400 (MSK) Message-ID: <527A0325.1020407@msgid.tls.msk.ru> Date: Wed, 06 Nov 2013 12:51:49 +0400 From: Michael Tokarev Organization: Telecom Service, JSC User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:17.0) Gecko/20130922 Icedove/17.0.9 MIME-Version: 1.0 To: Laszlo Ersek References: <1383594685-19332-1-git-send-email-sw@weilnetz.de> <527805D2.3000107@redhat.com> In-Reply-To: <527805D2.3000107@redhat.com> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: quoted-printable X-detected-operating-system: by eggs.gnu.org: GNU/Linux 3.x X-Received-From: 86.62.121.231 Cc: QEMU Trivial , Peter Maydell , qemu-devel , Anthony Liguori , Stefan Weil Subject: [Qemu-trivial] C99 loop vars? [was: gtk: Fix compiler warnings with -Werror=sign-compare] X-BeenThere: qemu-trivial@nongnu.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Wed, 06 Nov 2013 08:52:12 -0000 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 wrote: >>> With -Werror=3Dsign-compare (not enabled by default), gcc shows these= errors: >>> >>> ui/gtk.c: In function =E2=80=98gtk_release_modifiers=E2=80=99: >>> ui/gtk.c:288:19: error: >>> comparison between signed and unsigned integer expressions [-Werror= =3Dsign-compare] >>> ui/gtk.c: In function =E2=80=98gd_key_event=E2=80=99: >>> ui/gtk.c:746:19: error: >>> comparison between signed and unsigned integer expressions [-Werror= =3Dsign-compare] >> >> If this warning is going to complain about entirely >> safe and idiomatic code like >> >> int i; >> static const int some_array[] =3D { >> 0x2a, 0x36, 0x1d, 0x9d, 0x38, 0xb8, 0xdb, 0xdd, >> }; >> >> for (i =3D 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 =3D 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=3Dc99, 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 From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from eggs.gnu.org ([2001:4830:134:3::10]:34460) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1VdyqC-0004PV-Ta for qemu-devel@nongnu.org; Wed, 06 Nov 2013 03:52:01 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1Vdyq6-0002PY-SW for qemu-devel@nongnu.org; Wed, 06 Nov 2013 03:51:56 -0500 Message-ID: <527A0325.1020407@msgid.tls.msk.ru> Date: Wed, 06 Nov 2013 12:51:49 +0400 From: Michael Tokarev MIME-Version: 1.0 References: <1383594685-19332-1-git-send-email-sw@weilnetz.de> <527805D2.3000107@redhat.com> In-Reply-To: <527805D2.3000107@redhat.com> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: quoted-printable Subject: [Qemu-devel] C99 loop vars? [was: gtk: Fix compiler warnings with -Werror=sign-compare] List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , 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 wrote: >>> With -Werror=3Dsign-compare (not enabled by default), gcc shows these= errors: >>> >>> ui/gtk.c: In function =E2=80=98gtk_release_modifiers=E2=80=99: >>> ui/gtk.c:288:19: error: >>> comparison between signed and unsigned integer expressions [-Werror= =3Dsign-compare] >>> ui/gtk.c: In function =E2=80=98gd_key_event=E2=80=99: >>> ui/gtk.c:746:19: error: >>> comparison between signed and unsigned integer expressions [-Werror= =3Dsign-compare] >> >> If this warning is going to complain about entirely >> safe and idiomatic code like >> >> int i; >> static const int some_array[] =3D { >> 0x2a, 0x36, 0x1d, 0x9d, 0x38, 0xb8, 0xdb, 0xdd, >> }; >> >> for (i =3D 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 =3D 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=3Dc99, 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