Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

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>

authored by

Pavel Kretov and committed by
Jonathan Corbet
09677e0f 696156f0

+70 -67
+70 -67
Documentation/CodingStyle
··· 56 56 break; 57 57 } 58 58 59 - 60 59 Don't put multiple statements on a single line unless you have 61 60 something to hide: 62 61 ··· 155 156 156 157 Do not unnecessarily use braces where a single statement will do. 157 158 158 - if (condition) 159 - action(); 159 + if (condition) 160 + action(); 160 161 161 162 and 162 163 163 - if (condition) 164 - do_this(); 165 - else 166 - do_that(); 164 + if (condition) 165 + do_this(); 166 + else 167 + do_that(); 167 168 168 169 This does not apply if only one branch of a conditional statement is a single 169 170 statement; in the latter case use braces in both branches: 170 171 171 - if (condition) { 172 - do_this(); 173 - do_that(); 174 - } else { 175 - otherwise(); 176 - } 172 + if (condition) { 173 + do_this(); 174 + do_that(); 175 + } else { 176 + otherwise(); 177 + } 177 178 178 179 3.1: Spaces 179 180 ··· 185 186 "struct fileinfo info;" is declared). 186 187 187 188 So use a space after these keywords: 189 + 188 190 if, switch, case, for, do, while 191 + 189 192 but not with sizeof, typeof, alignof, or __attribute__. E.g., 193 + 190 194 s = sizeof(struct file); 191 195 192 196 Do not add spaces around (inside) parenthesized expressions. This example is ··· 211 209 = + - < > * / % | & ^ <= >= == != ? : 212 210 213 211 but no space after unary operators: 212 + 214 213 & * + - ~ ! sizeof typeof alignof __attribute__ defined 215 214 216 215 no space before the postfix increment & decrement unary operators: 216 + 217 217 ++ -- 218 218 219 219 no space after the prefix increment & decrement unary operators: 220 + 220 221 ++ -- 221 222 222 223 and no space around the '.' and "->" structure member operators. ··· 273 268 Chapter 5: Typedefs 274 269 275 270 Please don't use things like "vps_t". 276 - 277 271 It's a _mistake_ to use typedef for structures and pointers. When you see a 278 272 279 273 vps_t a; 280 274 281 275 in the source, what does it mean? 282 - 283 276 In contrast, if it says 284 277 285 278 struct virtual_container *a; ··· 375 372 exported, the EXPORT* macro for it should follow immediately after the closing 376 373 function brace line. E.g.: 377 374 378 - int system_is_up(void) 379 - { 380 - return system_state == SYSTEM_RUNNING; 381 - } 382 - EXPORT_SYMBOL(system_is_up); 375 + int system_is_up(void) 376 + { 377 + return system_state == SYSTEM_RUNNING; 378 + } 379 + EXPORT_SYMBOL(system_is_up); 383 380 384 381 In function prototypes, include parameter names with their data types. 385 382 Although this is not required by the C language, it is preferred in Linux ··· 408 405 modifications are prevented 409 406 - saves the compiler work to optimize redundant code away ;) 410 407 411 - int fun(int a) 412 - { 413 - int result = 0; 414 - char *buffer; 408 + int fun(int a) 409 + { 410 + int result = 0; 411 + char *buffer; 415 412 416 - buffer = kmalloc(SIZE, GFP_KERNEL); 417 - if (!buffer) 418 - return -ENOMEM; 413 + buffer = kmalloc(SIZE, GFP_KERNEL); 414 + if (!buffer) 415 + return -ENOMEM; 419 416 420 - if (condition1) { 421 - while (loop1) { 422 - ... 417 + if (condition1) { 418 + while (loop1) { 419 + ... 420 + } 421 + result = 1; 422 + goto out_buffer; 423 423 } 424 - result = 1; 425 - goto out_buffer; 424 + ... 425 + out_buffer: 426 + kfree(buffer); 427 + return result; 426 428 } 427 - ... 428 - out_buffer: 429 - kfree(buffer); 430 - return result; 431 - } 432 429 433 430 A common type of bug to be aware of it "one err bugs" which look like this: 434 431 435 - err: 436 - kfree(foo->bar); 437 - kfree(foo); 438 - return ret; 432 + err: 433 + kfree(foo->bar); 434 + kfree(foo); 435 + return ret; 439 436 440 437 The bug in this code is that on some exit paths "foo" is NULL. Normally the 441 438 fix for this is to split it up into two error labels "err_bar:" and "err_foo:". ··· 615 612 616 613 Names of macros defining constants and labels in enums are capitalized. 617 614 618 - #define CONSTANT 0x12345 615 + #define CONSTANT 0x12345 619 616 620 617 Enums are preferred when defining several related constants. 621 618 ··· 626 623 627 624 Macros with multiple statements should be enclosed in a do - while block: 628 625 629 - #define macrofun(a, b, c) \ 630 - do { \ 631 - if (a == 5) \ 632 - do_this(b, c); \ 633 - } while (0) 626 + #define macrofun(a, b, c) \ 627 + do { \ 628 + if (a == 5) \ 629 + do_this(b, c); \ 630 + } while (0) 634 631 635 632 Things to avoid when using macros: 636 633 637 634 1) macros that affect control flow: 638 635 639 - #define FOO(x) \ 640 - do { \ 641 - if (blah(x) < 0) \ 642 - return -EBUGGERED; \ 643 - } while(0) 636 + #define FOO(x) \ 637 + do { \ 638 + if (blah(x) < 0) \ 639 + return -EBUGGERED; \ 640 + } while(0) 644 641 645 642 is a _very_ bad idea. It looks like a function call but exits the "calling" 646 643 function; don't break the internal parsers of those who will read the code. 647 644 648 645 2) macros that depend on having a local variable with a magic name: 649 646 650 - #define FOO(val) bar(index, val) 647 + #define FOO(val) bar(index, val) 651 648 652 649 might look like a good thing, but it's confusing as hell when one reads the 653 650 code and it's prone to breakage from seemingly innocent changes. ··· 659 656 must enclose the expression in parentheses. Beware of similar issues with 660 657 macros using parameters. 661 658 662 - #define CONSTANT 0x4000 663 - #define CONSTEXP (CONSTANT | 3) 659 + #define CONSTANT 0x4000 660 + #define CONSTEXP (CONSTANT | 3) 664 661 665 662 The cpp manual deals with macros exhaustively. The gcc internals manual also 666 663 covers RTL which is used frequently with assembly language in the kernel. ··· 799 796 For example, if you need to calculate the length of an array, take advantage 800 797 of the macro 801 798 802 - #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0])) 799 + #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0])) 803 800 804 801 Similarly, if you need to calculate the size of some structure member, use 805 802 806 - #define FIELD_SIZEOF(t, f) (sizeof(((t*)0)->f)) 803 + #define FIELD_SIZEOF(t, f) (sizeof(((t*)0)->f)) 807 804 808 805 There are also min() and max() macros that do strict type checking if you 809 806 need them. Feel free to peruse that header file to see what else is already ··· 816 813 indicated with special markers. For example, emacs interprets lines marked 817 814 like this: 818 815 819 - -*- mode: c -*- 816 + -*- mode: c -*- 820 817 821 818 Or like this: 822 819 823 - /* 824 - Local Variables: 825 - compile-command: "gcc -DMAGIC_DEBUG_FLAG foo.c" 826 - End: 827 - */ 820 + /* 821 + Local Variables: 822 + compile-command: "gcc -DMAGIC_DEBUG_FLAG foo.c" 823 + End: 824 + */ 828 825 829 826 Vim interprets markers that look like this: 830 827 831 - /* vim:set sw=8 noet */ 828 + /* vim:set sw=8 noet */ 832 829 833 830 Do not include any of these in source files. People have their own personal 834 831 editor configurations, and your source files should not override them. This ··· 905 902 place a comment after the #endif on the same line, noting the conditional 906 903 expression used. For instance: 907 904 908 - #ifdef CONFIG_SOMETHING 909 - ... 910 - #endif /* CONFIG_SOMETHING */ 905 + #ifdef CONFIG_SOMETHING 906 + ... 907 + #endif /* CONFIG_SOMETHING */ 911 908 912 909 913 910 Appendix I: References