LIBRENEITOR

Linux Kernel Coding Style - Eudyptula Task 4

Mon, Jul 8, 2019

Time to make our Linux kernel code stylish!

This is the Fourth solution of the Eudyptula Challenge! I already covered how to setup the emacs editor to editor the Linux Kernel with the style that Linus likes in this Post. But this post is not only about style but how can we check that automatically.

The fourth task instruct us to use /scripts/checkpatch.pl to fix style problems with coding_style_1.c and coding_style_2.c.

Let’s first install some checkpatch.pl’s dependencies:

sudo apt-get install codespell python-ply python-git

After we install those dependencies we can check the help of this script.

$ ./scripts/checkpatch.pl -h
Usage: ./scripts/checkpatch.pl [OPTION]... [FILE]...

Options:
  --patch                    treat FILE as patchfile (default)
  -f, --file                 treat FILE as regular source file
  --subjective, --strict     enable more subjective tests
  --codespell                Use the codespell dictionary for spelling/typos
                             (default:/usr/share/codespell/dictionary.txt)
  -h, --help, --version      display this help and exit

We can use --patch:

$ ./scripts/checkpatch.pl 0001-change-EXTRAVERSION-as-described-in-Eudyptula-Task-3.patch 
WARNING: Missing commit description - Add an appropriate one

mmm… that is true, I didn’t write a description in the commit of the task 3. I can see that the script is working.

Now, lets check the source files of this challenge.

./scripts/checkpatch.pl -f coding_style_1.c
WARNING: Missing or malformed SPDX-License-Identifier tag in line 1
#1: FILE: coding_style_1.c:1:
+/*

WARNING: Block comments should align the * on each line
#2: FILE: coding_style_1.c:2:
+/*
+* helloworld.c

total: 0 errors, 2 warnings, 24 lines checked

You may want to check what it is the SPDX-License-Identifier error, but in short it means the the problem is the lack of a comment specifying the license.

The solution for coding_style_1.c is:

$ diff -up coding_style_1.c.copy coding_style_1.c
--- coding_style_1.c.copy
+++ coding_style_1.c
@@ -1,6 +1,7 @@
+// SPDX-License-Identifier: GPL-2.0 WITH Linux-syscall-note
 /*
-* helloworld.c
-*/
+ * helloworld.c
+ */
 #include <linux/init.h>
 #include <linux/module.h>
 #include <linux/kernel.h>

Note that I used diff -up as recommended in Documentation/process/submitting-patches.rst.

Now, if run the check against the second file, the errors are very similar:

$ ./scripts/checkpatch.pl -f coding_style_2.c
WARNING: Missing or malformed SPDX-License-Identifier tag in line 1
#1: FILE: coding_style_2.c:1:
+#include <linux/module.h>

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

So, without any further explanation this is the solution:

$ diff -up coding_style_2.c.copy coding_style_2.c
--- coding_style_2.c.copy
+++ coding_style_2.c
@@ -1,3 +1,4 @@
+// SPDX-License-Identifier: GPL-2.0 WITH Linux-syscall-note
 #include <linux/module.h>
 #include <linux/kernel.h>
 #include <linux/delay.h>
@@ -31,7 +32,7 @@ int my_init(void)
 
 void my_exit(void)
 {
-	return;
+
 }

And we are done! I can tell you, this is the easiest task so far.