[<prev] [next>] [day] [month] [year] [list]
Message-ID: <d4f87590-6cbb-4ee9-bead-7d958fc1fa83@p183>
Date: Thu, 5 Oct 2023 19:50:27 +0300
From: Alexey Dobriyan <adobriyan@...il.com>
To: akpm@...ux-foundation.org
Cc: linux-kernel@...r.kernel.org, Dinh Nguyen <dinguyen@...nel.org>,
Peter Zijlstra <peterz@...radead.org>, x86@...nel.org
Subject: [PATCH v2] auto: add and use "auto" keyword (alias for __auto_type)
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; // OK, const is applied to top-level only
}
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@...il.com>
---
Documentation/process/coding-style.rst | 31 +++++++++++++++++++++++++------
arch/nios2/include/asm/uaccess.h | 4 ++--
arch/x86/include/asm/bug.h | 2 +-
include/linux/cleanup.h | 2 +-
include/linux/compiler_types.h | 2 ++
5 files changed, 31 insertions(+), 10 deletions(-)
--- 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
--- a/arch/nios2/include/asm/uaccess.h
+++ b/arch/nios2/include/asm/uaccess.h
@@ -172,14 +172,14 @@ do { \
#define __put_user(x, ptr) \
({ \
- __auto_type __pu_ptr = (ptr); \
+ auto __pu_ptr = (ptr); \
typeof(*__pu_ptr) __pu_val = (typeof(*__pu_ptr))(x); \
__put_user_common(__pu_val, __pu_ptr); \
})
#define put_user(x, ptr) \
({ \
- __auto_type __pu_ptr = (ptr); \
+ auto __pu_ptr = (ptr); \
typeof(*__pu_ptr) __pu_val = (typeof(*__pu_ptr))(x); \
access_ok(__pu_ptr, sizeof(*__pu_ptr)) ? \
__put_user_common(__pu_val, __pu_ptr) : \
--- a/arch/x86/include/asm/bug.h
+++ b/arch/x86/include/asm/bug.h
@@ -78,7 +78,7 @@ do { \
*/
#define __WARN_FLAGS(flags) \
do { \
- __auto_type __flags = BUGFLAG_WARNING|(flags); \
+ auto __flags = BUGFLAG_WARNING|(flags); \
instrumentation_begin(); \
_BUG_FLAGS(ASM_UD2, __flags, ASM_REACHABLE); \
instrumentation_end(); \
--- a/include/linux/cleanup.h
+++ b/include/linux/cleanup.h
@@ -40,7 +40,7 @@
#define __free(_name) __cleanup(__free_##_name)
#define no_free_ptr(p) \
- ({ __auto_type __ptr = (p); (p) = NULL; __ptr; })
+ ({ auto __ptr = (p); (p) = NULL; __ptr; })
#define return_ptr(p) return no_free_ptr(p)
--- a/include/linux/compiler_types.h
+++ b/include/linux/compiler_types.h
@@ -76,6 +76,8 @@ static inline void __chk_io_ptr(const volatile void __iomem *ptr) { }
#ifdef __KERNEL__
+#define auto __auto_type
+
/* Attributes */
#include <linux/compiler_attributes.h>
Powered by blists - more mailing lists