Clang静态分析器找不到stdio.h

时间:2016-06-07 08:39:29

标签: c clang clang-static-analyzer

我正在尝试在一个非常简单的程序中使用Clang静态分析器:

#include <stdio.h>
main ()
{
    printf("Hello, world !");
}

当我做的时候

  

clang helloworld.c

它成功编译了程序。

当我做的时候

  

clang -cc1 -analyze -analyzer-checker = unix helloworld.c

它引发了一个错误:

helloworld.c:1:10: fatal error: 'stdio.h' file not found
#include <stdio.h>
         ^
1 error generated.
  

clang --analyze -Xanalyzer -analyzer-checker = unix helloworld.c

不会打印任何内容。

问题是什么?我该如何解决? 我假设静态分析器没有看到头文件,尽管编译器可以使用它们。 求你帮帮我。

2 个答案:

答案 0 :(得分:4)

有时检查程序无法读取默认的包含路径。所以你可能想把它作为一个参数传递。 您可以使用以下命令找到确切的包含路径clang:

clang -E -x c - -v < /dev/null

然后您的最终查询将变为:

clang -I<path to include> --analyze -Xanalyzer -analyzer-checker=unix helloworld.c

答案 1 :(得分:0)

使用-cc1标志的解决方案:

查看clang正在接收的包含路径。标志-v是关键选项。以下是使用它的快速方法(由@Nishant给出)以及其打印的示例包含路径,

$ clang -E -x c - -v < /dev/null
...
#include <...> search starts here:
/usr/local/include
/home/codeman/.itsoflife/local/packages-live/llvm-clang6/build/lib/clang/6.0.1/include
/usr/include/x86_64-linux-gnu
/usr/include
...

在我的机器上,只需简单使用以下命令即可

$ clang --analyze -Xanalyzer -analyzer-checker=debug.DumpCFG main.c

但是,如您所指出的,以下形式失败

$ clang -cc1 -analyze -analyzer-checker=debug.DumpCFG main.c

对于第二个命令(使用-cc1),您可以创建一个环境变量,例如MY_INCLUDES,其中包含必需的包含。根据您使用的是~/.bashrc还是~/.zshrc,将以下代码(根据系统需要,包括必要的路径)粘贴到bashzsh中。 (不要忘记source ~/.bashrcsource ~/.zshrc

export MY_INCLUDES="-I/usr/local/include -I/home/codeman/.itsoflife/local/packages-live/llvm-clang6/build/lib/clang/6.0.1/include -I/usr/include/x86_64-linux-gnu -I/usr/include"

现在可以使用bash,

$ clang -cc1 $MY_INCLUDES -analyze -analyzer-checker=debug.DumpCFG main.c

在zsh上使用

$ clang -cc1 ${=MY_INCLUDES} -analyze -analyzer-checker=debug.DumpCFG main.c

请注意,在MY_INCLUDES之后但在-cc1文件之前使用main.c。此外,在zsh上,必须将=前缀与env变量一起使用,否则将其视为单个字符串(有关详细信息see this answer)。

相关问题