qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: Eric Blake <eblake@redhat.com>
To: "Michael S. Tsirkin" <mst@redhat.com>,
	Markus Armbruster <armbru@redhat.com>
Cc: Paolo Bonzini <pbonzini@redhat.com>,
	Richard Henderson <rth@twiddle.net>,
	qemu-devel@nongnu.org, Peter Maydell <peter.maydell@linaro.org>
Subject: Re: [Qemu-devel] [PATCH v2 3/4] compiler: expression version of QEMU_BUILD_BUG_ON
Date: Thu, 19 Jan 2017 14:58:48 -0600	[thread overview]
Message-ID: <57dd50a6-73ce-ecfa-4f5e-bca433465e6b@redhat.com> (raw)
In-Reply-To: <20170119212409-mutt-send-email-mst@kernel.org>

[-- Attachment #1: Type: text/plain, Size: 2546 bytes --]

On 01/19/2017 01:25 PM, Michael S. Tsirkin wrote:

>>>>> +#define QEMU_BUILD_BUG_ON_ZERO(x) (sizeof(int[(x) ? -1 : 1]) - sizeof(int))
>>>
>>> Linux here uses:
>>>
>>> #define BUILD_BUG_ON_ZERO(e) (sizeof(struct { int:-!!(e); }))
>>>
>>> and the issue is that sizeof(int[(x) ? -1 : 1]) could be
>>> runtime-evaluated (the type is a variable-length array).

Use of the struct with an int bitfield forces 'e' to be a non-zero
compile-time constant to compile; while [(x) ? -1 : 1] could indeed
compile even if not a compile-time constant.  So I agree that we want to
prefer something that forces x to be a compile-time constant.

>>
>> Let's copy both macros from Linux.
> 
> I prefer our variant, I don't think it's portable to assume that
> sizeof(struct {int:0}) is 0. Besides, Linux code is GPLv2 only and this
> file is 2 or later.

I agree that using the Linux version is problematic from a licensing
perspective, as well as the portability of getting sizeof() to produce
0.  But at least it uses bitfields to force a compile time constant.

Here's a third approach: gnulib has a LGPLv2+ version that does:

====
# define _GL_VERIFY_TYPE(R, DIAGNOSTIC) \
    struct { unsigned int _gl_verify_error_if_negative: (R) ? 1 : -1; }

#define _GL_VERIFY_TRUE(R, DIAGNOSTIC) \
   (!!sizeof (_GL_VERIFY_TYPE (R, DIAGNOSTIC)))

/* Verify requirement R at compile-time, as a declaration without a
   trailing ';'.  If R is false, fail at compile-time, preferably
   with a diagnostic that includes the string-literal DIAGNOSTIC.

   Unfortunately, unlike C11, this implementation must appear as an
   ordinary declaration, and cannot appear inside struct { ... }.  */

#ifdef _GL_HAVE__STATIC_ASSERT
# define _GL_VERIFY _Static_assert
#else
# define _GL_VERIFY(R, DIAGNOSTIC)                                     \
    extern int (*_GL_GENSYM (_gl_verify_function) (void))              \
      [_GL_VERIFY_TRUE (R, DIAGNOSTIC)]
#endif

/* Verify requirement R at compile-time.  Return the value of the
   expression E.  */

#define verify_expr(R, E) \
   (_GL_VERIFY_TRUE (R, "verify_expr (" #R ", " #E ")") ? (E) : (E))

/* Verify requirement R at compile-time, as a declaration without a
   trailing ';'.  */

#define verify(R) _GL_VERIFY (R, "verify (" #R ")")
====

where BUILD_BUG_ON_ZERO(e) pretty much maps to verify_expr(!(e), 0), if
you want to copy any of those ideas.

-- 
Eric Blake   eblake redhat com    +1-919-301-3266
Libvirt virtualization library http://libvirt.org


[-- Attachment #2: OpenPGP digital signature --]
[-- Type: application/pgp-signature, Size: 604 bytes --]

  reply	other threads:[~2017-01-19 20:58 UTC|newest]

Thread overview: 25+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-01-18 20:55 [Qemu-devel] [PATCH v2 0/4] virtio: ARRAY_SIZE fixups Michael S. Tsirkin
2017-01-18 20:55 ` [Qemu-devel] [PATCH v2 1/4] virtio: fix up max size checks Michael S. Tsirkin
2017-01-19  9:46   ` Cornelia Huck
2017-01-18 20:55 ` [Qemu-devel] [PATCH v2 2/4] compiler: drop ; after BUILD_BUG_ON Michael S. Tsirkin
2017-01-18 21:04   ` Peter Maydell
2017-01-18 21:16     ` Michael S. Tsirkin
2017-01-18 21:23       ` Michael S. Tsirkin
2017-01-18 21:53         ` Peter Maydell
2017-01-19  8:09   ` Markus Armbruster
2017-01-18 20:55 ` [Qemu-devel] [PATCH v2 3/4] compiler: expression version of QEMU_BUILD_BUG_ON Michael S. Tsirkin
2017-01-19  8:12   ` Markus Armbruster
2017-01-19 10:01     ` Paolo Bonzini
2017-01-19 13:33       ` Markus Armbruster
2017-01-19 19:25         ` Michael S. Tsirkin
2017-01-19 20:58           ` Eric Blake [this message]
2017-01-19 21:02             ` Michael S. Tsirkin
2017-01-20  7:21           ` Markus Armbruster
2017-01-20 17:50             ` Michael S. Tsirkin
2017-01-19 21:01         ` Michael S. Tsirkin
2017-01-18 20:55 ` [Qemu-devel] [PATCH v2 4/4] ARRAY_SIZE: check that argument is an array Michael S. Tsirkin
2017-01-19  8:20   ` Markus Armbruster
2017-01-19 11:00     ` Peter Maydell
2017-01-19 14:53   ` Eric Blake
2017-01-19 15:06     ` Michael S. Tsirkin
2017-01-18 21:06 ` [Qemu-devel] [PATCH v2 0/4] virtio: ARRAY_SIZE fixups no-reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=57dd50a6-73ce-ecfa-4f5e-bca433465e6b@redhat.com \
    --to=eblake@redhat.com \
    --cc=armbru@redhat.com \
    --cc=mst@redhat.com \
    --cc=pbonzini@redhat.com \
    --cc=peter.maydell@linaro.org \
    --cc=qemu-devel@nongnu.org \
    --cc=rth@twiddle.net \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).