From nobody Sat May 4 21:34:58 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) client-ip=208.118.235.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zoho.com; spf=pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; Return-Path: Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) by mx.zohomail.com with SMTPS id 1489510864258373.37341061662335; Tue, 14 Mar 2017 10:01:04 -0700 (PDT) Received: from localhost ([::1]:60795 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1cnpog-0004jA-8p for importer@patchew.org; Tue, 14 Mar 2017 13:00:58 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:50349) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1cnpoA-0004hH-6C for qemu-devel@nongnu.org; Tue, 14 Mar 2017 13:00:27 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1cnpo7-00041U-29 for qemu-devel@nongnu.org; Tue, 14 Mar 2017 13:00:26 -0400 Received: from www145.your-server.de ([78.47.15.66]:40203) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1cnpnx-0003wr-Ao; Tue, 14 Mar 2017 13:00:13 -0400 Received: from [88.198.220.130] (helo=sslproxy01.your-server.de) by www145.your-server.de with esmtpsa (TLSv1.2:DHE-RSA-AES256-GCM-SHA384:256) (Exim 4.85_2) (envelope-from ) id 1cnpnv-0000Av-If; Tue, 14 Mar 2017 18:00:11 +0100 Received: from [141.89.226.146] (helo=parabola-pocket.hpi.uni-potsdam.de) by sslproxy01.your-server.de with esmtpsa (TLSv1.2:DHE-RSA-AES128-GCM-SHA256:128) (Exim 4.84_2) (envelope-from ) id 1cnpnv-0001SU-BN; Tue, 14 Mar 2017 18:00:11 +0100 From: Andreas Grapentin To: qemu-devel@nongnu.org Date: Tue, 14 Mar 2017 17:59:53 +0100 Message-Id: <20170314165953.18506-1-andreas@grapentin.org> X-Mailer: git-send-email 2.12.0 MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable X-Authenticated-Sender: andreas@grapentin.org X-Virus-Scanned: Clear (ClamAV 0.99.2/23203/Tue Mar 14 13:13:21 2017) X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x (no timestamps) [generic] [fuzzy] X-Received-From: 78.47.15.66 Subject: [Qemu-devel] [PATCH v2] use _Static_assert in QEMU_BUILD_BUG_ON X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: qemu-trivial@nongnu.org, Andreas Grapentin Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail: RSF_0 Z_629925259 SPT_0 QEMU_BUILD_BUG_ON should use C11's _Static_assert, if the compiler supports= it, to provide more readable messages on failure. We check for _Static_assert in configure, and set CONFIG_STATIC_ASSERT accordingly. QEMU_BUILD_BUG_ON invokes _Static_assert if CONFIG_STATIC_ASSE= RT is defined, and reverts to the old way otherwise. That way, systems without C11 conforming compiler will still have the old messages, as verified by intentionally breaking the configure check. the following example output was generated by inverting the condition in QEMU_BUILD_BUG_ON: without _Static_assert: > In file included from /qemu/include/qemu/osdep.h:36:0, > from /qemu/qga/commands.c:13: > /qemu/qga/commands.c: In function =E2=80=98qmp_guest_exec_status=E2=80=99: > /qemu/include/qemu/compiler.h:89:12: error: negative width in bit-field = =E2=80=98=E2=80=99 > struct { \ > ^ > /qemu/include/qemu/compiler.h:96:38: note: in expansion of macro QEMU_BU= ILD_BUG_ON_STRUCT=E2=80=99 > #define QEMU_BUILD_BUG_ON(x) typedef QEMU_BUILD_BUG_ON_STRUCT(x) \ > ^~~~~~~~~~~~~~~~~~~~~~~~ > /qemu/include/qemu/atomic.h:146:5: note: in expansion of macro =E2=80=98Q= EMU_BUILD_BUG_ON=E2=80=99 > QEMU_BUILD_BUG_ON(sizeof(*ptr) > sizeof(void *)); \ > ^~~~~~~~~~~~~~~~~ > /qemu/include/qemu/atomic.h:417:5: note: in expansion of macro =E2=80=98a= tomic_load_acquire=E2=80=99 > atomic_load_acquire(ptr) > ^~~~~~~~~~~~~~~~~~~ > /qemu/qga/commands.c:160:21: note: in expansion of macro =E2=80=98atomic_= mb_read=E2=80=99 > bool finished =3D atomic_mb_read(&gei->finished); > ^~~~~~~~~~~~~~ with _Static_assert: > In file included from /qemu/include/qemu/osdep.h:36:0, > from /qemu/qga/commands.c:13: > /qemu/qga/commands.c: In function =E2=80=98qmp_guest_exec_status=E2=80=99: > /qemu/include/qemu/compiler.h:94:30: error: static assertion failed: "not= expecting: sizeof(*&gei->finished) > sizeof(void *)" > #define QEMU_BUILD_BUG_ON(x) _Static_assert((x), #x) > ^ > /qemu/include/qemu/atomic.h:146:5: note: in expansion of macro =E2=80=98Q= EMU_BUILD_BUG_ON=E2=80=99 > QEMU_BUILD_BUG_ON(sizeof(*ptr) > sizeof(void *)); \ > ^~~~~~~~~~~~~~~~~ > /qemu/include/qemu/atomic.h:417:5: note: in expansion of macro =E2=80=98a= tomic_load_acquire=E2=80=99 > atomic_load_acquire(ptr) > ^~~~~~~~~~~~~~~~~~~ > /qemu/qga/commands.c:160:21: note: in expansion of macro =E2=80=98atomic_= mb_read=E2=80=99 > bool finished =3D atomic_mb_read(&gei->finished); > ^~~~~~~~~~~~~~ Signed-off-by: Andreas Grapentin Reviewed-by: Eric Blake Reviewed-by: Richard Henderson --- configure | 18 ++++++++++++++++++ include/qemu/compiler.h | 4 +++- 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/configure b/configure index 75c7c3526c..e9b33d9cf8 100755 --- a/configure +++ b/configure @@ -4725,6 +4725,20 @@ if compile_prog "" "" ; then fi =20 ########################################## +# check for _Static_assert() + +have_static_assert=3Dno +cat > $TMPC << EOF +_Static_assert(1, "success"); +int main(void) { + return 0; +} +EOF +if compile_prog "" "" ; then + have_static_assert=3Dyes +fi + +########################################## # End of CC checks # After here, no more $cc or $ld runs =20 @@ -5694,6 +5708,10 @@ if test "$have_sysmacros" =3D "yes" ; then echo "CONFIG_SYSMACROS=3Dy" >> $config_host_mak fi =20 +if test "$have_static_assert" =3D "yes" ; then + echo "CONFIG_STATIC_ASSERT=3Dy" >> $config_host_mak +fi + # Hold two types of flag: # CONFIG_THREAD_SETNAME_BYTHREAD - we've got a way of setting the name = on # a thread we have a handle to diff --git a/include/qemu/compiler.h b/include/qemu/compiler.h index e0ce9ffb28..37a65d4bb7 100644 --- a/include/qemu/compiler.h +++ b/include/qemu/compiler.h @@ -90,7 +90,9 @@ int:(x) ? -1 : 1; \ } =20 -#ifdef __COUNTER__ +#if defined(CONFIG_STATIC_ASSERT) +#define QEMU_BUILD_BUG_ON(x) _Static_assert(!(x), "not expecting: " #x) +#elif defined(__COUNTER__) #define QEMU_BUILD_BUG_ON(x) typedef QEMU_BUILD_BUG_ON_STRUCT(x) \ glue(qemu_build_bug_on__, __COUNTER__) __attribute__((unused)) #else --=20 2.12.0