我是 sparse
的新手,我正在使用它来清除代码中的噪音。最近,在代码行的某处:kzalloc(sizeof(bool) * nvhost_syncpt_nb_pts(sp), GFP_KERNEL);
我遇到了这个 sparse
警告:
warning: expression using sizeof bool
我想知道,为什么 sparse
会报告此警告。在谷歌搜索中,我发现 sizeof(bool) 依赖于编译器,这是非常明显的。请帮助我为什么这个警告是由 sparse
提出的,我认为不应该报告?如果我错了,请纠正我。
我正在使用 gcc 版本 4.6.3 (Ubuntu/Linaro 4.6.3-1ubuntu5)
。假设 bool 在这里定义为:
#if !defined(__cplusplus)
# if defined(_MSC_VER) || !defined(__STDC_VERSION__) || \
(__STDC_VERSION__ < 199901L)
// The Visual Studio C compiler and older versions of GCC do not support C99
// and thus have no bool or stdbool.h. Make a simple definition of bool,
// true, and false to make this deprecated interface compile in C. Force it
// to 1 byte to have some chance of ABI compatibility between C and C++, in
// case we don't remove this.
typedef char bool;
# define false 0
# define true 1
# else
// In C99-compliant compilers, we can include stdbool.h to get a bool
// definition.
# include <stdbool.h>
# endif
#endif
/**
* @}
* End addtogroup PP
*/
#endif /* PPAPI_C_DEV_DEPRECATED_BOOL_H_ */
最佳答案
如果您查看 evaluate.c
中的稀疏 git 源代码树:
static struct symbol *evaluate_sizeof(struct expression *expr)
{
...
if (size == 1 && is_bool_type(type)) {
if (Wsizeof_bool)
warning(expr->pos, "expression using sizeof bool");
size = bits_in_char;
}
...
}
ant 然后在 parse.c
中:
keyword_table[] = {
...
{ "_Bool", NS_TYPEDEF, .type = &bool_ctype, .op = &spec_op },
...
};
如果未被 Wsizeof_bool
覆盖,您将看到每个 sizeof(_Bool)
都会被报告为警告。
现在考虑您的代码:
kzalloc(sizeof(bool) * nvhost_syncpt_nb_pts(sp), GFP_KERNEL);
我不确定这里的 bool
是什么,但如果它是来自 include/linux/types.h
的 bool
: typedef _Bool bool;
难怪你会收到警告。至于警告背后的原因,您似乎已经自己弄清楚了:
在谷歌搜索中,我发现 sizeof(bool) 依赖于编译器,这一点非常明显。
但作为附加文档this可能会有帮助。
如果您认为该警告与您的代码无关,请使用 -Wno-sizeof-bool
将其删除。
关于c - 为什么稀疏报告 sizeof(bool) 警告?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26481379/