From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from eggs.gnu.org ([2001:4830:134:3::10]:60250) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1cYaN4-0003TK-AZ for qemu-devel@nongnu.org; Tue, 31 Jan 2017 10:29:27 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1cYaMz-0004Ef-E4 for qemu-devel@nongnu.org; Tue, 31 Jan 2017 10:29:26 -0500 Received: from mx1.redhat.com ([209.132.183.28]:55240) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1cYaMz-0004Ea-8D for qemu-devel@nongnu.org; Tue, 31 Jan 2017 10:29:21 -0500 Date: Tue, 31 Jan 2017 17:29:15 +0200 From: "Michael S. Tsirkin" Message-ID: <20170131172752-mutt-send-email-mst@kernel.org> References: <1485873796-15095-1-git-send-email-mst@redhat.com> <87zii75jze.fsf@dusky.pond.sub.org> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <87zii75jze.fsf@dusky.pond.sub.org> Subject: Re: [Qemu-devel] [PATCH] QEMU_BUILD_BUG_ON: use __COUNTER__ List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: Markus Armbruster Cc: qemu-devel@nongnu.org, Peter Maydell , Paolo Bonzini On Tue, Jan 31, 2017 at 04:26:13PM +0100, Markus Armbruster wrote: > "Michael S. Tsirkin" 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 > > --- > > 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