linux 源码:__bitwise和__force 的作用

__bitwise和__force 只限于用sparse,编译的时候一点用没有

__bitwise 的定义如下:

#ifdef __CHECKER__
#define __bitwise	__attribute__((bitwise))
#else
#define __bitwise
#endif

demo

typedef unsigned int __bitwise slab_flags_t;
typedef unsigned int __bitwise fmode_t;

那就看看__attribute__((bitwise)) 有啥作用吧
参考连接:https://man7.org/linux/man-pages/man1/sparse.1.html

 -Wbitwise
          Warn about unsupported operations or type mismatches with
          restricted integer types.
			Sparse supports an extended attribute,
          __attribute__((bitwise)), which creates a new restricted
          integer type from a base integer type, distinct from the
          base integer type and from any other restricted integer
          type not declared in the same declaration or typedef.  For
          example, this allows programs to create typedefs for
          integer types with specific endianness.  With -Wbitwise,
          Sparse will warn on any use of a restricted type in
          arithmetic operations other than bitwise operations, and
          on any conversion of one restricted type into another,
          except via a cast that includes __attribute__((force)).

翻译过来就是说,凡是被这个修饰的类型,与基础整数类型和其他不是同一个声明里的整数类型都不一样,如果相互赋值会弹警告
demo

#include<stdio.h>
#include <fcntl.h>
#define WILL_CREATE(flags)	(flags & (O_CREAT | __O_TMPFILE))
#define __bitwise	__attribute__((bitwise))
#define __force	__attribute__((force))
typedef  int __bitwise slab_flags_t;
typedef  int __bitwise fmode_t;
int main(){
   slab_flags_t a;
   fmode_t b;
   a=b;
   return 0;
}

运行

pipishuo@pipishuo-OMEN-by-HP-Laptop-16-b0xxx:~/Desktop/New Folder/technology/_bitwise$ sparse main.c 
main.c:8:10: warning: non-ANSI function declaration of function 'main'
main.c:11:5: warning: incorrect type in assignment (different base types)
main.c:11:5:    expected restricted slab_flags_t [usertype] a
main.c:11:5:    got restricted fmode_t [usertype] b

第一条不用管, 看 明明两个都是int,相互赋值就弹警告

稍微改一下

#include<stdio.h>
#include <fcntl.h>
#define WILL_CREATE(flags)	(flags & (O_CREAT | __O_TMPFILE))
#define __bitwise	__attribute__((bitwise))
#define __force	__attribute__((force))
typedef  int  slab_flags_t;
typedef  int  fmode_t;
int main(){
   slab_flags_t a;
   fmode_t b;
   a=b;
   return 0;
}
pipishuo@pipishuo-OMEN-by-HP-Laptop-16-b0xxx:~/Desktop/New Folder/technology/_bitwise$ sparse main.c 
main.c:8:10: warning: non-ANSI function declaration of function 'main'

这时候就不弹了

那如果就想赋值还不弹呢
用__force,定义如下

#ifdef __CHECKER__
...
# define __force	__attribute__((force))
...
#else
# define __force	
#endif

demo

#include<stdio.h>
#include <fcntl.h>
#define WILL_CREATE(flags)	(flags & (O_CREAT | __O_TMPFILE))
#define __bitwise	__attribute__((bitwise))
#define __force	__attribute__((force))
typedef  int  __bitwise slab_flags_t;
typedef  int  __bitwise fmode_t;
int main(){
   slab_flags_t a;
   fmode_t b;
   a=(__force slab_flags_t)b;
   return 0;
}
pipishuo@pipishuo-OMEN-by-HP-Laptop-16-b0xxx:~/Desktop/New Folder/technology/_bitwise$ sparse -Wbitwise main.c 
main.c:8:10: warning: non-ANSI function declaration of function 'main'

这样就不弹了

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值