On Sat, Aug 16, 2014 at 5:35 AM, David Herrmann <dh.herrm...@gmail.com>
wrote:

> Hi
>
> On Fri, Aug 15, 2014 at 5:22 PM, Daniele Nicolodi <dani...@grinta.net>
> wrote:
> > On 15/08/2014 16:30, David Herrmann wrote:
> >> Ok, took me a while, but I now figured out how to cause compilation to
> >> fail even in expressions that initialize types (_Static_assert is not
> >> allowed there):
> >>   #define assert_const(expr)
> >> ((void)(__builtin_types_compatible_p(int[(expr) ? 1 : -1], int[1])))
> >>
> >> Btw., I like that more than our current assert_cc() fallback. But I
> >> leave it up to you to decide.
> >>
> >> Anyhow, I found a way to make CONST_MAX work:
> >> #define CONST_MAX(_A, _B)
> >> (__builtin_choose_expr(__builtin_constant_p(_A) &&
> >> __builtin_constant_p(_B), ((_A) > (_B)) ? (_A) : (_B), (void)0))
> >>
> >> This will return (void) in case _A or _B is not constant. Works fine
> >> on LLVM, I now have to test it on gcc. If it works, I will commit it
> >> and fix resolvd.
> >
> > Hello,
> >
> > this may be completely stupid, but if the only use case you have for
> > CONST_MAX() is for computing the size of a data structure, I find
> > something like
> >
> > #define MAXSIZE(A, B) sizeof(union { __typeof(A) a; __typeof(B) b;})
> >
> > a little more clear and less magic, and I believe it has the same
> > guarantees that the solution you found.
>
> Your MAXSIZE macro might add padding:
>         union A {
>                 int a;
>                 char b[5];
>         };
> This union has size 8, not 5 (64bit). But CONST_MAX would return 5.
> Not sure whether that really matters, though. And we could probably
> add __packed__ to the definition.
>
> However, I noticed that GCC complains about using
> statement-expressions to initialize static-const structure members,
> even with my 'const' annotations added to MAX. *sigh*
> Thus, I think I'll keep CONST_MAX, as we'd require a 3rd macro otherwise.
>
> If you know a way to unify them all, please lemme know.
>
> Thanks for getting this in! I had given up on receiving constructive
feedback on my attempt to solve this earlier [1]. Glad your patch garnered
useful feedback that got to a committable result!

-Dan

[1]
http://lists.freedesktop.org/archives/systemd-devel/2014-August/021761.html
_______________________________________________
systemd-devel mailing list
systemd-devel@lists.freedesktop.org
http://lists.freedesktop.org/mailman/listinfo/systemd-devel

Reply via email to