<div dir="ltr">On Sat, Aug 16, 2014 at 5:35 AM, David Herrmann <span dir="ltr"><<a href="mailto:dh.herrmann@gmail.com" target="_blank">dh.herrmann@gmail.com</a>></span> wrote:<br><div class="gmail_extra"><div class="gmail_quote">
<blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">Hi<br>
<div class=""><br>
On Fri, Aug 15, 2014 at 5:22 PM, Daniele Nicolodi <<a href="mailto:daniele@grinta.net">daniele@grinta.net</a>> wrote:<br>
> On 15/08/2014 16:30, David Herrmann wrote:<br>
>> Ok, took me a while, but I now figured out how to cause compilation to<br>
>> fail even in expressions that initialize types (_Static_assert is not<br>
>> allowed there):<br>
>>   #define assert_const(expr)<br>
>> ((void)(__builtin_types_compatible_p(int[(expr) ? 1 : -1], int[1])))<br>
>><br>
>> Btw., I like that more than our current assert_cc() fallback. But I<br>
>> leave it up to you to decide.<br>
>><br>
>> Anyhow, I found a way to make CONST_MAX work:<br>
>> #define CONST_MAX(_A, _B)<br>
>> (__builtin_choose_expr(__builtin_constant_p(_A) &&<br>
>> __builtin_constant_p(_B), ((_A) > (_B)) ? (_A) : (_B), (void)0))<br>
>><br>
>> This will return (void) in case _A or _B is not constant. Works fine<br>
>> on LLVM, I now have to test it on gcc. If it works, I will commit it<br>
>> and fix resolvd.<br>
><br>
> Hello,<br>
><br>
> this may be completely stupid, but if the only use case you have for<br>
> CONST_MAX() is for computing the size of a data structure, I find<br>
> something like<br>
><br>
> #define MAXSIZE(A, B) sizeof(union { __typeof(A) a; __typeof(B) b;})<br>
><br>
> a little more clear and less magic, and I believe it has the same<br>
> guarantees that the solution you found.<br>
<br>
</div>Your MAXSIZE macro might add padding:<br>
        union A {<br>
                int a;<br>
                char b[5];<br>
        };<br>
This union has size 8, not 5 (64bit). But CONST_MAX would return 5.<br>
Not sure whether that really matters, though. And we could probably<br>
add __packed__ to the definition.<br>
<br>
However, I noticed that GCC complains about using<br>
statement-expressions to initialize static-const structure members,<br>
even with my 'const' annotations added to MAX. *sigh*<br>
Thus, I think I'll keep CONST_MAX, as we'd require a 3rd macro otherwise.<br>
<br>
If you know a way to unify them all, please lemme know.<br>
<br></blockquote></div>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!<br>
<br></div><div class="gmail_extra">-Dan<br><br>[1] <a href="http://lists.freedesktop.org/archives/systemd-devel/2014-August/021761.html">http://lists.freedesktop.org/archives/systemd-devel/2014-August/021761.html</a><br></div>
</div>