All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] utilize _Static_assert() for BUILD_BUG_ON() when the compiler supports it
@ 2012-11-02 14:47 Jan Beulich
  2012-11-05  2:19 ` Rusty Russell
  2012-11-05 22:29 ` Andrew Morton
  0 siblings, 2 replies; 13+ messages in thread
From: Jan Beulich @ 2012-11-02 14:47 UTC (permalink / raw)
  To: akpm, rusty; +Cc: linux-kernel

This makes the resulting diagnostics quite a bit more useful.

Signed-off-by: Jan Beulich <jbeulich@suse.com>

---
 include/linux/bug.h |   16 ++++++++++++++++
 1 file changed, 16 insertions(+)

--- 3.7-rc3/include/linux/bug.h
+++ 3.7-rc3-static-assert/include/linux/bug.h
@@ -27,8 +27,15 @@ struct pt_regs;
    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). */
+#if __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6)
+#define BUILD_BUG_ON_ZERO(e) \
+	sizeof(struct { _Static_assert(!(e), "!(" #e ")"); })
+#define BUILD_BUG_ON_NULL(e) \
+	((void *)sizeof(struct { _Static_assert(!(e), "!(" #e ")"); }))
+#else
 #define BUILD_BUG_ON_ZERO(e) (sizeof(struct { int:-!!(e); }))
 #define BUILD_BUG_ON_NULL(e) ((void *)sizeof(struct { int:-!!(e); }))
+#endif
 
 /*
  * BUILD_BUG_ON_INVALID() permits the compiler to check the validity of the
@@ -54,6 +61,15 @@ struct pt_regs;
  */
 #ifndef __OPTIMIZE__
 #define BUILD_BUG_ON(condition) ((void)sizeof(char[1 - 2*!!(condition)]))
+#elif __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 3)
+#define __build_bug_on_failed(n) __build_bug_on_##n##_failed
+#define _BUILD_BUG_ON(n, condition)				\
+	do {							\
+		extern void __compiletime_error(#condition)	\
+		__build_bug_on_failed(n)(void);			\
+		if (condition) __build_bug_on_failed(n)();	\
+	} while(0)
+#define BUILD_BUG_ON(condition...) _BUILD_BUG_ON(__COUNTER__, ##condition)
 #else
 extern int __build_bug_on_failed;
 #define BUILD_BUG_ON(condition)					\




^ permalink raw reply	[flat|nested] 13+ messages in thread

end of thread, other threads:[~2012-12-13 21:20 UTC | newest]

Thread overview: 13+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2012-11-02 14:47 [PATCH] utilize _Static_assert() for BUILD_BUG_ON() when the compiler supports it Jan Beulich
2012-11-05  2:19 ` Rusty Russell
2012-11-05  8:46   ` Jan Beulich
2012-11-05 22:29 ` Andrew Morton
2012-11-06  1:51   ` Rusty Russell
2012-11-06  9:23     ` Jan Beulich
2012-11-07  1:03       ` Rusty Russell
2012-11-07  8:05         ` Jan Beulich
2012-11-07 23:24           ` Rusty Russell
2012-12-13  0:29             ` Daniel Santos
2012-12-13  9:43               ` Jan Beulich
2012-12-13 21:20                 ` Daniel Santos
2012-12-13  0:48       ` Daniel Santos

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.