QEMU_BUILD_BUG_ON: use __COUNTER__
PATCH 1/1 QEMU_BUILD_BUG_ON: use __COUNTER__
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.
--- include/qemu/compiler.h | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-)
include/qemu/compiler.h +5 -1
@@ -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 #define QEMU_BUILD_BUG_ON_ZERO (x) (sizeof(QEMU_BUILD_BUG_ON_STRUCT(x)) - \ sizeof(QEMU_BUILD_BUG_ON_STRUCT(x)))
Signature · 1 line
MST
Quoted 6 lines · Michael S. Tsirkin
Some headers use
QEMU_BUILD_BUG_ON. This causes a problem if the C file including that header happens to haveQEMU_BUILD_BUG_ONat the same line number.Fix using a widely available extension:
__COUNTER__. If unavailable, provide a stub.
Specifically, it was introduced in gcc 4.3.0 (2008), and is in at least clang 3.1 (2012).
thanks
-- PMM
Quoted 5 lines · Michael S. Tsirkin
Some headers use
QEMU_BUILD_BUG_ON. This causes a problem if the C file including that header happens to haveQEMU_BUILD_BUG_ONat the same line number.Fix using a widely available extension:
__COUNTER__.
gcc 4.3 and later; clang documents it but doesn't say when it was added.
Quoted 1 line
If unavailable, provide a stub.
What fails in our build farm if we don't provide the stub? Can we just require that our compiler is new enough to have __COUNTER__, or will that break some of our existing targets?
Quoted 11 lines
Signed-off-by Michael S. Tsirkin <[email protected]>--- include/qemu/compiler.h | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-)include/qemu/compiler.h +0
-0
include/qemu/compiler.h line 92
Quoted 4 lines
include/qemu/compiler.h +1
-0@@ -89,8 +89,12 @@struct { \ int:(x) ? -1 : 1; \ } +#ifdef __COUNTER__
Better would be:
#if defined __COUNTER__ && __COUNTER__ != __COUNTER__
to ensure that it has the semantics we need (that's what gnulib's LGPLv2+ verify.h uses).
include/qemu/compiler.h line 94
Quoted 3 lines
include/qemu/compiler.h +1
-1#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))
But I agree that this is the right change to make.
include/qemu/compiler.h line 96
Quoted 2 lines
include/qemu/compiler.h +2
-0+#else +#define QEMU_BUILD_BUG_ON (x)
This means we lose sanity checks on older compilers. Gnulib documents that on older compilers, you can use:
extern int (*dummy (void)) [sizeof (struct {...})];
coupled with -Wno-redundant-decls, which lets you redeclare the same thing as many times as you want without any use of __LINE__ or __COUNTER__. On the other hand, I don't think we have to bend over backwards for compilers that old (RHEL 6 is at gcc 4.4.7, but 4.3 has __COUNTER__).
Signature · 2 lines
Eric Blake eblake redhat com +1-919-301-3266 Libvirt virtualization library http://libvirt.org
Quoted 7 lines · Eric Blake
Quoted 5 lines · Michael S. Tsirkin
Some headers use
QEMU_BUILD_BUG_ON. This causes a problem if the C file including that header happens to haveQEMU_BUILD_BUG_ONat the same line number.Fix using a widely available extension:
__COUNTER__.
Quoted 7 lines
gcc 4.3 and later; clang documents it but doesn't say when it was added.
Quoted 1 line
If unavailable, provide a stub.
What fails in our build farm if we don't provide the stub? Can we just require that our compiler is new enough to have
__COUNTER__, or will that break some of our existing targets?
I think the problem is not so much our build farm (which is I think all recent enough to postdate introduction of __COUNTER__) but the probability of there being users out there who are still using older compilers to build QEMU releases. We don't need to enforce compile time asserts on those old compilers but given that it's trivial to avoid actually breaking the build for them, why not provide the do-nothing fallback?
thanks
-- PMM
Quoted 16 lines · Peter Maydell
Quoted 7 lines · Eric Blake
Quoted 5 lines · Michael S. Tsirkin
Some headers use
QEMU_BUILD_BUG_ON. This causes a problem if the C file including that header happens to haveQEMU_BUILD_BUG_ONat the same line number.Fix using a widely available extension:
__COUNTER__.Quoted 7 lines
gcc 4.3 and later; clang documents it but doesn't say when it was added.
Quoted 1 line
If unavailable, provide a stub.
What fails in our build farm if we don't provide the stub? Can we just require that our compiler is new enough to have
__COUNTER__, or will that break some of our existing targets?
Quoted 8 lines
I think the problem is not so much our build farm (which is I think all recent enough to postdate introduction of
__COUNTER__) but the probability of there being users out there who are still using older compilers to build QEMU releases. We don't need to enforce compile time asserts on those old compilers but given that it's trivial to avoid actually breaking the build for them, why not provide the do-nothing fallback?
The do-nothing fallback works for me; I'm just debating whether it is dead code or whether we really would have a user building with a compiler that old.
Signature · 2 lines
Eric Blake eblake redhat com +1-919-301-3266 Libvirt virtualization library http://libvirt.org
Quoted 17 lines · Michael S. Tsirkin
Some headers use
QEMU_BUILD_BUG_ON. This causes a problem if the C file including that header happens to haveQEMU_BUILD_BUG_ONat the same line number.Fix using a widely available extension:
__COUNTER__. If unavailable, provide a stub.Signed-off-by Michael S. Tsirkin <[email protected]>--- include/qemu/compiler.h | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-)include/qemu/compiler.h +0
-0
include/qemu/compiler.h line 97
Quoted 10 lines
include/qemu/compiler.h +5
-1@@ -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.
include/qemu/compiler.h line 100
Quoted 3 lines
include/qemu/compiler.h +0
-0#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 BUGGYQEMU_BUILD_BUG_ON(1);
#endif
Quoted 3 lines · Eric Blake
The do-nothing fallback works for me; I'm just debating whether it is dead code or whether we really would have a user building with a compiler that old.
OSX 10.6's clang (whatever that was) is probably the most likely I'd guess.
If we did want to require a newer compiler there are a bunch of QEMU_GNUC_PREREQ(3,4) calls that could be obsoleted. But we should probably do that as a separate thread to get some consensus on what the minimum version should be, and put a helpful check in configure and a line in the release notes and so on...
thanks
-- PMM
Quoted 8 lines · Peter Maydell
Quoted 3 lines · Eric Blake
The do-nothing fallback works for me; I'm just debating whether it is dead code or whether we really would have a user building with a compiler that old.
OSX 10.6's clang (whatever that was) is probably the most likely I'd guess.
Quoted 6 lines
If we did want to require a newer compiler there are a bunch of
QEMU_GNUC_PREREQ(3,4)calls that could be obsoleted. But we should probably do that as a separate thread to get some consensus on what the minimum version should be, and put a helpful check in configure and a line in the release notes and so on...
We already require 4.1 for atomics.
Paolo
Quoted 33 lines · Markus Armbruster
Quoted 17 lines · Michael S. Tsirkin
Some headers use
QEMU_BUILD_BUG_ON. This causes a problem if the C file including that header happens to haveQEMU_BUILD_BUG_ONat the same line number.Fix using a widely available extension:
__COUNTER__. If unavailable, provide a stub.Signed-off-by Michael S. Tsirkin <[email protected]>--- include/qemu/compiler.h | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-)include/qemu/compiler.h +0
-0include/qemu/compiler.h line 97
Quoted 10 lines
include/qemu/compiler.h +5
-1@@ -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).
Quoted 3 lines
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.
Quoted 25 lines
include/qemu/compiler.h line 100
Quoted 3 lines
include/qemu/compiler.h +0
-0#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 BUGGYQEMU_BUILD_BUG_ON(1);
#endif
Quoted 16 lines · Peter Maydell
Quoted 7 lines · Eric Blake
Quoted 5 lines · Michael S. Tsirkin
Some headers use
QEMU_BUILD_BUG_ON. This causes a problem if the C file including that header happens to haveQEMU_BUILD_BUG_ONat the same line number.Fix using a widely available extension:
__COUNTER__.Quoted 7 lines
gcc 4.3 and later; clang documents it but doesn't say when it was added.
Quoted 1 line
If unavailable, provide a stub.
What fails in our build farm if we don't provide the stub? Can we just require that our compiler is new enough to have
__COUNTER__, or will that break some of our existing targets?
Quoted 8 lines
I think the problem is not so much our build farm (which is I think all recent enough to postdate introduction of
__COUNTER__) but the probability of there being users out there who are still using older compilers to build QEMU releases. We don't need to enforce compile time asserts on those old compilers but given that it's trivial to avoid actually breaking the build for them, why not provide the do-nothing fallback?
I don't think we actually need __COUNTER__.
If we want it, providing a fallback is nice. However, the "expand to nothing" fallback is syntactically unsound. A sound fallback expands to the exact same syntactic construct as the real thing does, in this case a declaration. When it doesn't, we risk parse errors.
Of course, we may decide to accept that risk.
Quoted 2 lines · Markus Armbruster
__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).
Since __COUNTER__ is magic it doesn't appear in the -dM output. You can test for it working with
echo 'counter is __COUNTER__ __COUNTER__' | clang -E -x c -
thanks
-- PMM
Quoted 34 lines · Michael S. Tsirkin
Quoted 33 lines · Markus Armbruster
Quoted 17 lines · Michael S. Tsirkin
Some headers use
QEMU_BUILD_BUG_ON. This causes a problem if the C file including that header happens to haveQEMU_BUILD_BUG_ONat the same line number.Fix using a widely available extension:
__COUNTER__. If unavailable, provide a stub.Signed-off-by Michael S. Tsirkin <[email protected]>--- include/qemu/compiler.h | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-)include/qemu/compiler.h +0
-0include/qemu/compiler.h line 97
Quoted 10 lines
include/qemu/compiler.h +5
-1@@ -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).
Quoted 8 lines
Quoted 3 lines
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.
Quoted 1 line · Paolo Bonzini
We already require 4.1 for atomics.
Oh good. I've just sent a patch which gets rid of all the checks for "gcc must be at least X" where X is 4.1 or earlier.
thanks
-- PMM
Quoted 10 lines · Markus Armbruster
Quoted 4 lines
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")
Do we know if pragma GCC diagnostic push is supported in our minimum compiler requirements (gcc 4.1, and presumably clang just ignores the pragma)?
Quoted 6 lines
Drawback: natural use like
QEMU_BUILD_BUG_ON(x);expands into a declaration followed by an extra semicolon.
Can you emit a mid-declaration _Pragma, as in:
extern char ...[...] \_Pragma("GCC diagnostic pop") \
__attribute__((__unused__));
Also, I guess this points out that we are inconsistent on whether we use the __-decorated variants of all __attribute__ arguments; system headers should always do it, since __unused__ is reserved to the implementation but the user is free to #define unused and mess up the parse of the header; but we aren't a system header and it obviously hasn't tripped us up yet.
Signature · 2 lines
Eric Blake eblake redhat com +1-919-301-3266 Libvirt virtualization library http://libvirt.org
Quoted 3 lines · Michael S. Tsirkin
Some headers use
QEMU_BUILD_BUG_ON. This causes a problem if the C file including that header happens to haveQEMU_BUILD_BUG_ONat the same line number.
Do we actually hit this problem in practice ? Even if we do hit the problem, it surely has a trivial workaround of just inserting/removing a blank line somewhere in the file before the QEMU_BUILD_BUG_ON.
IOW, is it really a benefit to change to use COUNTER, given that it is less portable that what we have today ?
Regards,
Daniel
Signature · 3 lines
|: http://berrange.com -o- http://www.flickr.com/photos/dberrange/ :| |: http://libvirt.org -o- http://virt-manager.org :| |: http://entangle-photo.org -o- http://search.cpan.org/~danberr/ :|
Quoted 12 lines · Eric Blake
Quoted 10 lines · Markus Armbruster
Quoted 4 lines
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")
Quoted 3 lines
Do we know if pragma GCC diagnostic push is supported in our minimum compiler requirements (gcc 4.1, and presumably clang just ignores the pragma)?
Nope; that's gcc 4.6 or newer. They're also a bit flaky between gcc and clang because they rely on the warning being the same in both cases. Using them in QEMU_BUILD_BUG_ON would be massively overkill, especially since gcc 4.6 and on support _Static_assert() which will get us better error messages than this stuff.
thanks
-- PMM
Quoted 9 lines · Daniel P. Berrange
Quoted 3 lines · Michael S. Tsirkin
Some headers use
QEMU_BUILD_BUG_ON. This causes a problem if the C file including that header happens to haveQEMU_BUILD_BUG_ONat the same line number.Do we actually hit this problem in practice ? Even if we do hit the problem, it surely has a trivial workaround of just inserting/removing a blank line somewhere in the file before the
QEMU_BUILD_BUG_ON.
Quoted 5 lines
IOW, is it really a benefit to change to use COUNTER, given that it is less portable that what we have today ?
Regards,
Daniel
It failed for me, and I'm not inclined to insert empty lines to work around this for reasons for taste :)
Quoted 4 lines
Signature · 3 lines
|: http://berrange.com -o- http://www.flickr.com/photos/dberrange/ :| |: http://libvirt.org -o- http://virt-manager.org :| |: http://entangle-photo.org -o- http://search.cpan.org/~danberr/ :|
Quoted 27 lines · Markus Armbruster
Quoted 16 lines · Peter Maydell
Quoted 7 lines · Eric Blake
Quoted 5 lines · Michael S. Tsirkin
Some headers use
QEMU_BUILD_BUG_ON. This causes a problem if the C file including that header happens to haveQEMU_BUILD_BUG_ONat the same line number.Fix using a widely available extension:
__COUNTER__.Quoted 7 lines
gcc 4.3 and later; clang documents it but doesn't say when it was added.
Quoted 1 line
If unavailable, provide a stub.
What fails in our build farm if we don't provide the stub? Can we just require that our compiler is new enough to have
__COUNTER__, or will that break some of our existing targets?Quoted 8 lines
I think the problem is not so much our build farm (which is I think all recent enough to postdate introduction of
__COUNTER__) but the probability of there being users out there who are still using older compilers to build QEMU releases. We don't need to enforce compile time asserts on those old compilers but given that it's trivial to avoid actually breaking the build for them, why not provide the do-nothing fallback?
Quoted 1 line
I don't think we actually need
__COUNTER__.
How would you fix the conflict above?
Quoted 5 lines
If we want it, providing a fallback is nice. However, the "expand to nothing" fallback is syntactically unsound. A sound fallback expands to the exact same syntactic construct as the real thing does, in this case a declaration. When it doesn't, we risk parse errors. Of course, we may decide to accept that risk.
That's the best I could do.
Markus Armbruster
Reviewed-byQuoted 11 lines · Daniel P. Berrange
Quoted 3 lines · Michael S. Tsirkin
Some headers use
QEMU_BUILD_BUG_ON. This causes a problem if the C file including that header happens to haveQEMU_BUILD_BUG_ONat the same line number.Do we actually hit this problem in practice ? Even if we do hit the problem, it surely has a trivial workaround of just inserting/removing a blank line somewhere in the file before the
QEMU_BUILD_BUG_ON.IOW, is it really a benefit to change to use COUNTER, given that it is less portable that what we have today ?
__COUNTER__ is the best solution I've seen so far.
The proposed fallback for compilers lacking __COUNTER__ is not quite correct, but it's relatively unlikely to break, and I'm vanishingly unlikely to compile with such an old compiler ever again, so I'm okay with it. I'd also be okay with making __COUNTER__ support a hard requirement.
I guess that means:
Eric Blake
Reviewed-byQuoted 14 lines · Markus Armbruster
Quoted 11 lines · Daniel P. Berrange
Quoted 3 lines · Michael S. Tsirkin
Some headers use
QEMU_BUILD_BUG_ON. This causes a problem if the C file including that header happens to haveQEMU_BUILD_BUG_ONat the same line number.Do we actually hit this problem in practice ? Even if we do hit the problem, it surely has a trivial workaround of just inserting/removing a blank line somewhere in the file before the
QEMU_BUILD_BUG_ON.IOW, is it really a benefit to change to use COUNTER, given that it is less portable that what we have today ?
Quoted 7 lines
__COUNTER__is the best solution I've seen so far.The proposed fallback for compilers lacking
__COUNTER__is not quite correct, but it's relatively unlikely to break, and I'm vanishingly unlikely to compile with such an old compiler ever again, so I'm okay with it. I'd also be okay with making__COUNTER__support a hard requirement.
Making it a hard requirement is as simple as:
#if !defined __COUNTER__ || __COUNTER__ == __COUNTER__ #error Upgrade your compiler
#endif
Quoted 3 lines
I guess that means:
Reviewed-by Markus Armbruster <[email protected]>
Since the v4 pull request was botched, if you'd like to add R-b for the v5 pull request, you can also add
Signature · 2 lines
Eric Blake eblake redhat com +1-919-301-3266 Libvirt virtualization library http://libvirt.org