qemu-devel
[Top][All Lists]
Advanced

[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 16:26:13 +0100
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/25.1 (gnu/linux)

"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.

>  
>  #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



reply via email to

[Prev in Thread] Current Thread [Next in Thread]