utils: Add some initial build time assertion macros
These are useful to assert constants during build time. We evaluate the expression to 0 when valid, so we can safely use the evaluated value.
This commit is contained in:
@@ -59,6 +59,20 @@
|
|||||||
*/
|
*/
|
||||||
#define BUF_LEN 512
|
#define BUF_LEN 512
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Build assertion macro for integer expressions, evaluates to 0
|
||||||
|
*/
|
||||||
|
#define BUILD_ASSERT(x) (sizeof(char[(x) ? 0 : -1]))
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Build time check to assert a is an array, evaluates to 0
|
||||||
|
*
|
||||||
|
* The address of an array element has a pointer type, which is not compatible
|
||||||
|
* to the array type.
|
||||||
|
*/
|
||||||
|
#define BUILD_ASSERT_ARRAY(a) \
|
||||||
|
BUILD_ASSERT(!__builtin_types_compatible_p(typeof(a), typeof(&(a)[0])))
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* General purpose boolean type.
|
* General purpose boolean type.
|
||||||
*/
|
*/
|
||||||
|
|||||||
Reference in New Issue
Block a user