1 /* Test C23 enumerations with values not representable in int. */
2 /* { dg-do compile } */
3 /* { dg-options "-std=c23 -pedantic-errors" } */
5 /* Check a type while defining an enum (via a diagnostic for incompatible
6 pointer types if the wrong type was chosen). */
7 #define TYPE_CHECK(cst, type) \
8 cst ## _type_check = sizeof (1 ? (type *) 0 : (typeof (cst) *) 0)
10 /* Test various explicit values not representable in int. */
12 enum e1
{ e1a
= -__LONG_LONG_MAX__
- 1, TYPE_CHECK (e1a
, long long),
13 e1b
= 0, TYPE_CHECK (e1b
, int),
14 e1c
= __LONG_LONG_MAX__
, TYPE_CHECK (e1c
, long long),
15 e1d
= 1, TYPE_CHECK (e1d
, int) };
17 extern typeof (e1a
) e1v
;
18 extern typeof (e1b
) e1v
;
19 extern typeof (e1c
) e1v
;
20 extern typeof (e1d
) e1v
;
21 static_assert (sizeof (enum e1
) >= sizeof (long long));
22 static_assert (e1a
== -__LONG_LONG_MAX__
- 1);
23 static_assert (e1b
== 0);
24 static_assert (e1c
== __LONG_LONG_MAX__
);
25 static_assert (e1d
== 1);
26 static_assert (e1a
< 0);
27 static_assert (e1c
> 0);
29 /* This is a test where values are representable in int. */
30 enum e2
{ e2a
= (long long) -__INT_MAX__
- 1, TYPE_CHECK (e2a
, int),
31 e2b
= (unsigned int) __INT_MAX__
, TYPE_CHECK (e2b
, int),
32 e2c
= 2, TYPE_CHECK (e2c
, int) };
34 extern typeof (e2a
) e2v
;
35 extern typeof (e2b
) e2v
;
36 extern typeof (e2c
) e2v
;
37 static_assert (e2a
== -__INT_MAX__
- 1);
38 static_assert (e2b
== __INT_MAX__
);
39 static_assert (e2c
== 2);
40 static_assert (e2a
< 0);
41 static_assert (e2b
> 0);
43 enum e3
{ e3a
= 0, TYPE_CHECK (e3a
, int),
44 e3b
= (unsigned int) -1, TYPE_CHECK (e3b
, unsigned int) };
46 extern typeof (e3a
) e3v
;
47 extern typeof (e3b
) e3v
;
48 static_assert (e3a
== 0u);
49 static_assert (e3b
== (unsigned int) -1);
50 static_assert (e3b
> 0);
52 /* Test handling of overflow and wraparound (choosing a wider type). */
53 #if __LONG_LONG_MAX__ > __INT_MAX__
54 enum e4
{ e4a
= __INT_MAX__
,
55 e4b
, e4c
, e4d
= ((typeof (e4b
)) -1) < 0,
56 e4e
= (unsigned int) -1,
57 e4f
, e4g
= ((typeof (e4e
)) -1) > 0,
58 TYPE_CHECK (e4a
, int), TYPE_CHECK (e4e
, unsigned int) };
60 extern typeof (e4a
) e4v
;
61 extern typeof (e4b
) e4v
;
62 extern typeof (e4c
) e4v
;
63 extern typeof (e4d
) e4v
;
64 extern typeof (e4e
) e4v
;
65 extern typeof (e4f
) e4v
;
66 extern typeof (e4g
) e4v
;
67 static_assert (e4a
== __INT_MAX__
);
68 static_assert (e4b
== (long long) __INT_MAX__
+ 1);
69 static_assert (e4c
== (long long) __INT_MAX__
+ 2);
70 static_assert (e4f
== (unsigned long long) (unsigned int) -1 + 1);
71 /* Verify the type chosen on overflow of a signed type while parsing was
73 static_assert (e4d
== 1);
74 /* Verify the type chosen on wraparound of an unsigned type while parsing was
76 static_assert (e4g
== 1);
79 /* Likewise, for overflow from long to long long. */
80 #if __LONG_LONG_MAX__ > __LONG_MAX__
81 enum e5
{ e5a
= __LONG_MAX__
,
82 e5b
, e5c
, e5d
= ((typeof (e5b
)) -1) < 0,
83 e5e
= (unsigned long) -1,
84 e5f
, e5g
= ((typeof (e5e
)) -1) > 0,
85 #if __LONG_MAX__ > __INT_MAX__
86 TYPE_CHECK (e5a
, long),
88 TYPE_CHECK (e5a
, int),
90 TYPE_CHECK (e5e
, unsigned long) };
92 extern typeof (e5a
) e5v
;
93 extern typeof (e5b
) e5v
;
94 extern typeof (e5c
) e5v
;
95 extern typeof (e5d
) e5v
;
96 extern typeof (e5e
) e5v
;
97 extern typeof (e5f
) e5v
;
98 extern typeof (e5g
) e5v
;
99 static_assert (e5a
== __LONG_MAX__
);
100 static_assert (e5b
== (long long) __LONG_MAX__
+ 1);
101 static_assert (e5c
== (long long) __LONG_MAX__
+ 2);
102 static_assert (e5f
== (unsigned long long) (unsigned long) -1 + 1);
103 /* Verify the type chosen on overflow of a signed type while parsing was
105 static_assert (e5d
== 1);
106 /* Verify the type chosen on wraparound of an unsigned type while parsing was
108 static_assert (e5g
== 1);