[PATCH v2 1/7] compiler_types.h: add "auto" as a macro for "__auto_type"

H. Peter Anvin posted 7 patches 2 months, 2 weeks ago
[PATCH v2 1/7] compiler_types.h: add "auto" as a macro for "__auto_type"
Posted by H. Peter Anvin 2 months, 2 weeks ago
"auto" was defined as a keyword back in the K&R days, but as a storage
type specifier.  No one ever used it, since it was and is the default
storage type for local variables.

C++11 recycled the keyword to allow a type to be declared based on the
type of an initializer.  This was finally adopted into standard C in
C23.

gcc and clang provide the "__auto_type" alias keyword as an extension
for pre-C23, however, there is no reason to pollute the bulk of the
source base with this temporary keyword; instead define "auto" as a
macro unless the compiler is running in C23+ mode.

This macro is added in <linux/compiler_types.h> because that header is
included in some of the tools headers, wheres <linux/compiler.h> is
not as it has a bunch of very kernel-specific things in it.

Signed-off-by: H. Peter Anvin (Intel) <hpa@zytor.com>
---
 include/linux/compiler_types.h | 13 +++++++++++++
 1 file changed, 13 insertions(+)

diff --git a/include/linux/compiler_types.h b/include/linux/compiler_types.h
index 2b77d12e07b2..c8b1ee37934e 100644
--- a/include/linux/compiler_types.h
+++ b/include/linux/compiler_types.h
@@ -13,6 +13,19 @@
 
 #ifndef __ASSEMBLY__
 
+/*
+ * C23 introduces "auto" as a standard way to define type-inferred
+ * variables, but "auto" has been a (useless) keyword even since K&R C,
+ * so it has always been "namespace reserved."
+ *
+ * Until at some future time we require C23 support, we need the gcc
+ * extension __auto_type, but there is no reason to put that elsewhere
+ * in the source code.
+ */
+#if __STDC_VERSION__ < 202311L
+# define auto __auto_type
+#endif
+
 /*
  * Skipped when running bindgen due to a libclang issue;
  * see https://github.com/rust-lang/rust-bindgen/issues/2244.
-- 
2.50.1
Re: [PATCH v2 1/7] compiler_types.h: add "auto" as a macro for "__auto_type"
Posted by Miguel Ojeda 2 months, 2 weeks ago
On Sun, Jul 20, 2025 at 8:52 AM H. Peter Anvin <hpa@zytor.com> wrote:
>
> gcc and clang provide the "__auto_type" alias keyword as an extension
> for pre-C23, however, there is no reason to pollute the bulk of the
> source base with this temporary keyword; instead define "auto" as a
> macro unless the compiler is running in C23+ mode.
>
> This macro is added in <linux/compiler_types.h> because that header is
> included in some of the tools headers, wheres <linux/compiler.h> is
> not as it has a bunch of very kernel-specific things in it.

Sounds good. I guess we could need a workaround if someone happened to
invent an attribute which requires using "auto" in it, since it is not
reserved there in C23 AFAIU. So FWIW:

Acked-by: Miguel Ojeda <ojeda@kernel.org>

> + * so it has always been "namespace reserved."

Not sure what this means (could we just say reserved?).

Thanks!

Relatedly, there are some proposed, further changes to `auto` for C2y:

    https://www.open-std.org/jtc1/sc22/WG14/www/docs/n3579.htm

Cheers,
Miguel