github:https://github.com/skullboyer/code-check

Code Check

  • 本仓介绍的内容涉及代码静态检查和编码风格检查
  • 但主要放在编码风格检查,lint是基于google编码风格检查cpplint的修改版,起别名也是为了区别
  • lint较于cpplint优势如下:
    • lint支持自定义编码风格检查(通过配置文件),而非cpplint特定于google风格
    • lint支持生成结果文件通过cppcheck上位机查看和跳转

仓说明

.
|————doc (说明文档及过程文件)
|
|————exe (打包好的可执行程序)
|
|————git_hook (嵌入git的钩子文件)
|
|————.scripts (特殊用法的脚本)
|
|____lint.py (cpplint修改版)

应用场景

嵌入git,在提交阶段进行检查

将git_hook中的文件放在自己项目的.git/hooks路径下,下次提交时便会触发代码检查

使用cppcheck上位机解析lint检查的结果

独立使用,基于特定的文件或文件夹

将.scripts中的脚本lint_folder.sh和format_cpplint.sh放在要检查的目录同一级即可

嵌入jenkins进行自动化构建检查

用法

./lint.exe --help

Syntax: lint  [--verbose=#] [--output=vs7] [--filter=-x,+y,...]
[--counting=total|toplevel|detailed] [--root=subdir]
[--linelength=digits] [--headers=x,y,...]
[--quiet][--help][--useage][--generate][--about]
<file> [file] ... Option: output=vs7
output is formate: 'emacs', 'vs7', 'eclipse' verbose=#
output level: 0-5, message less than [verbose] will not be printed quiet
Don't print anything if no errors are found filter=-x,+y,...
To see a list of all the categories used in cpplint, pass no arg: --filter=
Examples: --filter=-whitespace,+whitespace/braces
--filter=whitespace,runtime/printf,+runtime/printf_format
--filter=-,+build/include_what_you_use counting=total
Error statistics style. The total number of errors found is always printed
total => Total errors found:
toplevel => Category 'whitespace' errors found:
detailed => Category 'whitespace/parens' errors found: root=subdir
The root directory used for deriving header guard CPP variable.
Examples:
code directory: src/chrome/browser/ui/browser/
No flag => CHROME_BROWSER_UI_BROWSER_H_
--root=chrome => BROWSER_UI_BROWSER_H_
--root=chrome/browser => UI_BROWSER_H_
--root=.. => SRC_CHROME_BROWSER_UI_BROWSER_H_ linelength=digits
Code line length, default: 120 characters. extensions=extension,extension,...
The allowed file extensions that cpplint will check
Examples:
--extensions=hpp,cpp headers=x,y,...
Examples:
--headers=hpp,hxx
--headers=hpp help
Displays short usage information and exits. useage
Displays detaile usage information and exits. generate
Generate lint config file 'LINT.cfg' and exits about
Displays version information and exits.

配置文件

  1. 生成自定义编码风格配置文件LINT.cfg
$ ./lint.exe --generate
The LINT.cfg configuration file is generated successfully.
  1. 配置文件说明
# Copyright (c) 2022 skull.gu@gmail.com. All rights reserved.

# Stop searching for additional config files.
set noparent # Specifies the line of code for the project
linelength=120 # Error filter
# -: filter, +: pass
filter=+whitespace/preprocess # It's not worth lint-gardening the file.
exclude_files=doc # The root directories are specified relative to CPPLINT.cfg dir
root= # The header extensions
headers= # rule.1
# Naming rules for file names
# 0: indifferent, 1: pure lowercase, 2: lowercase +_, 3: lowercase + digit +_, 4: uppercase, 5: uppercase + digit +_
# default: 3
lint_file_naming= # rule.2
# Whether copyright is required at the beginning of the file
# start of file
# -1: forbidden, 0: indifferent, 1: required
# default: 1
lint_copyright_sof= # rule.3
# Whether a new line is required at the end of the file
# end of file
# -1: forbidden, 0: indifferent, 1: required
# default: 1
lint_newline_eof= # rule.4
# Whether to disable TAB
# -1: forbidden, 0: indifferent
# default: -1
lint_use_tab= # rule.5
# The code line length
# 0: indifferent, >0: length
# default: 120
lint_line_length= # rule.6
# The number of lines in the function body
# 0: indifferent, >0: length
# default: 80
lint_function_line= # rule.7
# Number of Spaces to indent code.
# 0: indifferent, >0: length
# default: 4
lint_space_indent= # rule.8
# Whether extra space at the end of a line is allowed
# -1: forbidden, 0: indifferent
# default: -1
lint_space_eol= # rule.9
# Whether to allow multiple instructions in a row
# -1: forbidden, 0: indifferent
# default: -1
lint_multiple_cmd= # rule.10
# Whether blocks of code are required to use curly braces
# -1: forbidden, 0: indifferent, 1: required
# default: 1
lint_block_braces= # rule.11
# Whether to leave a space before or after the keyword
# -1: forbidden, 0: indifferent, 1: required
# default: 1
lint_space_keyword= # rule.12
# Whether to require 1 space before and after the operator
# -1: forbidden, 0: indifferent, 1: required
# default: 1
lint_space_operator= # rule.13
# Whether to ask preprocessor keyword '#include|#define|if|#elif|#ifdef|#ifndef|#endif' thus
# 0: indifferent, 1: required
# default: 1
lint_preprocess_thus= # rule.14
# For preprocessor keyword '#include|#define|if|#elif|#ifdef|#ifndef|#endif' allow space after '#'
# -1: forbidden, 0: indifferent
# default: -1
lint_preprocess_space= # rule.15
# Code Style selection
# 1. K&R
# if () {
# a = b;
# }
# 2. Allman
# if ()
# {
# a = b;
# }
# 3. Whitesmiths
# if ()
# {
# a = b;
# }
# 4. GNU
# if ()
# {
# a = b;
# }
# default: 1
lint_code_style= # rule.16
# The function name is lowercase +_
# 0: indifferent, 1: required
# default: 1
lint_func_naming= # rule.17
# Macro naming rules
# 0: indifferent, 1: uppercase +_, 2: uppercase + number +_
# default: 1
lint_macro_naming= # rule.18
# Enum naming rules
# 0: indifferent, 1: uppercase +_, 2: uppercase + number +_
# default: 1
lint_enum_naming= # rule.19
# Whether devil numbers are allowed
# -1: forbidden, 0: indifferent
# default: -1
lint_devil_numbers= # rule.20
# Comment style selection
# 0: indifferent, 1: //, 2: /**/
# default: 0
lint_comment_style= # rule.21
# Whether to disallow more than one consecutive blank line
# 0: indifferent, 1: forbidden
# default: 1
lint_blank_line= # rule.22
# Whether the type conversion using C-style cast (static_cast | const_cast | reinterpret_cast)
# 0: indifferent, 1: required
# default: 0
lint_cstyle_cast= # rule.23
# Whether to disallow multiple code statements on the same line
# eg: "a = 1; b = 2;", "if (1) { c = 3; }"
# 0: indifferent, 1: forbidden
# default: 1
lint_multiple_code= # rule.24
# Whether comments are required after '#endif'
# 0: indifferent, 1: required
# default: 0
lint_comment_endif=
  1. 配置文件的与Lint存放在同一级目录,一般是在项目顶级目录
  • 在Lint时会读取配置文件,其中的选项参数决定检查的规则,如果没有找见配置文件则Lint使用默认配置进行规则检查

进展说明

  1. 文件名命名规则 [DONE]
  2. 文件首是否要求书写版权 [DONE]
  3. 文件尾是否要求新行 [DONE]
  4. 是否允许使用TAB [DONE]
  5. 代码行长度要求 [DONE]
  6. 函数体行数要求 [DONE]
  7. 代码缩进空格数 [DONE]
  8. 行尾多余空格是否允许 [DONE]
  9. 是否允许一行出现多条指令 [DONE]
  10. 是否要求代码块(if|else|for|while)使用花括号 [1] [DONE]
  11. 是否要求关键字前后留1个空格 [DONE]
  12. 是否要求运算符前后留1个空格(实现了部分) [TODO]
  13. 是否要求预处理关键字'#include|#>define|if|#elif|#if>def|#ifn>def|#endif'顶格 [DONE]
  14. 是否允许预处理关键字'#include|#>define|if|#elif|#if>def|#ifn>def|#endif'井号后有空格 [DONE]
  15. 代码风格选择(实现了'K&R', 'Allman') [TODO]
  16. 函数名命名规则为小写+_ [DONE]
  17. 宏命名规则 [DONE]
  18. 枚举命名规则 [1] [DONE]
  19. 是否允许出现魔鬼数字 [DONE]
  20. 注释风格选择 [DONE]
  21. 是否禁止连续空行超过1行 [DONE]
  22. 类型转换是否使用C-style cast(static_cast|const_cast|reinterpret_cast) [DONE]
  23. 是否禁止多条代码语句在同一行 [DONE]
  24. '#endif'后是否要求带注释 [DONE]

其他

  • 使用pyinstaller工具将python文件打包成可执行文件,优势:只要windows环境就能运行

    [注]:python2.7不能直接安装,需要特定版本: pip2 install pyinstaller==3.2.1
  • 大家在使用过程中,发现任何bug及改进点欢迎提issue反馈给我

【LINT】cpplint修改版:自定义编码风格检查工具lint的更多相关文章

  1. 痞子衡嵌入式:恩智浦SDK驱动代码风格检查工具预览版

    大家好,我是痞子衡,是正经搞技术的痞子. 接上文 <恩智浦SDK驱动代码风格.模板.检查工具> 继续聊,是的,过去的三天里我花了一些时间做了一个基于 PyQt5 的 GUI 工具,可以帮助 ...

  2. python代码风格检查工具──pylint

    pylint是一个python代码检查工具,可以帮助python程序员方便地检查程序代码的语法和风格,通过这个工具,可以使你的python代码尽量保持完美,哈哈.具体可以检查什么东西呢?比如你写了 f ...

  3. maven-代码风格检查工具

    目录 checkstyle findbugs pmd 其他 checkstyle checkstyle 用于对代码风格进行检查 checkstyle-maven插件 操作示例 mvn clean co ...

  4. 一些达成共识的JavaScript编码风格约定

    如果你的代码易于阅读,那么代码中bug也将会很少,因为一些bug可以很容被调试,并且,其他开发者参与你项目时的门槛也会比较低.因此,如果项目中有多人参与,采取一个有共识的编码风格约定非常有必要.与其他 ...

  5. 一些达成共识的JavaScript编码风格约定【转】

    如果你的代码易于阅读,那么代码中bug也将会很少,因为一些bug可以很容被调试,并且,其他开发者参与你项目时的门槛也会比较低.因此,如果项目中有多人参与,采取一个有共识的编码风格约定非常有必要.与其他 ...

  6. 四种java代码静态检查工具

    [转载]常用 Java 静态代码分析工具的分析与比较 转载自 开源中国社区 http://www.oschina.net/question/129540_23043       1月16日厦门 OSC ...

  7. Kotlin Android项目静态检查工具的使用

    Kotlin Android项目静态检查工具的使用 Kotlin Android项目可用的静态检查工具: Android官方的Lint, 第三方的ktlint和detekt. 静态检查工具 静态检查工 ...

  8. Python静态代码检查工具Flake8

    简介 Flake8 是由Python官方发布的一款辅助检测Python代码是否规范的工具,相对于目前热度比较高的Pylint来说,Flake8检查规则灵活,支持集成额外插件,扩展性强.Flake8是对 ...

  9. TypeScript TSLint(TypeScript代码检查工具)

    TSLint是TypeScript代码的样式风格检查工具.类似于JavaScript的ESLint,或者Ruby的Rubocop. 配置TSLint TSLint是一个外部工具,我们需要进行一次安装工 ...

随机推荐

  1. redis持久存储RDB和AOF的区别及优缺点

    1.前言 最近在项目中使用到Redis做缓存,方便多个业务进程之间共享数据.由于Redis的数据都存放在内存中,如果没有配置持久化,redis重启后数据就全丢失了,于是需要开启redis的持久化功能, ...

  2. (stm32学习总结)—LCD—液晶显示

    显示器简介 显示器属于计算机的 I/O 设备,即输入输出设备.它是一种将特定电子信息输出到屏幕上再反射到人眼的显示工具.常见的有 CRT 显示器.液晶显示器.LED 点阵显示器及OLED 显示器 本章 ...

  3. Unsafe Rust 能做什么

    在不安全的 Rust 中唯一不同的是,你可以: 对原始指针进行解引用 调用"不安全"的函数(包括 C 函数.编译器的内建指令和原始分配器. 实现"不安全"的特性 ...

  4. Spring Framework远程代码执行漏洞复现(CVE-2022-22965)

    1.漏洞描述 漏洞名称 Spring Framework远程代码执行漏洞 公开时间 2022-03-29 更新时间 2022-03-31 CVE编号 CVE-2022-22965 其他编号 QVD-2 ...

  5. 安装ESLint

    安装ESLint ESLint是静态代码检查工具,配合TypeScript使用可以帮助检查TypeScript的语法和代码风格. 添加ESLint到当前工程,yarn add -D eslint. 使 ...

  6. 程序人生:织梦dedecms后台/会员验证码关闭

    dedecms默认是所有的功能几乎只要用到验证码的地方我们都需要验证的,如果要关闭一些验证功能我们可以参考下面的教程,这里介绍了关闭后台,留言板,会员系统等验证码功能关闭了.提示:支持DedeCMS ...

  7. JavaScript中函数节流的理解

    函数节流的原理 函数节流,就是对会频繁触发的函数事件做一些限制,让这些函数可以在每隔一定的时间或者每次满足一定的条件下再触发.一般我们会给他起一个名字throttle.也就是节流的意思.一般这样的函数 ...

  8. 【分享】WeX5的正确打开方式(6)——数据组件初探

    本文是[WeX5的正确打开方式]系列的第6篇文章,简单介绍一下WeX5中数据组件的特性和结构形式. 数据组件的由来 上一篇 WeX5绑定机制我们实现了一个简单的记账本应用,当时所有数据都用 JSON ...

  9. pydev+eclipse写python代码

    首先,下载pydev:PyDev for Eclipse - Browse /pydev at SourceForge.net (建议下载到本地,之前看其他文章时,进行了如下安装: 启动 Eclips ...

  10. Shiro 安全框架详解一(概念+登录案例实现)

    shiro 安全框架详细教程 总结内容 一.RBAC 的概念 二.两种常用的权限管理框架 1. Apache Shiro 2. Spring Security 3. Shiro 和 Spring Se ...