Skip to content

Commit b3fc994

Browse files
rddunlapLinus Torvalds
authored andcommitted
[PATCH] CodingStyle updates
Add some kernel coding style comments, mostly pulled from emails by Andrew Morton, Jesper Juhl, and Randy Dunlap. - add paragraph on switch/case indentation (with fixes) - add paragraph on multiple-assignments - add more on Braces - add section on Spaces; add typeof, alignof, & __attribute__ with sizeof; add more on postfix/prefix increment/decrement operators - add paragraph on function breaks in source files; add info on function prototype parameter names - add paragraph on EXPORT_SYMBOL placement - add section on /*-comment style, long-comment style, and data declarations and comments - correct some chapter number references that were missed when chapters were renumbered Signed-off-by: Randy Dunlap <randy.dunlap@oracle.com> Acked-by: Jesper Juhl <jesper.juhl@gmail.com> Acked-by: Jan Engelhardt <jengelh@gmx.de> Signed-off-by: Andrew Morton <akpm@osdl.org> Signed-off-by: Linus Torvalds <torvalds@osdl.org>
1 parent 8d94cc5 commit b3fc994

1 file changed

Lines changed: 121 additions & 5 deletions

File tree

‎Documentation/CodingStyle‎

Lines changed: 121 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,37 @@ In short, 8-char indents make things easier to read, and have the added
3535
benefit of warning you when you're nesting your functions too deep.
3636
Heed that warning.
3737

38+
The preferred way to ease multiple indentation levels in a switch statement is
39+
to align the "switch" and its subordinate "case" labels in the same column
40+
instead of "double-indenting" the "case" labels. E.g.:
41+
42+
switch (suffix) {
43+
case 'G':
44+
case 'g':
45+
mem <<= 30;
46+
break;
47+
case 'M':
48+
case 'm':
49+
mem <<= 20;
50+
break;
51+
case 'K':
52+
case 'k':
53+
mem <<= 10;
54+
/* fall through */
55+
default:
56+
break;
57+
}
58+
59+
3860
Don't put multiple statements on a single line unless you have
3961
something to hide:
4062

4163
if (condition) do_this;
4264
do_something_everytime;
4365

66+
Don't put multiple assignments on a single line either. Kernel coding style
67+
is super simple. Avoid tricky expressions.
68+
4469
Outside of comments, documentation and except in Kconfig, spaces are never
4570
used for indentation, and the above example is deliberately broken.
4671

@@ -69,7 +94,7 @@ void fun(int a, int b, int c)
6994
next_statement;
7095
}
7196

72-
Chapter 3: Placing Braces
97+
Chapter 3: Placing Braces and Spaces
7398

7499
The other issue that always comes up in C styling is the placement of
75100
braces. Unlike the indent size, there are few technical reasons to
@@ -81,6 +106,20 @@ brace last on the line, and put the closing brace first, thusly:
81106
we do y
82107
}
83108

109+
This applies to all non-function statement blocks (if, switch, for,
110+
while, do). E.g.:
111+
112+
switch (action) {
113+
case KOBJ_ADD:
114+
return "add";
115+
case KOBJ_REMOVE:
116+
return "remove";
117+
case KOBJ_CHANGE:
118+
return "change";
119+
default:
120+
return NULL;
121+
}
122+
84123
However, there is one special case, namely functions: they have the
85124
opening brace at the beginning of the next line, thus:
86125

@@ -121,6 +160,49 @@ supply of new-lines on your screen is not a renewable resource (think
121160
25-line terminal screens here), you have more empty lines to put
122161
comments on.
123162

163+
3.1: Spaces
164+
165+
Linux kernel style for use of spaces depends (mostly) on
166+
function-versus-keyword usage. Use a space after (most) keywords. The
167+
notable exceptions are sizeof, typeof, alignof, and __attribute__, which look
168+
somewhat like functions (and are usually used with parentheses in Linux,
169+
although they are not required in the language, as in: "sizeof info" after
170+
"struct fileinfo info;" is declared).
171+
172+
So use a space after these keywords:
173+
if, switch, case, for, do, while
174+
but not with sizeof, typeof, alignof, or __attribute__. E.g.,
175+
s = sizeof(struct file);
176+
177+
Do not add spaces around (inside) parenthesized expressions. This example is
178+
*bad*:
179+
180+
s = sizeof( struct file );
181+
182+
When declaring pointer data or a function that returns a pointer type, the
183+
preferred use of '*' is adjacent to the data name or function name and not
184+
adjacent to the type name. Examples:
185+
186+
char *linux_banner;
187+
unsigned long long memparse(char *ptr, char **retptr);
188+
char *match_strdup(substring_t *s);
189+
190+
Use one space around (on each side of) most binary and ternary operators,
191+
such as any of these:
192+
193+
= + - < > * / % | & ^ <= >= == != ? :
194+
195+
but no space after unary operators:
196+
& * + - ~ ! sizeof typeof alignof __attribute__ defined
197+
198+
no space before the postfix increment & decrement unary operators:
199+
++ --
200+
201+
no space after the prefix increment & decrement unary operators:
202+
++ --
203+
204+
and no space around the '.' and "->" structure member operators.
205+
124206

125207
Chapter 4: Naming
126208

@@ -152,7 +234,7 @@ variable that is used to hold a temporary value.
152234

153235
If you are afraid to mix up your local variable names, you have another
154236
problem, which is called the function-growth-hormone-imbalance syndrome.
155-
See next chapter.
237+
See chapter 6 (Functions).
156238

157239

158240
Chapter 5: Typedefs
@@ -258,6 +340,20 @@ generally easily keep track of about 7 different things, anything more
258340
and it gets confused. You know you're brilliant, but maybe you'd like
259341
to understand what you did 2 weeks from now.
260342

343+
In source files, separate functions with one blank line. If the function is
344+
exported, the EXPORT* macro for it should follow immediately after the closing
345+
function brace line. E.g.:
346+
347+
int system_is_up(void)
348+
{
349+
return system_state == SYSTEM_RUNNING;
350+
}
351+
EXPORT_SYMBOL(system_is_up);
352+
353+
In function prototypes, include parameter names with their data types.
354+
Although this is not required by the C language, it is preferred in Linux
355+
because it is a simple way to add valuable information for the reader.
356+
261357

262358
Chapter 7: Centralized exiting of functions
263359

@@ -306,16 +402,36 @@ time to explain badly written code.
306402
Generally, you want your comments to tell WHAT your code does, not HOW.
307403
Also, try to avoid putting comments inside a function body: if the
308404
function is so complex that you need to separately comment parts of it,
309-
you should probably go back to chapter 5 for a while. You can make
405+
you should probably go back to chapter 6 for a while. You can make
310406
small comments to note or warn about something particularly clever (or
311407
ugly), but try to avoid excess. Instead, put the comments at the head
312408
of the function, telling people what it does, and possibly WHY it does
313409
it.
314410

315-
When commenting the kernel API functions, please use the kerneldoc format.
411+
When commenting the kernel API functions, please use the kernel-doc format.
316412
See the files Documentation/kernel-doc-nano-HOWTO.txt and scripts/kernel-doc
317413
for details.
318414

415+
Linux style for comments is the C89 "/* ... */" style.
416+
Don't use C99-style "// ..." comments.
417+
418+
The preferred style for long (multi-line) comments is:
419+
420+
/*
421+
* This is the preferred style for multi-line
422+
* comments in the Linux kernel source code.
423+
* Please use it consistently.
424+
*
425+
* Description: A column of asterisks on the left side,
426+
* with beginning and ending almost-blank lines.
427+
*/
428+
429+
It's also important to comment data, whether they are basic types or derived
430+
types. To this end, use just one data declaration per line (no commas for
431+
multiple data declarations). This leaves you room for a small comment on each
432+
item, explaining its use.
433+
434+
319435
Chapter 9: You've made a mess of it
320436

321437
That's OK, we all do. You've probably been told by your long-time Unix
@@ -591,4 +707,4 @@ Kernel CodingStyle, by greg@kroah.com at OLS 2002:
591707
http://www.kroah.com/linux/talks/ols_2002_kernel_codingstyle_talk/html/
592708

593709
--
594-
Last updated on 30 April 2006.
710+
Last updated on 2006-December-06.

0 commit comments

Comments
 (0)