PC-Lint使用

转载自:http://blog.csdn.net/feng_ma_niu/article/details/40631521。也进行了一些修改

1 PCLint简介

首先,提供一下PCLint的相关链接:

官网:http://www.gimpel.com/html/index.htm

最新9.0L安装包:http://download.csdn.net/detail/winking324/8162819

百度网盘:http://pan.baidu.com/s/1i3tIqLv

接下来,简单介绍一下PCLint到底是一个什么样的工具。

PCLint是GIMPELSOFTWARE公司研发的C/C++软件代码静态分析工具,可以说,PCLint是一种更加严格的编译器。其主要分为PCLint和FlexeLint,PCLint主要应用于Windows平台,以二进制可执行文件提供,而FlexeLint应用于其他平台,例如Linux,以源代码形式发布。

PCLint在全球拥有广泛的客户群,许多大型的软件研发组织都把PCLint检查作为代码走查的第一道工序。PCLint不但能够对程式进行全局分析,识别没有被适当检验的数组下标,报告未被初始化的变量,警告使用空指针连同冗余的代码,还能够有效地提出许多程序在空间利用、运行效率上的改进点。

总结下来,PCLint就是不通过执行程序进行代码分析,发现代码中潜在的Bug。通过个人实践来看,PCLint可以发现非逻辑上的80%左右的Bug,绝对是C++开发以及测试人员一个强大的助手。

2 PCLint安装及初步配置

安装包主要包含如下两部分:

PC-Lint使用

第一个 Update 为升级包,第二个 Gimpel_PC_Lint_9.zip 为 PCLint 的安装包。

1. 解压 PCLint 的安装包,并进行安装,直接 Next 到最后一步,选择执行配置程序,并点击 Finish 。如果这里没有选择执行配置程序,可以通过执行安装目录下的 CONFIG.exe 来执行配置程序。

PC-Lint使用

2. 接下来弹出 PCLint 的配置程序,点击下一步开始配置。

PC-Lint使用

3. 说明执行完 PCLint 配置后,会生成执行脚本以及 PCLint 的配置文件( *.lnt ),继续下一步。

PC-Lint使用

4. 选择生成路径,安装路径即可,以及文件名称,然后下一步。

PC-Lint使用

5. 选择编译器, VisualStudio 现在最高只有 2008 ,所以只好选择这个了,选择后点击下一步。

PC-Lint使用

6. 选择平台,根据自己的需要,选择对应的平台,这里选择 32bit ,点击下一步。

PC-Lint使用

7. 选择依赖的库,对于 VisualStudio 开发来说,主要包括 Active Template Library(ATL) , Microsoft FoundationClass Library , Standard Template Library , Windows 32-bit 这四部分,选择后下一步。

PC-Lint使用

8. 选择一些特殊的优化建议,例如说代码安全性和效率上的。这里按照最大原则进行选择,有: Scott Meyers(Effective C++ More Effective C++ and Effective C++ 3rdEdition) , Dan Saks , MISRA 2004 ,然后下一步( PS : Effective C++ 这两本巨作真心的给力, C++ 入门必读经典)。

PC-Lint使用

9. 指定当前使用库的头文件所在位置,选择 Create –i options ,并下一步,否则执行 PCLint 时会因为找不到头文件而失败。

PC-Lint使用

10. 选择头文件路径,以分号或者换行( Ctrl + Enter )区分,这里仅添加 Visual Studio 的标准头文件。由于不同的公司或者组织,其文件组织方式可能不一样,又或者包含了其他的各种库,所以有需要可以在这里进行添加,或者执行 PCLint 时出现错误,修改 *.lnt 文件添加即可。

PC-Lint使用

11. 提示是否打开新的配置,选择否。

PC-Lint使用

12. 创建 OPTIONS.LNT 文件,就是简单几个问题,由于我这边显示内容不全,所以选择 No ,然后下一步,如果显示全的话可以分别看一下,就是一些实例代码,然后选择一下即可。

PC-Lint使用

13. 选择对应的开发环境,这里最高只有 VC9 ,所以对应选择这个即可。

PC-Lint使用

14. 添加 PCLint 到系统环境变量中。

PC-Lint使用

15. 经过漫长的配置设定,总算完成了。

PC-Lint使用

3 PCLint更新

经过上面复杂的安装及配置后,接着就需要更新一下 PCLint 程序,使其支持更多的 C++ 检查项等。

1. 解压压缩包中 Update 目录下所有文件到 PCLint 的安装路径,例如我的 PCLint 安装在 D 盘,则对应文件路径应该为:

D:\lint\l9-a-b.lp

D:\lint\lpatch.exe

D:\lint\Update.bat

2. 双击 Update.bat ,进行 PCLint 的更新,更新成功后的结果如图。

PC-Lint使用

最后一行,版本号应该为 9.00L 。如果不正确,请进行路径或者文件是否正确的检查。

另外,说明一下更新的方法,首先,要确定当前 PCLint 的版本号,方法为使用 cmd 定位在 PCLint 的安装路径下,执行 lint-nt.exe–v 即可。其次,去官网上下载最新的更新程序 lpatch.exe 以及更新包(例如 l9-a-b.lp 等),将其拷贝到安装路径下,执行命令lpatch.exe l9-a-b.lp 即可。

4 PCLint的命令行

PCLint主要使用命令行模式来进行文件的解析,这个GCC的编译器过程非常相似。其命令行格式为:

lint options file1 [ file2 file3... ]

既然将PCLint可以当做一个编译器,则options即为编译选项,file即为要编译的文件,对于C++来说,主要是*.cpp文件,由于*.h作为包含文件,是不会进行单独进行编译的,所以这一点需要注意。

如果对一个工程的文件进行检查时,则需要生成文件列表来进行,类似于Makefile文件,所以需要注意一下。

接下来是各个工具的具体配置方法,具体详细的各个参数的信息,还请查阅安装目录下的手册。

5 VisualStudio配置

5.1 扫描并创建cpp文件列表

这个过程就是上面说到的生成文件列表,在Visual Studio中,主要按照工程(或目录)来进行创建。

1. 选择工具,然后创建外部扩展工具。

PC-Lint使用

2. 新建一个外部扩展工具,名称PCLintCreation。

PC-Lint使用

参数如下:

Title:PCLintCreation

Command:安装路径\lint-nt.exe

Arguments:-v -os("$(TargetName).lnt") "$(ProjectFileName)"

Initial directory: $(ProjectDir)

注意: $(ProjectDir)这些宏是vs自定义的,如果需要查看vs中自定义了哪些这种宏,可以打开msdn,然后输入一个常见的宏,比如ProjectName,就可以很方便地查询到定义了哪些宏。

3. 同样,可以根据前文SourceMonitor配置时,进行快捷键的分配。

PC-Lint使用

4. 执行添加的PCLintCreation,会在对应的工程文件路径下,生成一个对应的*.lnt文件,内容如下:

PC-Lint使用

5. 需要注意的是 ,当该工程下文件发生变化时,需要重新执行此任务,保证文件列表的正确性。

5.2 对工程下所有文件进行检查

根据上一步扫描的文件,进行PCLint检查。

1. 选择工具,然后创建外部扩展工具,这个跟上部分的第一步相同。

2. 新建外部工具,名称PCLintCheck。

PC-Lint使用

参数如下:

Title:PCLintCheck

Command:D:\lint\lint-nt.exe

Arguments:-i"D:\lint"std.lnt env-vc12.lnt "$(TargetName).lnt"

Initial directory:$(ProjectDir)

需要注意的有几点 ,第一,由于PCLint对于C++11的支持可能没有Visual Studio 2013支持的那么广泛,所以会检测失败,所以不建议在Visual Studio 2013及以上版本进行使用。第二,第一个lint文件,std.lnt即为上篇文章中配置生成的文件,env-vc12.lnt为VC12(Visual Studio 2013)对应的配置文件,"$(TargetName).lnt"为上一步对工程扫描的文件列表。第三,如果提示缺少*.lnt文件,则将对应的文件拷贝到PCLint安装路径下即可。

注意如果是vs2008,那么需要使用env-vc9.lnt,env-vc9.lnt文件就是普通的文本文件,使用文本编辑工具即可查看它的内容。

如果出现类似Error 307: Can't open indirect file 'env-vc9.lnt'的错误,只需要到安装目录D:\lint下看看有没有‘env-vc9.lnt'文件,如果没有一般在D:\lint\lnt下是一定有的,大家只需要将该文件拷贝一份到D:\lint目录下即可。还可以重启vs试试看。

3. 执行PCLint,会在Output窗口中输出对应的Info,Warn,Error和Fatal等信息,这样根据信息查看对应的代码,并进行改正。

PC-Lint使用

5.3 对单个文件进行检查

如果仅仅是对单个文件进行检查,则不需要配置前两步即可,但这个步骤仅对于单个文件执行检查是有效的,不方便配置到Ant任务中。但是相对于整个工程的检查,效率比较高,所以适合快速检查文件,并修改问题,具体配置如下。

1. 选择工具,然后创建外部扩展工具,这个跟前面相同。

2. 新建外部工具,名称PCLintCheckItem。

PC-Lint使用

参数如下:

Title:PCLintCheckItem

Command:D:\lint\lint-nt.exe

Arguments:-i"D:\lint"std.lnt env-vc12.lnt "$(ItemFileName)$(ItemExt)"

Initial directory:$(ItemDir)

3. 执行PCLint,会对当前文件进行检查,并输出结果到Output窗口中,例如:

PC-Lint使用

6 env-vc9.Int文件

    第五歩中配置参数时都使用到了 env-vc12.lnt这个文件,由于这篇文章是转载的,而作者是以vs2013为例写的教程,所以他使用的是env-vc12.lnt这个文件,而我的编译工具是vs2008,所以使用的是env-vc9.lnt。至于具体某个版本的vs到底应该用哪个版本的env-vc.Int文件,方法很简单,打开env-vc.Int,文件的第一行就是这个Int文件是给哪个版本的vs使用的。

    这个env-vc9.lnt文件内其实就是上面第五歩配置的过程,下面是这个文件的内容。

[cpp] view plain copy
  1. /* 
  2.     env-vc9.lnt: environment parameters for Microsoft's Visual C++ 9.0 
  3.  
  4.     If you are using Microsoft Visual Studio 2008 and you wish to invoke 
  5.     PC-lint from that environment then add one or more commands to the 
  6.     Tools menu as follows. 
  7.  
  8.     Simple Check 
  9.     ------------ 
  10.    
  11.     For example, to add a simple facility to lint the current file you 
  12.     may do the following: 
  13.  
  14.     1.  From the Tools Menu choose "External Tools ..." 
  15.     2.  Click the "Add" button. 
  16.     3.  You will now be able to enter the fields of this Tool. 
  17.         Modify them so that they approximate the following: 
  18.  
  19.     Title:       PC-lint (Simple Check) 
  20.     Command:     c:\lint\lint-nt.exe 
  21.     Arguments:   -i"c:\lint" std.lnt env-vc9.lnt "$(ItemFileName)$(ItemExt)" 
  22.     Initial Directory:  $(ItemDir) 
  23.  
  24.     X_Use Output Window  __Prompt for arguments  __Close on exit 
  25.  
  26.         Please note that you will have to change the "Command:" path if the 
  27.         PC-lint Installation Directory is anything other than c:\lint and 
  28.         you will have to change the "Arguments:" line if the Configuration 
  29.         Directory is anything other than c:\lint 
  30.  
  31.     4.  Select OK to return to the main environment. 
  32.  
  33.     This will result in the Tools menu containing the additional item 
  34.     "PC-lint (Simple Check)".  Checking 'X' on 'Use Output Window' is 
  35.     important because in this way you can advance from error to error 
  36.     using the F8 key (Shift F8 to reverse). 
  37.  
  38.     Strings of the form $(...) are called macros and can be typed in 
  39.     directly as shown or can be selected from a menu by clicking 
  40.     a right arrow in the dialog box.  $(ItemFileName) refers to the 
  41.     file name of the currently edited file without its path and without 
  42.     its extension.  $(ItemExt) is its extension.  $(ItemDir) represents 
  43.     the file's directory. 
  44.  
  45.     You will probably want to advance your new tool upward into the initial 
  46.     position of all tools while you are testing and modifying the command. 
  47.     You can do this by using the "Move Up" button that appears on the External 
  48.     Tools dialog. 
  49.  
  50.     The benefits of using "Initial Directory" are that file-names in lint 
  51.     error messages will not be so long, and, also, this directory can contain 
  52.     a std.lnt that overrides the global std.lnt in the Configuration Directory. 
  53.  
  54.     This Simple Check is fine to check stand-alone modules but to check 
  55.     projects or to unit check modules that are in projects we need to 
  56.     go a bit further ... 
  57.  
  58.     Project Creation 
  59.     ---------------- 
  60.  
  61.     To lint an entire project we will need the names of all the modules 
  62.     in the project.  Visual Studio keeps these names (as well as some 
  63.     appropriate options such as define options (-d...) and include options 
  64.     (-i...) in a file named NAME.vcproj in the current project directory. 
  65.     NAME is the name of the project and is identified by the macro 
  66.     $(TargetName).  PC-lint can read the .vcproj file and generate the 
  67.     appropriate .lnt file.  We recommend creating a tool for this purpose. 
  68.     For this tool follow the steps 1-4 doing exactly the same thing as 
  69.     above except in step 3, the information entered should be: 
  70.  
  71.     Title:          PC-lint (Project Creation) 
  72.     Command:        c:\lint\lint-nt.exe 
  73.     Arguments:      -v -os("$(TargetName).lnt") "$(ProjectFileName)" 
  74.     Init. Dir.:     $(ProjectDir) 
  75.  
  76.     __Use Output Window  __Prompt for arguments  x_Close on exit 
  77.  
  78.     You will need to have an active project before this will work. 
  79.     If you don't already have one you can obtain an active project 
  80.     from the Solutions Explorer.  You then click the newly added 
  81.     "PC-lint (Project Creation)" tool on the tools menu to create 
  82.     NAME.lnt. 
  83.  
  84.     The file created is an ASCII file and we recommend that you open it 
  85.     within the IDE and examine it for any obvious flaws.  This is your 
  86.     chance to make any necessary modifications to the file as the process 
  87.     of conversion may be less than perfect. 
  88.  
  89.     Project Check 
  90.     ------------- 
  91.  
  92.     Interestingly, by opening up the NAME.lnt file created above and 
  93.     running the Simple Check described earlier you have the equivalent 
  94.     of a full project check.  However, we prefer to create a special 
  95.     Project Check tool. 
  96.  
  97.     Now that we have a project file we can create a new tool called 
  98.     "PC-lint (project check)".  For this tool again follow steps 1-4 doing 
  99.     exactly the same thing as above except in step 3, the information 
  100.     entered should be: 
  101.  
  102.     Title:          PC-lint (Project Check) 
  103.     Command:        c:\lint\lint-nt.exe 
  104.     Arguments:      -i"c:\lint" std.lnt env-vc9.lnt "$(TargetName).lnt" 
  105.     Init. Dir.:     $(ProjectDir) 
  106.  
  107.     X_Use Output Window  __Prompt for arguments  __Close on exit 
  108.  
  109.     Unit Check 
  110.     ---------- 
  111.  
  112.     You can almost do a unit check on any single module by using the 
  113.     Simple Check scheme suggested above.  The only problems are that you 
  114.     will need a -u option and you will not have the benefit of any -d or 
  115.     -i options that have been placed into NAME.lnt created in the Project 
  116.     Creation step.  For this reason we suggest the following tool for 
  117.     doing a unit check of any module that is part of a project and for 
  118.     which a .lnt project file has been generated. 
  119.  
  120.     Title:          PC-lint (Unit Check) 
  121.     Command:        c:\lint\lint-nt.exe 
  122.     Arguments:      -i"c:\lint" std.lnt env-vc9.lnt --u "$(TargetName).lnt" "$(ItemPath)" 
  123.     Init. Dir.:     $(ProjectDir) 
  124.  
  125.     X_Use Output Window  __Prompt for arguments  __Close on exit 
  126.  
  127.     Note that $(ItemPath) will provide a complete path name and in the 
  128.     absence of a project.lnt file it would cause full path names to 
  129.     appear in messages.  But a side effect of using the project file 
  130.     with the --u option means that we adopt the shorter names used 
  131.     in the project file. 
  132.  
  133.     Suppressing Messages 
  134.     ----------- -------- 
  135.  
  136.     Suppressing messages is normally done by adding message suppression 
  137.     options to a file.  For example, -e550 will suppress message 550. 
  138.     There are numerous other options to suppress messages. 
  139.  
  140.     As the documentation indicates, the file 
  141.  
  142.     c:\lint\options.lnt 
  143.  
  144.     (where c:\lint\ is the Configuration Directory) is the presumed container 
  145.     of your overall suppression policy. (Note: options.lnt is referenced 
  146.     by std.lnt).  Add a message suppression here and 
  147.     you will affect all linting employing that configuration. 
  148.  
  149.     To suppress messages for a particular project (or for all projects 
  150.     within a given project directory) you may do the following: 
  151.     Create a file std.lnt that is contained in the project directory. 
  152.     Make it refer back to the std.lnt in the Configuration Directory. 
  153.     Then add additional message suppression options or indeed any options 
  154.     you want.  For example it might contain: 
  155.  
  156.             c:\lint\std.lnt         // reference to original std.lnt 
  157.             -e550                   // project-specific option 
  158.  
  159.     In this way suppression is limited to a particular project. 
  160.  
  161.  
  162.     Tool Bar 
  163.     -------- 
  164.      
  165.     You also have the option of creating a PC-lint toolbar within your Visual 
  166.     C++ IDE.  First, create one or more tools as described above.  You 
  167.     will need to know the number(s) of the tool(s) you want to place on 
  168.     the tool bar.  This can only be done by the painful and laborious 
  169.     task of counting.  Using the list provided by "Tools"/"External Tools", 
  170.     jot down the numbers (starting with 1 at the top) of all the tools 
  171.     to be added to the tool bar.  We recommend placing all the PC-lint tools 
  172.     on a single tool bar.  Then select Customize from the Tools menu. 
  173.     Select the Toolbars tab and click the New... button. 
  174.     Give the Toolbar a name (E.g., PC-lint) in the dialog box provided and 
  175.     click "OK".  Confirm that the new toolbar is now floating on the 
  176.     desktop and that a check has been placed in the check box next to the 
  177.     new toolbar name.  Then click on the Commands tab and in the Categories 
  178.     box, scroll down to and click "Tools". In the Commands box, scroll down 
  179.     to the appropriate external command number(s) corresponding to the 
  180.     PC-lint commands you jotted down earlier. Drag the commands from the 
  181.     Commands box to the toolbar.  Their numeric name will change to the 
  182.     logical name. 
  183.  
  184.     If you want to add a button image to the toolbar, you can choose one 
  185.     via the Modify Selection button.  Click Close and you now have your 
  186.     own PC-lint for C/C++ button.  (Note: If you change the location of 
  187.     the PC-lint menu item on the Tools menu, you will change the subscript 
  188.     and you will need to change the button(s) on the toolbar.) 
  189.  
  190.  */  
  191.   
  192.   
  193. -"format=%(%F(%l):%) error %n: (%t -- %m)"     //  Messages will contain  
  194.     //  file information (%F), the line number (%l), the  
  195.     //  message number (%n), message type (%t) and message text (%m).  
  196.   
  197. -hF2    //  Make sure we ALWAYS provide file information ('F') and use 2  
  198.         //  lines (one for the source line in error and one for the message).  
  199.   
  200. -width(0) //  don't break messages at any particular width  
  201. -t4       //  Presume that tabs are every 4 stops  
  202. +e900     //  issue a message at termination.