代碼靜態分析工具PC-LINT安裝配置

代碼靜態分析工具PC-LINT安裝配置



  其中-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
class X
{
 int *p;
 public:
  X()
  { p = new int[20]; }
  void init()
  { memset( p, 20, 'a' ); }
  ~X()
  { delete p; }
};
  編譯這個文件,看下你的編譯器給你多少警告,再運行下lint, 可以自己對比一下。

  我的機器上,VC產生0 errors 0warnings, 而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 noassignment operator)
test.cpp(7): error 1733: (Info -- new in constructor for class 'X' which has nocopy 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 -- Referencecited 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) notreferenced)

  Toolreturned 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 *.cppC:\unix\usr\local\wbin\xargs lint-nt -i"c:\unix\usr\local" -uc:\pclint\std.lnt c:\pclint\env-vc6.lnt

  (iii)UseOutput 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-->CustomCommands-->Add, 輸入pclint(當然名字可以隨便).

  (b) Run中輸入: c:\pclint\lint-nt -uc:\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-->, 右側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-->, 右側Insert, OK.

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

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


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