C静态检查工具:cppcheck和PCLint
关键词:cppcheck,PC-Lint等等。
1 cppcheck
1.1 cppcheck安装
sudo apt-get install cppcheck
1.2 cppcheck使用说明
cppcheck -h可以查看使用方法:
-D<ID> Define preprocessor symbol. Unless --max-configs or--预处理宏定义。 --force is used, Cppcheck will only check the given configuration when -D is used. Example: '-DDEBUG=1 -D__cplusplus'. -U<ID> Undefine preprocessor symbol. Use -U to explicitly hide certain #ifdef <ID> code paths from checking. Example: '-UDEBUG' --enable=<id> Enable additional checks. The available ids are:--选择做哪些类型的检查。 * all Enable all checks. It is recommended to only use --enable=all when the whole program is scanned, because this enables unusedFunction. * warning Enable warning messages * style Enable all coding style checks. All messages with the severities 'style', 'performance' and 'portability' are enabled. * performance Enable performance messages * portability Enable portability messages * information Enable information messages * unusedFunction Check for unused functions. It is recommend to only enable this when the whole program is scanned. * missingInclude Warn if there are missing includes. For detailed information, use '--check-config'. Several ids can be given if you separate them with commas. See also --std -I <dir> Give path to search for include files. Give several -I--头文件包含。 parameters to give several paths. First given path is searched for contained header files first. If paths are relative to source files, this is not needed. --language=<language>, -x <language>--待检查语言。 Forces cppcheck to check all files as the given language. Valid values are: c, c++ --output-file=<file> Write results to file, rather than standard error.--将检查结果输出到文件中。 --project=<file> Run Cppcheck on project. The <file> can be a Visual Studio Solution (*.sln), Visual Studio Project (*.vcxproj), compile database (compile_commands.json), or Borland C++ Builder 6 (*.bpr). The files to analyse, include paths, defines, platform and undefines in the specified file will be used. --platform=<type>, --platform=<file> Specifies platform specific types and sizes. The available builtin platforms are:unix32, unix64, win32A, win32W, win64, avr8, native, unspecified。 --std=<id> Set standard.--语言标准。 The available options are:c89, c99, c11, c++03, c++11, c++14, c++17, c++20。--suppress=<spec> Suppress warnings that match <spec>. The format of--跳过某些类型的检查。 <spec> is: [error id]:[filename]:[line] The [filename] and [line] are optional. If [error id] is a wildcard '*', all error ids match. --template='<text>' Format the error messages. Available fields:--设置设置非xml文件的输出格式。 {file} file name {line} line number {column} column number {callstack} show a callstack. Example: [file.c:1] -> [file.c:100] {inconclusive:text} if warning is inconclusive, text is written {severity} severity {message} warning message {id} warning id {cwe} CWE id (Common Weakness Enumeration) {code} show the real code \t insert tab \n insert newline \r insert carriage return Example formats: '{file}:{line},{severity},{id},{message}' or '{file}({line}):({severity}) {message}' or '{callstack} {message}' Pre-defined templates: gcc (default), cppcheck1 (old default), vs, edit. --xml Write results in xml format to error stream (stderr).--将检查结果输出成xml格式。
更多细节参考:http://cppcheck.net/manual.pdf。
1.3 使用和分析
执行make cppcheck:
得到cppcheck.xml文件:
xml中给出了问题原因,不同严重等级,找到对应文件和行号即可修改。
2 PC-Lint
2.1 CONFIG.exe配置lnt文件
配置结束后得到std.lnt文件。
2.2 执行PC-Lint检查
pclint使用配置std.lnt进行测试:
lint-nt.exe std.lnt
使用pclint进行单元测试:
lint-nt.exe -u std.lnt
更多参考:《PC-lint/FlexeLint Reference Manual》3. GETTING STARTED WITH PC-LINT。
2.3 错误类型筛选
错误类型参考:《PC-lint/FlexeLint Reference Manual》 19. MESSAGES。
联系方式:arnoldlu@qq.com