Aka 'intmax_t', as recommended by newer standards. For example if (-0x80000000 < 0) is false, but #if (-0x80000000 < 0) is true because in C, 0x80000000 is an unsigned int, Whereas in a preprocessor expression, it is a signed long long, even without LL suffix.
Unified · Split
| @@ -542,11 +542,7 @@ ST_FUNC const char *get_tok_str(int v, CValue *cv) · 541 unmodified lines | ||
| 542 | 542 | case TOK_CLLONG: |
| 543 | 543 | case TOK_CULLONG: |
| 544 | 544 | /* XXX: not quite exact, but only useful for testing */ |
| 545 | −#ifdef _WIN32 | |
| 546 | − sprintf(p, "%u", (unsigned)cv->i); | |
| 547 | −#else | |
| 548 | 545 | sprintf(p, "%llu", (unsigned long long)cv->i); |
| 549 | −#endif | |
| 550 | 546 | break; |
| 551 | 547 | case TOK_LCHAR: |
| 552 | 548 | cstr_ccat(&cstr_buf, 'L'); |
| @@ -1493,8 +1489,7 @@ static int expr_preprocess(TCCState *s1) · 940 unmodified lines | ||
| 1493 | 1489 | if (tok != ')') |
| 1494 | 1490 | expect("')'"); |
| 1495 | 1491 | } |
| 1496 | − tok = TOK_CINT; | |
| 1497 | − tokc.i = c; | |
| 1492 | + goto c_number; | |
| 1498 | 1493 | } else if (tok == TOK___HAS_INCLUDE || |
| 1499 | 1494 | tok == TOK___HAS_INCLUDE_NEXT) { |
| 1500 | 1495 | t = tok; |
| @@ -1504,12 +1499,13 @@ static int expr_preprocess(TCCState *s1) · 3 unmodified lines | ||
| 1504 | 1499 | c = parse_include(s1, t - TOK___HAS_INCLUDE, 1); |
| 1505 | 1500 | if (tok != ')') |
| 1506 | 1501 | expect("')'"); |
| 1507 | − tok = TOK_CINT; | |
| 1508 | − tokc.i = c; | |
| 1502 | + goto c_number; | |
| 1509 | 1503 | } else { |
| 1510 | 1504 | /* if undefined macro, replace with zero */ |
| 1511 | − tok = TOK_CINT; | |
| 1512 | − tokc.i = 0; | |
| 1505 | + c = 0; | |
| 1506 | + c_number: | |
| 1507 | + tok = TOK_CLLONG; /* type intmax_t */ | |
| 1508 | + tokc.i = c; | |
| 1513 | 1509 | } |
| 1514 | 1510 | tok_str_add_tok(str); |
| 1515 | 1511 | } |
| @@ -2510,6 +2506,10 @@ static void parse_number(const char *p) · 994 unmodified lines | ||
| 2510 | 2506 | } |
| 2511 | 2507 | } |
| 2512 | 2508 | |
| 2509 | + /* in #if/#elif expressions, all numbers have type (u)intmax_t anyway */ | |
| 2510 | + if (pp_expr) | |
| 2511 | + lcount = 2; | |
| 2512 | + | |
| 2513 | 2513 | /* Determine if it needs 64 bits and/or unsigned in order to fit */ |
| 2514 | 2514 | if (ucount == 0 && b == 10) { |
| 2515 | 2515 | if (lcount <= (LONG_SIZE == 4)) { |
| @@ -39,3 +39,24 @@ NOT OK · 38 unmodified lines | ||
| 39 | 39 | line __LINE__ |
| 40 | 40 | #define __LINE__ # ## # |
| 41 | 41 | line __LINE__ |
| 42 | + | |
| 43 | +----- 10 ------ | |
| 44 | +/* preprocessor numbers are (u)intmax_t */ | |
| 45 | +#if -2147483648 < 0 | |
| 46 | +1 true | |
| 47 | +#endif | |
| 48 | +#if -0x80000000 < 0 | |
| 49 | +2 true | |
| 50 | +#endif | |
| 51 | +#if -9223372036854775808U > 0 | |
| 52 | +3 true | |
| 53 | +#endif | |
| 54 | +#if -0x8000000000000000 > 0 // unsigned by overflow | |
| 55 | +4 true | |
| 56 | +#endif | |
| 57 | +#if 1 << 31 > 2 && 1 << 32 > 2 && 1 << 63 < 2 && 1U << 63 > 2 | |
| 58 | +5 true | |
| 59 | +#endif | |
| 60 | +#if (1<<29) * 11 >= 1<<32 && defined DDD << 63 < 0 | |
| 61 | +6 true | |
| 62 | +#endif | |
| @@ -9,3 +9,10 @@ OK · 8 unmodified lines | ||
| 9 | 9 | ----- 5 ------ |
| 10 | 10 | line 39 |
| 11 | 11 | line ## |
| 12 | +----- 10 ------ | |
| 13 | +1 true | |
| 14 | +2 true | |
| 15 | +3 true | |
| 16 | +4 true | |
| 17 | +5 true | |
| 18 | +6 true | |