From: "Uecker, Martin" <Martin.Uecker@med.uni-goettingen.de>
To: "torvalds@linux-foundation.org" <torvalds@linux-foundation.org>
Cc: "linux-kernel@vger.kernel.org" <linux-kernel@vger.kernel.org>
Subject: Re: detecting integer constant expressions in macros
Date: Wed, 21 Mar 2018 00:10:40 +0000 [thread overview]
Message-ID: <1521591040.508.2.camel@med.uni-goettingen.de> (raw)
In-Reply-To: <CA+55aFyRgq7hbmxK9471xY-eja4X=gKHSjF4Q+a0FJjxVB1TYQ@mail.gmail.com>
Am Dienstag, den 20.03.2018, 16:08 -0700 schrieb Linus Torvalds:
> On Tue, Mar 20, 2018 at 3:13 PM, Uecker, Martin
> <Martin.Uecker@med.uni-goettingen.de> wrote:
> >
> > here is an idea:
>
> That's not "an idea".
>
> That is either genius, or a seriously diseased mind.
>
> I can't quite tell which.
>
> > a test for integer constant expressions which returns an
> > integer constant expression itself which should be suitable
> > for passing to __builtin_choose_expr might be:
> >
> > #define ICE_P(x) (sizeof(int) == sizeof(*(1 ? ((void*)((x) * 0l)) :
> > (int*)1)))
...
> So now the end result is (sizeof(*(void *)(x)), which on gcc is
> generally *different* from 'int'.
>
> So I see two issues:
>
> - "sizeof(*(void *)1)" is not necessalily well-defined. For gcc it
> is
> 1. But it could cause warnings.
It is a documented extension which enables pointer arithmetic
on void pointers, so I am sure neither gcc nor
clang has any problem with it. But one could also use
__builtin_types_compatible_p instead.
> - this will break the minds of everybody who ever sees that
> expression.
>
> Those two issues might be fine, though.
>
> > This also does not evaluate x itself on gcc although this is
> > not guaranteed by the standard. (And I haven't tried any older
> > gcc.)
>
> Oh, I think it's guaranteed by the standard that 'sizeof()' doesn't
> evaluate the argument value, only the type.
It has to evaluate the expression for the length of an array,
but it is not specified whether this is done if it does not have
any effect on the result. I would assume that any sane compiler
does not.
> I'm in awe of your truly marvelously disgusting hack. That is truly a
> work of art.
>
> I'm sure it doesn't work or causes warnings for various reasons, but
> it's still a thing of beaty.
I thought you might like it ;-)
Martin
next prev parent reply other threads:[~2018-03-21 0:18 UTC|newest]
Thread overview: 11+ messages / expand[flat|nested] mbox.gz Atom feed top
2018-03-20 22:13 Uecker, Martin
2018-03-20 23:07 ` Uecker, Martin
2018-03-20 23:09 ` Linus Torvalds
2018-03-20 23:08 ` Linus Torvalds
2018-03-21 0:10 ` Uecker, Martin [this message]
2018-03-21 0:30 ` Linus Torvalds
2018-03-21 9:51 ` Uecker, Martin
2018-03-21 10:22 ` Uecker, Martin
2018-03-21 10:35 ` David Laight
2018-03-21 21:10 ` Rasmus Villemoes
2018-03-21 22:27 ` Linus Torvalds
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=1521591040.508.2.camel@med.uni-goettingen.de \
--to=martin.uecker@med.uni-goettingen.de \
--cc=linux-kernel@vger.kernel.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
all inboxes | Powered by JetHome®