scan-build make无法检测到任何错误 [英] scan-build make does not detect any bugs
问题描述
我有一个非常简单的.c文件,其中包含一些明显的错误.
I have a very simple .c file, with some obvious bugs inside it.
#include <stdio.h>
struct S {
int x;
};
void f(struct S s){
}
void test() {
struct S s;
f(s); // warn
}
int test2(int x){
return 5/(x-x); // warn
}
int main(){
test();
test2(532);
printf("Hej\r\r");
}
我正在尝试使用clang的静态代码分析器工具(scan-build)来检测错误.当我直接在文件上运行该工具时,例如使用以下命令:
I am trying to use the clang's static code analyzer tool (scan-build) to detect errors. When I run the tool directly on the files, as for example using the following command:
扫描构建g ++ -o 1 1.c
scan-build g++ -o 1 1.c
我确实获得了预期的输出,包括编译器发出的警告,指出除以0.
I do get the intended output, including a warning from the compiler that mentions the division by 0.
scan-build:使用'/usr/lib/llvm-3.8/bin/clang'进行静态分析
scan-build: Using '/usr/lib/llvm-3.8/bin/clang' for static analysis
1.c:在"int test2(int)"函数中: 1.c:16:11:警告:除以零[-Wdiv-by-zero]返回5/(x-x); ^
1.c: In function ‘int test2(int)’: 1.c:16:11: warning: division by zero [-Wdiv-by-zero] return 5/(x-x); ^
1.c:16:11:警告:被零归零5/(x-x);
1.c:16:11: warning: Division by zero return 5/(x-x);
〜^ ~~~~~ 1个警告生成. scan-build:找到1个错误. scan-build:运行'scan-view/tmp/scan-build-2016-07-11-152043-3028-1'检查错误报告.
~^~~~~~ 1 warning generated. scan-build: 1 bug found. scan-build: Run 'scan-view /tmp/scan-build-2016-07-11-152043-3028-1' to examine bug reports.
现在,我正在尝试将该命令放入一个非常简单的Makefile中.我的Makefile的内容是:
Now, I am trying to put that command into a very simple Makefile. The contents of my Makefile are:
all: 1.c
g++ -o 1 1.c
clean:
rm -f *.o 1
但是,每当我使用以下命令运行带有make的scan-build时:
However, whenever I run scan-build with make, using the following command:
扫描制作版
scan-build make
我仍然从编译器收到警告,但没有扫描构建工具!
I still get the warning from the compiler, but not the scan-build tool!!!
scan-build:使用'/usr/lib/llvm-3.8/bin/clang'进行静态分析
scan-build: Using '/usr/lib/llvm-3.8/bin/clang' for static analysis
g ++ -o 1 1.c
g++ -o 1 1.c
1.c:在"int test2(int)"函数中:
1.c: In function ‘int test2(int)’:
1.c:16:11:警告:被零除[-Wdiv-by-zero]返回5/(x-x);
1.c:16:11: warning: division by zero [-Wdiv-by-zero] return 5/(x-x);
^ scan-build:删除目录'/tmp/scan-build-2016-07-11-152326-3055-1',因为它不包含任何报告. scan-build:未发现错误.
^ scan-build: Removing directory '/tmp/scan-build-2016-07-11-152326-3055-1' because it contains no reports. scan-build: No bugs found.
我在C和C ++文件中都观察到了相同的行为.我看到有人遇到了
I have observed the same behavior in both C and C++ files. I see that someone had come across a similar error in the past (2012), however the proposed answer does not seem to work and seems to refer to C++ files only anyway. Any clues?
推荐答案
scan-build
通过替换CC
变量来工作.在您的Makefile中使用它
scan-build
works by substituting the CC
variable. Use it in your your makefile
CC=g++
all: 1.c
$(CC) -o 1 1.c
clean:
rm -f *.o 1
它有效
scan-build: Using '/usr/bin/clang' for static analysis
/usr/share/clang/scan-build/ccc-analyzer -o 1 1.c
1.c:16:17: warning: Division by zero
return 5/(x-x); // warn
~^~~~~~
1 warning generated.
scan-build: 1 bugs found.
scan-build: Run 'scan-view /tmp/scan-build-2016-07-11-160529-5951-1' to examine bug reports.
这篇关于scan-build make无法检测到任何错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!