【LINT】cpplint修改版:自定義編碼風格檢查工具lint


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修改版)

應用場景

  1. 嵌入git,在提交階段進行檢查

將git_hook中的文件放在自己項目的.git/hooks路徑下,下次提交時便會觸發代碼檢查

git提交時檢查

  1. 獨立使用,基於特定的文件或文件夾

將.scripts中的腳本lint_folder.sh和format_cpplint.sh放在要檢查的目錄同一級,將exe文件夾放在上一級即可

目錄檢查

  1. 嵌入jenkins進行自動化構建檢查

借用cppcheck上位機可以方便解析lint檢查的結果

確保檢查后的.xml放置在剛檢查文件夾的上一級,再使用cppcheck打開方可實現跳轉

解析lint結果

用法

./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反饋給我


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM