aboutsummaryrefslogtreecommitdiffstats
path: root/Documentation/CodingStyle
diff options
context:
space:
mode:
Diffstat (limited to 'Documentation/CodingStyle')
-rw-r--r--Documentation/CodingStyle70
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
392locations and some common work such as cleanup has to be done. If there is no 392locations and some common work such as cleanup has to be done. If there is no
393cleanup needed then just return directly. 393cleanup needed then just return directly.
394 394
395The rationale is: 395Choose label names which say what the goto does or why the goto exists. An
396example of a good name could be "out_buffer:" if the goto frees "buffer". Avoid
397using GW-BASIC names like "err1:" and "err2:". Also don't name them after the
398goto location like "err_kmalloc_failed:"
399
400The 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:
403int fun(int a) 408int 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 ...
419out: 425out_buffer:
420 kfree(buffer); 426 kfree(buffer);
421 return result; 427 return result;
422} 428}
423 429
430A common type of bug to be aware of it "one err bugs" which look like this:
431
432err:
433 kfree(foo->bar);
434 kfree(foo);
435 return ret;
436
437The 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:".
439
440
424 Chapter 8: Commenting 441 Chapter 8: Commenting
425 442
426Comments are good, but there is also a danger of over-commenting. NEVER 443Comments 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
867Wherever possible, don't use preprocessor conditionals (#if, #ifdef) in .c
868files; doing so makes code harder to read and logic harder to follow. Instead,
869use such conditionals in a header file defining functions for use in those .c
870files, providing no-op stub versions in the #else case, and then call those
871functions unconditionally from .c files. The compiler will avoid generating
872any code for the stub calls, producing identical results, but the logic will
873remain easy to follow.
874
875Prefer to compile out entire functions, rather than portions of functions or
876portions of expressions. Rather than putting an ifdef in an expression, factor
877out part or all of the expression into a separate helper function and apply the
878conditional to that function.
879
880If you have a function or variable which may potentially go unused in a
881particular configuration, and the compiler would warn about its definition
882going unused, mark the definition as __maybe_unused rather than wrapping it in
883a preprocessor conditional. (However, if a function or variable *always* goes
884unused, delete it.)
885
886Within code, where possible, use the IS_ENABLED macro to convert a Kconfig
887symbol into a C boolean expression, and use it in a normal C conditional:
888
889 if (IS_ENABLED(CONFIG_SOMETHING)) {
890 ...
891 }
892
893The compiler will constant-fold the conditional away, and include or exclude
894the block of code just as with an #ifdef, so this will not add any runtime
895overhead. However, this approach still allows the C compiler to see the code
896inside the block, and check it for correctness (syntax, types, symbol
897references, etc). Thus, you still have to use an #ifdef if the code inside the
898block references symbols that will not exist if the condition is not met.
899
900At the end of any non-trivial #if or #ifdef block (more than a few lines),
901place a comment after the #endif on the same line, noting the conditional
902expression 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