decl_enum.c revision 1.6
1/*	$NetBSD: decl_enum.c,v 1.6 2024/10/29 20:48:31 rillig Exp $	*/
2# 3 "decl_enum.c"
3
4/*
5 * Tests for enum declarations.
6 */
7
8
9// Initializing an enum from a 64-bit value cuts off the upper bits.
10// TIME_MIN thus gets truncated from 0x8000_0000_0000_0000 to 0.
11// TIME_MAX thus gets truncated from 0x7fff_ffff_ffff_ffff to -1.
12enum {
13	/* expect+1: warning: constant -0x8000000000000000 too large for 'int' [56] */
14	TIME_MIN = (long long)(1ULL << 63),
15	/* expect+1: warning: constant 0x7fffffffffffffff too large for 'int' [56] */
16	TIME_MAX = (long long)~(1ULL << 63),
17};
18
19
20/* cover 'enumerator_list: error' */
21enum {
22	/* expect+1: error: syntax error 'goto' [249] */
23	goto
24};
25
26/* cover 'enum_specifier: enum error' */
27/* expect+1: error: syntax error 'goto' [249] */
28enum goto {
29	A
30};
31/* expect-1: warning: empty declaration [0] */
32
33
34/*
35 * Ensure that nested enum declarations get the value of each enum constant
36 * right.  The variable containing the "current enum value" does not account
37 * for these nested declarations.  Such declarations don't occur in practice
38 * though.
39 */
40enum outer {
41	o1 = sizeof(
42	    enum inner {
43		    i1 = 10000, i2, i3
44	    }
45	),
46	/*
47	 * The only attribute that GCC 12 allows for enum constants is
48	 * __deprecated__, and there is no way to smuggle an integer constant
49	 * expression into the attribute.  If there were a way, and the
50	 * expression contained an enum declaration, the value of the outer
51	 * enum constant would become the value of the last seen inner enum
52	 * constant.  This is because 'enumval' is a simple scalar variable,
53	 * not a stack.  If it should ever become necessary to account for
54	 * nested enum declarations, a field should be added in decl_level.
55	 */
56	o2 __attribute__((__deprecated__)),
57	o3 = i3
58};
59
60/* expect+1: error: negative array dimension (-10000) [20] */
61typedef int reveal_i1[-i1];
62/* expect+1: error: negative array dimension (-10001) [20] */
63typedef int reveal_i2[-i2];
64/* expect+1: error: negative array dimension (-10002) [20] */
65typedef int reveal_i3[-i3];
66
67/* expect+1: error: negative array dimension (-4) [20] */
68typedef int reveal_o1[-o1];
69/* expect+1: error: negative array dimension (-5) [20] */
70typedef int reveal_o2[-o2];
71/* expect+1: error: negative array dimension (-10002) [20] */
72typedef int reveal_o3[-o3];
73
74/* Since C99, a trailing comma is allowed in an enum declaration. */
75enum trailing_comma {
76	constant,
77};
78