aboutsummaryrefslogtreecommitdiffstats
path: root/Documentation/CodingStyle
diff options
context:
space:
mode:
authorPavel Kretov <firegurafiku@gmail.com>2015-02-16 12:26:18 -0500
committerJonathan Corbet <corbet@lwn.net>2015-02-27 17:02:02 -0500
commit09677e0ff8a115162cfa763b7ad2d753f11fce9f (patch)
tree17fb822a0799d53309cdd295019596cd12525167 /Documentation/CodingStyle
parent696156f03f97aa3be4ec5f8d85ff3465bbf404fe (diff)
Documentation/CodingStyle: improve text layout
Try to make coding style documentation look a bit more readable and consistent with the following: - indent every code example in C to first tab-stop; - surround every code example with empty lines, both top and bottom; - remove empty lines where text looked way too spare; - do not indent examples in elisp and kconfig; - do not do any non-whitespace changes. Signed-off-by: Pavel Kretov <firegurafiku@gmail.com> Signed-off-by: Jonathan Corbet <corbet@lwn.net>
Diffstat (limited to 'Documentation/CodingStyle')
-rw-r--r--Documentation/CodingStyle141
1 files changed, 72 insertions, 69 deletions
diff --git a/Documentation/CodingStyle b/Documentation/CodingStyle
index 6e0b7b99df18..e55accfca276 100644
--- a/Documentation/CodingStyle
+++ b/Documentation/CodingStyle
@@ -56,7 +56,6 @@ instead of "double-indenting" the "case" labels. E.g.:
56 break; 56 break;
57 } 57 }
58 58
59
60Don't put multiple statements on a single line unless you have 59Don't put multiple statements on a single line unless you have
61something to hide: 60something to hide:
62 61
@@ -156,25 +155,25 @@ comments on.
156 155
157Do not unnecessarily use braces where a single statement will do. 156Do not unnecessarily use braces where a single statement will do.
158 157
159if (condition) 158 if (condition)
160 action(); 159 action();
161 160
162and 161and
163 162
164if (condition) 163 if (condition)
165 do_this(); 164 do_this();
166else 165 else
167 do_that(); 166 do_that();
168 167
169This does not apply if only one branch of a conditional statement is a single 168This does not apply if only one branch of a conditional statement is a single
170statement; in the latter case use braces in both branches: 169statement; in the latter case use braces in both branches:
171 170
172if (condition) { 171 if (condition) {
173 do_this(); 172 do_this();
174 do_that(); 173 do_that();
175} else { 174 } else {
176 otherwise(); 175 otherwise();
177} 176 }
178 177
179 3.1: Spaces 178 3.1: Spaces
180 179
@@ -186,8 +185,11 @@ although they are not required in the language, as in: "sizeof info" after
186"struct fileinfo info;" is declared). 185"struct fileinfo info;" is declared).
187 186
188So use a space after these keywords: 187So use a space after these keywords:
188
189 if, switch, case, for, do, while 189 if, switch, case, for, do, while
190
190but not with sizeof, typeof, alignof, or __attribute__. E.g., 191but not with sizeof, typeof, alignof, or __attribute__. E.g.,
192
191 s = sizeof(struct file); 193 s = sizeof(struct file);
192 194
193Do not add spaces around (inside) parenthesized expressions. This example is 195Do not add spaces around (inside) parenthesized expressions. This example is
@@ -209,12 +211,15 @@ such as any of these:
209 = + - < > * / % | & ^ <= >= == != ? : 211 = + - < > * / % | & ^ <= >= == != ? :
210 212
211but no space after unary operators: 213but no space after unary operators:
214
212 & * + - ~ ! sizeof typeof alignof __attribute__ defined 215 & * + - ~ ! sizeof typeof alignof __attribute__ defined
213 216
214no space before the postfix increment & decrement unary operators: 217no space before the postfix increment & decrement unary operators:
218
215 ++ -- 219 ++ --
216 220
217no space after the prefix increment & decrement unary operators: 221no space after the prefix increment & decrement unary operators:
222
218 ++ -- 223 ++ --
219 224
220and no space around the '.' and "->" structure member operators. 225and no space around the '.' and "->" structure member operators.
@@ -268,13 +273,11 @@ See chapter 6 (Functions).
268 Chapter 5: Typedefs 273 Chapter 5: Typedefs
269 274
270Please don't use things like "vps_t". 275Please don't use things like "vps_t".
271
272It's a _mistake_ to use typedef for structures and pointers. When you see a 276It's a _mistake_ to use typedef for structures and pointers. When you see a
273 277
274 vps_t a; 278 vps_t a;
275 279
276in the source, what does it mean? 280in the source, what does it mean?
277
278In contrast, if it says 281In contrast, if it says
279 282
280 struct virtual_container *a; 283 struct virtual_container *a;
@@ -372,11 +375,11 @@ In source files, separate functions with one blank line. If the function is
372exported, the EXPORT* macro for it should follow immediately after the closing 375exported, the EXPORT* macro for it should follow immediately after the closing
373function brace line. E.g.: 376function brace line. E.g.:
374 377
375int system_is_up(void) 378 int system_is_up(void)
376{ 379 {
377 return system_state == SYSTEM_RUNNING; 380 return system_state == SYSTEM_RUNNING;
378} 381 }
379EXPORT_SYMBOL(system_is_up); 382 EXPORT_SYMBOL(system_is_up);
380 383
381In function prototypes, include parameter names with their data types. 384In function prototypes, include parameter names with their data types.
382Although this is not required by the C language, it is preferred in Linux 385Although this is not required by the C language, it is preferred in Linux
@@ -405,34 +408,34 @@ The rationale for using gotos is:
405 modifications are prevented 408 modifications are prevented
406- saves the compiler work to optimize redundant code away ;) 409- saves the compiler work to optimize redundant code away ;)
407 410
408int fun(int a) 411 int fun(int a)
409{ 412 {
410 int result = 0; 413 int result = 0;
411 char *buffer; 414 char *buffer;
412 415
413 buffer = kmalloc(SIZE, GFP_KERNEL); 416 buffer = kmalloc(SIZE, GFP_KERNEL);
414 if (!buffer) 417 if (!buffer)
415 return -ENOMEM; 418 return -ENOMEM;
416 419
417 if (condition1) { 420 if (condition1) {
418 while (loop1) { 421 while (loop1) {
419 ... 422 ...
423 }
424 result = 1;
425 goto out_buffer;
420 } 426 }
421 result = 1; 427 ...
422 goto out_buffer; 428 out_buffer:
429 kfree(buffer);
430 return result;
423 } 431 }
424 ...
425out_buffer:
426 kfree(buffer);
427 return result;
428}
429 432
430A common type of bug to be aware of it "one err bugs" which look like this: 433A common type of bug to be aware of it "one err bugs" which look like this:
431 434
432err: 435 err:
433 kfree(foo->bar); 436 kfree(foo->bar);
434 kfree(foo); 437 kfree(foo);
435 return ret; 438 return ret;
436 439
437The bug in this code is that on some exit paths "foo" is NULL. Normally the 440The bug in this code is that on some exit paths "foo" is NULL. Normally the
438fix for this is to split it up into two error labels "err_bar:" and "err_foo:". 441fix 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.
612 615
613Names of macros defining constants and labels in enums are capitalized. 616Names of macros defining constants and labels in enums are capitalized.
614 617
615#define CONSTANT 0x12345 618 #define CONSTANT 0x12345
616 619
617Enums are preferred when defining several related constants. 620Enums are preferred when defining several related constants.
618 621
@@ -623,28 +626,28 @@ Generally, inline functions are preferable to macros resembling functions.
623 626
624Macros with multiple statements should be enclosed in a do - while block: 627Macros with multiple statements should be enclosed in a do - while block:
625 628
626#define macrofun(a, b, c) \ 629 #define macrofun(a, b, c) \
627 do { \ 630 do { \
628 if (a == 5) \ 631 if (a == 5) \
629 do_this(b, c); \ 632 do_this(b, c); \
630 } while (0) 633 } while (0)
631 634
632Things to avoid when using macros: 635Things to avoid when using macros:
633 636
634