See: https://savannah.nongnu.org/bugs/?58606 and: mpfr-4.1.1/tests/tcmp_ui.c The code '__builtin_constant_p ((i++, 7))' was not working. I Fixed it in tccgen.c I added a testcase in tests/tcctest.c. I also wanted to add a test like '__builtin_constant_p ((10, 7))' but gcc needs -O1 for that to work. clang (and now tcc) work as expected.
Unified · Split
| @@ -44,6 +44,7 @@ static int nb_sym_pools; · 43 unmodified lines | ||
| 44 | 44 | static Sym *all_cleanups, *pending_gotos; |
| 45 | 45 | static int local_scope; |
| 46 | 46 | static int in_sizeof; |
| 47 | +static int constant_p; | |
| 47 | 48 | ST_DATA char debug_modes; |
| 48 | 49 | |
| 49 | 50 | ST_DATA SValue *vtop; |
| @@ -5567,8 +5568,10 @@ ST_FUNC void unary(void) · 5517 unmodified lines | ||
| 5567 | 5568 | } |
| 5568 | 5569 | break; |
| 5569 | 5570 | case TOK_builtin_constant_p: |
| 5571 | + constant_p = 1; | |
| 5570 | 5572 | parse_builtin_params(1, "e"); |
| 5571 | − n = (vtop->r & (VT_VALMASK | VT_LVAL)) == VT_CONST && | |
| 5573 | + n = constant_p && | |
| 5574 | + (vtop->r & (VT_VALMASK | VT_LVAL)) == VT_CONST && | |
| 5572 | 5575 | !((vtop->r & VT_SYM) && vtop->sym->a.addrtaken); |
| 5573 | 5576 | vtop--; |
| 5574 | 5577 | vpushi(n); |
| @@ -6424,6 +6427,8 @@ ST_FUNC void gexpr(void) · 849 unmodified lines | ||
| 6424 | 6427 | expr_eq(); |
| 6425 | 6428 | if (tok != ',') |
| 6426 | 6429 | break; |
| 6430 | + constant_p &= (vtop->r & (VT_VALMASK | VT_LVAL)) == VT_CONST && | |
| 6431 | + !((vtop->r & VT_SYM) && vtop->sym->a.addrtaken); | |
| 6427 | 6432 | vpop(); |
| 6428 | 6433 | next(); |
| 6429 | 6434 | } |
| @@ -3884,6 +3884,7 @@ void builtin_test(void) · 3883 unmodified lines | ||
| 3884 | 3884 | #endif |
| 3885 | 3885 | printf("res9 = %d\n", __builtin_constant_p("hi")); |
| 3886 | 3886 | printf("res10 = %d\n", __builtin_constant_p(func())); |
| 3887 | + printf("res11 = %d\n", __builtin_constant_p((i++, 7))); | |
| 3887 | 3888 | s = 1; |
| 3888 | 3889 | ll = 2; |
| 3889 | 3890 | i = __builtin_choose_expr (1 != 0, ll, s); |