public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
From: David Laight <David.Laight@ACULAB.COM>
To: "linux-kernel@vger.kernel.org" <linux-kernel@vger.kernel.org>
Cc: "'linux@rasmusvillemoes.dk'" <linux@rasmusvillemoes.dk>,
	'Steven Rostedt' <rostedt@goodmis.org>,
	"keescook@chromium.org" <keescook@chromium.org>,
	"'bvanassche@acm.org'" <bvanassche@acm.org>,
	"torvalds@linux-foundation.org" <torvalds@linux-foundation.org>,
	Nathan Chancellor <nathan@kernel.org>,
	"Nick Desaulniers" <ndesaulniers@google.com>,
	Arnd Bergmann <arnd@arndb.de>,
	"akpm@linux-foundation.org" <akpm@linux-foundation.org>,
	"'Martin.Uecker@med.uni-goettingen.de'" 
	<Martin.Uecker@med.uni-goettingen.de>
Subject: [PATCH next 1/1] Use _Generic() to implement __is_constexpr()
Date: Fri, 10 Nov 2023 10:34:31 +0000	[thread overview]
Message-ID: <083d8bc4740246408dd2cc4510502530@AcuMS.aculab.com> (raw)

This is somewhat more obvious than relying on sizeof(void) being 1.
It is also portable to any c11 compiler.

Change the constants to 0 - they have no bearing on the result and
just confuse the reader.

It relies on the type of (expr ? NULL : (type *)any) being 'type *'
but that of (expr ? (void *)x : (type *)any) being 'void *'.

Signed-off-by: David Laight <david.laight@aculab.com>
---

I'm not 100% sure all of 'tools' sets -std=gnu99 or -std=c11 to get
_Generic() supported.

This version will also be usable in 'nolibc' where sizeof(void) is
an error.

 include/linux/compiler.h       | 2 +-
 tools/include/linux/compiler.h | 2 +-
 2 files changed, 2 insertions(+), 2 deletions(-)

diff --git a/include/linux/compiler.h b/include/linux/compiler.h
index bb1339c7057b..2e11e216fd69 100644
--- a/include/linux/compiler.h
+++ b/include/linux/compiler.h
@@ -233,7 +233,7 @@ static inline void *offset_to_ptr(const int *off)
  * Glory to Martin Uecker <Martin.Uecker@med.uni-goettingen.de>
  */
 #define __is_constexpr(x) \
-	(sizeof(int) == sizeof(*(8 ? ((void *)((long)(x) * 0l)) : (int *)8)))
+	_Generic(0 ? (void *)((long)(x) * 0) : (char *)0, char *: 1, void *: 0)
 
 /*
  * Whether 'type' is a signed type or an unsigned type. Supports scalar types,
diff --git a/tools/include/linux/compiler.h b/tools/include/linux/compiler.h
index 7b65566f3e42..6e7f05196414 100644
--- a/tools/include/linux/compiler.h
+++ b/tools/include/linux/compiler.h
@@ -69,7 +69,7 @@
  * Glory to Martin Uecker <Martin.Uecker@med.uni-goettingen.de>
  */
 #define __is_constexpr(x) \
-	(sizeof(int) == sizeof(*(8 ? ((void *)((long)(x) * 0l)) : (int *)8)))
+	_Generic(0 ? (void *)((long)(x) * 0) : (char *)0, char *: 1, void *: 0)
 
 #ifdef __ANDROID__
 /*
-- 
2.17.1

-
Registered Address Lakeside, Bramley Road, Mount Farm, Milton Keynes, MK1 1PT, UK
Registration No: 1397386 (Wales)


                 reply	other threads:[~2023-11-10 17:57 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

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=083d8bc4740246408dd2cc4510502530@AcuMS.aculab.com \
    --to=david.laight@aculab.com \
    --cc=Martin.Uecker@med.uni-goettingen.de \
    --cc=akpm@linux-foundation.org \
    --cc=arnd@arndb.de \
    --cc=bvanassche@acm.org \
    --cc=keescook@chromium.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux@rasmusvillemoes.dk \
    --cc=nathan@kernel.org \
    --cc=ndesaulniers@google.com \
    --cc=rostedt@goodmis.org \
    --cc=torvalds@linux-foundation.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