对C语言开发的项目来说,BUILD_BUG_ON就是这么一个可以在编译期间检查代码静态约束的宏。 #define BUILD_BUG_ON(condition) ((void)sizeof(char[1 - 2*!!(condition)])) BUILD_BUG_ON的原理如下: 当condition为false(0)时,由于1-2*!!(condition)为1,宏被展开为sizeof(char[1]),这是合法语句,并且编译...
在linux 3.19内核/include/linux/bug.h中,还有个BUILD_BUG_ON宏的升级版BUILD_BUG_ON_MSG(cond, msg),这个宏额外多了一个msg参数,用来设定不满足条件时编译错误所打印的信息,不过该宏是通过gcc的拓展特性实现,并不通用。
aren't permitted). */#defineBUILD_BUG_ON_ZERO(e)(sizeof(struct{int:-!!(e);}))#defineBUILD_BUG_ON_NULL(e)((void*)sizeof(struct{int:-!!(e);})) 分析第一个,它表示的是:检查表达式e是否为0,为0编译通过且返回0;如果不为0,则编译不通过。 可能从这个宏的名字上看可能容易理解错,或者改...
1#defineBUILD_BUG_ON_ZERO(e) (sizeof(struct{int : -!!(e);}))2#defineBUILD_BUG_ON_NULL(e) ((void*)sizeof(struct{int : -!!(e);}))34#defineBUILD_BUG_ON(condition) ((void)BUILD_BUG_ON_ZERO(condition))56#defineMAYBE_BUILD_BUG_ON(condition) ((void)sizeof(char[1 - 2 * !!
BUILD_BUG_ON(offsetof(stype, ename) != eoffset); \ BUILD_BUG_ON(sizeof(etype) != sizeof_field(stype, ename)); \ } while (0) 该宏的具体实现为 #define BUILD_BUG_ON(condition) ((void)sizeof(char[1 - 2*!!(condition)])) 其作用是在编译的时候如果condition为真,则编译出错。即:...
BUILD_BUG_ON(zero) 编译器无论如何也是无法检查的,因为变量的值是运行时。 在网上搜索了下,还有用用位域来强制编译器检查的,也是可以做借鉴的。 #defineBUILD_BUG_ON_ZERO(e) (sizeof(struct{int : -!!(e);}))#defineBUILD_BUG_ON_NULL(e) ((void*)sizeof(struct{int : -!!(e);}))#define...
一起来认识Linux中的 BUILD_BUG_ON 宏 简介: 该宏在Linux内核中实现的,可以在编译的时候检查传入参数的合法性,而assert是在运行到断言的时候才会知道参数的合法性。 原型 include/linux/kernel.h /Force a compilation error if condition is true/ define BUILD_BUG_ON(condition) ((void)sizeof(char[1 - 2...
io_uring: Remove unnecessary BUILD_BUG_ON Browse files In the io_uring_cmd_prep_async() there is an unnecessary compilation time check to check if cmd is correctly placed at field 48 of the SQE. This is unnecessary, since this check is already in place at io_uring_init(): BUILD_BUG...
+ BUILD_BUG_ON(SHADOW_STACK_SIZE % sizeof(long)); + /* * We must make sure the ret_stack is tested before we read * anything else. @@ -326,6 +328,8 @@ ftrace_graph_get_ret_stack(struct task_struct *task, int idx)
#define BUILD_BUG_ON(condition) ((void)sizeof(char[1 - 2*!!(condition)])) BUILD_BUG_ON宏中的condition如果为真就会报错。假设条件为真,则BUILD_BUG_ON变化为:(void) sizeof (char[-1]),这种语法是在难理解,因为char[-1]本来就是非法的。结果导致编译错误。