文章目录

  • 前言
  • 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参数的每个可选项的含义相关推荐

  1. ArcEngine中IFeatureClass.Search(filter, Recycling)方法中Recycling参数的理解

    转自 ArcEngine中IFeatureClass.Search(filter, Recycling)方法中Recycling参数的理解 ArcGIS Engine中总调用IFeatureClass ...

  2. Linux下高性能网络编程中的几个TCP/IP选项

    Linux下高性能网络编程中的几个TCP/IP选项 转自:http://blog.chinaunix.net/u/12592/showart.php?id=2064847 最近在新的平台上测试程序,以 ...

  3. java 拦截器响应中取所有参数,spring boot拦截器中获取request post请求中的参数

    最近有一个需要从拦截器中获取post请求的参数的需求,这里记录一下处理过程中出现的问题. 首先想到的就是request.getParameter(String )方法,但是这个方法只能在get请求中取 ...

  4. Intel Realsense D435 pyrealsense2 get_option_description() rs.option中获取参数描述

    通过get_option_description()函数能够打印rs.option中的参数描述,如: sensor = pipeline.get_active_profile().get_device ...

  5. Hadoop-2.8.0集群搭建、hadoop源码编译和安装、host配置、ssh免密登录、hadoop配置文件中的参数配置参数总结、hadoop集群测试,安装过程中的常见错误

    25. 集群搭建 25.1 HADOOP集群搭建 25.1.1集群简介 HADOOP集群具体来说包含两个集群:HDFS集群和YARN集群,两者逻辑上分离,但物理上常在一起 HDFS集群: 负责海量数据 ...

  6. java虚拟机调优_Java虚拟机中JVM参数调优及其有用的命令

    3.1参数及调优 1.-XX:-HeapDumpOnOutOfMemoryError:当首次遭遇内存溢出时Dump出此时的堆内存. 2.-XX:HeapDumpPath=./java_pid.hpro ...

  7. python filter map区别_python中filter、map、reduce的区别

    python中有一些非常有趣的函数,今天也来总结一下,不过该类的网上资料也相当多,也没多少干货,只是习惯性将一些容易遗忘的功能进行整理. lambda 为关键字.filter,map,reduce为内 ...

  8. Jquery中AJAX参数详细介绍

    转载:http://www.cnblogs.com/qiufuwu618/archive/2012/12/20/2826190.html Jquery中AJAX参数详细列表: 参数名 类型 描述 ur ...

  9. vue 如何将参数放到连接上_通过Vue路由传参的两种方式及Vue组件中接收参数的方式...

    1. Vue传参方式 1.1 通过动态路由传参 我们经常需要把某种模式匹配到的所有路由,全都映射到同个组件.例如,我们有一个 User 组件,对于所有 ID 各不相同的用户,都要使用这个组件来渲染.那 ...

最新文章

  1. morphologyEx 形态学
  2. 直播预告 | 共识、区块链和全球一体化经济
  3. rufus中gpt和mrb磁盘_UEFI/BIOS/MBR/GPT启动过程详解与常见系统启动问题
  4. Unity发布WebGl注意事项
  5. JDK 9 对字符串 String 的优化,挺有意思!
  6. Android Multimedia框架总结(二十)MediaCodec状态周期及Codec与输入/输出Buffer过程(附实例)
  7. 【转】多态与 new [C#]
  8. QT与Coin3D实现机器人的仿真
  9. Visio 2010工具产品密钥
  10. Linux自学之旅-基础命令(Ext4文件系统)
  11. MFC Date Time Picker的使用
  12. 最详细的vs2015使用教程(有图)
  13. php在线售卡系统,云尚在线发卡系统PHP源码|专门为个人或小型企业提供在线售卡,订单处理系统...
  14. word“您的组织策略阻止我们为您完成此操作”解决记录
  15. Spring系列九:Spring 事务
  16. 皇后游戏c语言,C语言中关于4皇后或8皇后问题!
  17. 盘点Hadoop生态圈:13个让大象飞起来的开源工具
  18. 读《解忧杂货店》有感
  19. 通过集成第三方IM实现聊天应用
  20. 软件功能测试包含了哪些测试项目?功能测试报告收费标准

热门文章

  1. 优恩-关于ESD管(ESD静电二极管)的产品特性
  2. 日活、周活(周重活)、月活 统计
  3. Enhancement(5)--Field Exits {转载}
  4. 为什么RGB 与 CMYK的差异,会有所不同?
  5. c语言中scanf(%d%*c, n);的意思
  6. Win10重装的方法?一键重装Win10的图文版教程
  7. 要只看每日减少的新增数量,别忘了疫情最开始的时候可能也只有几个人患病
  8. WEB前端学习笔记-HTML
  9. TP-LINK telnet远程 重启路由器(转)
  10. c语言源码什么意思,请问C语言源代码什么意思?