[PATCH 24/45] C++: Fix BUILD_BUG_ON_ZERO()

David Howells
2018-04-01T20:42:44Z

Fix BUILD_BUG_ON_ZERO() to work in C++. The construction format:

(struct { ... })

is something C++ doesn't like.

Signed-off-by: David Howells <dhowells@redhat.com>
---

include/linux/build_bug.h | 5 ++++-
1 file changed, 4 insertions(+), 1 deletion(-)

diff --git a/include/linux/build_bug.h b/include/linux/build_bug.h
index 43d1fd50d433..7445385cd26f 100644
--- a/include/linux/build_bug.h
+++ b/include/linux/build_bug.h
@@ -20,13 +20,16 @@
#define BUILD_BUG_ON_NOT_POWER_OF_2(n) \
BUILD_BUG_ON((n) == 0 || (((n) & ((n) - 1)) != 0))

+extern int __build_bug_on_zero(void)
+ __compiletime_error("Build bug on zero");
+
/*
* Force a compilation error if condition is true, but also produce a
* result (of value 0 and type size_t), so the expression can be used
* e.g. in a structure initializer (or where-ever else comma expressions
* aren't permitted).
*/
-#define BUILD_BUG_ON_ZERO(e) (sizeof(struct { int:(-!!(e)); }))
+#define BUILD_BUG_ON_ZERO(e) ((e) ? 0 : __build_bug_on_zero())

/*
* BUILD_BUG_ON_INVALID() permits the compiler to check the validity of the