From: Alexey Dobriyan <adobriyan@gmail.com>
To: akpm@linux-foundation.org
Cc: linux-kernel@vger.kernel.org, adobriyan@gmail.com
Subject: [PATCH 2/8] auto: add "auto" keyword as alias for __auto_type
Date: Thu, 18 May 2023 18:46:43 +0300 [thread overview]
Message-ID: <20230518154648.581643-2-adobriyan@gmail.com> (raw)
In-Reply-To: <20230518154648.581643-1-adobriyan@gmail.com>
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
next prev parent reply other threads:[~2023-05-18 15:47 UTC|newest]
Thread overview: 9+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-05-18 15:46 [PATCH 1/8] auto, kbuild: flatten KBUILD_CFLAGS Alexey Dobriyan
2023-05-18 15:46 ` Alexey Dobriyan [this message]
2023-05-18 15:46 ` [PATCH 3/8] auto, proc: use "auto" instead of quite chatty macros Alexey Dobriyan
2023-05-18 15:46 ` [PATCH 4/8] auto: promote DIV64_U64_ROUND_UP macro to function Alexey Dobriyan
2023-05-18 15:46 ` [PATCH 5/8] auto: promote DIV64_U64_ROUND_CLOSEST " Alexey Dobriyan
2023-05-18 15:46 ` [PATCH 6/8] auto: promote DIV_U64_ROUND_CLOSEST " Alexey Dobriyan
2023-05-18 15:46 ` [PATCH 7/8] auto: promote DIV_S64_ROUND_CLOSEST " Alexey Dobriyan
2023-05-18 20:37 ` [PATCH 1/8] auto, kbuild: flatten KBUILD_CFLAGS Andrew Morton
2023-05-19 11:15 ` Alexey Dobriyan
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20230518154648.581643-2-adobriyan@gmail.com \
--to=adobriyan@gmail.com \
--cc=akpm@linux-foundation.org \
--cc=linux-kernel@vger.kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®