1 PC-lint最佳指南
PC-Lint 最佳使用指南是随安装包提供的官方文档——Reference Manual for PC-lint/FlexeLint A Diagnostic Facility for C and C++(PC-lint.pdf)。IDE或编辑器(Virual Studio/SourceInsight)的配置可以搜索相关资料[1][2][3]。
2 PC-lint配置自动生成
对于PC-lint检查频繁执行,项目文件频繁变更的CI任务,可以通过脚本(bat/Python)自动生成PC-lint的相关配置项,例如,源文件完整路径列表、头文件路径等。这便于PC-lint检查的自动化部署。
其中,头文件可以通过在PC-lint配置文件中通过-i
参数添加,也可以通过Windows系统环境变量INCLUDE
添加,如下:
/* std.lnt */
-iD:\MinGW\lib\gcc\mingw32\6.3.0\include
-iD:\MinGW\lib\gcc\mingw32\6.3.0\include-fixed
-iD:\MinGW\include
/* lint.bat */
@set INCLUDE=%INCLUDE%;D:\MinGW\lib\gcc\mingw32\6.3.0\include
@set INCLUDE=%INCLUDE%;D:\MinGW\lib\gcc\mingw32\6.3.0\include-fixed
@set INCLUDE=%INCLUDE%;D:\MinGW\include
注意: Windows命令dir
带有/s /b
参数是显示当前目录下符合条件的所有文件的绝对路径,如下:
D:\cJSONFiles\cJSON>dir *.c /s /b
输出:
D:\cJSONFiles\cJSON\cJSON.c
D:\cJSONFiles\cJSON\test.c
3 PC-lint输出格式设置
PC-lint输出格式选项可以在配置文件中自定义设置,具体可参考lnt/
目录下的Virual Studio/SourceInsight环境配置项,例如,env-si.lnt
、env-vc12.lnt
等。此外,将PC-lint检测结果输出到文本文件,便于错误观察。
注意:当用bat脚本生成PC-lint配置文件时,需要注意bat
脚本中的%
需要转义为%%
。
-"format=%%f %%l %%t %%n: %%m"
4 PC-lint GCC环境配置
PC-lint检测GCC编译环境时,可能会提示找不到lint_cmac.h(C)
或lint_cppmac.h(C++)
,参考co-gcc.lnt
解决。
5 PC-lint 示例
@echo off
set PCLINT_DIR=C:\lint\
set GCC_DIR=D:\MinGW\
rem Generate cjson source file lists
set CJSON_DIR=%~dp0%
set INCLUDE=%INCLUDE%;%CJSON_DIR%
set CJSON_SRC=%CJSON_DIR%cjson_src.lnt
dir %CJSON_DIR%*.c /s /b > %CJSON_SRC%
rem Generate cjson pc-lint configure file(*.lnt).
set CJSON_CONFIG=%PCLINT_DIR%cjson_config.lnt
echo co-gcc.lnt > %CJSON_CONFIG%
echo -si4 -sl4 -sp8 >> %CJSON_CONFIG%
echo -hrfa_3 >> %CJSON_CONFIG%
echo -"format=%%f %%l %%t %%n: %%m" >> %CJSON_CONFIG%
echo -i%GCC_DIR%lib\gcc\mingw32\6.3.0\include >> %CJSON_CONFIG%
echo -i%GCC_DIR%lib\gcc\mingw32\6.3.0\include-fixed >> %CJSON_CONFIG%
echo -i%GCC_DIR%include >> %CJSON_CONFIG%
rem Do pc-lint check...
set RESULT_FILE=%CJSON_DIR%pclint_result.txt
%PCLINT_DIR%lint-nt -I%PCLINT_DIR% %CJSON_CONFIG% %CJSON_SRC% > %RESULT_FILE%
6 参考资料
[1] PC-Lint使用
[2] PClint和SI的结合静态代码检视
[3] PC-lint的配置及使用
[4] PC-Lint培训材料
[5] PC-lint 中文手册
[6] PCLint输出格式选项
[7] PC-lint配置及使用说明
[8] Reference Manual for PC-lint/FlexeLint