写这篇记录,是由于在用clang分析几个C/C++开源项目时,我猪脑子同样地搞不清楚为何一个待分析的文件,好比:test.c,既然能够用相似这样的命令” clang -cc1 -analyze -analyzer-checker=osx.KeychainAPI test.c ”来分析,为何还要用scan-build呢?那么用python和GnuWin32测试和分析器分析之间又有什么关系呢?测试是在测什么呢?html
1.clang分析和scan-build分析区别前端
咱们知道clang是一个C/C++/Object C等编译器前端,生成中间文件,其中clang static analyzer是clang重要的一部分。当咱们在命令行 clang --analyze就能够运行静态分析器。python
scan-build is a command line utility that enables a user to run the static analyzer over their codebase as part of performing a regular build (from the command line).git
让咱们用心理解下上面这段话:scan-build是一个命令行实用程序,它使用户可以运行静态分析器,做用于什么呢?Over codebase,因此codebase(代码库)这个分析对象很重要,后面是说,做为执行正常生成的一部分。github
During a project build, as source files are compiled they are also analyzed in tandem by the static analyzer.Upon completion of the build, results are then presented to the user within a web browser.web
在一个项目生成过程当中,当源码文件被编译时,它们也被静态分析器一个接一个地分析。生成完成,结果就会由Web浏览器呈现给用户。编程
如今终于知道了,用clang -cc1 -analyze test.c, 这样的命令只能分析一个单独的文件,若是要分析一个project,文件之间有组织和结构,也就是咱们上面说到的codebase,就须要用scan-build来分析了。windows
2.clang命令使用和scan-build使用xcode
下面简单罗列一下关于clang 和 scan-build在命令行下怎么用?浏览器
cmd clang -help OVERVIEW: clang LLVM compiler USAGE: clang.exe [options] <inputs> OPTIONS: --analyze Run the static analyzer
-c Only run preprocess, compile, and assemble steps -E Only run the preprocessor -g Generate source-level debug information -help Display available options -o <file> Write output to <file> -Xanalyzer <arg> Pass <arg> to the static analyzer -Xclang <arg> Pass <arg> to the clang compiler
经常使用命令行:
clang -help clang -cc1 -analyzer-checker-help clang --analyze -Xclang -analyzer-checker=alpha.core.FixedAddr test.c clang --analyze -Xanalyzer -analyzer-checker=alpha.unix.SimpleStream dblclose.c
A complete list of options can be obtained by running scan-build with no arguments or scan-build -h.
cmd scan-build / scan-build -h USAGE: scan-build [options] <build command> [build options] <build command> scan-build make scan-build xcodebuild In the first case scan-build analyzes the code of a project built with make and in the second case scan-build analyzes a project built using xcodebuild. [build options]:for example -j4 OPTIONS: generally ,we don't use any options. -o -h/ -V --use-analyzer
经常使用命令:
首先指向待分析项目的目录下:
Myproject >> scan-build --use-analyzer=D:\LLVM\build\Debug\bin\clang.exe make
针对上面这一行命令,我想多说一些,由于感受确实涉及到不少没有接触过的内容。我从接触计算机甚至编程以来,都是使用Windows,因此对Linux几乎一无所知,只知道是另外一种操做系统。忽然,最近有这样一些字母频繁地出现,就不得不稍微地了解一点东西了。好比:GNU,GnuWin32,WinGW,Makefile这就都是什么鬼。
安装了GnuWin32,就可让Windows命令行知道”make”是什么意思,一些习惯于在Linux上开发的人员,该在windows下开发时,须要一套相似Linux的命令行工具,因而GnuWin32应运而生。
当未安装WinGW时,执行上面的命令,你会一直被提示,gcc找不到,这是由于scan-build的工做原理是这样的:scan-build has little or no knowledge about how you build your code. It works by overriding the CC and CXX environment variables to (hopefully) change your build to use a "fake" compiler instead of the one that would normally build your project. This fake compiler executes either clang or gcc (depending on the platform) to compile your code and then executes the static analyzer to analyze your code.
因此能够看到,scan-build须要用到gcc/clang做为编译器,而咱们没有安装gcc,因此才被提示。WinGW是什么呢?和GCC有什么关系呢?Minimalist GNU for Windows,又称mingw32,是将GCC编译器和GNU Binutils移植到Win32平台下的产物,包括一系列头文件(Win32API)、库和可执行文件。
咱们按照教程安装好以后,终于能够正常分析一个开源软件了,真是开心,然而事情并非想象的这样。
Makefile找不到,这又是什么?makefile其实就是make这个命令所要执行的对象,咱们知道Linux下的make至关于Windows VS下的build,但是我总得知道怎么生成,才能生成啊,好比我得知道编译main.c以前先要编译的头文件之类的,这些要有一个文件告诉make。这个文件就是makefile。这里有一篇极好的文章推荐给你们,说得特别清晰。http://blog.csdn.net/liang13664759/article/details/1771246
到这里总算了解scan-build了,对于一些待分析开源软件没有makefile的现象,只能参考上面的文章,本身写一个makefile了。
3.clang-test
首先咱们简单说一下clang-test与clang -analyzer有什么联系和区别。由于总感受都是在测试检测什么的啊,我大概真是有病,说实话,真是没啥联系。Clang虽然是用来检测别人代码的缺陷的,因此它用clang -analyze和scan-build检测输入的工程或源码的缺陷,但实际上它自己本身也是一个工程,一个工程若是作了二次开发,固然须要作单元测试什么的,因此才会说”Every patch should be well tested with Clang regression tests”,每一次修改(补丁)都须要作clang回归测试。怎么作呢?
来官网瞅两眼(http://clang.llvm.org/get_started.html#build),咱们记得在学习配置时,最后有提到关于测试的事情。首先在测试前,必定要确保咱们安装了Python和GnuWin32,关于GnuWin32,你要肯定一下你安装的是getGunWin 32仍是GunWin 32,真是人生到处都是陷阱![捂脸]
http://clang.llvm.org/hacking.html#testingWindows 咱们很是高兴找到了方法,官方文档值得信赖啊,而后咱们来看个神坑:
--param=build_mode=Win32 --param=build_config=Debug
生成模式:Win32; 生成配置: Debug 彷佛没什么错,咱们看到VS上显示的就是Debug和Win32
但是,若是咱们按照上面的方式作,命令行会返回找不到一堆东西,因而咱们就奇怪了,由于按照命令行提示,咱们在指定路径下也找不到对应的文件,这也不能怪电脑啊,个人天。哈哈,不过不要紧,又发现一个连接:
咱们来看下:http://llvm.org/docs/GettingStartedVS.html
--param build_config=Win32 --param build_mode=Debug
生成模式:Debug; 生成配置: Win32
请告诉我,发生了什么,个人哥,怎么还不同啊,因而咱们把上面的换一下,啊,问题居然解决了,因此,综上,咱们应该在测试时用的是相似下面的例子:
python D:\LLVM\llvm\utils\lit\lit.py -sv --param build_config=Win32 --param build_mode=Debug --param=clang_site_config=D:\LLVM\build\tools\clang\test\lit.site.cfg D:\LLVM\llvm\tools\clang\test\Sema
总要找下缘由的,咱们找到D:\LLVM\build\test\Unit下的配置文件lit.site.cfg,看下里面的内容,咱们就大体明白了。
补充说明:
在安装完成MinGW以后,使用scan-build时可能会出现没法定位程序输入点 动态连接库xxx.dll...具体我也记不清楚了,这个错误多是由于在添加环境变量Path时,gcc的位置位于某个也含有这个动态库的软件或应用的后面,系统找的时候,先找到了path里前面的应用程序的路径中的这个动态库,而没有继续去找gcc中的,因此引发错误,解决办法是把gcc的bin目录放path中相对前面一点的位置,放在你认为可能含有这个.dll的目录前面。。。。
参考文献
http://clang-analyzer.llvm.org/scan-build.html
http://clang-analyzer.llvm.org/checker_dev_manual.html
http://xinsuiyuer.github.io/blog/2014/01/12/clang-static-analyzer/