From: Andrew Cooper Date: Thu, 28 Sep 2017 09:22:46 +0000 (+0100) Subject: build: Support BUILD_BUG_ON() with compilers lacking _Static_assert() X-Git-Url: http://xenbits.xensource.com/gitweb?a=commitdiff_plain;h=2d84064ef83da678cfb5b1d217ff42cbf952e5ac;p=people%2Fandrewcoop%2Fxen-test-framework.git 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 Signed-off-by: Andrew Cooper --- diff --git a/include/xtf/compiler-gcc.h b/include/xtf/compiler-gcc.h new file mode 100644 index 0000000..f6f76c4 --- /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 +#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) \ ({ \