【2013-12-12】C语言代码检测:cppcheck的使用
[历史归档]本文原发布于 cstriker1407.info 个人博客,内容为历史存档,仅供参考。
发布时间:2013-12-12| 标题:C语言代码检测:cppcheck的使用|分类:编程 / C && C++ |标签:C&&C++·代码检测·cppcheck
C语言代码检测:cppcheck的使用
- 官网地址:
- cppcheck的使用:
- 界面语言的设置:
- 保存输出文件:
- 直接检查文件或目录:
- cppcheck的输出文件结果实例:
- 输出XML1:
- 输出XML2:
- 输出TXT:
- 输出表格:
- 更多参考:
cppcheck是一个轻量的跨平台的C C++ 代码静态检查工具,使用也很简单,这里备份下它的使用方法:
官网地址:
【 http://cppcheck.sourceforge.net/ 】
下载链接如下图,可以下载windows安装包,cppcheck源码,以及eclipse插件:
cppcheck的下载和安装在windows上是非常简单的,直接【 next 】即可。
cppcheck的使用:
界面语言的设置:
可在【 编辑 - 首选项 - 语言 】里选择
保存输出文件:
可在【 文件 - 保存结果到文件 】里选择
直接检查文件或目录:
可在【 检查 - 文件/目录 】里选择
检查结束后如下图:
cppcheck的输出文件结果实例:
cppcheck有4种输出结果:
输出XML1:
<?xml version="1.0" encoding="utf-8"?><results><errormsg="The scope of the variable 'len' can be reduced. Warning: Be careful when fixing this message, especially when there are inner loops. Here is an example where cppcheck will write that the scope for 'i' can be reduced: void f(int x) { int i = 0; if (x) { // it's safe to move 'int i = 0;' here for (int n = 0; n<10; ++n) { // it is possible but not safe to move 'int i = 0;' here do_something(&i); } } } When you see this message it is always safe to reduce the variable scope 1 level."severity="style"id="variableScope"line="255"file="utilssrc\argparse.c"/><errormsg="The scope of the variable 'c' can be reduced. Warning: Be careful when fixing this message, especially when there are inner loops. Here is an example where cppcheck will write that the scope for 'i' can be reduced: void f(int x) { int i = 0; if (x) { // it's safe to move 'int i = 0;' here for (int n = 0; n<10; ++n) { // it is possible but not safe to move 'int i = 0;' here do_something(&i); } } } When you see this message it is always safe to reduce the variable scope 1 level."severity="style"id="variableScope"line="287"file="utilssrc\linked_list_base.c"/><errormsg="Variable 'r' is reassigned a value before the old one has been used."severity="performance"id="redundantAssignment"line="280"file="utilssrc\queue_base.c"/><errormsg="Variable 'r' is reassigned a value before the old one has been used."severity="performance"id="redundantAssignment"line="294"file="utilssrc\queue_base.c"/><errormsg="Variable 'error' is reassigned a value before the old one has been used."severity="performance"id="redundantAssignment"line="48"file="utilssrc\queue_internal.c"/><errormsg="Variable 'error' is reassigned a value before the old one has been used."severity="performance"id="redundantAssignment"line="49"file="utilssrc\queue_internal.c"/><errormsg="Variable 'error' is reassigned a value before the old one has been used."severity="performance"id="redundantAssignment"line="52"file="utilssrc\queue_internal.c"/><errormsg="Variable 'error' is reassigned a value before the old one has been used."severity="performance"id="redundantAssignment"line="55"file="utilssrc\queue_internal.c"/><errormsg="Variable 'error' is reassigned a value before the old one has been used."severity="performance"id="redundantAssignment"line="57"file="utilssrc\queue_internal.c"/><errormsg="Variable 'error' is reassigned a value before the old one has been used."severity="performance"id="redundantAssignment"line="60"file="utilssrc\queue_internal.c"/><errormsg="Memory leak: new_el"severity="error"id="memleak"line="162"file="utilssrc\queue_internal.c"/></results>输出XML2:
<?xml version="1.0" encoding="utf-8"?><resultsversion="2"><cppcheckversion="1.62"/>-<errors>-<errorverbose="The scope of the variable 'len' can be reduced. Warning: Be careful when fixing this message, especially when there are inner loops. Here is an example where cppcheck will write that the scope for 'i' can be reduced: void f(int x) { int i = 0; if (x) { // it's safe to move 'int i = 0;' here for (int n = 0; n<10; ++n) { // it is possible but not safe to move 'int i = 0;' here do_something(&i); } } } When you see this message it is always safe to reduce the variable scope 1 level."msg="The scope of the variable 'len' can be reduced."severity="style"id="variableScope"><locationline="255"file="utilssrc\argparse.c"/></error>-<errorverbose="The scope of the variable 'c' can be reduced. Warning: Be careful when fixing this message, especially when there are inner loops. Here is an example where cppcheck will write that the scope for 'i' can be reduced: void f(int x) { int i = 0; if (x) { // it's safe to move 'int i = 0;' here for (int n = 0; n<10; ++n) { // it is possible but not safe to move 'int i = 0;' here do_something(&i); } } } When you see this message it is always safe to reduce the variable scope 1 level."msg="The scope of the variable 'c' can be reduced."severity="style"id="variableScope"><locationline="287"file="utilssrc\linked_list_base.c"/></error>-<errorverbose="Variable 'r' is reassigned a value before the old one has been used."msg="Variable 'r' is reassigned a value before the old one has been used."severity="performance"id="redundantAssignment"><locationline="279"file="utilssrc\queue_base.c"/><locationline="280"file="utilssrc\queue_base.c"/></error>-<errorverbose="Variable 'r' is reassigned a value before the old one has been used."msg="Variable 'r' is reassigned a value before the old one has been used."severity="performance"id="redundantAssignment"><locationline="293"file="utilssrc\queue_base.c"/><locationline="294"file="utilssrc\queue_base.c"/></error>-<errorverbose="Variable 'error' is reassigned a value before the old one has been used."msg="Variable 'error' is reassigned a value before the old one has been used."severity="performance"id="redundantAssignment"><locationline="46"file="utilssrc\queue_internal.c"/><locationline="48"file="utilssrc\queue_internal.c"/></error>-<errorverbose="Variable 'error' is reassigned a value before the old one has been used."msg="Variable 'error' is reassigned a value before the old one has been used."severity="performance"id="redundantAssignment"><locationline="48"file="utilssrc\queue_internal.c"/><locationline="49"file="utilssrc\queue_internal.c"/></error>-<errorverbose="Variable 'error' is reassigned a value before the old one has been used."msg="Variable 'error' is reassigned a value before the old one has been used."severity="performance"id="redundantAssignment"><locationline="49"file="utilssrc\queue_internal.c"/><locationline="52"file="utilssrc\queue_internal.c"/></error>-<errorverbose="Variable 'error' is reassigned a value before the old one has been used."msg="Variable 'error' is reassigned a value before the old one has been used."severity="performance"id="redundantAssignment"><locationline="52"file="utilssrc\queue_internal.c"/><locationline="55"file="utilssrc\queue_internal.c"/></error>-<errorverbose="Variable 'error' is reassigned a value before the old one has been used."msg="Variable 'error' is reassigned a value before the old one has been used."severity="performance"id="redundantAssignment"><locationline="55"file="utilssrc\queue_internal.c"/><locationline="57"file="utilssrc\queue_internal.c"/></error>-<errorverbose="Variable 'error' is reassigned a value before the old one has been used."msg="Variable 'error' is reassigned a value before the old one has been used."severity="performance"id="redundantAssignment"><locationline="57"file="utilssrc\queue_internal.c"/><locationline="60"file="utilssrc\queue_internal.c"/></error>-<errorverbose="Memory leak: new_el"msg="Memory leak: new_el"severity="error"id="memleak"><locationline="162"file="utilssrc\queue_internal.c"/></error></errors></results>输出TXT:
: (style) The scope of the variable 'len' can be reduced. : (style) The scope of the variable 'c' can be reduced. -> : (performance) Variable 'r' is reassigned a value before the old one has been used. -> : (performance) Variable 'r' is reassigned a value before the old one has been used. -> : (performance) Variable 'error' is reassigned a value before the old one has been used. -> : (performance) Variable 'error' is reassigned a value before the old one has been used. -> : (performance) Variable 'error' is reassigned a value before the old one has been used. -> : (performance) Variable 'error' is reassigned a value before the old one has been used. -> : (performance) Variable 'error' is reassigned a value before the old one has been used. -> : (performance) Variable 'error' is reassigned a value before the old one has been used. : (error) Memory leak: new_el输出表格:
更多参考:
【 http://blog.csdn.net/e5max/article/details/11489137 】
【 http://blog.csdn.net/akof1314/article/details/7477014 】
【 http://www.cnblogs.com/lancidie/archive/2013/04/13/3019505.html 】
【 http://www.ibm.com/developerworks/cn/java/j-codan/ 】
【 http://www.wuqifu.com/2013/05/07/c静态代码检查工具cppcheck/ 】
