pclint 2

代碼靜態分析工具PC-LINT安裝配置--step by step
                             作者:ehui928
                             2006-5-20
 PC-Lint是C/C++軟件代碼靜態分析工具,你可以把它看作是一種更加嚴格的編譯器。它不僅可以檢查出一般的語法錯誤,還可以檢查出那些雖然符合語法要求但不易發現的潛在錯誤。
 C語言的靈活性帶來了代碼效率的提升,但相應帶來了代碼編寫的隨意性,另外C編譯器不進行強制類型檢查,也帶來了代碼編寫的隱患。PCLint識別並報告C語言中的編程陷阱和格式缺陷的發生。它進行程序的全局分析,能識別沒有被適當檢驗的數組下標,報告未被初始化的變量,警告使用空指針,冗餘的代碼,等等。軟件除錯是軟件項目開發成本和延誤的主要因素。PClint能夠幫你在程序動態測試之前發現編碼錯誤。這樣消除錯誤的成本更低。
 使用PC-Lint在代碼走讀和單元測試之前進行檢查,可以提前發現程序隱藏錯誤,提高代碼質量,節省測試時間。並提供編碼規則檢查,規範軟件人員的編碼行爲。
由於PC-LINT對於一般程序員來說可能比較陌生,有好多人安裝了也不知道怎樣配置和使用。
 
 下面我就根據自己的安裝和配置心得對PC-Lint的安裝、配置及使用進行下詳細說明.本人主要介紹了將PC-Lint集成到VC++6.0和SourceInsight的方法和步驟。

(一)Windows下C/C++開發工具中,VC6使用較爲普遍,因此這裏先講下VC6.0環境中集成pclint的步驟.

首先, 當然要下載軟件,正版軟件要200多$呢,買不起!所以只好網上找免費的拉。從http://www.61ic.com/down/othe/pclint.rar處可以下載到一個8.0版本的pclint.

1.將pclint.rar解壓至c:/, 這樣lint文件就位與c:/pclint(安裝目錄)下了。
2.將c:/pclint/lnt 下的3個文件lib-w32.lnt,env-vc6.lnt,co-msc60.lnt拷貝至c:/pclint下, 再在安裝目錄下創建std.lnt和options.lnt兩個文件,其中std.lnt的內容如下

// contents of std.lnt

c:/pclint/co-msc60.lnt
c:/pclint/lib-w32.lnt
c:/pclint/options.lnt  -si4 -sp4
-i"D:/Program Files;D:/Program Files/Microsoft Visual Studio/VC98/Include"

//end

其中-i後面的路徑名爲VC的安裝路徑和VC Include 文件路徑,根據自己的修改便可。

options.lnt 內容可爲空,爲定製內容,以後需要時再添加。

準備工作做完了,下一步就是要將pclint集成到VC6中去,先配置lint使之能對單個C或C++文件進行檢查。

1.打開VC6,tools--->customize-->tools 新建一個名爲pclint的項,在下面填入
command:  C:/pclint/lint-nt.exe
arguments:  -u c:/pclint/std.lnt  c:/pclint/env-vc6.lnt  "$(FilePath)"

Use Output Window  打上勾
close 完成。 這個在你VC窗口tools菜單下應該多了一個pclint選項,可以用它來運行lint程序,對你的c/c++代碼進行靜態檢查了。

現在就可以用個小程序測試一下pclint了

//test1.cpp
#include <string.h>
class X
{
 int *p;
public:
 X()
 { p = new int[20]; }
 void init()
 { memset( p, 20, 'a'  ); }
 ~X()
 { delete p; }
};

編譯這個文件,看下你的編譯器給你多少警告,再運行下lint, 可以自己對比一下。
我的機器上,VC產生0 errors 0 warnings, 而lint程序產生了如下8條警告信息,有些還是很有用處的提示,這裏就不一一分析了.

test.cpp(12): error 783: (Info -- Line does not end with new-line)
test.cpp(7): error 1732: (Info -- new in constructor for class 'X' which has no assignment operator)
test.cpp(7): error 1733: (Info -- new in constructor for class 'X' which has no copy constructor)
    { memset( p, 20, 'a'  ); }
test.cpp(9): error 669: (Warning -- Possible data overrun for function 'memset(void *, int, unsigned int)', argument 3 (size=97) exceeds argument 1 (size=80) [Reference: test.cpp: lines 7, 9])
test.cpp(7): error 831: (Info -- Reference cited in prior message)
test.cpp(9): error 831: (Info -- Reference cited in prior message)
    { delete p; }
test.cpp(11): error 424: (Warning -- Inappropriate deallocation (delete) for 'new[]' data)


 --- Wrap-up for Module: test.cpp

test.cpp(2): error 753: (Info -- local class 'X' (line 2, file test.cpp) not referenced)
Tool returned code: 8


2.通常一個VC項目中包含多個C或C++文件,有時需要同時對這一系列的文件進行lint檢查,我們可以通過配置一個pclint_project來達到目的。

和前面第一步中的方法基本一樣,不過這裏我們需要用到unix中的find等命令來查找當前目錄下的C和C++文件,然後再將它們送給lint程序處理,所以得先從http://www.weihenstephan.de/~syring/win32/UnxUtils.zip下載UnxUtils.zip.
接着按下列步驟進行:

(i)解壓UnxUtils.zip至c:/unix下, 可以看到C:/unix/usr/local/wbin有很多unix下的命令,等下會用到

(ii)打開VC6,tools--->customize-->tools 新建一個名爲pclint_project的項,只不過下面的commands和arguments內容不同。

commands: C:/unix/usr/local/wbin/find.exe
arguments: $(FileDir) -name *.c  -o -name *.cpp | C:/unix/usr/local/wbin/xargs lint-nt            -i"c:/unix/usr/local"  -u c:/pclint/std.lnt  c:/pclint/env-vc6.lnt

(iii)Use Output Window打上勾,close退出。好了,這時VC tools菜單下應該又多了一個pclint_project項了,你以後可以用它來對一個VC項目運行lint檢查程序了.


(二)SourceInsight中集成pclint程序的方法.

Windows平臺下也有好多人都喜歡用SourceInsight編輯C/C++程序,如果將pclint集成到SourceInsight中,那就相當於給SourceInsight增加了一個C/C++編譯器,而且它的檢查更嚴格,能發現一些編譯器發現不了的問題,可以大大減少程序中潛伏的BUG。這樣的話,相信更多人會喜歡SourceInsight這個工具了。

下面簡要地介紹下pclint集成到SourceInsight中的方法

有了上面VC中集成pclint的經驗, 下面的事情就應該比較輕鬆了,
(a)打開你的SourceInsight, 選擇Options-->Custom Commands-->Add, 輸入pclint(當然名字可以隨便).

(b) Run中輸入: c:/pclint/lint-nt  -u  c:/pclint/std.lnt  c:/pclint/env-vc6.lnt %f

(c)Dir留空,將Iconic Window, Capture Output, Parse Links in OutPut, File,then Line 四項前打上勾。

(d)然後點右側 Menu--->Menu-->View--><end of menu>, 右側Insert, OK.

(e)此時在SourceInsight中的View菜單下多了個pclint選項,可以用它來對單個C/C++文件進行靜態檢查。


用類似的方法可以配置對一個SourceInsight工程文件的lint檢查。

(a)打開你的SourceInsight, 選擇Options-->Custom Commands-->Add, 輸入pclint_project(當然名字可以隨便).

(b) Run中輸入: C:/unix/usr/local/wbin/find.exe %d -name *.c  -o -name *.cpp  | C:/unix/usr/local/wbin/xargs lint-nt
-i"C:/unix/usr/local" -u c:/pclint/std.lnt  c:/pclint/env-vc6.lnt

(c)Dir留空,將Iconic Window, Capture Output, Parse Links in OutPut, File,then Line 四項前打上勾。

(d)然後點右側 Menu--->Menu-->View--><end of menu>, 右側Insert, OK.

(e)此時在SourceInsight中的View菜單下多了個pclint_project選項,可以用它來一個工程中的C/C++文件進行靜態檢查。


本文主要對pclint集成到VC及SourceInsight環境中的方法根據本人安裝和使用心得做了較詳細介紹,希望對以前沒使用過pclint的朋友們能有所幫助,不足之處還請多指正!

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章