From 8efede20efbaaeef8dc9d2289d6cc0add48b56e0 Mon Sep 17 00:00:00 2001 From: Lucas De Marchi Date: Mon, 15 Apr 2013 14:09:05 -0300 Subject: [PATCH] Use _Static_assert Both GCC and clang already supports C11's _Static_assert, so use it instead of defining our own macro. --- libkmod/macro.h | 36 +++++++----------------------------- 1 file changed, 7 insertions(+), 29 deletions(-) diff --git a/libkmod/macro.h b/libkmod/macro.h index f09bb2b..c6ba855 100644 --- a/libkmod/macro.h +++ b/libkmod/macro.h @@ -21,25 +21,16 @@ #include -#define BUILD_ASSERT(cond) \ - do { (void) sizeof(char [1 - 2*!(cond)]); } while(0) - -#define EXPR_BUILD_ASSERT(cond) \ - (sizeof(char [1 - 2*!(cond)]) - 1) +#define assert_cc(expr) \ + _Static_assert((expr), #expr) #if HAVE_TYPEOF -#define check_type(expr, type) \ - ((typeof(expr) *)0 != (type *)0) - #define check_types_match(expr1, expr2) \ ((typeof(expr1) *)0 != (typeof(expr2) *)0) #else /* Without typeof, we can only test the sizes. */ -#define check_type(expr, type) \ - EXPR_BUILD_ASSERT(sizeof(expr) == sizeof(type)) - #define check_types_match(expr1, expr2) \ - EXPR_BUILD_ASSERT(sizeof(expr1) == sizeof(expr2)) + assert_cc(sizeof(expr1) == sizeof(expr2)) #endif /* HAVE_TYPEOF */ #define container_of(member_ptr, containing_type, member) \ @@ -47,26 +38,13 @@ ((char *)(member_ptr) - offsetof(containing_type, member)) \ - check_types_match(*(member_ptr), ((containing_type *)0)->member)) -/* - * BUILD_ASSERT_OR_ZERO - assert a build-time dependency, as an expression. - * @cond: the compile-time condition which must be true. - * - * Your compile will fail if the condition isn't true, or can't be evaluated - * by the compiler. This can be used in an expression: its value is "0". - * - * Example: - * #define foo_to_char(foo) \ - * ((char *)(foo) \ - * + BUILD_ASSERT_OR_ZERO(offsetof(struct foo, string) == 0)) - */ -#define BUILD_ASSERT_OR_ZERO(cond) \ - (sizeof(char [1 - 2*!(cond)]) - 1) /* Two gcc extensions. * &a[0] degrades to a pointer: a different type from an array */ -#define _array_size_chk(arr) \ - BUILD_ASSERT_OR_ZERO(!__builtin_types_compatible_p(typeof(arr), \ - typeof(&(arr)[0]))) +#define _array_size_chk(arr) ({ \ + assert_cc(!__builtin_types_compatible_p(typeof(arr), typeof(&(arr)[0]))); \ + 0; \ + }) #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]) + _array_size_chk(arr)) -- 2.7.4