【问题标题】:Clang static analyzer can't find stdio.hClang 静态分析器找不到 stdio.h
【发布时间】:2016-10-07 01:36:14
【问题描述】:

我正在尝试在一个非常简单的程序上使用 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

不打印任何东西。


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

【问题讨论】:

  • 我正在运行 Debian 8、gcc 4.7.4、clang 3.4.1
  • 看起来最后一个代码块(使用 clang 驱动程序)运行正常 - 我已经从这里的错误代码上对其进行了测试:stackoverflow.com/a/3486939/1301604

标签: c clang clang-static-analyzer


【解决方案1】:

有时检查器无法读取默认包含路径。因此,您可能希望将其作为参数传递。 您可以使用以下命令找到确切的包含路径:

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

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

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

【讨论】:

    【解决方案2】:

    使用-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,其中包含必要的包含。根据您使用的是bash 还是zsh,将下面的代码(根据您的系统包含必要的包含路径)粘贴到~/.bashrc~/.zshrc。 (别忘了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)。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2010-12-24
      • 2012-08-11
      • 2016-11-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多