[cfe-dev] Incongruency in __builtin_constant_p with pointer argument

Eli Friedman eli.friedman at gmail.com
Mon May 2 23:44:32 PDT 2011


On Mon, May 2, 2011 at 11:13 PM, Abramo Bagnara
<abramo.bagnara at gmail.com> wrote:
> Il 27/04/2011 23:30, Abramo Bagnara ha scritto:
>>
>> $ cat t.c
>> typedef const int * ptr;
>>
>> const int i = 0;
>> const ptr p = &i;
>>
>> int a[__builtin_constant_p(i) ? 1 : -1];
>> int b[__builtin_constant_p(p) ? 1 : -1];
>>
>> $ clang -S t.c
>> t.c:7:7: error: 'b' declared as an array with a negative size
>> int b[__builtin_constant_p(p) ? 1 : -1];
>>       ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
>> 1 error generated.
>> $ gcc -S t.c
>> t.c:6: error: size of array ‘a’ is negative
>> t.c:7: error: size of array ‘b’ is negative
>>
>> I'm perfectly fine with the fact that clang treats const typed variable
>> as evaluatable constant (although gcc don't), but this should be the
>> same for both cases.
>>
>> The fix is trivial and I'll commit it myself, but before I'd like to
>> know if it is preferred that __builtin_constant_p returns true for both
>> or false for both.
>
> Ping.
>
> We have also other fixes for clang constant expr evaluator, but we need
> to know the wanted policy. Currently it is not clear what is wishable
> and implementation is often incongruent.
>
> Also it need to be decided if __builtin_constant_p is tied to constant
> expr evaluator or not, i.e. if the value returned by this builtin should
> be the same of constant expr evaluator once taken in account the side
> effects.

Have you read http://gcc.gnu.org/onlinedocs/gcc/Other-Builtins.html#Other-Builtins
?  You're trying to impose semantics on __builtin_constant_p that
aren't part of its definition.  Currently, the results just sort of
fall out of whatever the AST-level constant folder happens to know how
to fold.  That isn't a guarantee, though; see also
http://llvm.org/bugs/show_bug.cgi?id=4898.

-Eli




More information about the cfe-dev mailing list