From: "Michael S. Tsirkin" <mst@redhat.com>
To: Markus Armbruster <armbru@redhat.com>
Cc: qemu-devel@nongnu.org, Peter Maydell <peter.maydell@linaro.org>,
Paolo Bonzini <pbonzini@redhat.com>
Subject: Re: [Qemu-devel] [PATCH] QEMU_BUILD_BUG_ON: use __COUNTER__
Date: Tue, 31 Jan 2017 17:29:15 +0200 [thread overview]
Message-ID: <20170131172752-mutt-send-email-mst@kernel.org> (raw)
In-Reply-To: <87zii75jze.fsf@dusky.pond.sub.org>
On Tue, Jan 31, 2017 at 04:26:13PM +0100, Markus Armbruster wrote:
> "Michael S. Tsirkin" <mst@redhat.com> writes:
>
> > Some headers use QEMU_BUILD_BUG_ON. This causes a problem
> > if the C file including that header happens to have
> > QEMU_BUILD_BUG_ON at the same line number.
> >
> > Fix using a widely available extension: __COUNTER__.
> > If unavailable, provide a stub.
> >
> > Signed-off-by: Michael S. Tsirkin <mst@redhat.com>
> > ---
> > include/qemu/compiler.h | 6 +++++-
> > 1 file changed, 5 insertions(+), 1 deletion(-)
> >
> > diff --git a/include/qemu/compiler.h b/include/qemu/compiler.h
> > index e0fb18b..bad25a9 100644
> > --- a/include/qemu/compiler.h
> > +++ b/include/qemu/compiler.h
> > @@ -89,8 +89,12 @@
> > struct { \
> > int:(x) ? -1 : 1; \
> > }
> > +#ifdef __COUNTER__
> > #define QEMU_BUILD_BUG_ON(x) typedef QEMU_BUILD_BUG_ON_STRUCT(x) \
> > - glue(qemu_build_bug_on__, __LINE__) __attribute__((unused))
> > + glue(qemu_build_bug_on__, __COUNTER__) __attribute__((unused))
> > +#else
> > +#define QEMU_BUILD_BUG_ON(x)
> > +#endif
>
> __COUNTER__ was added to GNU cpp in 2007. Good. What about clang?
> "clang -dM -E -x c /dev/null" comes up empty on my machine (3.8.0).
>
> Can we use an extern declaration instead? You can have any number of
> these, as long as they match. Have a look at the appended sketch. It
> compiles without -DBUGGY, and errors out with -DBUGGY, as it should.
I tried that first thing.
This generates lots of warnings if you have multiple users
within a function: gcc is unhappy about the redundant extern
declarations.
> >
> > #define QEMU_BUILD_BUG_ON_ZERO(x) (sizeof(QEMU_BUILD_BUG_ON_STRUCT(x)) - \
> > sizeof(QEMU_BUILD_BUG_ON_STRUCT(x)))
>
> By the way, QEMU_BUILD_BUG_ON_ZERO() could use a comment explaining its
> value.
>
>
>
> #define QEMU_BUILD_BUG_ON_STRUCT(x) \
> struct { \
> int:(x) ? -1 : 1; \
> }
>
> #define QEMU_BUILD_BUG_ON_ZERO(x) (sizeof(QEMU_BUILD_BUG_ON_STRUCT(x)) - \
> sizeof(QEMU_BUILD_BUG_ON_STRUCT(x)))
>
> #define QEMU_BUILD_BUG_ON(x) \
> extern char qemu_build_bug_on_[QEMU_BUILD_BUG_ON_ZERO((x))]
>
> QEMU_BUILD_BUG_ON(0);
> QEMU_BUILD_BUG_ON(0);
> #ifdef BUGGY
> QEMU_BUILD_BUG_ON(1);
> #endif
next prev parent reply other threads:[~2017-01-31 15:29 UTC|newest]
Thread overview: 20+ messages / expand[flat|nested] mbox.gz Atom feed top
2017-01-31 14:43 [Qemu-devel] [PATCH] QEMU_BUILD_BUG_ON: use __COUNTER__ Michael S. Tsirkin
2017-01-31 15:02 ` Peter Maydell
2017-01-31 15:03 ` Eric Blake
2017-01-31 15:12 ` Peter Maydell
2017-01-31 15:16 ` Eric Blake
2017-01-31 15:26 ` Peter Maydell
2017-01-31 15:28 ` Paolo Bonzini
2017-01-31 16:17 ` Peter Maydell
2017-01-31 15:34 ` Markus Armbruster
2017-01-31 17:58 ` Michael S. Tsirkin
2017-01-31 15:26 ` Markus Armbruster
2017-01-31 15:29 ` Michael S. Tsirkin [this message]
2017-01-31 16:07 ` Markus Armbruster
2017-01-31 16:37 ` Eric Blake
2017-01-31 16:54 ` Peter Maydell
2017-01-31 15:47 ` Peter Maydell
2017-01-31 16:50 ` Daniel P. Berrange
2017-01-31 17:56 ` Michael S. Tsirkin
2017-01-31 18:43 ` Markus Armbruster
2017-01-31 18:58 ` Eric Blake
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=20170131172752-mutt-send-email-mst@kernel.org \
--to=mst@redhat.com \
--cc=armbru@redhat.com \
--cc=pbonzini@redhat.com \
--cc=peter.maydell@linaro.org \
--cc=qemu-devel@nongnu.org \
/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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.