]> xenbits.xensource.com Git - people/andrewcoop/xen-test-framework.git/commitdiff
build: Support BUILD_BUG_ON() with compilers lacking _Static_assert()
authorAndrew Cooper <andrew.cooper3@citrix.com>
Thu, 28 Sep 2017 09:22:46 +0000 (10:22 +0100)
committerAndrew Cooper <andrew.cooper3@citrix.com>
Fri, 29 Sep 2017 14:34:49 +0000 (15:34 +0100)
Implement enough compatibility so the code can use Clang's __has_extension()
logic when compiled with GCC.

Reported-by: Glenn Enright <glenn@rimuhosting.com>
Signed-off-by: Andrew Cooper <andrew.cooper3@citrix.com>
include/xtf/compiler-gcc.h [new file with mode: 0644]
include/xtf/compiler.h
include/xtf/lib.h

diff --git a/include/xtf/compiler-gcc.h b/include/xtf/compiler-gcc.h
new file mode 100644 (file)
index 0000000..f6f76c4
--- /dev/null
@@ -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:
+ */
index 9dd6734dcd133b16cf8bf7b19617d52a1ac250f6..aa5fd6b53c862cd64e5c7fca155ccbd649e3de1f 100644 (file)
@@ -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__))
index d792a8d853fd3b7eccb9b3c1019d595cc185e8be..abf8f2534417db76ece8c8781fb726ce3235b81e 100644 (file)
@@ -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)                                       \
     ({                                                  \