diff options
Diffstat (limited to 'Documentation/CodingStyle')
-rw-r--r-- | Documentation/CodingStyle | 70 |
1 files changed, 65 insertions, 5 deletions
diff --git a/Documentation/CodingStyle b/Documentation/CodingStyle index 3171822c22a5..618a33c940df 100644 --- a/Documentation/CodingStyle +++ b/Documentation/CodingStyle | |||
@@ -392,7 +392,12 @@ The goto statement comes in handy when a function exits from multiple | |||
392 | locations and some common work such as cleanup has to be done. If there is no | 392 | locations and some common work such as cleanup has to be done. If there is no |
393 | cleanup needed then just return directly. | 393 | cleanup needed then just return directly. |
394 | 394 | ||
395 | The rationale is: | 395 | Choose label names which say what the goto does or why the goto exists. An |
396 | example of a good name could be "out_buffer:" if the goto frees "buffer". Avoid | ||
397 | using GW-BASIC names like "err1:" and "err2:". Also don't name them after the | ||
398 | goto location like "err_kmalloc_failed:" | ||
399 | |||
400 | The rationale for using gotos is: | ||
396 | 401 | ||
397 | - unconditional statements are easier to understand and follow | 402 | - unconditional statements are easier to understand and follow |
398 | - nesting is reduced | 403 | - nesting is reduced |
@@ -403,9 +408,10 @@ The rationale is: | |||
403 | int fun(int a) | 408 | int fun(int a) |
404 | { | 409 | { |
405 | int result = 0; | 410 | int result = 0; |
406 | char *buffer = kmalloc(SIZE); | 411 | char *buffer; |
407 | 412 | ||
408 | if (buffer == NULL) | 413 | buffer = kmalloc(SIZE, GFP_KERNEL); |
414 | if (!buffer) | ||
409 | return -ENOMEM; | 415 | return -ENOMEM; |
410 | 416 | ||
411 | if (condition1) { | 417 | if (condition1) { |
@@ -413,14 +419,25 @@ int fun(int a) | |||
413 | ... | 419 | ... |
414 | } | 420 | } |
415 | result = 1; | 421 | result = 1; |
416 | goto out; | 422 | goto out_buffer; |
417 | } | 423 | } |
418 | ... | 424 | ... |
419 | out: | 425 | out_buffer: |
420 | kfree(buffer); | 426 | kfree(buffer); |
421 | return result; | 427 | return result; |
422 | } | 428 | } |
423 | 429 | ||
430 | A common type of bug to be aware of it "one err bugs" which look like this: | ||
431 | |||
432 | err: | ||
433 | kfree(foo->bar); | ||
434 | kfree(foo); | ||
435 | return ret; | ||
436 | |||
437 | 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:". | ||
439 | |||
440 | |||
424 | Chapter 8: Commenting | 441 | Chapter 8: Commenting |
425 | 442 | ||
426 | Comments are good, but there is also a danger of over-commenting. NEVER | 443 | Comments are good, but there is also a danger of over-commenting. NEVER |
@@ -845,6 +862,49 @@ next instruction in the assembly output: | |||
845 | : /* outputs */ : /* inputs */ : /* clobbers */); | 862 | : /* outputs */ : /* inputs */ : /* clobbers */); |
846 | 863 | ||
847 | 864 | ||
865 | Chapter 20: Conditional Compilation | ||
866 | |||
867 | Wherever possible, don't use preprocessor conditionals (#if, #ifdef) in .c | ||
868 | files; doing so makes code harder to read and logic harder to follow. Instead, | ||
869 | use such conditionals in a header file defining functions for use in those .c | ||
870 | files, providing no-op stub versions in the #else case, and then call those | ||
871 | functions unconditionally from .c files. The compiler will avoid generating | ||
872 | any code for the stub calls, producing identical results, but the logic will | ||
873 | remain easy to follow. | ||
874 | |||
875 | Prefer to compile out entire functions, rather than portions of functions or | ||
876 | portions of expressions. Rather than putting an ifdef in an expression, factor | ||
877 | out part or all of the expression into a separate helper function and apply the | ||
878 | conditional to that function. | ||
879 | |||
880 | If you have a function or variable which may potentially go unused in a | ||
881 | particular configuration, and the compiler would warn about its definition | ||
882 | going unused, mark the definition as __maybe_unused rather than wrapping it in | ||
883 | a preprocessor conditional. (However, if a function or variable *always* goes | ||
884 | unused, delete it.) | ||
885 | |||
886 | Within code, where possible, use the IS_ENABLED macro to convert a Kconfig | ||
887 | symbol into a C boolean expression, and use it in a normal C conditional: | ||
888 | |||
889 | if (IS_ENABLED(CONFIG_SOMETHING)) { | ||
890 | ... | ||
891 | } | ||
892 | |||
893 | The compiler will constant-fold the conditional away, and include or exclude | ||
894 | the block of code just as with an #ifdef, so this will not add any runtime | ||
895 | overhead. However, this approach still allows the C compiler to see the code | ||
896 | inside the block, and check it for correctness (syntax, types, symbol | ||
897 | references, etc). Thus, you still have to use an #ifdef if the code inside the | ||
898 | block references symbols that will not exist if the condition is not met. | ||
899 | |||
900 | At the end of any non-trivial #if or #ifdef block (more than a few lines), | ||
901 | place a comment after the #endif on the same line, noting the conditional | ||
902 | expression used. For instance: | ||
903 | |||
904 | #ifdef CONFIG_SOMETHING | ||
905 | ... | ||
906 | #endif /* CONFIG_SOMETHING */ | ||
907 | |||
848 | 908 | ||
849 | Appendix I: References | 909 | Appendix I: References |
850 | 910 | ||