Skip to content

Commit 53f3031

Browse files
authored
[C99] Fix definitions of INTn_C macros (#133916)
C99 introduced macros of the form `INTn_C(v)` which expand to a signed or unsigned integer constant with the specified value `v` and the type `int_leastN_t`. Clang's initial implementation of these macros used token pasting to form the integer constants, but this means that users cannot define a macro named `L`, `U`, `UL`, etc before including `<stdint.h>` (in freestanding mode, where Clang's header is being used) because that could form invalid token pasting results. The new definitions now use the predefined `__INTn_C` macros instead of using token pasting. This matches the behavior of GCC. Fixes #85995
1 parent 5bbcc76 commit 53f3031

File tree

4 files changed

+62
-163
lines changed

4 files changed

+62
-163
lines changed

clang/docs/ReleaseNotes.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -337,6 +337,9 @@ Bug Fixes in This Version
337337
- Fixed a problematic case with recursive deserialization within ``FinishedDeserializing()`` where
338338
``PassInterestingDeclsToConsumer()`` was called before the declarations were safe to be passed. (#GH129982)
339339
- Fixed a modules crash where an explicit Constructor was deserialized. (#GH132794)
340+
- Defining an integer literal suffix (e.g., ``LL``) before including
341+
``<stdint.h>`` in a freestanding build no longer causes invalid token pasting
342+
when using the ``INTn_C`` macros. (#GH85995)
340343

341344
Bug Fixes to Compiler Builtins
342345
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

clang/lib/Headers/stdint.h

Lines changed: 18 additions & 129 deletions
Original file line numberDiff line numberDiff line change
@@ -317,166 +317,55 @@ typedef __UINTMAX_TYPE__ uintmax_t;
317317
* integer width that the target implements, so corresponding macros are
318318
* defined below, too.
319319
*
320-
* These macros are defined using the same successive-shrinking approach as
321-
* the type definitions above. It is likewise important that macros are defined
322-
* in order of decending width.
323-
*
324320
* Note that C++ should not check __STDC_CONSTANT_MACROS here, contrary to the
325321
* claims of the C standard (see C++ 18.3.1p2, [cstdint.syn]).
326322
*/
327323

328-
#define __int_c_join(a, b) a ## b
329-
#define __int_c(v, suffix) __int_c_join(v, suffix)
330-
#define __uint_c(v, suffix) __int_c_join(v##U, suffix)
331-
332-
333-
#ifdef __INT64_TYPE__
334-
# undef __int64_c_suffix
335-
# undef __int32_c_suffix
336-
# undef __int16_c_suffix
337-
# undef __int8_c_suffix
338-
# ifdef __INT64_C_SUFFIX__
339-
# define __int64_c_suffix __INT64_C_SUFFIX__
340-
# define __int32_c_suffix __INT64_C_SUFFIX__
341-
# define __int16_c_suffix __INT64_C_SUFFIX__
342-
# define __int8_c_suffix __INT64_C_SUFFIX__
343-
# endif /* __INT64_C_SUFFIX__ */
344-
#endif /* __INT64_TYPE__ */
345-
346324
#ifdef __int_least64_t
347-
# ifdef __int64_c_suffix
348-
# define INT64_C(v) __int_c(v, __int64_c_suffix)
349-
# define UINT64_C(v) __uint_c(v, __int64_c_suffix)
350-
# else
351-
# define INT64_C(v) v
352-
# define UINT64_C(v) v ## U
353-
# endif /* __int64_c_suffix */
325+
#define INT64_C(v) __INT64_C(v)
326+
#define UINT64_C(v) __UINT64_C(v)
354327
#endif /* __int_least64_t */
355328

356329

357330
#ifdef __INT56_TYPE__
358-
# undef __int32_c_suffix
359-
# undef __int16_c_suffix
360-
# undef __int8_c_suffix
361-
# ifdef __INT56_C_SUFFIX__
362-
# define INT56_C(v) __int_c(v, __INT56_C_SUFFIX__)
363-
# define UINT56_C(v) __uint_c(v, __INT56_C_SUFFIX__)
364-
# define __int32_c_suffix __INT56_C_SUFFIX__
365-
# define __int16_c_suffix __INT56_C_SUFFIX__
366-
# define __int8_c_suffix __INT56_C_SUFFIX__
367-
# else
368-
# define INT56_C(v) v
369-
# define UINT56_C(v) v ## U
370-
# endif /* __INT56_C_SUFFIX__ */
331+
#define INT56_C(v) __INT56_C(v)
332+
#define UINT56_C(v) __UINT56_C(v)
371333
#endif /* __INT56_TYPE__ */
372334

373335

374336
#ifdef __INT48_TYPE__
375-
# undef __int32_c_suffix
376-
# undef __int16_c_suffix
377-
# undef __int8_c_suffix
378-
# ifdef __INT48_C_SUFFIX__
379-
# define INT48_C(v) __int_c(v, __INT48_C_SUFFIX__)
380-
# define UINT48_C(v) __uint_c(v, __INT48_C_SUFFIX__)
381-
# define __int32_c_suffix __INT48_C_SUFFIX__
382-
# define __int16_c_suffix __INT48_C_SUFFIX__
383-
# define __int8_c_suffix __INT48_C_SUFFIX__
384-
# else
385-
# define INT48_C(v) v
386-
# define UINT48_C(v) v ## U
387-
# endif /* __INT48_C_SUFFIX__ */
337+
#define INT48_C(v) __INT48_C(v)
338+
#define UINT48_C(v) __UINT48_C(v)
388339
#endif /* __INT48_TYPE__ */
389340

390341

391342
#ifdef __INT40_TYPE__
392-
# undef __int32_c_suffix
393-
# undef __int16_c_suffix
394-
# undef __int8_c_suffix
395-
# ifdef __INT40_C_SUFFIX__
396-
# define INT40_C(v) __int_c(v, __INT40_C_SUFFIX__)
397-
# define UINT40_C(v) __uint_c(v, __INT40_C_SUFFIX__)
398-
# define __int32_c_suffix __INT40_C_SUFFIX__
399-
# define __int16_c_suffix __INT40_C_SUFFIX__
400-
# define __int8_c_suffix __INT40_C_SUFFIX__
401-
# else
402-
# define INT40_C(v) v
403-
# define UINT40_C(v) v ## U
404-
# endif /* __INT40_C_SUFFIX__ */
343+
#define INT40_C(v) __INT40_C(v)
344+
#define UINT40_C(v) __UINT40_C(v)
405345
#endif /* __INT40_TYPE__ */
406346

407347

408-
#ifdef __INT32_TYPE__
409-
# undef __int32_c_suffix
410-
# undef __int16_c_suffix
411-
# undef __int8_c_suffix
412-
# ifdef __INT32_C_SUFFIX__
413-
# define __int32_c_suffix __INT32_C_SUFFIX__
414-
# define __int16_c_suffix __INT32_C_SUFFIX__
415-
# define __int8_c_suffix __INT32_C_SUFFIX__
416-
# endif /* __INT32_C_SUFFIX__ */
417-
#endif /* __INT32_TYPE__ */
418-
419348
#ifdef __int_least32_t
420-
# ifdef __int32_c_suffix
421-
# define INT32_C(v) __int_c(v, __int32_c_suffix)
422-
# define UINT32_C(v) __uint_c(v, __int32_c_suffix)
423-
# else
424-
# define INT32_C(v) v
425-
# define UINT32_C(v) v ## U
426-
# endif /* __int32_c_suffix */
349+
#define INT32_C(v) __INT32_C(v)
350+
#define UINT32_C(v) __UINT32_C(v)
427351
#endif /* __int_least32_t */
428352

429353

430354
#ifdef __INT24_TYPE__
431-
# undef __int16_c_suffix
432-
# undef __int8_c_suffix
433-
# ifdef __INT24_C_SUFFIX__
434-
# define INT24_C(v) __int_c(v, __INT24_C_SUFFIX__)
435-
# define UINT24_C(v) __uint_c(v, __INT24_C_SUFFIX__)
436-
# define __int16_c_suffix __INT24_C_SUFFIX__
437-
# define __int8_c_suffix __INT24_C_SUFFIX__
438-
# else
439-
# define INT24_C(v) v
440-
# define UINT24_C(v) v ## U
441-
# endif /* __INT24_C_SUFFIX__ */
355+
#define INT24_C(v) __INT24_C(v)
356+
#define UINT24_C(v) __UINT24_C(v)
442357
#endif /* __INT24_TYPE__ */
443358

444359

445-
#ifdef __INT16_TYPE__
446-
# undef __int16_c_suffix
447-
# undef __int8_c_suffix
448-
# ifdef __INT16_C_SUFFIX__
449-
# define __int16_c_suffix __INT16_C_SUFFIX__
450-
# define __int8_c_suffix __INT16_C_SUFFIX__
451-
# endif /* __INT16_C_SUFFIX__ */
452-
#endif /* __INT16_TYPE__ */
453-
454360
#ifdef __int_least16_t
455-
# ifdef __int16_c_suffix
456-
# define INT16_C(v) __int_c(v, __int16_c_suffix)
457-
# define UINT16_C(v) __uint_c(v, __int16_c_suffix)
458-
# else
459-
# define INT16_C(v) v
460-
# define UINT16_C(v) v ## U
461-
# endif /* __int16_c_suffix */
361+
#define INT16_C(v) __INT16_C(v)
362+
#define UINT16_C(v) __UINT16_C(v)
462363
#endif /* __int_least16_t */
463364

464365

465-
#ifdef __INT8_TYPE__
466-
# undef __int8_c_suffix
467-
# ifdef __INT8_C_SUFFIX__
468-
# define __int8_c_suffix __INT8_C_SUFFIX__
469-
# endif /* __INT8_C_SUFFIX__ */
470-
#endif /* __INT8_TYPE__ */
471-
472366
#ifdef __int_least8_t
473-
# ifdef __int8_c_suffix
474-
# define INT8_C(v) __int_c(v, __int8_c_suffix)
475-
# define UINT8_C(v) __uint_c(v, __int8_c_suffix)
476-
# else
477-
# define INT8_C(v) v
478-
# define UINT8_C(v) v ## U
479-
# endif /* __int8_c_suffix */
367+
#define INT8_C(v) __INT8_C(v)
368+
#define UINT8_C(v) __UINT8_C(v)
480369
#endif /* __int_least8_t */
481370

482371

@@ -938,8 +827,8 @@ typedef __UINTMAX_TYPE__ uintmax_t;
938827
#endif
939828

940829
/* 7.18.4.2 Macros for greatest-width integer constants. */
941-
#define INTMAX_C(v) __int_c(v, __INTMAX_C_SUFFIX__)
942-
#define UINTMAX_C(v) __int_c(v, __UINTMAX_C_SUFFIX__)
830+
#define INTMAX_C(v) __INTMAX_C(v)
831+
#define UINTMAX_C(v) __UINTMAX_C(v)
943832

944833
/* C23 7.22.3.x Width of other integer types. */
945834
#if defined(__STDC_VERSION__) && __STDC_VERSION__ >= 202311L

clang/test/C/drs/dr209.c

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
RUN: %clang_cc1 -std=c17 -ffreestanding -fsyntax-only -verify -pedantic %s
55
RUN: %clang_cc1 -std=c2x -ffreestanding -fsyntax-only -verify -pedantic %s
66
*/
7+
// expected-no-diagnostics
78

89
/* WG14 DR209: partial
910
* Problem implementing INTN_C macros
@@ -33,8 +34,7 @@ void dr209(void) {
3334
(void)_Generic(INT16_C(0), __typeof__(+(int_least16_t){0}) : 1);
3435
(void)_Generic(INT32_C(0), __typeof__(+(int_least32_t){0}) : 1);
3536
(void)_Generic(INT64_C(0), __typeof__(+(int_least64_t){0}) : 1);
36-
// FIXME: This is not the expected behavior; the type of the expanded value
37-
// in both of these cases should be 'int',
37+
// The type of the expanded value in both of these cases should be 'int',
3838
//
3939
// C99 7.18.4p3: The type of the expression shall have the same type as would
4040
// an expression of the corresponding type converted according to the integer
@@ -53,8 +53,6 @@ void dr209(void) {
5353
//
5454
(void)_Generic(UINT8_C(0), __typeof__(+(uint_least8_t){0}) : 1);
5555
(void)_Generic(UINT16_C(0), __typeof__(+(uint_least16_t){0}) : 1);
56-
// expected-error@-2 {{controlling expression type 'unsigned int' not compatible with any generic association type}}
57-
// expected-error@-2 {{controlling expression type 'unsigned int' not compatible with any generic association type}}
5856
(void)_Generic(UINT32_C(0), __typeof__(+(uint_least32_t){0}) : 1);
5957
(void)_Generic(UINT64_C(0), __typeof__(+(uint_least64_t){0}) : 1);
6058
}

0 commit comments

Comments
 (0)