關於clang, scan-build, 和clang test


寫這篇記錄,是因為在用clang分析幾個C/C++開源項目時,我豬腦子一樣地搞不清楚為什么一個待分析的文件,比如:test.c,既然可以用類似這樣的命令” clang -cc1 -analyze -analyzer-checker=osx.KeychainAPI test.c ”來分析,為什么還要用scan-build呢?那么用pythonGnuWin32測試和分析器分析之間又有什么關系呢?測試是在測什么呢?

 1.clang分析和scan-build分析區別

我們知道clang是一個C/C++/Object C等編譯器前端,生成中間文件,其中clang static analyzerclang重要的一部分。當我們在命令行 clang --analyze就可以運行靜態分析器。

 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).

讓我們用心理解下上面這段話:scan-build是一個命令行實用程序,它使用戶能夠運行靜態分析器,作用於什么呢?Over codebase,所以codebase(代碼庫)這個分析對象很重要,后面是說,作為執行正常生成的一部分。

 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瀏覽器呈現給用戶。

 現在終於知道了,用clang -cc1 -analyze test.c, 這樣的命令只能分析一個單獨的文件,如果要分析一個project,文件之間有組織和結構,也就是我們上面說到的codebase,就需要用scan-build來分析了。

 2.clang命令使用和scan-build使用

下面簡單羅列一下關於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幾乎一無所知,只知道是另一種操作系統。突然,最近有這樣一些字母頻繁地出現,就不得不稍微地了解一點東西了。比如:GNUGnuWin32WinGWMakefile這就都是什么鬼。

安裝了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-testclang -analyzer有什么聯系和區別。因為總感覺都是在測試檢測什么的啊,我大概真是有病,說實話,真是沒啥聯系。Clang雖然是用來檢測別人代碼的缺陷的,所以它用clang -analyzescan-build檢測輸入的工程或源碼的缺陷,但實際上它本身自己也是一個工程,一個工程如果做了二次開發,當然需要做單元測試什么的,所以才會說”Every patch should be well tested with Clang regression tests”,每一次修改(補丁)都需要做clang回歸測試。怎么做呢?

來官網瞅兩眼(http://clang.llvm.org/get_started.html#build),我們記得在學習配置時,最后有提到關於測試的事情。首先在測試前,一定要確保我們安裝了PythonGnuWin32,關於GnuWin32,你要確定一下你安裝的是getGunWin 32還是GunWin 32,真是人生處處都是陷阱![捂臉]

http://clang.llvm.org/hacking.html#testingWindows 我們非常高興找到了方法,官方文檔值得信賴啊,然后我們來看個神坑:

--param=build_mode=Win32 --param=build_config=Debug

生成模式:Win32; 生成配置: Debug     似乎沒什么錯,我們看到VS上顯示的就是DebugWin32

可是,如果我們按照上面的方式做,命令行會返回找不到一堆東西,於是我們就奇怪了,因為按照命令行提示,我們在指定路徑下也找不到對應的文件,這也不能怪電腦啊,我的天。哈哈,不過沒關系,又發現一個鏈接:

我們來看下: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/

http://blog.csdn.net/liang13664759/article/details/1771246


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM