ens*_*nsc 8 c gcc static-analysis clang
有什么方法可以检测bool代码中值的错误使用,例如
#include <stdbool.h>
void *foo(void)
{
return false;
}
int bar(void)
{
return true;
}
Run Code Online (Sandbox Code Playgroud)
gcc(8.3.1)和clang(7.0.1)均接受两个功能,而没有任何警告
$ gcc -Wall -W -pedantic -c x.c
$ clang -Xclang -analyzer-checker=alpha --analyze -Wall -W -pedantic -c x.c
$ clang -Wall -W -pedantic -c x.c
$
Run Code Online (Sandbox Code Playgroud)
编译为C ++代码可以检测到该问题,foo()但这不是一种选择,但是其余代码是C,而不是C ++。
是否还有其他(-W)选项或开关可为这些情况创建诊断?
让这个例子变得不那么简单:
bool x;
void *foo(void)
{
return x;
}
int bar(void)
{
return x;
}
Run Code Online (Sandbox Code Playgroud)
它根本想要编译。
通常 true 和 false 只是定义并且具有价值1和0
来自 stdbool.h 头文件
#ifndef _STDBOOL_H
#define _STDBOOL_H
#ifndef __cplusplus
#define bool _Bool
#define true 1
#define false 0
#else /* __cplusplus */
Run Code Online (Sandbox Code Playgroud)
在第一个示例中,您只返回零,大多数编译器不会发出警告,因为它们将其视为 NULL。尝试返回true,您将收到警告。
| 归档时间: |
|
| 查看次数: |
211 次 |
| 最近记录: |