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

Code Check

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

仓说明

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

应用场景

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

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

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

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

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

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

用法

  1. ./lint.exe --help
  2. Syntax: lint [--verbose=#] [--output=vs7] [--filter=-x,+y,...]
  3. [--counting=total|toplevel|detailed] [--root=subdir]
  4. [--linelength=digits] [--headers=x,y,...]
  5. [--quiet][--help][--useage][--generate][--about]
  6. <file> [file] ...
  7. Option:
  8. output=vs7
  9. output is formate: 'emacs', 'vs7', 'eclipse'
  10. verbose=#
  11. output level: 0-5, message less than [verbose] will not be printed
  12. quiet
  13. Don't print anything if no errors are found
  14. filter=-x,+y,...
  15. To see a list of all the categories used in cpplint, pass no arg: --filter=
  16. Examples: --filter=-whitespace,+whitespace/braces
  17. --filter=whitespace,runtime/printf,+runtime/printf_format
  18. --filter=-,+build/include_what_you_use
  19. counting=total
  20. Error statistics style. The total number of errors found is always printed
  21. total => Total errors found:
  22. toplevel => Category 'whitespace' errors found:
  23. detailed => Category 'whitespace/parens' errors found:
  24. root=subdir
  25. The root directory used for deriving header guard CPP variable.
  26. Examples:
  27. code directory: src/chrome/browser/ui/browser/
  28. No flag => CHROME_BROWSER_UI_BROWSER_H_
  29. --root=chrome => BROWSER_UI_BROWSER_H_
  30. --root=chrome/browser => UI_BROWSER_H_
  31. --root=.. => SRC_CHROME_BROWSER_UI_BROWSER_H_
  32. linelength=digits
  33. Code line length, default: 120 characters.
  34. extensions=extension,extension,...
  35. The allowed file extensions that cpplint will check
  36. Examples:
  37. --extensions=hpp,cpp
  38. headers=x,y,...
  39. Examples:
  40. --headers=hpp,hxx
  41. --headers=hpp
  42. help
  43. Displays short usage information and exits.
  44. useage
  45. Displays detaile usage information and exits.
  46. generate
  47. Generate lint config file 'LINT.cfg' and exits
  48. about
  49. Displays version information and exits.

配置文件

  1. 生成自定义编码风格配置文件LINT.cfg
  1. $ ./lint.exe --generate
  2. The LINT.cfg configuration file is generated successfully.
  1. 配置文件说明
  1. # Copyright (c) 2022 skull.gu@gmail.com. All rights reserved.
  2. # Stop searching for additional config files.
  3. set noparent
  4. # Specifies the line of code for the project
  5. linelength=120
  6. # Error filter
  7. # -: filter, +: pass
  8. filter=+whitespace/preprocess
  9. # It's not worth lint-gardening the file.
  10. exclude_files=doc
  11. # The root directories are specified relative to CPPLINT.cfg dir
  12. root=
  13. # The header extensions
  14. headers=
  15. # rule.1
  16. # Naming rules for file names
  17. # 0: indifferent, 1: pure lowercase, 2: lowercase +_, 3: lowercase + digit +_, 4: uppercase, 5: uppercase + digit +_
  18. # default: 3
  19. lint_file_naming=
  20. # rule.2
  21. # Whether copyright is required at the beginning of the file
  22. # start of file
  23. # -1: forbidden, 0: indifferent, 1: required
  24. # default: 1
  25. lint_copyright_sof=
  26. # rule.3
  27. # Whether a new line is required at the end of the file
  28. # end of file
  29. # -1: forbidden, 0: indifferent, 1: required
  30. # default: 1
  31. lint_newline_eof=
  32. # rule.4
  33. # Whether to disable TAB
  34. # -1: forbidden, 0: indifferent
  35. # default: -1
  36. lint_use_tab=
  37. # rule.5
  38. # The code line length
  39. # 0: indifferent, >0: length
  40. # default: 120
  41. lint_line_length=
  42. # rule.6
  43. # The number of lines in the function body
  44. # 0: indifferent, >0: length
  45. # default: 80
  46. lint_function_line=
  47. # rule.7
  48. # Number of Spaces to indent code.
  49. # 0: indifferent, >0: length
  50. # default: 4
  51. lint_space_indent=
  52. # rule.8
  53. # Whether extra space at the end of a line is allowed
  54. # -1: forbidden, 0: indifferent
  55. # default: -1
  56. lint_space_eol=
  57. # rule.9
  58. # Whether to allow multiple instructions in a row
  59. # -1: forbidden, 0: indifferent
  60. # default: -1
  61. lint_multiple_cmd=
  62. # rule.10
  63. # Whether blocks of code are required to use curly braces
  64. # -1: forbidden, 0: indifferent, 1: required
  65. # default: 1
  66. lint_block_braces=
  67. # rule.11
  68. # Whether to leave a space before or after the keyword
  69. # -1: forbidden, 0: indifferent, 1: required
  70. # default: 1
  71. lint_space_keyword=
  72. # rule.12
  73. # Whether to require 1 space before and after the operator
  74. # -1: forbidden, 0: indifferent, 1: required
  75. # default: 1
  76. lint_space_operator=
  77. # rule.13
  78. # Whether to ask preprocessor keyword '#include|#define|if|#elif|#ifdef|#ifndef|#endif' thus
  79. # 0: indifferent, 1: required
  80. # default: 1
  81. lint_preprocess_thus=
  82. # rule.14
  83. # For preprocessor keyword '#include|#define|if|#elif|#ifdef|#ifndef|#endif' allow space after '#'
  84. # -1: forbidden, 0: indifferent
  85. # default: -1
  86. lint_preprocess_space=
  87. # rule.15
  88. # Code Style selection
  89. # 1. K&R
  90. # if () {
  91. # a = b;
  92. # }
  93. # 2. Allman
  94. # if ()
  95. # {
  96. # a = b;
  97. # }
  98. # 3. Whitesmiths
  99. # if ()
  100. # {
  101. # a = b;
  102. # }
  103. # 4. GNU
  104. # if ()
  105. # {
  106. # a = b;
  107. # }
  108. # default: 1
  109. lint_code_style=
  110. # rule.16
  111. # The function name is lowercase +_
  112. # 0: indifferent, 1: required
  113. # default: 1
  114. lint_func_naming=
  115. # rule.17
  116. # Macro naming rules
  117. # 0: indifferent, 1: uppercase +_, 2: uppercase + number +_
  118. # default: 1
  119. lint_macro_naming=
  120. # rule.18
  121. # Enum naming rules
  122. # 0: indifferent, 1: uppercase +_, 2: uppercase + number +_
  123. # default: 1
  124. lint_enum_naming=
  125. # rule.19
  126. # Whether devil numbers are allowed
  127. # -1: forbidden, 0: indifferent
  128. # default: -1
  129. lint_devil_numbers=
  130. # rule.20
  131. # Comment style selection
  132. # 0: indifferent, 1: //, 2: /**/
  133. # default: 0
  134. lint_comment_style=
  135. # rule.21
  136. # Whether to disallow more than one consecutive blank line
  137. # 0: indifferent, 1: forbidden
  138. # default: 1
  139. lint_blank_line=
  140. # rule.22
  141. # Whether the type conversion using C-style cast (static_cast | const_cast | reinterpret_cast)
  142. # 0: indifferent, 1: required
  143. # default: 0
  144. lint_cstyle_cast=
  145. # rule.23
  146. # Whether to disallow multiple code statements on the same line
  147. # eg: "a = 1; b = 2;", "if (1) { c = 3; }"
  148. # 0: indifferent, 1: forbidden
  149. # default: 1
  150. lint_multiple_code=
  151. # rule.24
  152. # Whether comments are required after '#endif'
  153. # 0: indifferent, 1: required
  154. # default: 0
  155. 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. CHAR 和 VARCHAR 的区别?

    1.CHAR 和 VARCHAR 类型在存储和检索方面有所不同 2.CHAR 列长度固定为创建表时声明的长度,长度值范围是 1 到 255 当 CHAR 值被存储时,它们被用空格填充到特定长度,检索  ...

  2. 什么是可重入锁(ReentrantLock)?

    举例来说明锁的可重入性 public class UnReentrant{ Lock lock = new Lock(); public void outer(){ lock.lock(); inne ...

  3. 通过pink构造简易部署脚本

    安装git        https://www.cnblogs.com/youxiu326/p/10540753.html 安装maven https://www.cnblogs.com/youxi ...

  4. github 编写README时常用的写法

    参考:https://github.com/HeTingwei/ReadmeLearn#%E7%BC%96%E5%86%99readme%E6%97%B6%E5%B8%B8%E7%94%A8%E7%9 ...

  5. C++大作业——教职工管理系统

    教职工信息管理系统 1.问题描述: 设计一个学校职工管理系统,要求实现如下功能:建立职工信息数据, 包括职工编号.姓名. 性别.工资.出生时间.岗位.参加工作时间和年 龄(必须计算得到),初始模拟数据 ...

  6. android webview与jquery mobile相互通信

    最近做android项目中遇到要在webview中做与js交互相关的东东,涉及到js中调用android本地的方法,于是查了资料整理了一下android和js互相调用的过程.如下demo,demo的主 ...

  7. ecahrts实现动态刷新(隔几秒重新显示)

    代码: <script> var chartDom = document.getElementById('main3'); var myChart = echarts.init(chart ...

  8. 论文阅读总结-Patient clustering improves efficiency of federated machine learning to predict mortality and hospital stay time using distributed electronic medical records

    一.论文提出的方法: 使用进入ICU前48h的用药特征作为预测因子预测重症监护患者的死亡率和ICU住院时间. 用到了联邦学习,自编码器,k-means聚类算法,社区检测. 数据集:从50家患者人数超过 ...

  9. IDEA中Tomcat找不到war包导出按钮解决办法

    解决办法 (1) 打开Idea,点击File,然后点击Project Structure-,进入项目结构 (2) 具体步骤看下图: (3) 具体步骤如下图: (4) 具体步骤如下图: (5) 问题解决 ...

  10. web.xml 配置 contextConfigLocation

    web.xml中classpath:和classpath*:  有什么区别? classpath:只会到你的class路径中查找找文件; classpath*:不仅包含class路径,还包括jar文件 ...