cpplint中filter参数
前言
cpplint 是一款优秀的代码格式检查工具,有了它可以统一整个团队的代码风格,完整的工具就是一个Python脚本,如果安装了Python环境,直接使用 pip install cpplint 命令就可以安装了,非常的方便。
具体的使用方法可以通过 cpplint --help 查询,语法如下:
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] ...
Style checker for C/C++ source files.
This is a fork of the Google style checker with minor extensions.
其中有一句 [--filter=-x,+y,...] 就是本文总结的重点。
filter是什么
这个filter究竟是什么呢?我将它强行解释成代码的“过滤器”,cpplint 是一款检查C++源代码风格的工具,遵循的是Google的编码风格,但是这些规则并不是对于所有人都合适,我们应该有目的进行选择,这个filter参数就是用来屏蔽或者启用一些规则的,我们还是从帮助文档里来看,其中有一段
filter=-x,+y,...
Specify a comma-separated list of category-filters to apply: only
error messages whose category names pass the filters will be printed.
(Category names are printed with the message and look like
"[whitespace/indent]".) Filters are evaluated left to right.
"-FOO" means "do not print categories that start with FOO".
"+FOO" means "do print categories that start with FOO".
Examples: --filter=-whitespace,+whitespace/braces
--filter=-whitespace,-runtime/printf,+runtime/printf_format
--filter=-,+build/include_what_you_use
To see a list of all the categories used in cpplint, pass no arg:
--filter=
这一段说明了filter参数的用法,就是以+或者 - 开头接着写规则名,就表示启用或者屏蔽这些规则,使用 --filter= 参数会列举出所有规则,我们来看一下:
C:\Users\Albert>cpplint --filter=
build/class
build/c++11
build/c++14
build/c++tr1
build/deprecated
build/endif_comment
build/explicit_make_pair
build/forward_decl
build/header_guard
build/include
build/include_subdir
build/include_alpha
build/include_order
build/include_what_you_use
build/namespaces_headers
build/namespaces_literals
build/namespaces
build/printf_format
build/storage_class
legal/copyright
readability/alt_tokens
readability/braces
readability/casting
readability/check
readability/constructors
readability/fn_size
readability/inheritance
readability/multiline_comment
readability/multiline_string
readability/namespace
readability/nolint
readability/nul
readability/strings
readability/todo
readability/utf8
runtime/arrays
runtime/casting
runtime/explicit
runtime/int
runtime/init
runtime/invalid_increment
runtime/member_string_references
runtime/memset
runtime/indentation_namespace
runtime/operator
runtime/printf
runtime/printf_format
runtime/references
runtime/string
runtime/threadsafe_fn
runtime/vlog
whitespace/blank_line
whitespace/braces
whitespace/comma
whitespace/comments
whitespace/empty_conditional_body
whitespace/empty_if_body
whitespace/empty_loop_body
whitespace/end_of_line
whitespace/ending_newline
whitespace/forcolon
whitespace/indent
whitespace/line_length
whitespace/newline
whitespace/operators
whitespace/parens
whitespace/semicolon
whitespace/tab
whitespace/todo
对照表格
总体来说规则还是很多的,想要在一段代码中展示出所有的情况不太容易,所以整理了下面的表格,对一些规则做了解释,因为有些情况我也没有遇到,所以先空着,后面再慢慢补充,这也是做这篇总结的目的,当有一种规则需求时先来查一下,越来越完整。
filter 解释
build/class
build/c++11
build/c++14
build/c++tr1
build/deprecated
build/endif_comment
build/explicit_make_pair
build/forward_decl
build/header_guard ①头文件需要添加只被包含一次的宏,#ifndef、#define
build/include
build/include_subdir
build/include_alpha
build/include_order
build/include_what_you_use
build/namespaces_headers
build/namespaces_literals
build/namespaces ①不要引入整个命名空间,仅引入需要使用的成员
build/printf_format
build/storage_class
legal/copyright ①文件中缺少版权信息
readability/alt_tokens
readability/braces ①如果if一个分支包含大括号,那么其他分支也应该包括大括号
readability/casting
readability/check
readability/constructors
readability/fn_size
readability/inheritance
readability/multiline_comment
readability/multiline_string
readability/namespace
readability/nolint
readability/nul
readability/strings
readability/todo ①TODO注释中应包括用户名
readability/utf8 ①文件应该使用utf8编码
runtime/arrays
runtime/casting
runtime/explicit
runtime/int
runtime/init
runtime/invalid_increment
runtime/member_string_references
runtime/memset
runtime/indentation_namespace
runtime/operator
runtime/printf ①使用sprintf替换strcpy、strcat
runtime/printf_format
runtime/references ①确认是否要使用常引用
runtime/string
runtime/threadsafe_fn
runtime/vlog
whitespace/blank_line
whitespace/braces ①左大括号应该放在上一行末尾
whitespace/comma ①逗号后面应该有空格
whitespace/comments ①//后应该紧跟着一个空格
whitespace/empty_conditional_body
whitespace/empty_if_body
whitespace/empty_loop_body
whitespace/end_of_line
whitespace/ending_newline ①文件末尾需要空行
whitespace/forcolon
whitespace/indent ①public、protected、private前需要1个空格
whitespace/line_length ①代码行长度有限制
whitespace/newline
whitespace/operators ①操作符前后需要有空格
whitespace/parens ①if、while、for、switch后的小括号前需要有空格。②小括号中的首个参数前和最后参数尾不应有空格
whitespace/semicolon ①分号后缺少空格,比如{ return 1;}
whitespace/tab ①使用空格代替tab
whitespace/todo ①TODO注释前空格太多。②TODO注释中用户名后应该有一个空格
总结
cpplint 是一个检查c++代码风格的小工具
cpplint.py 其实是一个Python脚本文件,使用前可以先安装Python环境
使用 cpplint 时默认遵循的是Google的代码风格
为了让代码检测符合自己的习惯,需要使用--filter=参数选项,有多种规则可以选择或者忽略
--filter=中的规则是一个大类,比如 whitespace/parens 既检查小括号前缺少空格的情况,也会检查小括号中多空格的情况
原文链接:https://blog.csdn.net/albertsh/article/details/118076005