zoukankan      html  css  js  c++  java
  • C/C++代码静态检查工具Cppcheck在VS2008开发环境中的安装配置和使用

    Cppcheck is an analysis tool for C/C++code. Unlike C/C++ compilers and many other analysis tools, it doesn’t detect syntax errors. Cppcheck only detects the types of bugs that the compilers normally fail to detect. The goal is no false positives.

    Cppcheck is rarely wrong about reported errors. But there are many bugs that it doesn’t detect.

    它可以检查不通过编译的文件。

    执行的检查包括:

    (1)、自动变量检查;(2)、数组的边界检查;(3)、class类检查;(4)、过期的函数,废弃函数调用检查;(5)、异常内存使用,释放检查;(6)、内存泄漏检查,主要是通过内存引用指针;(7)、操作系统资源释放检查,中断,文件描述符等;(8)、异常STL 函数使用检查;(9)、代码格式错误,以及性能因素检查。

    安装步骤:

    (1)、从http://sourceforge.net/projects/cppcheck/下载最新版本cppcheck-1.58-x86-Setup.msi,将其安装到D:ProgramFilesCppcheck路径下(注意:不要包含中文路径,也可以从https://github.com/danmar/cppcheck/ 下载源代码);

    (2)、打开vs2008,Tools-->ExternalTools-->点击Add,Title:Cppcheck;Command:D:ProgramFilesCppcheckcppcheck.exe;Argments:--quiet --verbose --template=vs$(ItemPath);Initial directory:$(ItemDir);选中Use Output window;点击OK.

    例如,在F: estCppcheck文件夹下创建了一个Cppcheck工程,F: estCppcheckCppcheck文件夹下存放着一些.cpp文件:

    1. #include "stdafx.h"  
    2. #include <iostream>  
    3.   
    4. using namespace std;  
    5.   
    6. int *p;  
    7.   
    8. int fun1(int sz)  
    9. {  
    10.     delete [] p;  
    11.   
    12.     //Exception thrown in invalid state, 'p' points at deallocated memory.  
    13.     if (sz <= 0)  
    14.     {  
    15.         throw std::runtime_error("size <= 0");  
    16.     }  
    17.   
    18.     p = new int[sz];  
    19. }  
    20.   
    21.   
    22. void *CreateFred()  
    23. {  
    24.     return malloc(100);  
    25. }  
    26.   
    27. void DestroyFred(void *p)  
    28. {  
    29.     free(p);  
    30. }  
    31.   
    32. void f(int x)  
    33. {  
    34.     //(style) Variable ’i’ is assigned a value that is never used  
    35.     //(style) The scope of the variable i can be reduced  
    36.     int i;  
    37.   
    38.     if (x == 0)  
    39.     {  
    40.         i = 0;  
    41.     }  
    42. }  
    43.   
    44. void foo(int x)  
    45. {  
    46.     void *f = CreateFred();  
    47.   
    48.     if (x == 1)  
    49.     {  
    50.         return;  
    51.     }  
    52.     //Memory leak: f  
    53.     DestroyFred(f);  
    54. }  
    55.   
    56. int _tmain(int argc, _TCHAR* argv[])  
    57. {  
    58.     //error: Array 'a[10]' accessed at index 10, which is out of bounds.  
    59.     //Variable 'a' is assigned a value that is never used.  
    60.     char a[10];  
    61.   
    62.     a[10] = 0;  
    63.   
    64.     return 0;  
    65. }  

    (1)、checking all files in a folder:

    D:ProgramFilesCppcheck>cppcheckF: estCppcheckCppcheck

    (2)、stylistic issues(with --enable=style you enable most warning, styleand performance messages):

    D:ProgramFilesCppcheck>cppcheck--enable=style F: estCppcheckCppcheckCppcheck.cpp

    (3)、unused functions:

    D:ProgramFilesCppcheck>cppcheck--enable=unusedFunction F: estCppcheckCppcheck

    (4)、enable all checks:

    D:ProgramFilesCppcheck>cppcheck--enable=all F: estCppcheckCppcheck

    (5)、saving results in file:

    D:ProgramFilesCppcheck>cppcheck --enable=allF: estCppcheckCppcheck 2> F: estCppcheckCppcheckerr.txt

    (6)、multithreaded checking(use 2 threads to check a folder):

    D:ProgramFilesCppcheck>cppcheck-j 2 F: estCppcheckCppcheck

    (7)、xml output:

    D:ProgramFilesCppcheck>cppcheck--xml-version=2 F: estCppcheckCppcheckCppcheck.cpp

    (8)、reformatting the output(to get Visual Studio compatible output):

    D:ProgramFilesCppcheck>cppcheck--template=vs F: estCppcheckCppcheckCppcheck.cpp

    参考文献:

    1、http://sourceforge.net/apps/mediawiki/cppcheck/index.php?title=Main_Page

    2、http://blog.csdn.net/akof1314/article/details/7477014

    3、http://www.cppblog.com/jinq0123/archive/2012/04/10/170739.html

    4、http://blog.sina.com.cn/s/blog_7a4cdec80100s661.html

    5、http://avitebskiy.blogspot.tw/2012/10/poor-mans-visual-studio-cppcheck.html

    代码检查工具列表:

    1、http://en.wikibooks.org/wiki/Introduction_to_Software_Engineering/Tools/Static_Code_Analysis

    2、http://en.wikipedia.org/wiki/List_of_tools_for_static_code_analysis

    3、http://www.cert.org/secure-coding/tools.html

    4、http://spinroot.com/static/

    5、http://www.kuqin.com/testing/20111116/314953.html

    from:http://blog.csdn.net/fengbingchun/article/details/8887843

    http://blog.csdn.net/flyingleo1981/article/details/51320575

  • 相关阅读:
    JavaScript的兼容小坑和调试小技巧
    前端jQuery实现瀑布流
    angular常用属性大全
    Eclipse易卡死
    工作反思
    半年回忆
    努力做到
    产品经理如何应对技术的「做不了」这样的问题(转)
    优秀的产品经理我还有多远
    简历技巧
  • 原文地址:https://www.cnblogs.com/findumars/p/6143425.html
Copyright © 2011-2022 走看看