当前位置: 首页 > news >正文

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: 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

这些选项还挺多的,一共有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 既检查小括号前缺少空格的情况,也会检查小括号中多空格的情况

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

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

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

相关文章:

  • 手把手搭建一个redis集群
  • 换个角度来看看C++中的左值、右值、左值引用、右值引用
  • C/C++中的数据类型转换()/static_cast/dynamic_cast/const_cast/reinterpret_cast
  • C++11中std::move和std::forward到底干了啥
  • 使用box2dweb做一个下落的小球,宝宝玩的不亦乐乎
  • C++中使用std::sort自定义排序规则时要注意的崩溃问题
  • 从一个小题中的应用来体会下std::tie的便利之处
  • Floyd-Warshall——仅用4行代码就能解决多源最短路径问题的算法
  • Dijkstra——通过不断松弛来解决单源最短路径问题的算法
  • C++11中的std::atomic保证的原子性是什么
  • .bat批处理(十):从路径字符串中截取盘符、文件名、后缀名等信息
  • linux环境下从路径字符串中截取目录和文件名信息
  • MD5是用来加密的吗?BCrypt又是什么呢
  • 树的带权路径长度和哈夫曼树
  • 完全图与强连通图的那些坑
  • 9月CHINA-PUB-OPENDAY技术沙龙——IPHONE
  • 「前端早读君006」移动开发必备:那些玩转H5的小技巧
  • 【Under-the-hood-ReactJS-Part0】React源码解读
  • 2017前端实习生面试总结
  • 4个实用的微服务测试策略
  • CSS选择器——伪元素选择器之处理父元素高度及外边距溢出
  • es6
  • JS进阶 - JS 、JS-Web-API与DOM、BOM
  • JS正则表达式精简教程(JavaScript RegExp 对象)
  • Python十分钟制作属于你自己的个性logo
  • React16时代,该用什么姿势写 React ?
  • Shell编程
  • vue2.0项目引入element-ui
  • 表单中readonly的input等标签,禁止光标进入(focus)的几种方式
  • 基于axios的vue插件,让http请求更简单
  • 前端性能优化——回流与重绘
  • 问题之ssh中Host key verification failed的解决
  • 一起参Ember.js讨论、问答社区。
  • 智能网联汽车信息安全
  • ​ ​Redis(五)主从复制:主从模式介绍、配置、拓扑(一主一从结构、一主多从结构、树形主从结构)、原理(复制过程、​​​​​​​数据同步psync)、总结
  • #stm32整理(一)flash读写
  • #Z0458. 树的中心2
  • $jQuery 重写Alert样式方法
  • (02)Cartographer源码无死角解析-(03) 新数据运行与地图保存、加载地图启动仅定位模式
  • (2/2) 为了理解 UWP 的启动流程,我从零开始创建了一个 UWP 程序
  • (c语言)strcpy函数用法
  • (pojstep1.3.1)1017(构造法模拟)
  • (附源码)计算机毕业设计ssm本地美食推荐平台
  • (五)关系数据库标准语言SQL
  • (已更新)关于Visual Studio 2019安装时VS installer无法下载文件,进度条为0,显示网络有问题的解决办法
  • (转)Java socket中关闭IO流后,发生什么事?(以关闭输出流为例) .
  • (转)shell调试方法
  • (转)微软牛津计划介绍——屌爆了的自然数据处理解决方案(人脸/语音识别,计算机视觉与语言理解)...
  • .NET CLR Hosting 简介
  • .NET 自定义中间件 判断是否存在 AllowAnonymousAttribute 特性 来判断是否需要身份验证
  • .Net调用Java编写的WebServices返回值为Null的解决方法(SoapUI工具测试有返回值)
  • .net下的富文本编辑器FCKeditor的配置方法
  • .NET性能优化(文摘)
  • .php结尾的域名,【php】php正则截取url中域名后的内容
  • .vue文件怎么使用_vue调试工具vue-devtools的安装