diff options
Diffstat (limited to 'Documentation/CodingStyle')
| -rw-r--r-- | Documentation/CodingStyle | 149 |
1 files changed, 76 insertions, 73 deletions
diff --git a/Documentation/CodingStyle b/Documentation/CodingStyle index 4d4f06d47e06..f4b78eafd92a 100644 --- a/Documentation/CodingStyle +++ b/Documentation/CodingStyle | |||
| @@ -13,7 +13,7 @@ and NOT read it. Burn them, it's a great symbolic gesture. | |||
| 13 | Anyway, here goes: | 13 | Anyway, here goes: |
| 14 | 14 | ||
| 15 | 15 | ||
| 16 | Chapter 1: Indentation | 16 | Chapter 1: Indentation |
| 17 | 17 | ||
| 18 | Tabs are 8 characters, and thus indentations are also 8 characters. | 18 | Tabs are 8 characters, and thus indentations are also 8 characters. |
| 19 | There are heretic movements that try to make indentations 4 (or even 2!) | 19 | There are heretic movements that try to make indentations 4 (or even 2!) |
| @@ -56,7 +56,6 @@ instead of "double-indenting" the "case" labels. E.g.: | |||
| 56 | break; | 56 | break; |
| 57 | } | 57 | } |
| 58 | 58 | ||
| 59 | |||
| 60 | Don't put multiple statements on a single line unless you have | 59 | Don't put multiple statements on a single line unless you have |
| 61 | something to hide: | 60 | something to hide: |
| 62 | 61 | ||
| @@ -156,25 +155,25 @@ comments on. | |||
| 156 | 155 | ||
| 157 | Do not unnecessarily use braces where a single statement will do. | 156 | Do not unnecessarily use braces where a single statement will do. |
| 158 | 157 | ||
| 159 | if (condition) | 158 | if (condition) |
| 160 | action(); | 159 | action(); |
| 161 | 160 | ||
| 162 | and | 161 | and |
| 163 | 162 | ||
| 164 | if (condition) | 163 | if (condition) |
| 165 | do_this(); | 164 | do_this(); |
| 166 | else | 165 | else |
| 167 | do_that(); | 166 | do_that(); |
| 168 | 167 | ||
| 169 | This does not apply if only one branch of a conditional statement is a single | 168 | This does not apply if only one branch of a conditional statement is a single |
| 170 | statement; in the latter case use braces in both branches: | 169 | statement; in the latter case use braces in both branches: |
| 171 | 170 | ||
| 172 | if (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 | ||
| 188 | So use a space after these keywords: | 187 | So use a space after these keywords: |
| 188 | |||
| 189 | if, switch, case, for, do, while | 189 | if, switch, case, for, do, while |
| 190 | |||
| 190 | but not with sizeof, typeof, alignof, or __attribute__. E.g., | 191 | but not with sizeof, typeof, alignof, or __attribute__. E.g., |
| 192 | |||
| 191 | s = sizeof(struct file); | 193 | s = sizeof(struct file); |
| 192 | 194 | ||
| 193 | Do not add spaces around (inside) parenthesized expressions. This example is | 195 | Do not add spaces around (inside) parenthesized expressions. This example is |
| @@ -209,12 +211,15 @@ such as any of these: | |||
| 209 | = + - < > * / % | & ^ <= >= == != ? : | 211 | = + - < > * / % | & ^ <= >= == != ? : |
| 210 | 212 | ||
| 211 | but no space after unary operators: | 213 | but no space after unary operators: |
| 214 | |||
| 212 | & * + - ~ ! sizeof typeof alignof __attribute__ defined | 215 | & * + - ~ ! sizeof typeof alignof __attribute__ defined |
| 213 | 216 | ||
| 214 | no space before the postfix increment & decrement unary operators: | 217 | no space before the postfix increment & decrement unary operators: |
| 218 | |||
| 215 | ++ -- | 219 | ++ -- |
| 216 | 220 | ||
| 217 | no space after the prefix increment & decrement unary operators: | 221 | no space after the prefix increment & decrement unary operators: |
| 222 | |||
| 218 | ++ -- | 223 | ++ -- |
| 219 | 224 | ||
| 220 | and no space around the '.' and "->" structure member operators. | 225 | and 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 | ||
| 270 | Please don't use things like "vps_t". | 275 | Please don't use things like "vps_t". |
| 271 | |||
| 272 | It's a _mistake_ to use typedef for structures and pointers. When you see a | 276 | It'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 | ||
| 276 | in the source, what does it mean? | 280 | in the source, what does it mean? |
| 277 | |||
| 278 | In contrast, if it says | 281 | In 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 | |||
| 372 | exported, the EXPORT* macro for it should follow immediately after the closing | 375 | exported, the EXPORT* macro for it should follow immediately after the closing |
| 373 | function brace line. E.g.: | 376 | function brace line. E.g.: |
| 374 | 377 | ||
| 375 | int 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 | } |
| 379 | EXPORT_SYMBOL(system_is_up); | 382 | EXPORT_SYMBOL(system_is_up); |
| 380 | 383 | ||
| 381 | In function prototypes, include parameter names with their data types. | 384 | In function prototypes, include parameter names with their data types. |
| 382 | Although this is not required by the C language, it is preferred in Linux | 385 | Although 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 | ||
| 408 | int 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 | ... | ||
| 425 | out_buffer: | ||
| 426 | kfree(buffer); | ||
| 427 | return result; | ||
| 428 | } | ||
| 429 | 432 | ||
| 430 | A common type of bug to be aware of it "one err bugs" which look like this: | 433 | A common type of bug to be aware of it "one err bugs" which look like this: |
| 431 | 434 | ||
| 432 | err: | 435 | err: |
| 433 | kfree(foo->bar); | 436 | kfree(foo->bar); |
| 434 | kfree(foo); | 437 | kfree(foo); |
| 435 | return ret; | 438 | return ret; |
| 436 | 439 | ||
| 437 | The bug in this code is that on some exit paths "foo" is NULL. Normally the | 440 | The bug in this code is that on some exit paths "foo" is NULL. Normally the |
| 438 | fix for this is to split it up into two error labels "err_bar:" and "err_foo:". | 441 | fix for this is to split it up into two error labels "err_bar:" and "err_foo:". |
| @@ -503,9 +506,9 @@ values. To do the latter, you can stick the following in your .emacs file: | |||
| 503 | (defun c-lineup-arglist-tabs-only (ignored) | 506 | (defun c-lineup-arglist-tabs-only (ignored) |
| 504 | "Line up argument lists by tabs, not spaces" | 507 | "Line up argument lists by tabs, not spaces" |
| 505 | (let* ((anchor (c-langelem-pos c-syntactic-element)) | 508 | (let* ((anchor (c-langelem-pos c-syntactic-element)) |
| 506 | (column (c-langelem-2nd-pos c-syntactic-element)) | 509 | (column (c-langelem-2nd-pos c-syntactic-element)) |
| 507 | (offset (- (1+ column) anchor)) | 510 | (offset (- (1+ column) anchor)) |
| 508 | (steps (floor offset c-basic-offset))) | 511 | (steps (floor offset c-basic-offset))) |
| 509 | (* (max steps 1) | 512 | (* (max steps 1) |
| 510 | c-basic-offset))) | 513 | c-basic-offset))) |
| 511 | 514 | ||
| @@ -612,7 +615,7 @@ have a reference count on it, you almost certainly have a bug. | |||
| 612 | 615 | ||
| 613 | Names of macros defining constants and labels in enums are capitalized. | 616 | Names of macros defining constants and labels in enums are capitalized. |
| 614 | 617 | ||
| 615 | #define CONSTANT 0x12345 | 618 | #define CONSTANT 0x12345 |
| 616 | 619 | ||
| 617 | Enums are preferred when defining several related constants. | 620 | Enums are preferred when defining several related constants. |
| 618 | 621 | ||
| @@ -623,28 +626,28 @@ Generally, inline functions are preferable to macros resembling functions. | |||
| 623 | 626 | ||
| 624 | Macros with multiple statements should be enclosed in a do - while block: | 627 | Macros 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 | ||
| 632 | Things to avoid when using macros: | 635 | Things to avoid when using macros: |
| 633 | 636 | ||
| 634 | 1) macros that affect control flow: | 637 | 1) macros that affect control flow: |
| 635 | 638 | ||
| 636 | #define FOO(x) \ | 639 | #define FOO(x) \ |
| 637 | do { \ | 640 | do { \ |
| 638 | if (blah(x) < 0) \ | 641 | if (blah(x) < 0) \ |
| 639 | return -EBUGGERED; \ | 642 | return -EBUGGERED; \ |
| 640 | } while(0) | 643 | } while(0) |
| 641 | 644 | ||
| 642 | is a _very_ bad idea. It looks like a function call but exits the "calling" | 645 | is a _very_ bad idea. It looks like a function call but exits the "calling" |
| 643 | function; don't break the internal parsers of those who will read the code. | 646 | function; don't break the internal parsers of those who will read the code. |
| 644 | 647 | ||
| 645 | 2) macros that depend on having a local variable with a magic name: | 648 | 2) macros that depend on having a local variable with a magic name: |
| 646 | 649 | ||
| 647 | #define FOO(val) bar(index, val) | 650 | #define FOO(val) bar(index, val) |
| 648 | 651 | ||
| 649 | might look like a good thing, but it's confusing as hell when one reads the | 652 | might look like a good thing, but it's confusing as hell when one reads the |
| 650 | code and it's prone to breakage from seemingly innocent changes. | 653 | code 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. | |||
| 656 | must enclose the expression in parentheses. Beware of similar issues with | 659 | must enclose the expression in parentheses. Beware of similar issues with |
| 657 | macros using parameters. | 660 | macros using parameters. |
| 658 | 661 | ||
| 659 | #define CONSTANT 0x4000 | 662 | #define CONSTANT 0x4000 |
| 660 | #define CONSTEXP (CONSTANT | 3) | 663 | #define CONSTEXP (CONSTANT | 3) |
| 661 | 664 | ||
| 662 | 5) namespace collisions when defining local variables in macros resembling | 665 | 5) namespace collisions when defining local variables in macros resembling |
| 663 | functions: | 666 | functions: |
| @@ -809,11 +812,11 @@ you should use, rather than explicitly coding some variant of them yourself. | |||
| 809 | For example, if you need to calculate the length of an array, take advantage | 812 | For example, if you need to calculate the length of an array, take advantage |
| 810 | of the macro | 813 | of the macro |
| 811 | 814 | ||
| 812 | #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0])) | 815 | #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0])) |
| 813 | 816 | ||
| 814 | Similarly, if you need to calculate the size of some structure member, use | 817 | Similarly, if you need to calculate the size of some structure member, use |
| 815 | 818 | ||
| 816 | #define FIELD_SIZEOF(t, f) (sizeof(((t*)0)->f)) | 819 | #define FIELD_SIZEOF(t, f) (sizeof(((t*)0)->f)) |
| 817 | 820 | ||
| 818 | There are also min() and max() macros that do strict type checking if you | 821 | There are also min() and max() macros that do strict type checking if you |
| 819 | need them. Feel free to peruse that header file to see what else is already | 822 | need them. Feel free to peruse that header file to see what else is already |
| @@ -826,19 +829,19 @@ Some editors can interpret configuration information embedded in source files, | |||
| 826 | indicated with special markers. For example, emacs interprets lines marked | 829 | indicated with special markers. For example, emacs interprets lines marked |
| 827 | like this: | 830 | like this: |
| 828 | 831 | ||
| 829 | -*- mode: c -*- | 832 | -*- mode: c -*- |
| 830 | 833 | ||
| 831 | Or like this: | 834 | Or like this: |
| 832 | 835 | ||
| 833 | /* | 836 | /* |
| 834 | Local Variables: | 837 | Local Variables: |
| 835 | compile-command: "gcc -DMAGIC_DEBUG_FLAG foo.c" | 838 | compile-command: "gcc -DMAGIC_DEBUG_FLAG foo.c" |
| 836 | End: | 839 | End: |
| 837 | */ | 840 | */ |
| 838 | 841 | ||
| 839 | Vim interprets markers that look like this: | 842 | Vim interprets markers that look like this: |
| 840 | 843 | ||
| 841 | /* vim:set sw=8 noet */ | 844 | /* vim:set sw=8 noet */ |
| 842 | 845 | ||
| 843 | Do not include any of these in source files. People have their own personal | 846 | Do not include any of these in source files. People have their own personal |
| 844 | editor configurations, and your source files should not override them. This | 847 | editor configurations, and your source files should not override them. This |
| @@ -915,9 +918,9 @@ At the end of any non-trivial #if or #ifdef block (more than a few lines), | |||
| 915 | place a comment after the #endif on the same line, noting the conditional | 918 | place a comment after the #endif on the same line, noting the conditional |
| 916 | expression used. For instance: | 919 | expression used. For instance: |
| 917 | 920 | ||
| 918 | #ifdef CONFIG_SOMETHING | 921 | #ifdef CONFIG_SOMETHING |
| 919 | ... | 922 | ... |
| 920 | #endif /* CONFIG_SOMETHING */ | 923 | #endif /* CONFIG_SOMETHING */ |
| 921 | 924 | ||
| 922 | 925 | ||
| 923 | Appendix I: References | 926 | Appendix I: References |
