[PATCH] __builtin_constant_p inside an initializer
Mark Shinwell
shinwell@codesourcery.com
Thu Jun 1 16:32:00 GMT 2006
Consider the following code:
int main (int argc, char *argv[])
{
static int a[] = { __builtin_constant_p (argc) ? 1 : 0 };
return a[0];
}
With 4.x series compilers this compiles successfully at -O0 but not
at -O2, for example, failing with an error of the form
foo.c: In function 'main':
foo.c:3: error: initializer element is not constant
foo.c:3: error: (near initialization for 'a[0]')
The underlying reason for this is that the folding function
fold_builtin_constant_p defers the decision to evaluate the
builtin (to a constant zero or one expression) until optimization.
A special case catches this at -O0 and changes that decision to
"evaluate this to a zero". However at other optimization levels, the
__builtin_constant_p expression is not known to be a constant early
enough for the above error to be suppressed.
To fix this, the attached patch notes when folding inside an initializer,
and makes fold_builtin_constant_p give a definite answer rather than
deferring until a later stage. This does not fix all of the situations
where similar problems could manifest themselves, of course: use of
__builtin_constant_p inside an array size bound would be one such.
OK for mainline?
Mark
--
gcc/ChangeLog:
2006-06-01 Mark Shinwell <shinwell@codesourcery.com>
* tree.h: Declare folding_initializer.
* builtins.c (fold_builtin_constant_p): Give definite answer
if folding inside an initializer.
* fold-const.c: Define folding_initializer.
(START_FOLD_INIT): Save and then set folding_initializer.
(END_FOLD_INIT): Restore folding_initializer.
gcc/testsuite/ChangeLog:
2006-06-01 Mark Shinwell <shinwell@codesourcery.com>
* gcc.c-torture/compile/builtin_constant_p.c: New test.
-------------- next part --------------
An embedded and charset-unspecified text was scrubbed...
Name: builtin_constant_p.c
URL: <http://gcc.gnu.org/pipermail/gcc-patches/attachments/20060601/63edd9da/attachment.c>
-------------- next part --------------
An embedded and charset-unspecified text was scrubbed...
Name: builtin_constant_p.patch
URL: <http://gcc.gnu.org/pipermail/gcc-patches/attachments/20060601/63edd9da/attachment.ksh>
More information about the Gcc-patches
mailing list