c - scan-build make 没有检测到任何错误

标签 c makefile clang static-analysis scan-build

我有一个非常简单的 .c 文件,里面有一些明显的错误。

#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) 来检测错误。当我直接在文件上运行该工具时,例如使用以下命令:

scan-build g++ -o 1 1.c

我确实得到了预期的输出,包括来自编译器的警告,其中提到除以 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.

现在,我试图将该命令放入一个非常简单的 Makefile 中。我的 Makefile 的内容是:

all: 1.c
    g++ -o 1 1.c
clean:
    rm -f *.o 1

但是,每当我使用 make 运行 scan-build 时,使用以下命令:

scan-build make

我仍然收到来自编译器的警告,但不是扫描构建工具!!!

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.

我在 C 和 C++ 文件中观察到相同的行为。我看到有人遇到了 similar error在过去(2012 年),但是建议的答案似乎不起作用,而且似乎只引用 C++ 文件。有什么线索吗?

最佳答案

scan-build 通过替换 CC 变量来工作。在您的 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.

关于c - scan-build make 没有检测到任何错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38308269/

相关文章:

c - 为什么 "storage size of ' array' 不是常量”

makefile - 在msysgit中使用make

c - 对 SDL_Freesurface 的 undefined reference

xcode - OSX 10.8、Xcode 4.4 Make 和 gcc 从环境中消失

c - 如何对这种风格的函数定义进行 clang 格式

c - 基于时间等待事件,子进程结束,数据到达文件描述符

c - "Segmentation fault"同时执行动态 malloc 代码

c - `write(2)` 对本地文件系统的原子性

C++编译clang错误多个输出文件

macos - GStreamer 非法硬件指令