History

pp-expression numbers always long long

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.

grischka committed 1 year ago

Commit b3381261 parent f57cc343 files · +38 −10Browse files

Unified · Split

3 files
  1. tccpp.c +10 −10
  2. tests/pp/21.c +21 −0
  3. tests/pp/21.expect +7 −0
tccpp.c+10−10
@@ -542,11 +542,7 @@ ST_FUNC const char *get_tok_str(int v, CValue *cv) · 541 unmodified lines
542 case TOK_CLLONG:542 case TOK_CLLONG:
543 case TOK_CULLONG:543 case TOK_CULLONG:
544 /* XXX: not quite exact, but only useful for testing */544 /* XXX: not quite exact, but only useful for testing */
545#ifdef _WIN32
546 sprintf(p, "%u", (unsigned)cv->i);
547#else
548 sprintf(p, "%llu", (unsigned long long)cv->i);545 sprintf(p, "%llu", (unsigned long long)cv->i);
549#endif
550 break;546 break;
551 case TOK_LCHAR:547 case TOK_LCHAR:
552 cstr_ccat(&cstr_buf, 'L');548 cstr_ccat(&cstr_buf, 'L');
@@ -1493,8 +1489,7 @@ static int expr_preprocess(TCCState *s1) · 940 unmodified lines
1493 if (tok != ')')1489 if (tok != ')')
1494 expect("')'");1490 expect("')'");
1495 }1491 }
1496 tok = TOK_CINT;1492 goto c_number;
1497 tokc.i = c;
1498 } else if (tok == TOK___HAS_INCLUDE ||1493 } else if (tok == TOK___HAS_INCLUDE ||
1499 tok == TOK___HAS_INCLUDE_NEXT) {1494 tok == TOK___HAS_INCLUDE_NEXT) {
1500 t = tok;1495 t = tok;
@@ -1504,12 +1499,13 @@ static int expr_preprocess(TCCState *s1) · 3 unmodified lines
1504 c = parse_include(s1, t - TOK___HAS_INCLUDE, 1);1499 c = parse_include(s1, t - TOK___HAS_INCLUDE, 1);
1505 if (tok != ')')1500 if (tok != ')')
1506 expect("')'");1501 expect("')'");
1507 tok = TOK_CINT;1502 goto c_number;
1508 tokc.i = c;
1509 } else {1503 } else {
1510 /* if undefined macro, replace with zero */1504 /* if undefined macro, replace with zero */
1511 tok = TOK_CINT;1505 c = 0;
1512 tokc.i = 0;1506 c_number:
1507 tok = TOK_CLLONG; /* type intmax_t */
1508 tokc.i = c;
1513 }1509 }
1514 tok_str_add_tok(str);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 /* Determine if it needs 64 bits and/or unsigned in order to fit */2513 /* Determine if it needs 64 bits and/or unsigned in order to fit */
2514 if (ucount == 0 && b == 10) {2514 if (ucount == 0 && b == 10) {
2515 if (lcount <= (LONG_SIZE == 4)) {2515 if (lcount <= (LONG_SIZE == 4)) {
tests/pp/21.c+21−0
@@ -39,3 +39,24 @@ NOT OK · 38 unmodified lines
39line __LINE__39line __LINE__
40#define __LINE__ # ## #40#define __LINE__ # ## #
41line __LINE__41line __LINE__
42
43----- 10 ------
44/* preprocessor numbers are (u)intmax_t */
45#if -2147483648 < 0
461 true
47#endif
48#if -0x80000000 < 0
492 true
50#endif
51#if -9223372036854775808U > 0
523 true
53#endif
54#if -0x8000000000000000 > 0 // unsigned by overflow
554 true
56#endif
57#if 1 << 31 > 2 && 1 << 32 > 2 && 1 << 63 < 2 && 1U << 63 > 2
585 true
59#endif
60#if (1<<29) * 11 >= 1<<32 && defined DDD << 63 < 0
616 true
62#endif
tests/pp/21.expect+7−0
@@ -9,3 +9,10 @@ OK · 8 unmodified lines
9----- 5 ------9----- 5 ------
10line 3910line 39
11line ##11line ##
12----- 10 ------
131 true
142 true
153 true
164 true
175 true
186 true

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t