代码风格审查工具Cpplint

1,简介

Cpplint是一个Python脚本,作为一款开源免费的代码静态检测工具,Google也使用它作为自己的C++代码检测工具,也就是说,只要你想代码遵从Google C++代码规范,那么Cpplint将会提供很好的代码静态检测支持。

  • Cpplint.py支持的文件格式包括.cc、.h、.cpp、.cu、.cuh。
  • Cpplint只是一个代码风格检测工具,其并不对代码逻辑、语法错误等进行检查。

2,安装

使用python安装cpplint模块

pip install cpplint 

最新版本v1.4.5 支持python2和python3版本。

3,命令行的详细使用

写一个测试文件test.cpp,内容如下:

#include <iostream>
 
using namespace std;
 
int main()
{
	cout<<"hello !"<<endl;
	
	return 0;
}

运行代码检查

cpplint.exe .\test.cc

会发现有以下错误:

常见报错原因

No copyright message found 没有写Copyright引用
Tab found; better to use spaces 没有使用四个空格代替缩进
Lines should be <= 80 characters long 存在大于80字符的行
Should have a space between // and comment 应该在//和注释之间有一个空格
An else should appear on the same line as the preceding } 符号“{”要代码同一行
If an else has a brace on one side, it should have it on both [readability/braces] 上两个错误经常一起出现,为大括号的位置不合规范
Extra space for operator ++ ++符号和变量间不能有空格
Redundant blank line at the end of a code block should be deleted 代码块最后的空行应该被删除
Line contains invalid UTF-8 (or Unicode replacement character) 使用了中文注释报的错
Line ends in whitespace 代码行最后存在空格
   

按照错误提示,对test.cpp文件进行修改,内容如下:

/*
# Copyright (c) 2015 Bingchun Feng. All rights reserved.
*/

#include <iostream>

int main() {
  std::cout << "hello !" << std::endl;
  return 0;
}

 执行,如下,发现所有错误全部清理掉了:

4,参考链接

  1. 谷歌编码风格
  2. Google C++ Style Guide
  3. Cpplint

 

 

 

 

发布了18 篇原创文章 · 获赞 18 · 访问量 1万+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章