@@ -65,7 +65,174 @@ have plenty of code in the tree that is just waiting to get cleaned up.
6565The code in the drivers/staging/ tree consists of a lot of drivers that
6666do not meet the normal Linux kernel coding guidelines. The code is in
6767that location so that other developers can help on cleaning it up, and
68- getting it merged into the main portion of the Linux kernel tree.
68+ getting it merged into the main portion of the Linux kernel tree.
69+
70+ Every driver in the drivers/staging directory contains a TODO file that
71+ lists the things that need to be done on it in order for the code to be
72+ moved to the proper location in the kernel tree. The majority of the
73+ drivers all contain the following line in their TODO file:
74+ - fix checkpatch.pl issues
75+
76+ Let's look into what this means and how you can help out with this task.
77+
78+ -- Coding Style
79+
80+ Every large body of code needs to have a set of coding style rules in
81+ order for it to be a viable project that a large number of developers
82+ can work on. Numerous research studies have been made on this topic,
83+ and they all conclude that having a common guideline makes a very large
84+ difference.
85+
86+ It is not merely a matter of aesthetics that programs
87+ should be written in a particular style. Rather there
88+ is a psychological basis for writing programs in a
89+ conventional manner: programmers have strong expectations
90+ that other programmers will follow these discourse rules.
91+ If the rules are violated, then the utility afforded by
92+ the expectations that programmers have built up over time
93+ is effectively nullified.”
94+ – Soloway & Ehrlich
95+
96+ What this means is that once programmers get used to a common style, the
97+ patterns of the code go away when it is looked at, and the meaning shows
98+ through very easily.
99+
100+ The goal of any Linux kernel developer is to have other developers help
101+ find problems in their code, and by keeping all of the code in the same
102+ format, it makes it much easier for anyone else to pick it up, modify
103+ it, or notice bugs in it. As every line of kernel code is reviewed by
104+ at least 2 developers before it is accepted, having a common style
105+ guideline is a very important thing.
106+
107+ The Linux kernel coding style can be found in the file
108+ Documentation/CodingStyle in the kernel source tree. The important
109+ thing to remember when reading it, is not that this style is somehow
110+ better than any other style, just that it is consistent.
111+
112+ In order to help developers easily find coding style issues, the script
113+ scripts/checkpatch.pl in the kernel source tree has been developed.
114+ This script can point out problems very easily, and should always be run
115+ by a developer on their changes, instead of having a reviewer waste
116+ their time by pointing out problems later on.
117+
118+ The drivers in the drivers/staging/ directory all usually have coding
119+ style issues as they were developed by people not familiar with the
120+ Linux kernel guidelines. One of the first thing that needs to be done
121+ to the code, is to fix it up to follow the correct rules.
122+
123+ And this is where you come in, by running the checkpatch.pl tool, you
124+ can find a large number of problems that need to be fixed.
125+
126+ -- Specific rules
127+
128+ Let us look at some of the common rules that are part of the kernel
129+ guidelines.
130+
131+ --- Whitespace
132+
133+ The first rule that everyone needs to follow is to use the 'tab'
134+ character, and not use spaces, to indent code. Also, the 'tab'
135+ character should represent 8 spaces. Following along with the 8
136+ character tab indentation, the code should not flow past the 80
137+ character line limit on the right side of the screen.
138+
139+ Note, numerous developers have complained about the 80 character limit
140+ recently, and there are some places where it is acceptable to go beyond
141+ that limit. If you find that you are being forced to do strange
142+ line-wrapping formatting just to fit into the 80 character limit, with
143+ all of your code on the right hand side of the screen, it is better to
144+ refactor the logic to prevent this from happening in the first place.
145+ Forcing an 80 character limit, also forces developers to break their
146+ logic up into tinyer, easier to understand chunks, which makes it easier
147+ to review and follow as well.
148+
149+ So yes, there is a method to the madness of the 80 character limit.
150+
151+ --- Braces
152+
153+ Opening braces should be placed on the same line of the statement they
154+ are being used for, with one exception as show below. Closing braces
155+ should be placed back at the original indentation. This can be shown
156+ with the following example:
157+
158+ if (error != -ENODEV) {
159+ foo();
160+ bar();
161+ }
162+
163+ If you need to add an else statement to an if statement, put it on the
164+ same line as the closing brace, as shown here:
165+
166+ if (error != -ENODEV) {
167+ foo();
168+ bar();
169+ } else {
170+ report_error();
171+ goto exit;
172+ }
173+
174+ If braces are not needed for a statement, do not put them in, as they
175+ are unnecessary:
176+
177+ if (error != -ENODEV)
178+ foo();
179+ else
180+ goto exit;
181+
182+ The one exception for opening braces, is for function declarations,
183+ those go on a new line as shown here:
184+
185+ int function(int *baz)
186+ {
187+ do_something(baz);
188+ return 0;
189+ }
190+
191+ -- checkpatch.pl
192+
193+ With these simple whitespace and brace rules now understood, let us run
194+ the checkpatch.pl script on some code and see what it tells us:
195+
196+ $ ./scripts/checkpatch.pl --help
197+ Usage: checkpatch.pl [OPTION]... [FILE]...
198+ Version: 0.30
199+
200+ Options:
201+ -q, --quiet quiet
202+ --no-tree run without a kernel tree
203+ --no-signoff do not check for 'Signed-off-by' line
204+ --patch treat FILE as patchfile (default)
205+ --emacs emacs compile window format
206+ --terse one line per report
207+ -f, --file treat FILE as regular source file
208+ --subjective, --strict enable more subjective tests
209+ --root=PATH PATH to the kernel tree root
210+ --no-summary suppress the per-file summary
211+ --mailback only produce a report in case of warnings/errors
212+ --summary-file include the filename in summary
213+ --debug KEY=[0|1] turn on/off debugging of KEY, where KEY is one of
214+ 'values', 'possible', 'type', and 'attr' (default
215+ is all off)
216+ --test-only=WORD report only warnings/errors containing WORD
217+ literally
218+ -h, --help, --version display this help and exit
219+
220+ When FILE is - read standard input.
221+
222+ Some common options that we will be using is the --terse and --file
223+ options, as those allow us to see the problems in a much simpler report,
224+ and they work on an entire file, not just a single patch.
225+
226+ So, let's pick a file in the kernel and see what checkpatch.pl tells us
227+ about it:
228+
229+ $ ./scripts/checkpatch.pl --file --terse drivers/staging/
230+
231+ This is because of one thing that all developers have in
232+ common, they have a brain.
233+
234+
235+ The Linux kernel is no exception and describes al
69236
70237
71238
0 commit comments