Check kernel code with checkpatch

tux_compcheckpatch.pl is a perl script to verify that your code conforms to the Linux kernel coding style. If you are an aspiring kernel dev, you must be very careful¬†that you follow the right coding guidelines. It’s difficult to get it right the first time because you may need to unlearn a few practices learnt over years of coding. checkpatch.pl assists you in the process. This article explores how to check the coding style (and spelling etc.) of a dummy kernel module implemented in sample_module.c.

Start with downloading the checkpatch.pl and spelling.txt files from Linus’s kernel tree on GitHub. Save both under the same directory as the C source file. Run checkpatch.

$ chmod +c checkpatch.pl
$ ./checkpatch.pl --no-tree -f sample_module.c

Options used:

–no-tree : By default checkpatch must be run from the top-level dir. of a kernel tree. This option overrides the default behaviour.
-f : By default checkpatch verifies kernel patches. This option instructs it to treat the file as a regular source file.

Take a look at other available options:

$ ./checkpatch.pl --help

And finally, the sample output for our module:

$ ./checkpatch.pl --no-tree -f sample_module.c

WARNING: void function return statements are not generally useful
#35: FILE: coding_style.c:35:
+ return;
+}

total: 0 errors, 1 warnings, 38 lines checked

coding_style.c has style problems, please review.

If any of these errors are false positives, please report
them to the maintainer, see CHECKPATCH in MAINTAINERS.

Leave a Reply

Your email address will not be published. Required fields are marked *