summaryrefslogtreecommitdiffstats
path: root/Documentation/process
diff options
context:
space:
mode:
authorJason Gunthorpe <jgg@ziepe.ca>2019-01-18 17:50:47 -0500
committerJonathan Corbet <corbet@lwn.net>2019-01-20 21:07:39 -0500
commit7967656ffbfa493f5546c0f18bf8a28f702c4baa (patch)
tree64fc3eaa5ae81d1778891bc47c15a226ba2430df /Documentation/process
parentb04c11c988f4fcfba037ce6c356ea5ac9e23df63 (diff)
coding-style: Clarify the expectations around bool
There has been some confusion since checkpatch started warning about bool use in structures, and people have been avoiding using it. Many people feel there is still a legitimate place for bool in structures, so provide some guidance on bool usage derived from the entire thread that spawned the checkpatch warning. Link: https://lkml.kernel.org/r/CA+55aFwVZk1OfB9T2v014PTAKFhtVan_Zj2dOjnCy3x6E4UJfA@mail.gmail.com Signed-off-by: Joe Perches <joe@perches.com> Acked-by: Joe Perches <joe@perches.com> Reviewed-by: Bart Van Assche <bvanassche@acm.org> Acked-by: Jani Nikula <jani.nikula@intel.com> Reviewed-by: Joey Pabalinas <joeypabalinas@gmail.com> Signed-off-by: Jason Gunthorpe <jgg@mellanox.com> Signed-off-by: Jonathan Corbet <corbet@lwn.net>
Diffstat (limited to 'Documentation/process')
-rw-r--r--Documentation/process/coding-style.rst38
1 files changed, 34 insertions, 4 deletions
diff --git a/Documentation/process/coding-style.rst b/Documentation/process/coding-style.rst
index 4e611a01adc6..8ea913e99fa1 100644
--- a/Documentation/process/coding-style.rst
+++ b/Documentation/process/coding-style.rst
@@ -938,7 +938,37 @@ result. Typical examples would be functions that return pointers; they use
938NULL or the ERR_PTR mechanism to report failure. 938NULL or the ERR_PTR mechanism to report failure.
939 939
940 940
94117) Don't re-invent the kernel macros 94117) Using bool
942--------------
943
944The Linux kernel bool type is an alias for the C99 _Bool type. bool values can
945only evaluate to 0 or 1, and implicit or explicit conversion to bool
946automatically converts the value to true or false. When using bool types the
947!! construction is not needed, which eliminates a class of bugs.
948
949When working with bool values the true and false definitions should be used
950instead of 1 and 0.
951
952bool function return types and stack variables are always fine to use whenever
953appropriate. Use of bool is encouraged to improve readability and is often a
954better option than 'int' for storing boolean values.
955
956Do not use bool if cache line layout or size of the value matters, as its size
957and alignment varies based on the compiled architecture. Structures that are
958optimized for alignment and size should not use bool.
959
960If a structure has many true/false values, consider consolidating them into a
961bitfield with 1 bit members, or using an appropriate fixed width type, such as
962u8.
963
964Similarly for function arguments, many true/false values can be consolidated
965into a single bitwise 'flags' argument and 'flags' can often be a more
966readable alternative if the call-sites have naked true/false constants.
967
968Otherwise limited use of bool in structures and arguments can improve
969readability.
970
97118) Don't re-invent the kernel macros
942------------------------------------- 972-------------------------------------
943 973
944The header file include/linux/kernel.h contains a number of macros that 974The header file include/linux/kernel.h contains a number of macros that
@@ -961,7 +991,7 @@ need them. Feel free to peruse that header file to see what else is already
961defined that you shouldn't reproduce in your code. 991defined that you shouldn't reproduce in your code.
962 992
963 993
96418) Editor modelines and other cruft 99419) Editor modelines and other cruft
965------------------------------------ 995------------------------------------
966 996
967Some editors can interpret configuration information embedded in source files, 997Some editors can interpret configuration information embedded in source files,
@@ -995,7 +1025,7 @@ own custom mode, or may have some other magic method for making indentation
995work correctly. 1025work correctly.
996 1026
997 1027
99819) Inline assembly 102820) Inline assembly
999------------------- 1029-------------------
1000 1030
1001In architecture-specific code, you may need to use inline assembly to interface 1031In architecture-specific code, you may need to use inline assembly to interface
@@ -1027,7 +1057,7 @@ the next instruction in the assembly output:
1027 : /* outputs */ : /* inputs */ : /* clobbers */); 1057 : /* outputs */ : /* inputs */ : /* clobbers */);
1028 1058
1029 1059
103020) Conditional Compilation 106021) Conditional Compilation
1031--------------------------- 1061---------------------------
1032 1062
1033Wherever possible, don't use preprocessor conditionals (#if, #ifdef) in .c 1063Wherever possible, don't use preprocessor conditionals (#if, #ifdef) in .c