2016年4月13日 星期三

使用Linux script檢查coding style

1. checkpatch.pl Script位於kernel目錄下
./scripts/checkpatch.pl --help
Options:
  -q, --quiet                quiet
  --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
  -f, --file                 treat FILE as regular source file
  --subjective, --strict     enable more subjective tests
  --types TYPE(,TYPE2...)    show only these comma separated message types
  --ignore TYPE(,TYPE2...)   ignore various comma separated message types
  --max-line-length=n        set the maximum line length, if exceeded, warn
  --min-conf-desc-length=n   set the min description length, if shorter, warn
  --show-types               show the message "types" in the output
  --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
                             ".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
  --color                    Use colors when output is STDOUT (default: on)
  -h, --help, --version      display this help and exit
2. 帶入--file參數,檢查C source
$ scripts/checkpatch.pl --file *.c
3. 檢查patch file
$ scripts/checkpatch.pl *.patch
4. 一個範例 hello.c
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/init.h>

MODULE_LICENSE("GPL");
MODULE_AUTHOR("Allen");
MODULE_DESCRIPTION("A Simple Hello World module");

static int __init hello_init(void)
{
        printk(KERN_INFO "Enter module. Hello world!\n");
        return 0;
}

static void __exit hello_exit(void)
{
        printk(KERN_INFO "Exit module.\n");
}

module_init(hello_init);
module_exit(hello_exit);
$ ./scripts/checkpatch.pl --file hello.c 
WARNING: Prefer [subsystem eg: netdev]_info([subsystem]dev, ... then dev_info(dev, ... then pr_info(...  to printk(KERN_INFO ...
#11: FILE: ../../module/hello.c:11:
+ printk(KERN_INFO "Enter module. Hello world!\n");

WARNING: Prefer [subsystem eg: netdev]_info([subsystem]dev, ... then dev_info(dev, ... then pr_info(...  to printk(KERN_INFO ...
#17: FILE: ../../module/hello.c:17:
+ printk(KERN_INFO "Exit module.\n");

total: 0 errors, 2 warnings, 21 lines checked

../../module/hello.c has style problems, please review.

If any of these errors are false positives, please report
them to the maintainer, see CHECKPATCH in MAINTAINERS.
5. 參考來源
https://github.com/torvalds/linux/blob/master/scripts/checkpatch.pl

沒有留言:

張貼留言