[Bug middle-end/14505] __builtin_constant_p(__func__) is not true
jakub at gcc dot gnu.org
gcc-bugzilla@gcc.gnu.org
Tue Apr 5 06:33:00 GMT 2016
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=14505
Jakub Jelinek <jakub at gcc dot gnu.org> changed:
What |Removed |Added
----------------------------------------------------------------------------
CC| |jakub at gcc dot gnu.org
--- Comment #6 from Jakub Jelinek <jakub at gcc dot gnu.org> ---
Returning 1 for addresses of objects is highly undesirable. Not only it isn't
really constant on many targets or with many options (e.g. in shared libraries,
or PIE executables), but even in position dependent code you don't know the
address at compile time, only at link time. And, as we don't return 1 for any
object addresses, so why should __func__ be an exception?
Try:
int
foo ()
{
static int a;
static const int b = 3;
static const char c[3] = "ab";
static const char *d = "cd";
static const char *const e = "ef";
return 1 * __builtin_constant_p (&a)
+ 2 * __builtin_constant_p (&b)
+ 4 *__builtin_constant_p (c)
+ 8 * __builtin_constant_p (c[0])
+ 16 *__builtin_constant_p (d)
+ 32 * __builtin_constant_p (d[0])
+ 64 *__builtin_constant_p (e)
+ 128 * __builtin_constant_p (e[0]);
}
The returned value is 8 + 32 + 128, which is IMHO right, what is really
constant are the individual characters of the string literals.
So, if you want to test whether __func__ is constant, you should test whether
e.g. __func__[0] is __builtin_constant_p, the address of that can be anything
and is not known at compile time.
More information about the Gcc-bugs
mailing list