Clang gives a different answer for __builtin_constant_p than GCC
Consider the following C code:
_Static_assert(__builtin_constant_p((1,2)), "what?");
In Clang, the static assertion fails and in GCC the static assertion passes. C disallows comma operators explicitly as constant expressions, and Clang does get that correct in some cases:
_Static_assert((1, 2), "what?");
In both Clang and GCC the static assertion fails (as it should): https://godbolt.org/z/odTKMrqWe
@llvm/issue-subscribers-c
@llvm/issue-subscribers-clang-frontend
A bit off-topic: FYI: In C23 6.6 Constant expressions, paragraph 10 changed:
An implementation may accept other forms of constant expressions, however, they are not an integer constant expression.
So the gcc documents for builtin_constant_p says:
You can use the built-in function __builtin_constant_p to determine if a value is known to be constant at compile time and hence that GCC can
perform constant-folding on expressionsinvolving that value.
constant folding seems a much less strict term and sounds like it basically covers whatever the implementation can turn into a constant at compile-time and (1,2) looks like it reasonably fits that category. Although I can see in general case guaranteeing that clang folds for all the same cases may not be reasonable.
Consider __builtin_constant_p in GCC returns 1 and constant folding was performed, clang returns 1 for compatibility with GCC but does not perform constant folding (For instance, the example code mentioned in https://github.com/llvm/llvm-project/issues/58078).
I'm wondering if we should actually return whether or not the expression in __builtin_constant_p is "folded" in clang AST, using the single recursive evaluation method Expr::Evaluate. In this way, we may return different values from GCC, but implemented this builtin function as the same semantic as GCC documentation.
If so, https://github.com/llvm/llvm-project/issues/58078 may duplicate to this issue (and should be closed).
Link: https://clang.llvm.org/docs/InternalsManual.html#constant-folding-in-the-clang-ast Link: https://gcc.gnu.org/onlinedocs/gcc/Other-Builtins.html#index-_005f_005fbuiltin_005fconstant_005fp
I'm wondering if we should actually return whether or not the expression in __builtin_constant_p is "folded" in clang AST, using the single recursive evaluation method Expr::Evaluate. In this way, we may return different values from GCC, but implemented this builtin function as the same semantic as GCC documentation.
I think that an important part of current semantics of GCC's builtin is, that whenever the builtin is evaluated in a const-context, the expression passed to the builtin is also evaluated in a const-context. This results in the behaviour you described in the GCC bug-report and it allows me to define IS_CONSTEXPR like I currently do. Can clang's builtin be changed to do the same?
I think that would allow you to return the actual value of whether the expression is "folded" in clang, but it would also increase the compatibility with GCC.
Any progress on this?
Some other discussions on __builtin_constant_p
https://github.com/llvm/llvm-project/pull/159599 https://github.com/llvm/llvm-project/issues/154024 https://github.com/llvm/llvm-project/issues/35381
Suprisingly, I went by link in topic message ( https://godbolt.org/z/odTKMrqWe ) and Godbolt shows me opposite:
- GCC fails static assertion
_Static_assert(__builtin_constant_p((1,2)), "what?");, but Clang doesn't.
What did I wrong?