From nobody Thu Nov 6 03:04:48 2025 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 1493979707614308.90922206374694; Fri, 5 May 2017 03:21:47 -0700 (PDT) Received: from localhost ([::1]:46075 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1d6aMs-0000Tz-7p for importer@patchew.org; Fri, 05 May 2017 06:21:46 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:47039) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1d6aFA-0002E9-L0 for qemu-devel@nongnu.org; Fri, 05 May 2017 06:13:49 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1d6aF9-0006c7-Ee for qemu-devel@nongnu.org; Fri, 05 May 2017 06:13:48 -0400 Received: from mx1.redhat.com ([209.132.183.28]:49516) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1d6aF9-0006bw-6A for qemu-devel@nongnu.org; Fri, 05 May 2017 06:13:47 -0400 Received: from int-mx10.intmail.prod.int.phx2.redhat.com (int-mx10.intmail.prod.int.phx2.redhat.com [10.5.11.23]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 173408050D; Fri, 5 May 2017 10:13:46 +0000 (UTC) Received: from donizetti.redhat.com (ovpn-116-145.ams2.redhat.com [10.36.116.145]) by int-mx10.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id v45ADbkM026855; Fri, 5 May 2017 06:13:44 -0400 DMARC-Filter: OpenDMARC Filter v1.3.2 mx1.redhat.com 173408050D Authentication-Results: ext-mx03.extmail.prod.ext.phx2.redhat.com; dmarc=none (p=none dis=none) header.from=redhat.com Authentication-Results: ext-mx03.extmail.prod.ext.phx2.redhat.com; spf=pass smtp.mailfrom=pbonzini@redhat.com DKIM-Filter: OpenDKIM Filter v2.11.0 mx1.redhat.com 173408050D From: Paolo Bonzini To: qemu-devel@nongnu.org Date: Fri, 5 May 2017 12:13:21 +0200 Message-Id: <20170505101337.4650-6-pbonzini@redhat.com> In-Reply-To: <20170505101337.4650-1-pbonzini@redhat.com> References: <20170505101337.4650-1-pbonzini@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.68 on 10.5.11.23 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.27]); Fri, 05 May 2017 10:13:46 +0000 (UTC) Content-Transfer-Encoding: quoted-printable X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] [fuzzy] X-Received-From: 209.132.183.28 Subject: [Qemu-devel] [PULL 05/21] 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: Andreas Grapentin Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Type: text/plain; charset="utf-8" From: Andreas Grapentin 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 =C3=A2=E2=82=AC=CB=9Cqmp_guest_exec_sta= tus=C3=A2=E2=82=AC=E2=84=A2: > /qemu/include/qemu/compiler.h:89:12: error: negative width in bit-field = =C3=A2=E2=82=AC=CB=9C=C3=A2=E2=82=AC=E2=84=A2 > struct { \ > ^ > /qemu/include/qemu/compiler.h:96:38: note: in expansion of macro QEMU_BU= ILD_BUG_ON_STRUCT=C3=A2=E2=82=AC=E2=84=A2 > #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 =C3=A2=E2= =82=AC=CB=9CQEMU_BUILD_BUG_ON=C3=A2=E2=82=AC=E2=84=A2 > QEMU_BUILD_BUG_ON(sizeof(*ptr) > sizeof(void *)); \ > ^~~~~~~~~~~~~~~~~ > /qemu/include/qemu/atomic.h:417:5: note: in expansion of macro =C3=A2=E2= =82=AC=CB=9Catomic_load_acquire=C3=A2=E2=82=AC=E2=84=A2 > atomic_load_acquire(ptr) > ^~~~~~~~~~~~~~~~~~~ > /qemu/qga/commands.c:160:21: note: in expansion of macro =C3=A2=E2=82=AC= =CB=9Catomic_mb_read=C3=A2=E2=82=AC=E2=84=A2 > 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 =C3=A2=E2=82=AC=CB=9Cqmp_guest_exec_sta= tus=C3=A2=E2=82=AC=E2=84=A2: > /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 =C3=A2=E2= =82=AC=CB=9CQEMU_BUILD_BUG_ON=C3=A2=E2=82=AC=E2=84=A2 > QEMU_BUILD_BUG_ON(sizeof(*ptr) > sizeof(void *)); \ > ^~~~~~~~~~~~~~~~~ > /qemu/include/qemu/atomic.h:417:5: note: in expansion of macro =C3=A2=E2= =82=AC=CB=9Catomic_load_acquire=C3=A2=E2=82=AC=E2=84=A2 > atomic_load_acquire(ptr) > ^~~~~~~~~~~~~~~~~~~ > /qemu/qga/commands.c:160:21: note: in expansion of macro =C3=A2=E2=82=AC= =CB=9Catomic_mb_read=C3=A2=E2=82=AC=E2=84=A2 > bool finished =3D atomic_mb_read(&gei->finished); > ^~~~~~~~~~~~~~ Signed-off-by: Andreas Grapentin Message-Id: <20170314165953.18506-1-andreas@grapentin.org> Signed-off-by: Paolo Bonzini --- configure | 18 ++++++++++++++++++ include/qemu/compiler.h | 4 +++- 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/configure b/configure index ff2c81f2c7..7c020c076b 100755 --- a/configure +++ b/configure @@ -4853,6 +4853,20 @@ EOF 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 @@ -5848,6 +5862,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 18e610083a..340e5fdc09 100644 --- a/include/qemu/compiler.h +++ b/include/qemu/compiler.h @@ -82,7 +82,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.2