Use _Static_assert
authorLucas De Marchi <lucas.de.marchi@gmail.com>
Mon, 15 Apr 2013 17:09:05 +0000 (14:09 -0300)
committerLucas De Marchi <lucas.de.marchi@gmail.com>
Mon, 15 Apr 2013 17:09:05 +0000 (14:09 -0300)
Both GCC and clang already supports C11's _Static_assert, so use it
instead of defining our own macro.

libkmod/macro.h

index f09bb2b..c6ba855 100644 (file)
 
 #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)              \
         ((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))