Linux内核代码格式审查
Linux内核代码格式审查
前言
Linux内核代码有一套自己的代码格式规范,相关的规范说明在内核代码树目录:Documentation/CodingStyle
或Documentation/process/coding-style.rst
。
具体细节大家可以自己去看,这里就不贴了。
以前提交内核补丁,一直用scripts/checkpatch.pl
来过一遍代码格式,没怎么去研究。
最近刚好检查格式报了个错,以前没怎么遇到,于是就去看了下。
报错信息:
ERROR: do not set execute permissions for source files
从提示上看很明显是源码文件权限问题。
Checkpatch 介绍
内核官方与它相关的文档:https://www.kernel.org/doc/html/latest/dev-tools/checkpatch.html
下面是它的介绍(保持原汁原味):
Checkpatch (scripts/checkpatch.pl) is a perl script which checks for trivial style violations in patches and optionally corrects them. Checkpatch can also be run on file contexts and without the kernel tree.
Checkpatch is not always right. Your judgement takes precedence over checkpatch messages. If your code looks better with the violations, then its probably best left alone.
重点:Checkpatch不仅可以检查格式,也可以修正格式。它检查和修正不一定总是对的!只能作为参考,以自己的判断为准!
Checkpatch 使用方式
scripts/checkpatch.pl
脚本的帮助信息,使用方式:
Usage: ./scripts/checkpatch.pl [OPTION]... [FILE]...
Version: 0.32
Options:
-q, --quiet quiet
-v, --verbose verbose mode
--no-tree run without a kernel tree
--no-signoff do not check for 'Signed-off-by' line
--patch treat FILE as patchfile (default)
--emacs emacs compile window format
--terse one line per report
--showfile emit diffed file position, not input file position
-g, --git treat FILE as a single commit or git revision range
single git commit with:
<rev>
<rev>^
<rev>~n
multiple git commits with:
<rev1>..<rev2>
<rev1>...<rev2>
<rev>-<count>
git merges are ignored
-f, --file treat FILE as regular source file
--subjective, --strict enable more subjective tests
--list-types list the possible message types
--types TYPE(,TYPE2...) show only these comma separated message types
--ignore TYPE(,TYPE2...) ignore various comma separated message types
--show-types show the specific message type in the output
--max-line-length=n set the maximum line length, (default 100)
if exceeded, warn on patches
requires --strict for use with --file
--min-conf-desc-length=n set the min description length, if shorter, warn
--tab-size=n set the number of spaces for tab (default 8)
--root=PATH PATH to the kernel tree root
--no-summary suppress the per-file summary
--mailback only produce a report in case of warnings/errors
--summary-file include the filename in summary
--debug KEY=[0|1] turn on/off debugging of KEY, where KEY is one of
'values', 'possible', 'type', and 'attr' (default
is all off)
--test-only=WORD report only warnings/errors containing WORD
literally
--fix EXPERIMENTAL - may create horrible results
If correctable single-line errors exist, create
"<inputfile>.EXPERIMENTAL-checkpatch-fixes"
with potential errors corrected to the preferred
checkpatch style
--fix-inplace EXPERIMENTAL - may create horrible results
Is the same as --fix, but overwrites the input
file. It's your fault if there's no backup or git
--ignore-perl-version override checking of perl version. expect
runtime errors.
--codespell Use the codespell dictionary for spelling/typos
(default:/usr/share/codespell/dictionary.txt)
--codespellfile Use this codespell dictionary
--typedefsfile Read additional types from this file
--color[=WHEN] Use colors 'always', 'never', or only when output
is a terminal ('auto'). Default is 'auto'.
--kconfig-prefix=WORD use WORD as a prefix for Kconfig symbols (default
CONFIG_)
-h, --help, --version display this help and exit
When FILE is - read standard input.
说明:
--fix
: 可以使用该选项去尝试修复格式问题。-g, --git
: 后面跟commit id可以直接去检查git提交,默认输入是补丁文件--subjective, --strict
: 执行更严格的格式审查
Checkpatch 审查输出等级
ERROR
This is the most strict level. Messages of type ERROR must be taken seriously as they denote things that are very likely to be wrong.WARNING
This is the next stricter level. Messages of type WARNING requires a more careful review. But it is milder than an ERROR.CHECK
This is the mildest level. These are things which may require some thought.
Checkpatch 审查类型
- 内存分配方式(Allocation style)
- API使用(API usage)
- 代码注释(Comments)
- 提交信息(Commit message)
- 比较风格(Comparison style)
- 缩进和换行(Indentation and Line Breaks)
- 宏、属性和符号(Macros, Attributes and Symbols)
- 函数和变量(Functions and Variables)
- 权限(Permissions)
- 间距和括号(Spacing and Brackets)
- 其他(Others)