Cpplint - Static code checker for C++ (C++ 静态代码审查工具)
Cpplint - Static code checker for C {C 静态代码审查工具}1. Installation2. Usage2.1. cpplint main.cpp2.2. cpplint --linelength120 main.cpp2.3. cpplint --linelength120 --verbose3 main.cpp3. cpplint --help4. 在 Visual Studio Code 中配置 cpplint (Static code checker for C)Referencescpplinthttps://github.com/cpplint/cpplintGoogle Style Guideshttps://github.com/google/styleguideGoogle C Style Guidehttps://google.github.io/styleguide/cppguide.htmlGoogle C 风格指南https://zh-google-styleguide.readthedocs.io/en/latest/google-cpp-styleguide/Cpplint is a command-line tool to check C/C files for style issues following Google’s C style guide.Cpplint 是一个命令行工具可按照 Google 的 C 样式指南检查 C/C 文件中的样式问题。Cpplint used to be developed and maintained by Google Inc. at google/styleguide. Nowadays, Google is no longer maintaining the public version of cpplint, and pretty much everything in their repo’s PRs and issues about cpplint have gone unimplemented.cpplintorcpplint.pyis an open source lint-like tool developed by Google, designed to ensure that C code conforms to Google’s coding style guides.1. InstallationTo installcpplintfrom PyPI, run:$ pip install cpplintThen run it with:$ cpplint [OPTIONS] filesFor full usage instructions, run:$ cpplint --help(base) yongqiangyongqiang:~$ pip --version pip 23.1.2 from /home/yongqiang/miniconda3/lib/python3.11/site-packages/pip (python 3.11) (base) yongqiangyongqiang:~$ (base) yongqiangyongqiang:~$ pip install cpplint Collecting cpplint Downloading cpplint-2.0.2-py3-none-any.whl (81 kB) ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 82.0/82.0 kB 88.9 kB/s eta 0:00:00 Installing collected packages: cpplint Successfully installed cpplint-2.0.2 (base) yongqiangyongqiang:~$ (base) yongqiangyongqiang:~$ cpplint --version Cpplint fork (https://github.com/cpplint/cpplint) cpplint 2.0.2 Python 3.11.4 (main, Jul 5 2023, 13:45:01) [GCC 11.2.0] (base) yongqiangyongqiang:~$ (base) yongqiangyongqiang:~$ pip list | grep cpplint cpplint 2.0.2 (base) yongqiangyongqiang:~$(base) yongqiangyongqiang:~$ pip uninstall cpplint Found existing installation: cpplint 2.0.2 Uninstalling cpplint-2.0.2: Would remove: /home/yongqiang/miniconda3/bin/cpplint /home/yongqiang/miniconda3/lib/python3.11/site-packages/cpplint-2.0.2.dist-info/* /home/yongqiang/miniconda3/lib/python3.11/site-packages/cpplint.py Proceed (Y/n)? y Successfully uninstalled cpplint-2.0.2 (base) yongqiangyongqiang:~$2. Usage2.1.cpplint main.cpp#include iostream int main() { std::cout Hello, World! std::endl; return 0; }(base) yongqiangyongqiang:~/CLionProjects/yongqiang$ cpplint main.cpp main.cpp:0: No copyright message found. You should have a line: Copyright [year] Copyright Owner [legal/copyright] [5] Done processing main.cpp Total errors found: 1 (base) yongqiangyongqiang:~/CLionProjects/yongqiang$/* * Copyright 2020 Yongqiang Cheng. All Rights Reserved. */ #include iostream int main() { std::cout Hello, World! std::endl; return 0; }(base) yongqiangyongqiang:~/CLionProjects/yongqiang$ cpplint main.cpp Done processing main.cpp (base) yongqiangyongqiang:~/CLionProjects/yongqiang$2.2.cpplint --linelength120 main.cpp每行允许的最长长度默认是 80 字符。--linelengthdigits This is the allowed line length for the project. The default value is 80 characters. Examples: --linelength120(base) yongqiangyongqiang:~/CLionProjects/yongqiang$ cpplint --linelength120 main.cpp Done processing main.cpp (base) yongqiangyongqiang:~/CLionProjects/yongqiang$2.3.cpplint --linelength120 --verbose3 main.cpp--verbose#Specify a number 0-5 to restrict errors to certain verbosity levels. Errors with lower verbosity levels have lower confidence and are more likely to be false positives.指定数字 0-5以将错误限制在某些详细程度上。详细程度较低的错误的置信度较低更有可能是误报。指定输出的错误级别。对于发现的每个问题cpplint 都会给出 12345 的置信度评分分数越高就代表问题越肯定可以通过--verbose#选项控制输出哪些级别。--verbose3置信度评分为 1、2 的将不会再输出。个人建议不需要使用该参数。3.cpplint --help(base) yongqiangyongqiang:~$ cpplint --help Syntax: cpplint.py [--verbose#] [--outputemacs|eclipse|vs7|junit|sed|gsed] [--filter-x,y,...] [--countingtotal|toplevel|detailed] [--rootsubdir] [--repositorypath] [--linelengthdigits] [--headersx,y,...] [--recursive] [--excludepath] [--extensionshpp,cpp,...] [--includeorderdefault|standardcfirst] [--quiet] [--version] file [file] ... Style checker for C/C source files. This is a fork of the Google style checker with minor extensions. The style guidelines this tries to follow are those in https://google.github.io/styleguide/cppguide.html Every problem is given a confidence score from 1-5, with 5 meaning we are certain of the problem, and 1 meaning it could be a legitimate construct. This will miss some errors, and is not a substitute for a code review. To suppress false-positive errors of a certain category, add a NOLINT(category) comment to the line. NOLINT or NOLINT(*) suppresses errors of all categories on that line. The files passed in will be linted; at least one file must be provided. Default linted extensions are [cu, h, c, hxx, cuh, cc, cxx, hpp, cpp, h, hh, c]. Other file types will be ignored. Change the extensions with the --extensions flag. Flags: outputemacs|eclipse|vs7|junit|sed|gsed By default, the output is formatted to ease emacs parsing. Visual Studio compatible output (vs7) may also be used. Further support exists for eclipse (eclipse), and JUnit (junit). XML parsers such as those used in Jenkins and Bamboo may also be used. The sed format outputs sed commands that should fix some of the errors. Note that this requires gnu sed. If that is installed as gsed on your system (common e.g. on macOS with homebrew) you can use the gsed output format. Sed commands are written to stdout, not stderr, so you should be able to pipe output straight to a shell to run the fixes. verbose# Specify a number 0-5 to restrict errors to certain verbosity levels. Errors with lower verbosity levels have lower confidence and are more likely to be false positives. quiet Dont print anything if no errors are found. 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 and FOO means do not print categories that start with FOO. FOO means do print categories that start with FOO. Examples: --filter-whitespace,whitespace/braces --filterwhitespace,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 countingtotal|toplevel|detailed The total number of errors found is always printed. If toplevel is provided, then the count of errors in each of the top-level categories like build and whitespace will also be printed. If detailed is provided, then a count is provided for each category like build/class. repositorypath The top level directory of the repository, used to derive the header guard CPP variable. By default, this is determined by searching for a path that contains .git, .hg, or .svn. When this flag is specified, the given path is used instead. This option allows the header guard CPP variable to remain consistent even if members of a team have different repository root directories (such as when checking out a subdirectory with SVN). In addition, users of non-mainstream version control systems can use this flag to ensure readable header guard CPP variables. Examples: Assuming that Alice checks out ProjectName and Bob checks out ProjectName/trunk and trunk contains src/chrome/ui/browser.h, then with no --repository flag, the header guard CPP variable will be: Alice TRUNK_SRC_CHROME_BROWSER_UI_BROWSER_H_ Bob SRC_CHROME_BROWSER_UI_BROWSER_H_ If Alice uses the --repositorytrunk flag and Bob omits the flag or uses --repository. then the header guard CPP variable will be: Alice SRC_CHROME_BROWSER_UI_BROWSER_H_ Bob SRC_CHROME_BROWSER_UI_BROWSER_H_ rootsubdir The root directory used for deriving header guard CPP variable. This directory is relative to the top level directory of the repository which by default is determined by searching for a directory that contains .git, .hg, or .svn but can also be controlled with the --repository flag. If the specified directory does not exist, this flag is ignored. Examples: Assuming that src is the top level directory of the repository (and cwdtop/src), the header guard CPP variables for src/chrome/browser/ui/browser.h are: No flag CHROME_BROWSER_UI_BROWSER_H_ --rootchrome BROWSER_UI_BROWSER_H_ --rootchrome/browser UI_BROWSER_H_ --root.. SRC_CHROME_BROWSER_UI_BROWSER_H_ linelengthdigits This is the allowed line length for the project. The default value is 80 characters. Examples: --linelength120 recursive Search for files to lint recursively. Each directory given in the list of files to be linted is replaced by all files that descend from that directory. Files with extensions not in the valid extensions list are excluded. excludepath Exclude the given path from the list of files to be linted. Relative paths are evaluated relative to the current directory and shell globbing is performed. This flag can be provided multiple times to exclude multiple files. Examples: --excludeone.cc --excludesrc/*.cc --excludesrc/*.cc --excludetest/*.cc extensionsextension,extension,... The allowed file extensions that cpplint will check Examples: --extensionscu,h,c,hxx,cuh,cc,cxx,hpp,cpp,h,hh,c includeorderdefault|standardcfirst For the build/include_order rule, the default is to blindly assume angle bracket includes with file extension are c-system-headers (default), even knowing this will have false classifications. The default is established at google. standardcfirst means to instead use an allow-list of known c headers and treat all others as separate group of other system headers. The C headers included are those of the C-standard lib and closely related ones. headersx,y,... The header extensions that cpplint will treat as .h in checks. Values are automatically added to --extensions list. (by default, only files with extensions {h, hxx, cuh, hpp, h, hh} will be assumed to be headers) Examples: --headersh,hxx,cuh,hpp,h,hh --headershpp,hxx --headershpp cpplint.py supports per-directory configurations specified in CPPLINT.cfg files. CPPLINT.cfg file can contain a number of keyvalue pairs. Currently the following options are supported: set noparent filterfilter1,-filter2,... exclude_filesregex linelength80 rootsubdir headersx,y,... set noparent option prevents cpplint from traversing directory tree upwards looking for more .cfg files in parent directories. This option is usually placed in the top-level project directory. The filter option is similar in function to --filter flag. It specifies message filters in addition to the |_DEFAULT_FILTERS| and those specified through --filter command-line flag. exclude_files allows to specify a regular expression to be matched against a file name. If the expression matches, the file is skipped and not run through the linter. linelength allows to specify the allowed line length for the project. The root option is similar in function to the --root flag (see example above). Paths are relative to the directory of the CPPLINT.cfg. The headers option is similar in function to the --headers flag (see example above). CPPLINT.cfg has an effect on files in the same directory and all sub-directories, unless overridden by a nested configuration file. Example file: filter-build/include_order,build/include_alpha exclude_files.*\.cc The above example disables build/include_order warning and enables build/include_alpha as well as excludes all .cc from being processed by linter, in the current directory (where the .cfg file is located) and all sub-directories. (base) yongqiangyongqiang:~$4. 在 Visual Studio Code 中配置 cpplint (Static code checker for C)/home/yongqiang/miniconda3/bin/cpplint(base) yongqiangyongqiang:~$ which cpplint /home/yongqiang/miniconda3/bin/cpplint (base) yongqiangyongqiang:~$cpplintCpplint PathThe path to the cpplint executable. If not set, the default location will be used.Line LengthThis is the allowed line length for the project.VerboseSpecify a number 0-5 to restrict errors to certain verbosity levels. Errors with lower verbosity levels have lower confidence and are more likely to be false positives.检测级别 0 最严格5 最宽松有很多在 1、2 属于问题的提示在 3、4 中可能就不算是问题。在 Visual Studio Code 中的 PROBLEMS 窗口No copyright message found. You should have a line: Copyright [year] Copyright Owner [legal/copyright]References[1] Yongqiang Cheng (程永强), https://yongqiang.blog.csdn.net/[2] pipx - Install and Run Python Applications in Isolated Environments, https://pipx.pypa.io/stable/[3] Cpplint - Static code checker for C (C 静态代码审查工具), https://mp.weixin.qq.com/s/dDiD0e4xlaXoAoTNXv4BDQ