@@ -56,7 +56,6 @@ instead of "double-indenting" the "case" labels. E.g.:
5656 break;
5757 }
5858
59-
6059Don't put multiple statements on a single line unless you have
6160something to hide:
6261
@@ -156,25 +155,25 @@ comments on.
156155
157156Do not unnecessarily use braces where a single statement will do.
158157
159- if (condition)
160- action();
158+ if (condition)
159+ action();
161160
162161and
163162
164- if (condition)
165- do_this();
166- else
167- do_that();
163+ if (condition)
164+ do_this();
165+ else
166+ do_that();
168167
169168This does not apply if only one branch of a conditional statement is a single
170169statement; in the latter case use braces in both branches:
171170
172- if (condition) {
173- do_this();
174- do_that();
175- } else {
176- otherwise();
177- }
171+ if (condition) {
172+ do_this();
173+ do_that();
174+ } else {
175+ otherwise();
176+ }
178177
179178 3.1: Spaces
180179
@@ -186,8 +185,11 @@ although they are not required in the language, as in: "sizeof info" after
186185"struct fileinfo info;" is declared).
187186
188187So use a space after these keywords:
188+
189189 if, switch, case, for, do, while
190+
190191but not with sizeof, typeof, alignof, or __attribute__. E.g.,
192+
191193 s = sizeof(struct file);
192194
193195Do not add spaces around (inside) parenthesized expressions. This example is
@@ -209,12 +211,15 @@ such as any of these:
209211 = + - < > * / % | & ^ <= >= == != ? :
210212
211213but no space after unary operators:
214+
212215 & * + - ~ ! sizeof typeof alignof __attribute__ defined
213216
214217no space before the postfix increment & decrement unary operators:
218+
215219 ++ --
216220
217221no space after the prefix increment & decrement unary operators:
222+
218223 ++ --
219224
220225and no space around the '.' and "->" structure member operators.
@@ -268,13 +273,11 @@ See chapter 6 (Functions).
268273 Chapter 5: Typedefs
269274
270275Please don't use things like "vps_t".
271-
272276It's a _mistake_ to use typedef for structures and pointers. When you see a
273277
274278 vps_t a;
275279
276280in the source, what does it mean?
277-
278281In contrast, if it says
279282
280283 struct virtual_container *a;
@@ -372,11 +375,11 @@ In source files, separate functions with one blank line. If the function is
372375exported, the EXPORT* macro for it should follow immediately after the closing
373376function brace line. E.g.:
374377
375- int system_is_up(void)
376- {
377- return system_state == SYSTEM_RUNNING;
378- }
379- EXPORT_SYMBOL(system_is_up);
378+ int system_is_up(void)
379+ {
380+ return system_state == SYSTEM_RUNNING;
381+ }
382+ EXPORT_SYMBOL(system_is_up);
380383
381384In function prototypes, include parameter names with their data types.
382385Although this is not required by the C language, it is preferred in Linux
@@ -405,34 +408,34 @@ The rationale for using gotos is:
405408 modifications are prevented
406409- saves the compiler work to optimize redundant code away ;)
407410
408- int fun(int a)
409- {
410- int result = 0;
411- char *buffer;
412-
413- buffer = kmalloc(SIZE, GFP_KERNEL);
414- if (!buffer)
415- return -ENOMEM;
416-
417- if (condition1) {
418- while (loop1) {
419- ...
411+ int fun(int a)
412+ {
413+ int result = 0;
414+ char *buffer;
415+
416+ buffer = kmalloc(SIZE, GFP_KERNEL);
417+ if (!buffer)
418+ return -ENOMEM;
419+
420+ if (condition1) {
421+ while (loop1) {
422+ ...
423+ }
424+ result = 1;
425+ goto out_buffer;
420426 }
421- result = 1;
422- goto out_buffer;
427+ ...
428+ out_buffer:
429+ kfree(buffer);
430+ return result;
423431 }
424- ...
425- out_buffer:
426- kfree(buffer);
427- return result;
428- }
429432
430433A common type of bug to be aware of it "one err bugs" which look like this:
431434
432- err:
433- kfree(foo->bar);
434- kfree(foo);
435- return ret;
435+ err:
436+ kfree(foo->bar);
437+ kfree(foo);
438+ return ret;
436439
437440The bug in this code is that on some exit paths "foo" is NULL. Normally the
438441fix for this is to split it up into two error labels "err_bar:" and "err_foo:".
@@ -612,7 +615,7 @@ have a reference count on it, you almost certainly have a bug.
612615
613616Names of macros defining constants and labels in enums are capitalized.
614617
615- #define CONSTANT 0x12345
618+ #define CONSTANT 0x12345
616619
617620Enums are preferred when defining several related constants.
618621
@@ -623,28 +626,28 @@ Generally, inline functions are preferable to macros resembling functions.
623626
624627Macros with multiple statements should be enclosed in a do - while block:
625628
626- #define macrofun(a, b, c) \
627- do { \
628- if (a == 5) \
629- do_this(b, c); \
630- } while (0)
629+ #define macrofun(a, b, c) \
630+ do { \
631+ if (a == 5) \
632+ do_this(b, c); \
633+ } while (0)
631634
632635Things to avoid when using macros:
633636
6346371) macros that affect control flow:
635638
636- #define FOO(x) \
637- do { \
638- if (blah(x) < 0) \
639- return -EBUGGERED; \
640- } while(0)
639+ #define FOO(x) \
640+ do { \
641+ if (blah(x) < 0) \
642+ return -EBUGGERED; \
643+ } while(0)
641644
642645is a _very_ bad idea. It looks like a function call but exits the "calling"
643646function; don't break the internal parsers of those who will read the code.
644647
6456482) macros that depend on having a local variable with a magic name:
646649
647- #define FOO(val) bar(index, val)
650+ #define FOO(val) bar(index, val)
648651
649652might look like a good thing, but it's confusing as hell when one reads the
650653code and it's prone to breakage from seemingly innocent changes.
@@ -656,8 +659,8 @@ bite you if somebody e.g. turns FOO into an inline function.
656659must enclose the expression in parentheses. Beware of similar issues with
657660macros using parameters.
658661
659- #define CONSTANT 0x4000
660- #define CONSTEXP (CONSTANT | 3)
662+ #define CONSTANT 0x4000
663+ #define CONSTEXP (CONSTANT | 3)
661664
662665The cpp manual deals with macros exhaustively. The gcc internals manual also
663666covers RTL which is used frequently with assembly language in the kernel.
@@ -796,11 +799,11 @@ you should use, rather than explicitly coding some variant of them yourself.
796799For example, if you need to calculate the length of an array, take advantage
797800of the macro
798801
799- #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
802+ #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
800803
801804Similarly, if you need to calculate the size of some structure member, use
802805
803- #define FIELD_SIZEOF(t, f) (sizeof(((t*)0)->f))
806+ #define FIELD_SIZEOF(t, f) (sizeof(((t*)0)->f))
804807
805808There are also min() and max() macros that do strict type checking if you
806809need them. Feel free to peruse that header file to see what else is already
@@ -813,19 +816,19 @@ Some editors can interpret configuration information embedded in source files,
813816indicated with special markers. For example, emacs interprets lines marked
814817like this:
815818
816- -*- mode: c -*-
819+ -*- mode: c -*-
817820
818821Or like this:
819822
820- /*
821- Local Variables:
822- compile-command: "gcc -DMAGIC_DEBUG_FLAG foo.c"
823- End:
824- */
823+ /*
824+ Local Variables:
825+ compile-command: "gcc -DMAGIC_DEBUG_FLAG foo.c"
826+ End:
827+ */
825828
826829Vim interprets markers that look like this:
827830
828- /* vim:set sw=8 noet */
831+ /* vim:set sw=8 noet */
829832
830833Do not include any of these in source files. People have their own personal
831834editor configurations, and your source files should not override them. This
@@ -902,9 +905,9 @@ At the end of any non-trivial #if or #ifdef block (more than a few lines),
902905place a comment after the #endif on the same line, noting the conditional
903906expression used. For instance:
904907
905- #ifdef CONFIG_SOMETHING
906- ...
907- #endif /* CONFIG_SOMETHING */
908+ #ifdef CONFIG_SOMETHING
909+ ...
910+ #endif /* CONFIG_SOMETHING */
908911
909912
910913 Appendix I: References
0 commit comments