[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [Qemu-devel] [PATCH] QEMU_BUILD_BUG_ON: use __COUNTER__
From: |
Markus Armbruster |
Subject: |
Re: [Qemu-devel] [PATCH] QEMU_BUILD_BUG_ON: use __COUNTER__ |
Date: |
Tue, 31 Jan 2017 17:07:10 +0100 |
User-agent: |
Gnus/5.13 (Gnus v5.13) Emacs/25.1 (gnu/linux) |
"Michael S. Tsirkin" <address@hidden> writes:
> On Tue, Jan 31, 2017 at 04:26:13PM +0100, Markus Armbruster wrote:
>> "Michael S. Tsirkin" <address@hidden> 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 <address@hidden>
>> > ---
>> > 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(x) \
_Pragma("GCC diagnostic push") \
_Pragma("GCC diagnostic ignored \"-Wredundant-decls\"") \
extern char qemu_build_bug_on_[QEMU_BUILD_BUG_ON_ZERO((x))]
__attribute__((unused)); \
_Pragma("GCC diagnostic pop")
Drawback: natural use like
QEMU_BUILD_BUG_ON(x);
expands into a declaration followed by an extra semicolon.
Re: [Qemu-devel] [PATCH] QEMU_BUILD_BUG_ON: use __COUNTER__, Markus Armbruster, 2017/01/31
Re: [Qemu-devel] [PATCH] QEMU_BUILD_BUG_ON: use __COUNTER__, Peter Maydell, 2017/01/31
Re: [Qemu-devel] [PATCH] QEMU_BUILD_BUG_ON: use __COUNTER__, Daniel P. Berrange, 2017/01/31