cpplint中filter参数的每个可选项的含义

2023-12-04 00:32

本文主要是介绍cpplint中filter参数的每个可选项的含义,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

文章目录

  • 前言
  • 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: onlyerror 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_useTo see a list of all the categories used in cpplint, pass no arg:--filter=

这一段说明了filter参数的用法,就是以+或者 - 开头接着写规则名,就表示启用或者屏蔽这些规则,使用 --filter= 参数会列举出所有规则,我们来看一下:

C:\Users\Albert>cpplint --filter=build/classbuild/c++11build/c++14build/c++tr1build/deprecatedbuild/endif_commentbuild/explicit_make_pairbuild/forward_declbuild/header_guardbuild/includebuild/include_subdirbuild/include_alphabuild/include_orderbuild/include_what_you_usebuild/namespaces_headersbuild/namespaces_literalsbuild/namespacesbuild/printf_formatbuild/storage_classlegal/copyrightreadability/alt_tokensreadability/bracesreadability/castingreadability/checkreadability/constructorsreadability/fn_sizereadability/inheritancereadability/multiline_commentreadability/multiline_stringreadability/namespacereadability/nolintreadability/nulreadability/stringsreadability/todoreadability/utf8runtime/arraysruntime/castingruntime/explicitruntime/intruntime/initruntime/invalid_incrementruntime/member_string_referencesruntime/memsetruntime/indentation_namespaceruntime/operatorruntime/printfruntime/printf_formatruntime/referencesruntime/stringruntime/threadsafe_fnruntime/vlogwhitespace/blank_linewhitespace/braceswhitespace/commawhitespace/commentswhitespace/empty_conditional_bodywhitespace/empty_if_bodywhitespace/empty_loop_bodywhitespace/end_of_linewhitespace/ending_newlinewhitespace/forcolonwhitespace/indentwhitespace/line_lengthwhitespace/newlinewhitespace/operatorswhitespace/parenswhitespace/semicolonwhitespace/tabwhitespace/todo

这些选项还挺多的,一共有69项,但是现在有一个问题,我就一直没找到这些选项都代表什么含义,有些从名字可以推断出来,比如 whitespace/line_length 应该是指每行的长度限制,但是 whitespace/comma 单单从名字你知道他们是什么意思吗?所以我想简单总结一下。

一个小实验

都说cpplint非常好用,那么接下来我们看看这个工具要怎么用,先新建一个文件teststyle,在里面随便写一些C++代码,如下:

#include <iostream>
#include <map>
using namespace std;class Style
{public:void test() {cout << "This is style class" << endl;}void showName(string& extraMsg){cout << extraMsg << className << endl;}
public:string className;
};int main()
{Style s;string msg_fjakdjfkadjfkadjffjadfkasdjffajsdfkadvljakdjfakdfjkadfjkasdjfkasdfj="class_name:";s.showName( msg_fjakdjfkadjfkadjffjadfkasdjffajsdfkadvljakdjfakdfjkadfjkasdjfkasdfj );if(s.className == ""){cout << "class name for s is empty." << endl;}return 0;
}

这段临时“发挥”的代码可以正常编译运行,然后用cpplint工具检测一下:

> cpplint .\teststyle.cpp
.\teststyle.cpp:0:  No copyright message found.  You should have a line: "Copyright [year] <Copyright Owner>"  [legal/copyright] [5]
.\teststyle.cpp:3:  Do not use namespace using-directives.  Use using-declarations instead.  [build/namespaces] [5]
.\teststyle.cpp:6:  { should almost always be at the end of the previous line  [whitespace/braces] [4]
.\teststyle.cpp:7:  public: should be indented +1 space inside class Style  [whitespace/indent] [3]
.\teststyle.cpp:8:  Tab found; better to use spaces  [whitespace/tab] [1]
.\teststyle.cpp:12:  Is this a non-const reference? If so, make const or use a pointer: string& extraMsg  [runtime/references] [2]
.\teststyle.cpp:13:  { should almost always be at the end of the previous line  [whitespace/braces] [4]
.\teststyle.cpp:16:  public: should be indented +1 space inside class Style  [whitespace/indent] [3]
.\teststyle.cpp:21:  { should almost always be at the end of the previous line  [whitespace/braces] [4]
.\teststyle.cpp:23:  Lines should be <= 80 characters long  [whitespace/line_length] [2]
.\teststyle.cpp:23:  Missing spaces around =  [whitespace/operators] [4]
.\teststyle.cpp:24:  Lines should be <= 80 characters long  [whitespace/line_length] [2]
.\teststyle.cpp:24:  Extra space after ( in function call  [whitespace/parens] [4]
.\teststyle.cpp:24:  Extra space before )  [whitespace/parens] [2]
.\teststyle.cpp:26:  Missing space before ( in if(  [whitespace/parens] [5]
.\teststyle.cpp:27:  { should almost always be at the end of the previous line  [whitespace/braces] [4]
.\teststyle.cpp:32:  Could not find a newline character at the end of the file.  [whitespace/ending_newline] [5]
Done processing .\teststyle.cpp
Total errors found: 17

这么一小段代码居然报出了17个错误,厉不厉害?刺不刺激?下面来逐个解释一下:

.\teststyle.cpp:0: No copyright message found. You should have a line: "Copyright [year] " [legal/copyright] [5]

[legal/copyright] 表示文件中应该有形如 Copyright [year] <Copyright Owner> 版权信息

\teststyle.cpp:3: Do not use namespace using-directives. Use using-declarations instead. [build/namespaces] [5]

[legal/copyright] 表示第3行 using namespace std; 应该使用 using-declarations 而不要使用 using-directives,这个规则可以简单的理解为使用命名空间,每次只引用其中的成员,而不要把整个命名空间都引入。

.\teststyle.cpp:6: { should almost always be at the end of the previous line [whitespace/braces] [4]

[whitespace/braces] 表示第6行的大括号应该放在上一行末尾

.\teststyle.cpp:7: public: should be indented +1 space inside class Style [whitespace/indent] [3]

[whitespace/indent] 表示第7行 public: 应该在行首只保留一个空格

.\teststyle.cpp:8: Tab found; better to use spaces [whitespace/tab] [1]

[whitespace/tab] 表示代码中第8行出现了Tab字符,应该使用空格代替

.\teststyle.cpp:12: Is this a non-const reference? If so, make const or use a pointer: string& extraMsg [runtime/references] [2]

[runtime/references] 表示代码第12行建议使用常引用

.\teststyle.cpp:23: Lines should be <= 80 characters long [whitespace/line_length] [2]

[whitespace/line_length] 表示代码第23行长度超过了80个字符

.\teststyle.cpp:23: Missing spaces around = [whitespace/operators] [4]

[whitespace/operators] 表示代码第23行在赋值符号 = 前后应该有一个空格

.\teststyle.cpp:24: Extra space after ( in function call [whitespace/parens] [4]

[whitespace/parens] 表示代码第24行在小括号后面出现了多余的空格

.\teststyle.cpp:26: Missing space before ( in if( [whitespace/parens] [5]

[whitespace/parens] 表示代码第26行if后面缺少空格

.\teststyle.cpp:32: Could not find a newline character at the end of the file. [whitespace/ending_newline] [5]

[whitespace/ending_newline] 表示32行,文件末尾应该是一个空行

按照上面cpplint提示修改代码如下:

// Copyright [2021] <Copyright albert>
#include <iostream>
#include <map>
using std::cout;
using std::endl;
using std::string;class Style {public:void test() {cout << "This is style class" << endl;}void showName(const string& extraMsg) {cout << extraMsg << className << endl;}public:string className;
};int main() {Style s;string msg = "class_name:";s.showName(msg);if (s.className == "") {cout << "class name for s is empty." << endl;}return 0;
}

自己指定筛选规则

有些人按照上面默认的规则修改代码之后感觉清爽了不少,而有些人却更加郁闷了,因为这些规则是google内部自己根据需要制定的,并不能满足所有人的需求,所以自己需要有目的的做出选择,比如我就决定项目中不写版权信息,那么再使用cpplint 时可以把检测版权信息的规则过滤掉:cpplint --filter="-legal/copyright" .\teststyle.cpp

对照表格

总体来说规则还是很多的,想要在一段代码中展示出所有的情况不太容易,所以整理了下面的表格,对一些规则做了解释,因为有些情况我也没有遇到,所以先空着,后面再慢慢补充,这也是做这篇总结的目的,当有一种规则需求时先来查一下,越来越完整。

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 既检查小括号前缺少空格的情况,也会检查小括号中多空格的情况

==>> 反爬链接,请勿点击,原地爆炸,概不负责!<<==

生活中会有一些感悟的瞬间,娃娃哭闹时大人们总是按照自己的经验来出处理,碰上倔脾气小孩往往毫无作用。其实孩子是最单纯的,想要什么不想要什么都摆在脸上,愿望一旦被满足立马就不哭了,而大人才世界是难处理的,长大的人类善于隐藏和伪装,想要的不一定说出来,说出来不一定是想要的,所以很多人才会羡慕小孩子的天真和无邪~

努力吧!哪管什么真理无穷,进一步有进一步的欢喜

这篇关于cpplint中filter参数的每个可选项的含义的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/451443

相关文章

ABAP怎么把传入的参数刷新到内表里面呢?

1.在执行相关的功能操作之前,优先执行这一段代码,把输入的数据更新入内表里面 DATA: lo_guid TYPE REF TO cl_gui_alv_grid.CALL FUNCTION 'GET_GLOBALS_FROM_SLVC_FULLSCR'IMPORTINGe_grid = lo_guid.CALL METHOD lo_guid->check_changed_data.CALL M

我与Bloom filter

1 海量网页判断用Bloom Filter 面试的时候,一个面试官问我说:“有一个网络爬虫,爬虫程序会不停地爬取页面上的每一个网页,并把爬取后的网页给存储起来,那么爬虫如何判定现在在爬的网页有没有被爬过。” 我当时卡住了半天回答不上来。 面试官给我说用Bloom Filter。 Bloom Filter把爬取过的网页映射到Bloom Filter内,如果再爬取到该网页,Bloom Filt

Java面试八股之JVM参数-XX:+UseCompressedOops的作用

JVM参数-XX:+UseCompressedOops的作用 JVM参数-XX:+UseCompressedOops的作用是启用对象指针压缩(Ordinary Object Pointers compression)。这一特性主要应用于64位的Java虚拟机中,目的是为了减少内存使用。在传统的64位系统中,对象引用(即指针)通常占用8字节(64位),而大部分应用程序实际上并不需要如此大的地址空间

关于命令行参数argv(《学习OpenCV》)

在《学习OpenCV》这本书中,很多示例代码都用到了命令行参数。作为新手,之前总是很困扰,不知道怎么用。偶然的机会终于略知一二了。 在Visual Studio中,我们可以自行设置命令行参数。 如在这个示例程序中,我们想把图像存入argv[1]。 方法如下: 依次点击,项目、属性、配置属性、调试、命令参数。出现下面的界面: 然后进行编辑,即输入图像路径。如:E:\Lena.jpg

国产数据库 - 内核特性 - CloudberryDB中的Runtime Filter

国产数据库 - 内核特性 - CloudberryDB中的Runtime Filter 今年5月份GreenPlum官方将GitHub仓库代码全部删除,各个分支的issues和bugs讨论等信息全部清除,仅将master分支代码进行归档。对于国内应用GPDB的用户来说,这是一个挑战性事件,对与后期维护、升级等都变得非常困难。有幸HashData开源了基于GP衍生版本CloudberryDB版本,

Linux IPC 参数设定,echo 80 /proc/...

文章转自 http://blog.chinaunix.net/uid-22287947-id-1775633.html Linux IPC 参数设定- 命令方式: echo 80 > /proc/sys/vm/overcommit_ratio, etc MSGMNB  每个消息队列的最大字节限制。 MSGMNI  整个系统的最大数量的消息队列。 MSGGSZ  消息片断的大

mysql中in参数过多该如何优化

优化方式概述 未优化前 SELECT * FROM rb_product rb where sku in('1022044','1009786') 方案2示例 public static void main(String[] args) {//往list里面设置3000个值List<String> list = new ArrayList<>();for (int i = 0;

虚拟机常用参数汇总

内存分配相关 -Xmx 堆的大小上限 -Xms 堆区内存初始内存分配的大小 -XX:MaxPermSize 永久代上限 -XX:SurvivorRatio Eden与Survivor区的比例 -XX:+/-UseTLAB 是否使用TLAB来创建对象 -XX:PretenureSizeThreshold 晋升老年代对象大小 -XX:NewRatio 新生代(Eden+2S)和老年代的

C#启动另外一个C#程序,并传递参数

第一个程序:             using System.ComponentModel; using System.IO;         private void button1_Click(object sender, EventArgs e)         {             string target = Path.GetDirectory

c++程序启动一个C#程序,并给C#程序传递参数,程序间实现参数传递

http://www.dotblogs.com.tw/atowngit/archive/2009/12/26/12681.aspx http://hi.baidu.com/baokunkun/item/8e4bba994b9c41d07b7f01ac C++发送端代码: int main(int argc, ch