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

代码检查工具CppCheck

2015-01-06 10:55 253 查看

概要

CppCheck是C\C++的静态代码检查工具,可以发现代码的静态缺陷,发现代码的逻辑错误,不过他并不会检查代码的语法错误,他得位置是编译器的一个补充。该工具是跨平台的工具,Windows提供了GUI和命令行工具,而Linux也提供了该命令行工具。

CppCheck

cppcheck 是一个静态代码检查工具,支持c, c++ 代码;作为编译器的一种补充检查,cppcheck对产品的源代码执行严格的逻辑检查。 执行的检查包括:
1. 自动变量检查
2. 数组的边界检查
3. class类检查
4. 过期的函数,废弃函数调用检查
5. 异常内存使用,释放检查
6. 内存泄漏检查,主要是通过内存引用指针
7. 操作系统资源释放检查,中断,文件描述符等
8. 异常STL 函数使用检查
9. 代码格式错误,以及性能因素检查


安装及其使用(Mac)

安装很简单,我使用的时brew安装工具来安装CppCheck工具的,命令是
sudo brew install cppcheck

使用一般用下面的命令:

cppcheck -j 3 --enable=all src
-j 表示工作线程个数,一般工程比较大的时候好用,src是源代码目录

运行结果示例

31/31 files checked 100% done
Checking usage of global functions..
[chromium_src/chrome/browser/ui/views/color_chooser_win.cc:71]: (style) The function 'End' is never used.
[chromium_src/chrome/browser/printing/print_job_worker.cc:70]: (style) The function 'GetAppLocale' is never used.
[chromium_src/chrome/browser/printing/print_job_worker.cc:60]: (style) The function 'GetParentView' is never used.
[chromium_src/chrome/renderer/printing/print_web_view_helper.cc:414]: (style) The function 'IsLoadingSelection' is never used.
[chromium_src/chrome/browser/ui/views/color_chooser_win.cc:28]: (style) The function 'SetSelectedColor' is never used.
[chromium_src/chrome/browser/ui/views/color_chooser_aura.cc:63]: (style) The function 'ShowColorChooser' is never used.
[chromium_src/chrome/renderer/printing/print_web_view_helper.cc:559]: (style) The function 'allowsBrokenNullLayerTreeView' is never used.
[chromium_src/chrome/renderer/printing/print_web_view_helper.cc:573]: (style) The function 'createChildFrame' is never used.
[chromium_src/chrome/renderer/printing/print_web_view_helper.cc:563]: (style) The function 'didStopLoading' is never used.
[chromium_src/chrome/renderer/printing/print_web_view_helper.cc:581]: (style) The function 'frameDetached' is never used.
(information) Cppcheck cannot find all the include files (use --check-config for details)
因为该代码是直接从GitHub弄个过来的,所以问题不是很多,不过运行结果也发现了一些问题,包括

未使用的函数(妥妥滴可以用来删减不必要得代码啊)
(好吧...没其他问题发现了)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息