__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'
这样就不弹了