Commit 33ad474
authored
[Clang] Add predefined macros for integer constants (llvm#123514)
This adds predefined macros for integer constants to implement section 7.18.4 of ISO/IEC 9899:1999 in `<stdint.h>` in a safe way:
```
__INT8_C(c)
__INT16_C(c)
__INT32_C(c)
__INT64_C(c)
__INTMAX_C(c)
__UINT8_C(c)
__UINT16_C(c)
__UINT32_C(c)
__UINT64_C(c)
__UINTMAX_C(c)
```
Which improves compatibility with GCC and makes it trivial to implement
section 7.18.4 of ISO/IEC 9899:1999.
Clang defines `__INT<N>_C_SUFFIX__`, `__UINT<N>_C_SUFFIX__`,
`__INTAX_C_SUFFIX__` and `__UINTMAX_C_SUFFIX__`, but these macros are
useless for this purpose.
Let's say, for example, that `__INT64_C_SUFFIX__` expands to `L` or
`LL`. If the user defines them as a macros, the compiler will produce
errors if `INT64_C` is implemented in `<stdint.h>` using
`__INT64_C_SUFFIX__`:
**minimal-test.c:**
```cpp
#if defined(__clang__) & !defined(__INT64_C)
# pragma clang diagnostic push
# pragma clang diagnostic ignored "-Wreserved-identifier"
# define __PSTDC_INT_C_(literal, suffix) literal##suffix
# define __PSTDC_INT_C(literal, suffix) __PSTDC_INT_C_(literal, suffix)
# define INT64_C(literal) __PSTDC_INT_C(literal, __INT64_C_SUFFIX__)
# pragma clang diagnostic pop
#elif defined(__GNUC__)
# define INT64_C __INT64_C
#endif
typedef __INT64_TYPE__ int64_t;
#define L "Make Clang produce an error"
#define LL "Make Clang produce an error"
int main(int argc, char **argv)
{
(void)argc; (void)argv;
int64_t v = INT64_C(9223372036854775807);
(void)v;
return 0;
}
```
<img width="697" alt="imagen"
src="https://github.com/user-attachments/assets/6df97af6-7cfd-4cf9-85b7-d7c854509325"
/>
**test.c:**
```cpp
#if defined(__clang__) && !defined(__INT8_C)
# pragma clang diagnostic push
# pragma clang diagnostic ignored "-Wreserved-identifier"
# define __PSTDC_INT_C_(literal, suffix) literal##suffix
# define __PSTDC_INT_C(literal, suffix) __PSTDC_INT_C_(literal, suffix)
# define INT8_C(literal) __PSTDC_INT_C(literal, __INT8_C_SUFFIX__)
# define INT16_C(literal) __PSTDC_INT_C(literal, __INT16_C_SUFFIX__)
# define INT32_C(literal) __PSTDC_INT_C(literal, __INT32_C_SUFFIX__)
# define INT64_C(literal) __PSTDC_INT_C(literal, __INT64_C_SUFFIX__)
# define INTMAX_C(literal) __PSTDC_INT_C(literal, __INTMAX_C_SUFFIX__)
# define UINT8_C(literal) __PSTDC_INT_C(literal, __UINT8_C_SUFFIX__)
# define UINT16_C(literal) __PSTDC_INT_C(literal, __UINT16_C_SUFFIX__)
# define UINT32_C(literal) __PSTDC_INT_C(literal, __UINT32_C_SUFFIX__)
# define UINT64_C(literal) __PSTDC_INT_C(literal, __UINT64_C_SUFFIX__)
# define UINTMAX_C(literal) __PSTDC_INT_C(literal, __UINTMAX_C_SUFFIX__)
# pragma clang diagnostic pop
#else
# define INT8_C __INT8_C
# define INT16_C __INT16_C
# define INT32_C __INT32_C
# define INT64_C __INT64_C
# define INTMAX_C __INTMAX_C
# define UINT8_C __UINT8_C
# define UINT16_C __UINT16_C
# define UINT32_C __UINT32_C
# define UINT64_C __UINT64_C
# define UINTMAX_C __UINTMAX_C
#endif
typedef __INT8_TYPE__ int8_t;
typedef __INT16_TYPE__ int16_t;
typedef __INT32_TYPE__ int32_t;
typedef __INT64_TYPE__ int64_t;
typedef __INTMAX_TYPE__ intmax_t;
typedef __UINT8_TYPE__ uint8_t;
typedef __UINT16_TYPE__ uint16_t;
typedef __UINT32_TYPE__ uint32_t;
typedef __UINT64_TYPE__ uint64_t;
typedef __UINTMAX_TYPE__ uintmax_t;
#define L "Make Clang produce an error"
#define LL "Make Clang produce an error"
#define U "Make Clang produce an error"
#define UL "Make Clang produce an error"
#define ULL "Make Clang produce an error"
int main(int argc, char **argv)
{
(void)argc; (void)argv;
int8_t a = INT8_C (127);
int16_t b = INT16_C (32767);
int32_t c = INT32_C (2147483647);
int64_t d = INT64_C (9223372036854775807);
intmax_t e = INTMAX_C (9223372036854775807);
uint8_t f = UINT8_C (255);
uint16_t g = UINT16_C (65535);
uint32_t h = UINT32_C (4294967295);
uint64_t i = UINT64_C (18446744073709551615);
uintmax_t j = UINTMAX_C(18446744073709551615);
(void)a; (void)b; (void)c; (void)d; (void)e;
(void)f; (void)g; (void)h; (void)i; (void)j;
return 0;
}
```1 parent 0c78485 commit 33ad474
File tree
14 files changed
+498
-4
lines changed- clang
- docs
- lib/Frontend
- test/Preprocessor
14 files changed
+498
-4
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
479 | 479 | | |
480 | 480 | | |
481 | 481 | | |
| 482 | + | |
| 483 | + | |
| 484 | + | |
| 485 | + | |
| 486 | + | |
| 487 | + | |
| 488 | + | |
| 489 | + | |
| 490 | + | |
| 491 | + | |
| 492 | + | |
482 | 493 | | |
483 | 494 | | |
484 | 495 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
253 | 253 | | |
254 | 254 | | |
255 | 255 | | |
| 256 | + | |
| 257 | + | |
256 | 258 | | |
257 | 259 | | |
258 | 260 | | |
| |||
1164 | 1166 | | |
1165 | 1167 | | |
1166 | 1168 | | |
1167 | | - | |
1168 | | - | |
| 1169 | + | |
| 1170 | + | |
| 1171 | + | |
| 1172 | + | |
1169 | 1173 | | |
1170 | 1174 | | |
1171 | | - | |
1172 | | - | |
| 1175 | + | |
| 1176 | + | |
| 1177 | + | |
| 1178 | + | |
1173 | 1179 | | |
1174 | 1180 | | |
1175 | 1181 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
135 | 135 | | |
136 | 136 | | |
137 | 137 | | |
| 138 | + | |
138 | 139 | | |
139 | 140 | | |
140 | 141 | | |
141 | 142 | | |
142 | 143 | | |
| 144 | + | |
143 | 145 | | |
144 | 146 | | |
145 | 147 | | |
146 | 148 | | |
147 | 149 | | |
| 150 | + | |
148 | 151 | | |
149 | 152 | | |
150 | 153 | | |
151 | 154 | | |
152 | 155 | | |
| 156 | + | |
153 | 157 | | |
154 | 158 | | |
155 | 159 | | |
156 | 160 | | |
157 | 161 | | |
| 162 | + | |
158 | 163 | | |
159 | 164 | | |
160 | 165 | | |
| |||
287 | 292 | | |
288 | 293 | | |
289 | 294 | | |
| 295 | + | |
290 | 296 | | |
291 | 297 | | |
292 | 298 | | |
293 | 299 | | |
294 | 300 | | |
295 | 301 | | |
296 | 302 | | |
| 303 | + | |
297 | 304 | | |
298 | 305 | | |
299 | 306 | | |
300 | 307 | | |
301 | 308 | | |
302 | 309 | | |
303 | 310 | | |
| 311 | + | |
304 | 312 | | |
305 | 313 | | |
306 | 314 | | |
307 | 315 | | |
308 | 316 | | |
309 | 317 | | |
310 | 318 | | |
| 319 | + | |
311 | 320 | | |
312 | 321 | | |
313 | 322 | | |
314 | 323 | | |
315 | 324 | | |
316 | 325 | | |
317 | 326 | | |
| 327 | + | |
318 | 328 | | |
319 | 329 | | |
320 | 330 | | |
| |||
435 | 445 | | |
436 | 446 | | |
437 | 447 | | |
| 448 | + | |
438 | 449 | | |
439 | 450 | | |
440 | 451 | | |
441 | 452 | | |
442 | 453 | | |
| 454 | + | |
443 | 455 | | |
444 | 456 | | |
445 | 457 | | |
446 | 458 | | |
447 | 459 | | |
| 460 | + | |
448 | 461 | | |
449 | 462 | | |
450 | 463 | | |
451 | 464 | | |
452 | 465 | | |
| 466 | + | |
453 | 467 | | |
454 | 468 | | |
455 | 469 | | |
456 | 470 | | |
457 | 471 | | |
| 472 | + | |
458 | 473 | | |
459 | 474 | | |
460 | 475 | | |
| |||
538 | 553 | | |
539 | 554 | | |
540 | 555 | | |
| 556 | + | |
541 | 557 | | |
542 | 558 | | |
543 | 559 | | |
| 560 | + | |
544 | 561 | | |
545 | 562 | | |
546 | 563 | | |
| 564 | + | |
547 | 565 | | |
548 | 566 | | |
549 | 567 | | |
| 568 | + | |
550 | 569 | | |
551 | 570 | | |
552 | 571 | | |
| 572 | + | |
553 | 573 | | |
554 | 574 | | |
555 | 575 | | |
| |||
703 | 723 | | |
704 | 724 | | |
705 | 725 | | |
| 726 | + | |
706 | 727 | | |
707 | 728 | | |
708 | 729 | | |
| 730 | + | |
709 | 731 | | |
710 | 732 | | |
711 | 733 | | |
| 734 | + | |
712 | 735 | | |
713 | 736 | | |
714 | 737 | | |
| 738 | + | |
715 | 739 | | |
716 | 740 | | |
717 | 741 | | |
| 742 | + | |
718 | 743 | | |
719 | 744 | | |
720 | 745 | | |
| |||
867 | 892 | | |
868 | 893 | | |
869 | 894 | | |
| 895 | + | |
870 | 896 | | |
871 | 897 | | |
872 | 898 | | |
873 | 899 | | |
874 | 900 | | |
| 901 | + | |
875 | 902 | | |
876 | 903 | | |
877 | 904 | | |
878 | 905 | | |
879 | 906 | | |
| 907 | + | |
880 | 908 | | |
881 | 909 | | |
882 | 910 | | |
883 | 911 | | |
884 | 912 | | |
| 913 | + | |
885 | 914 | | |
886 | 915 | | |
887 | 916 | | |
888 | 917 | | |
889 | 918 | | |
| 919 | + | |
890 | 920 | | |
891 | 921 | | |
892 | 922 | | |
| |||
1013 | 1043 | | |
1014 | 1044 | | |
1015 | 1045 | | |
| 1046 | + | |
1016 | 1047 | | |
1017 | 1048 | | |
1018 | 1049 | | |
1019 | 1050 | | |
1020 | 1051 | | |
1021 | 1052 | | |
1022 | 1053 | | |
| 1054 | + | |
1023 | 1055 | | |
1024 | 1056 | | |
1025 | 1057 | | |
1026 | 1058 | | |
1027 | 1059 | | |
1028 | 1060 | | |
1029 | 1061 | | |
| 1062 | + | |
1030 | 1063 | | |
1031 | 1064 | | |
1032 | 1065 | | |
1033 | 1066 | | |
1034 | 1067 | | |
1035 | 1068 | | |
1036 | 1069 | | |
| 1070 | + | |
1037 | 1071 | | |
1038 | 1072 | | |
1039 | 1073 | | |
1040 | 1074 | | |
1041 | 1075 | | |
1042 | 1076 | | |
1043 | 1077 | | |
| 1078 | + | |
1044 | 1079 | | |
1045 | 1080 | | |
1046 | 1081 | | |
| |||
0 commit comments