格式
Syntax: cpplint.py [--verbose=#] [--output=emacs|eclipse|vs7|junit|sed|gsed]
[--filter=-x,+y,...]
[--counting=total|toplevel|detailed] [--root=subdir]
[--repository=path]
[--linelength=digits] [--headers=x,y,...]
[--recursive]
[--exclude=path]
[--extensions=hpp,cpp,...]
[--includeorder=default|standardcfirst]
[--quiet]
[--version]
<file> [file] ...
解析
-
–verbose=#
cpplint 把检查结果分成0 ~ 5
六个等级。等级 0 为输出全部结果。
换句话来说就是当错误等级 >= verbose 时,就输出。 -
–output=emacs|eclipse|vs7|junit|sed|gsed
默认情况下,输出都是被格式化的,以简化 emacs的解析。visual studio 也可以使用兼容的vs7来输出。输出也支持eclipse(eclipse) 及 JUnit(junit) 。像在 Jenkens 和 Bamboo 中支持的 XML解析器也受支持。
-
–filter=-x,+y,…
分类过滤器,决定是否要输出某些类型的错误。默认输出所有的错误类型。
-
:不输出这个类型的错误。
+
:输出这个类型的错误。
比如:
--filter=-whitespace,+whitespace/braces
--filter=whitespace,runtime/printf,+runtime/printf_format
--filter=-,+build/include_what_you_use
可以这样来查找所有的错误类型:
cpplint.py --filter=
build/class
build/c++11
build/c++14
build/c++tr1
build/deprecated
...
- –counting
输出结果统计,有三个选项, 默认选项是 total:
total
:只输出错误总数
toplevel
:输出各顶层分类的错误统计,比如说 ‘build’, ‘whitespace’
detailed
:输出各子分类的错误统计,比如说 ‘build/class’, ‘whitespace/braces’
例子:
cpplint.py --verbose=5 --counting=toplevel adb.cpp
>>>
adb.cpp:33: <chrono> is an unapproved C++11 header. [build/c++11] [5]
adb.cpp:34: <condition_variable> is an unapproved C++11 header. [build/c++11] [5]
adb.cpp:35: <mutex> is an unapproved C++11 header. [build/c++11] [5]
adb.cpp:37: <thread> is an unapproved C++11 header. [build/c++11] [5]
adb.cpp:62: Do not use namespace using-directives. Use using-declarations instead. [build/namespaces] [5]
adb.cpp:135: Missing space before ( in switch( [whitespace/parens] [5]
adb.cpp:135: Missing space before { [whitespace/braces] [5]
adb.cpp:322: Missing space before ( in switch( [whitespace/parens] [5]
adb.cpp:322: Missing space before { [whitespace/braces] [5]
adb.cpp:390: Missing space before ( in if( [whitespace/parens] [5]
Done processing adb.cpp
Category 'build' errors found: 5
Category 'whitespace' errors found: 5
Total errors found: 10
- –root
指定代码的根目录。这个参数会影响头文件保护的宏命名检查结果,比如, 有个头文件的路径为chrome/browser/ui/browser.h
, 则头文件保护的宏命名为:
未设置--root => CHROME_BROWSER_UI_BROWSER_H_
--root=chrome => BROWSER_UI_BROWSER_H_
--root=chrome/browser => UI_BROWSER_H_
-
–linelength
一行代码的长度限制。默认值为 80 个字符,超 80 个字符后,会提示错误。对于现代的大屏幕来说,我感觉 80 个字符太小了,设置为 120 比较合理。 -
–headers
设置头文件的扩展名,默认只接受 .h 后缀。例子:
--headers=hpp,hxx
--headers=hpp
- –extensions
设置 cpplint 要检查的文件扩展名,比如:
--extensions=hpp,cpp
- –quiet
如果没有错误,则不输出任何信息 - 配置文件
我们可以在代码目录里面放一个 CPPLINT.cfg 文件,把参数配置都写到里面来,比如:
set noparent
filter=+filter1,-filter2
exclude_files=regex
linelength=80
root=subdir
headers=x,y
规则说明
每一项规则都定义成一个函数,以 CheckXXX
方法命名。
检查到错误之后调用 Error(filename, linenum, category, confidence, message)
函数来输出错误。
filename
: 文件名
linenum
: 出错行数
category
: 错误类型
confidence
: 错误等级, 1 ~ 5
message
错误提示
我们只需要参考现有的规则添加即可。基本上都是对正则表达式的使用而已。也因为 cpplint 只是使用了模式匹配的方法来做检查,并没有进行语法分析,所以有很多规则没法检查。比如,命名规则等。这也是一个非常大的缺陷。不过目前也没有其他更优秀的开源工具了,就将就着用先了。
多文件扫描指导(参考)
- https://blog.csdn.net/qq_36631379/article/details/114701205
- https://tips.tutorialhorizon.com/2017/10/12/how-to-run-cpplint-on-recursively-on-multiple-folders/
- https://tips.tutorialhorizon.com/2017/05/23/run-cpplint-recursively-using-glob-pattern/