summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLucas De Marchi <lucas.de.marchi@gmail.com>2013-04-15 14:09:05 -0300
committerLucas De Marchi <lucas.de.marchi@gmail.com>2013-04-15 14:09:05 -0300
commit8efede20efbaaeef8dc9d2289d6cc0add48b56e0 (patch)
treef1c2374ba0692ba806c1ecedcd79f707e5dc5f1b
parent2ad09ccf0fc5232d9d45eb5b5b94193e64ed7ed2 (diff)
downloadkmod-8efede20efbaaeef8dc9d2289d6cc0add48b56e0.tar.gz
Use _Static_assert
Both GCC and clang already supports C11's _Static_assert, so use it instead of defining our own macro.
-rw-r--r--libkmod/macro.h36
1 files 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 <stddef.h>
-#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))