Messages in this thread Patch in this message | | | From | Alexey Dobriyan <> | Subject | [PATCH 2/8] auto: add "auto" keyword as alias for __auto_type | Date | Thu, 18 May 2023 18:46:43 +0300 |
| |
It has similar semantics to "auto" keyword from a language which can not be named on this mailing list, in particular:
{ int a; const auto b = a; // const int b = a; b = 1; // compile error } { char a; auto b = a; // char b = a; // no integer promotions static_assert(sizeof(b) == 1); } { int a; const auto p = &a; // int *const p = &a; *p = 1; // works because const is applied only to top-level }
It can be used to save on macroexpansion inside macro forests which use typeof() somewhere deep enough. It is cool regardless.
gcc 5.1 supports __auto_type.
Signed-off-by: Alexey Dobriyan <adobriyan@gmail.com> --- Documentation/process/coding-style.rst | 31 +++++++++++++++++++++----- include/linux/compiler_types.h | 2 ++ 2 files changed, 27 insertions(+), 6 deletions(-)
diff --git a/Documentation/process/coding-style.rst b/Documentation/process/coding-style.rst index 6db37a46d305..5bfa605c8bac 100644 --- a/Documentation/process/coding-style.rst +++ b/Documentation/process/coding-style.rst @@ -1018,7 +1018,26 @@ result. Typical examples would be functions that return pointers; they use NULL or the ERR_PTR mechanism to report failure. -17) Using bool +17) Using auto +-------------- + +Use ``auto`` macro-keyword (alias for ``__auto_type`` extension) in macros +with "evaluate argument once" idiom: + +.. code-block:: c + + #define min2(a, b) \ + ({ \ + auto a_ = (a); \ + auto b_ = (b); \ + a_ < b_ ? a_ : b_; \ + }) + +Read https://gcc.gnu.org/onlinedocs/gcc/Typeof.html before using ``auto`` or +changing anything with ``auto`` in it. + + +18) Using bool -------------- The Linux kernel bool type is an alias for the C99 _Bool type. bool values can @@ -1048,7 +1067,7 @@ readable alternative if the call-sites have naked true/false constants. Otherwise limited use of bool in structures and arguments can improve readability. -18) Don't re-invent the kernel macros +19) Don't re-invent the kernel macros ------------------------------------- The header file include/linux/kernel.h contains a number of macros that @@ -1071,7 +1090,7 @@ need them. Feel free to peruse that header file to see what else is already defined that you shouldn't reproduce in your code. -19) Editor modelines and other cruft +20) Editor modelines and other cruft ------------------------------------ Some editors can interpret configuration information embedded in source files, @@ -1105,7 +1124,7 @@ own custom mode, or may have some other magic method for making indentation work correctly. -20) Inline assembly +21) Inline assembly ------------------- In architecture-specific code, you may need to use inline assembly to interface @@ -1137,7 +1156,7 @@ the next instruction in the assembly output: : /* outputs */ : /* inputs */ : /* clobbers */); -21) Conditional Compilation +22) Conditional Compilation --------------------------- Wherever possible, don't use preprocessor conditionals (#if, #ifdef) in .c @@ -1186,7 +1205,7 @@ expression used. For instance: #endif /* CONFIG_SOMETHING */ -22) Do not crash the kernel +23) Do not crash the kernel --------------------------- In general, the decision to crash the kernel belongs to the user, rather diff --git a/include/linux/compiler_types.h b/include/linux/compiler_types.h index 547ea1ff806e..bf0ac2a04154 100644 --- a/include/linux/compiler_types.h +++ b/include/linux/compiler_types.h @@ -4,6 +4,8 @@ #ifndef __ASSEMBLY__ +#define auto __auto_type + /* * Skipped when running bindgen due to a libclang issue; * see https://github.com/rust-lang/rust-bindgen/issues/2244. -- 2.40.1
| |