Debian Codegraph @

clang/test/C/drs/dr209.c

raw
1 /* RUN: %clang_cc1 -std=c99 -ffreestanding -triple x86_64-unknown-linux -fsyntax-only -verify -pedantic -Wno-c11-extensions %s
2 RUN: %clang_cc1 -std=c99 -ffreestanding -triple x86_64-unknown-win32 -fms-compatibility -fsyntax-only -verify -pedantic -Wno-c11-extensions %s
3 RUN: %clang_cc1 -std=c11 -ffreestanding -fsyntax-only -verify -pedantic %s
4 RUN: %clang_cc1 -std=c17 -ffreestanding -fsyntax-only -verify -pedantic %s
5 RUN: %clang_cc1 -std=c2x -ffreestanding -fsyntax-only -verify -pedantic %s
6 */
7 // expected-no-diagnostics
8
9 /* WG14 DR209: partial
10 * Problem implementing INTN_C macros
11 */
12 #include <stdint.h>
13
14 #if INT8_C(0) != 0
15 #error "uh oh"
16 #elif INT16_C(0) != 0
17 #error "uh oh"
18 #elif INT32_C(0) != 0
19 #error "uh oh"
20 #elif INT64_C(0) != 0LL
21 #error "uh oh"
22 #elif UINT8_C(0) != 0U
23 #error "uh oh"
24 #elif UINT16_C(0) != 0U
25 #error "uh oh"
26 #elif UINT32_C(0) != 0U
27 #error "uh oh"
28 #elif UINT64_C(0) != 0ULL
29 #error "uh oh"
30 #endif
31
32 void dr209(void) {
33 (void)_Generic(INT8_C(0), __typeof__(+(int_least8_t){0}) : 1);
34 (void)_Generic(INT16_C(0), __typeof__(+(int_least16_t){0}) : 1);
35 (void)_Generic(INT32_C(0), __typeof__(+(int_least32_t){0}) : 1);
36 (void)_Generic(INT64_C(0), __typeof__(+(int_least64_t){0}) : 1);
37 // The type of the expanded value in both of these cases should be 'int',
38 //
39 // C99 7.18.4p3: The type of the expression shall have the same type as would
40 // an expression of the corresponding type converted according to the integer
41 // promotions.
42 //
43 // C99 7.18.4.1p1: The macro UINTN_C(value) shall expand to an integer
44 // constant expression corresponding to the type uint_leastN_t.
45 //
46 // C99 7.18.1.2p2: The typedef name uint_leastN_t designates an unsigned
47 // integer type with a width of at least N, ...
48 //
49 // So the value's type is the same underlying type as uint_leastN_t, which is
50 // unsigned char for uint_least8_t, and unsigned short for uint_least16_t,
51 // but then the value undergoes integer promotions which would convert both
52 // of those types to int.
53 //
54 (void)_Generic(UINT8_C(0), __typeof__(+(uint_least8_t){0}) : 1);
55 (void)_Generic(UINT16_C(0), __typeof__(+(uint_least16_t){0}) : 1);
56 (void)_Generic(UINT32_C(0), __typeof__(+(uint_least32_t){0}) : 1);
57 (void)_Generic(UINT64_C(0), __typeof__(+(uint_least64_t){0}) : 1);
58 }
59