您的位置:首页 > 编程语言

代码静态分析工具PC-LINT安装配置[转]

2007-12-12 16:00 706 查看
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

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-->, 右侧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的朋友们能有所帮助,不足之处还请多指正!

注意:

在配置 pclint_project后使用时,output窗口中总出现如下两行提示:

C:\unix\usr\local\wbin\xargs: cannot fork

Tool returned code: 1

解决方法:查看下xargs.exe在不在目录 C:\unix\usr\local\wbin\ 下;有关xargs的Error Message介绍,对"cannot fork"是这么解释的:This normally happens if you use the -exec action or a something similar (-ok and so forth) but the system has run out of free process slots. This is either because the system is very busy and the system has reached its maximum process limit, or because you have a resource limit in place and you've reached it. Check the system for runaway processes (if ps still works). Some process slots are normally reserved for use by root. 据此分析肯定不是最大进程数的限制,但是受resource limit的启发,觉得可能是某个程序没能访问到。然后仔细看TOOL的参数: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 唯一没有加绝对路径的就是lint-nt.exe了,于是尝试用c:\pclint\lint-nt替换原文中的lint-nt,再试就OK了。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: