[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Xen-devel] [PATCH XTF 1/4] build: Support BUILD_BUG_ON() with compilers lacking _Static_assert()



Implement enough compatibility so the code can use Clang's __has_extension()
logic when compiled with GCC.

Reported-by: Glenn Enright <glenn@xxxxxxxxxxxxxxx>
Signed-off-by: Andrew Cooper <andrew.cooper3@xxxxxxxxxx>
---
 include/xtf/compiler-gcc.h | 31 +++++++++++++++++++++++++++++++
 include/xtf/compiler.h     |  4 ++++
 include/xtf/lib.h          |  7 ++++++-
 3 files changed, 41 insertions(+), 1 deletion(-)
 create mode 100644 include/xtf/compiler-gcc.h

diff --git a/include/xtf/compiler-gcc.h b/include/xtf/compiler-gcc.h
new file mode 100644
index 0000000..2d3bc89
--- /dev/null
+++ b/include/xtf/compiler-gcc.h
@@ -0,0 +1,31 @@
+#ifndef XTF_COMPILER_GCC_H
+#define XTF_COMPILER_GCC_H
+
+#define GCC_VER (__GNUC__ * 10000 +            \
+                __GNUC_MINOR__ * 100 +         \
+                __GNUC_PATCHLEVEL__)
+
+/*
+ * The Clang __has_*() infrastructure is a very clean way to identify
+ * compiler support, without resorting to version checks.  Fake up
+ * enough support for XTF code to use, even on non-clang compilers.
+ */
+
+#ifndef __has_extension
+
+#define GCC_HAS_c_static_assert (GCC_VER >= 40600) /* _Static_assert() */
+
+#define __has_extension(x) GCC_HAS_ ## x
+#endif /* __has_extension */
+
+#endif /* XTF_COMPILER_GCC_H */
+
+/*
+ * Local variables:
+ * mode: C
+ * c-file-style: "BSD"
+ * c-basic-offset: 4
+ * tab-width: 4
+ * indent-tabs-mode: nil
+ * End:
+ */
diff --git a/include/xtf/compiler.h b/include/xtf/compiler.h
index 9dd6734..aa5fd6b 100644
--- a/include/xtf/compiler.h
+++ b/include/xtf/compiler.h
@@ -1,6 +1,10 @@
 #ifndef XTF_COMPILER_H
 #define XTF_COMPILER_H
 
+#ifdef __GNUC__
+#include <xtf/compiler-gcc.h>
+#endif
+
 #define __alias(x)            __attribute__((__alias__(x)))
 #define __aligned(x)          __attribute__((__aligned__(x)))
 #define __noreturn            __attribute__((__noreturn__))
diff --git a/include/xtf/lib.h b/include/xtf/lib.h
index d792a8d..abf8f25 100644
--- a/include/xtf/lib.h
+++ b/include/xtf/lib.h
@@ -28,8 +28,13 @@ void __noreturn panic(const char *fmt, ...) __printf(1, 2);
                   #cond, __FILE__, __LINE__);           \
     } while ( 0 )
 
-#define BUILD_BUG_ON(cond)                              \
+#if __has_extension(c_static_assert)
+# define BUILD_BUG_ON(cond)                             \
     _Static_assert(!(cond), "!(" #cond ")")
+#else
+# define BUILD_BUG_ON(cond)                             \
+    ((void)sizeof(struct { char: -!!(cond); }))
+#endif
 
 #define min(a, b)                                       \
     ({                                                  \
-- 
2.1.4


_______________________________________________
Xen-devel mailing list
Xen-devel@xxxxxxxxxxxxx
https://lists.xen.org/xen-devel

 


Rackspace

Lists.xenproject.org is hosted with RackSpace, monitoring our
servers 24x7x365 and backed by RackSpace's Fanatical Support®.