问题
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");
}
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:
scan-build g++ -o 1 1.c
I do get the intended output, including a warning from the compiler that mentions the division by 0.
scan-build: Using '/usr/lib/llvm-3.8/bin/clang' for static analysis
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: warning: Division by zero return 5/(x-x);
~^~~~~~ 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.
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
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: Using '/usr/lib/llvm-3.8/bin/clang' for static analysis
g++ -o 1 1.c
1.c: In function ‘int test2(int)’:
1.c:16:11: warning: division by zero [-Wdiv-by-zero] return 5/(x-x);
^ scan-build: Removing directory '/tmp/scan-build-2016-07-11-152326-3055-1' because it contains no reports. scan-build: No bugs found.
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?
回答1:
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
and it works
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.
来源:https://stackoverflow.com/questions/38308269/scan-build-make-does-not-detect-any-bugs