CPP-Lint之C++编码规范检查

简介:

如何使用C++编码规范检查工具CPP-lint对C++代码进行检查。通过检查引导开发人员以标准的、规范的方式来约定C++代码,执行统一C++编码规范,使开发人员养成良好的编码习惯。

范围:

对C++程序的格式等方面进行检查。

一、安装

cpplint.py是一个用来分析源文件,能检查出多种风格错误的工具。它并不完美,甚至还会漏报和误报,但它仍然是一个非常有用的工具。

1.2 安装Python

参考《Python安装(Windows)》

1.3 下载cpplint.py

从网站http://google-styleguide.googlecode.com/svn/trunk/cpplint获取cpplint.py文件。

将cpplint.py文件放在C:\cpplint\目录下。

二、使用

2.1 CPP-lint与VS2008的集成

2.1.1 单个文件分析

启动VS.NET,点击“工具->外部工具“:

新建配置:

标题	cpp-lint current file
命令	C:\Python27\python.exe
参数	C:\cpplint\cpplint.py $(ItemDir)$(ItemFileName)$(ItemExt)
初始目录	$(ItemDir)

勾选“使用输出窗口”

使用:点击“VS2008->工具->cpp-lint current file”

检查结果:

2.1.2 项目文件分析

网站http://www.weihenstephan.de/~syring/win32/UnxUtils.zip下载UnxUtils.zip。需要利用unix中的find等命令来查找当前目录下的C和C++文件,然后再将它们送给lint程序处理。

解压缩UnxUtils.zip到C盘,这样路径为C:\unxutils。

新建配置:

标题	cpp-lint project file(这个可以任意命名)
命令	C:\unxutils\cpp-lint_project.bat(自建批处理文件)
参数	$(ProjectDir)
初始目录	

勾选“使用输出窗口”; 点击“确定”

C:\unxutils\cpp-lint_project.bat文件内容:

cd %1
set str=%1
%str:~1,2%
C:\unxutils\usr\local\wbin\find -name *.c -o -name *.cpp -o -name *.h | C:\unxutils\usr\local\wbin\xargs C:\Python27\python.exe C:\cpplint\cpplint.py

使用:点击“VS2008->工具->cpp-lint project file”

检查结果:

C:\Windows\system32>cd "D:\chping\cppcheck\testpclint\testpclint\\" 

C:\Windows\system32>set str="D:\chping\cppcheck\testpclint\testpclint\\" 

C:\Windows\system32>D:

D:\chping\cppcheck\testpclint\testpclint>C:\unxutils\usr\local\wbin\find -name *.c -o -name *.cpp -o -name *.h   | C:\unxutils\usr\local\wbin\xargs C:\Python27\python.exe C:\cpplint\cpplint.py 
./main.cpp:0:  No copyright message found.  You should have a line: "Copyright [year] <Copyright Owner>"  [legal/copyright] [5]
./main.cpp:1:  Streams are highly discouraged.  [readability/streams] [3]
./main.cpp:2:  Do not use namespace using-directives.  Use using-declarations instead.  [build/namespaces] [5]
./main.cpp:4:  Extra space after ( in function call  [whitespace/parens] [4]
./main.cpp:5:  Tab found; better to use spaces  [whitespace/tab] [1]
Done processing ./main.cpp
./TestString.cpp:0:  No copyright message found.  You should have a line: "Copyright [year] <Copyright Owner>"  [legal/copyright] [5]
./TestString.cpp:1:  Include the directory when naming .h files  [build/include] [4]
./TestString.cpp:4:  { should almost always be at the end of the previous line  [whitespace/braces] [4]
./TestString.cpp:8:  { should almost always be at the end of the previous line  [whitespace/braces] [4]
Done processing ./TestString.cpp
./TestString.h:0:  No copyright message found.  You should have a line: "Copyright [year] <Copyright Owner>"  [legal/copyright] [5]
./TestString.h:0:  No #ifndef header guard found, suggested CPP variable is: D:_CHPING_CPPCHECK_TESTPCLINT_TESTPCLINT_TESTSTRING_H_  [build/header_guard] [5]
./TestString.h:4:  { should almost always be at the end of the previous line  [whitespace/braces] [4]
./TestString.h:5:  Labels should always be indented at least one space.  If this is a member-initializer list in a constructor or the base class list in a class definition, the colon should be on the following line.  [whitespace/labels] [4]
./TestString.h:6:  Tab found; better to use spaces  [whitespace/tab] [1]
./TestString.h:7:  Tab found; better to use spaces  [whitespace/tab] [1]
Done processing ./TestString.h
Total errors found: 15

2.2 CPP-lint与Source Insight的集成

2.2.1 单个文件分析

打开Source Insight。点击菜单“Options”->“Custom Commands…”

点击按钮“Add…”,添加新命令

输入“cpp-lint current file”(可以随意输入),点击按钮“OK”

按如下方式输入命令及选择:

Command	cpp-lint current file
Run	C:\Python27\python.exe C:\cpplint\cpplint.py %f
Pattern	^\([^]*\)\([0-9]+\)

点击按钮“Menu…”。在弹出窗口,Menu选择Search, Menu Contents选择<end of menu>

点击按钮“Insert”

添加快捷键,点击按钮“Keys…”

点击按钮“Assign New Key…”

按下快捷键,此处是ALT+CTRL+C(可自己随意定义)

点击按钮“OK“。在Search菜单下,已加入“cpp-lint current file”

使用:示例代码

#include <iostream>
using namespace std;

int main( void ) {
	int d = 6;
}

点击“Search->cpp-lint current file”

检查结果:

D:\chping\cppcheck\testpclint\testpclint\main.cpp:0:  No copyright message found.  You should have a line: "Copyright [year] <Copyright Owner>"  [legal/copyright] [5]
D:\chping\cppcheck\testpclint\testpclint\main.cpp:1:  Streams are highly discouraged.  [readability/streams] [3]
D:\chping\cppcheck\testpclint\testpclint\main.cpp:2:  Do not use namespace using-directives.  Use using-declarations instead.  [build/namespaces] [5]
D:\chping\cppcheck\testpclint\testpclint\main.cpp:4:  Extra space after ( in function call  [whitespace/parens] [4]
D:\chping\cppcheck\testpclint\testpclint\main.cpp:5:  Tab found; better to use spaces  [whitespace/tab] [1]
Done processing D:\chping\cppcheck\testpclint\testpclint\main.cpp
Total errors found: 5

2.2.2 项目文件分析

创建方式类似于2.2.1。其中命令改为:

Command	cpp-lint project file
Run	C:\unxutils\usr\local\wbin\find.exe %d -name *.c -o -name *.cpp | C:\unxutils\usr\local\wbin\xargs C:\Python27\python.exe C:\cpplint\cpplint.py %f
Pattern	^\([^]*\)\([0-9]+\)

示例:直接显示检查结果

2.3 CPP-lint命令模式

查看帮助,在命令行模式下输入帮助命令可以查看到的信息:

命令:python c:\cpplint\cpplint.py –help

只检查.cc,.cpp和.h文件:

输出格式默认emacs。且只有emacs和vs7格式。

2.3.1 单个文件分析

使用命令行模式进行代码规范检查。

命令:python C:\cpplint\cpplint.py 路径+文件名 –output=vs7

2.3.2 项目文件分析

自建批处理文件cpp-lint_projectcmd.bat。

C:\unxutils\usr\local\wbin\find "D:/chping/cppcheck/testpclint/testpclint" -name *.c -o -name *.cpp -o -name *.h | C:\unxutils\usr\local\wbin\xargs C:\Python27\python.exe C:\cpplint\cpplint.py

其中”D:/chping/cppcheck/testpclint/testpclint”指的是工程目录。检查其他工程时,替换该目录即可。

处理结果如下:

如果不希望在批处理文件中加入目录功能,如下:

C:\unxutils\usr\local\wbin\find -name *.c -o -name *.cpp -o -name *.h | C:\unxutils\usr\local\wbin\xargs c:\lint\lint-nt -i"c:\lint"  -u +fcp c:\lint\std.lnt

这样的话,只需要在命令模式下,将当前目录直接定位到工程目录即可:

2.4 Notepad++配置cpp-lint

2.4.1 单个文件分析

启动Notepad++,进入插件管理

选择安装NppExec插件

点击“Install”,进入安装状态

安装过程中,点击“是”

NppExec插件安装成功后,就能在插件菜单栏下看到。点击进入“Execute…”

输入命令,然后点击“Save”按钮
命令:C:\Python27\python.exe C:\cpplint\cpplint.py “$(FULL_CURRENT_PATH)”

在Script name:中输入命令名:cpplint current file(这个可以随意输入),再点击“Save”按钮

配置宏,进入宏配置界面

在Menu Item中输入如下的Associated script:下拉框中选择cppliint current file。

勾选“Place to the Macros submenu”,然后点击“Add/Modify”按钮,在点击“OK”按钮

这样在宏下面就可以看到cpplint current file了。

给新定义宏配置快捷键

自定义宏:进入”Plugin commands”Tab页面,找到“cpplint current file”,双击右侧,输入快捷键,然后点击“OK”按钮。

快捷键定义成功后,在菜单栏中就能看到

使用

发表回复