xref: /netbsd-src/tests/usr.bin/xlint/lint1/decl_enum.c (revision 39145755aefded12e59fb022380a62340beef446)
1*39145755Srillig /*	$NetBSD: decl_enum.c,v 1.6 2024/10/29 20:48:31 rillig Exp $	*/
2ae95f90eSrillig # 3 "decl_enum.c"
3ae95f90eSrillig 
4ae95f90eSrillig /*
5ae95f90eSrillig  * Tests for enum declarations.
6ae95f90eSrillig  */
7ae95f90eSrillig 
8b636d70fSrillig 
9b636d70fSrillig // Initializing an enum from a 64-bit value cuts off the upper bits.
10b636d70fSrillig // TIME_MIN thus gets truncated from 0x8000_0000_0000_0000 to 0.
11b636d70fSrillig // TIME_MAX thus gets truncated from 0x7fff_ffff_ffff_ffff to -1.
12b636d70fSrillig enum {
13*39145755Srillig 	/* expect+1: warning: constant -0x8000000000000000 too large for 'int' [56] */
14b636d70fSrillig 	TIME_MIN = (long long)(1ULL << 63),
15*39145755Srillig 	/* expect+1: warning: constant 0x7fffffffffffffff too large for 'int' [56] */
16b636d70fSrillig 	TIME_MAX = (long long)~(1ULL << 63),
17b636d70fSrillig };
18b636d70fSrillig 
19b636d70fSrillig 
20ae95f90eSrillig /* cover 'enumerator_list: error' */
21ae95f90eSrillig enum {
22ae95f90eSrillig 	/* expect+1: error: syntax error 'goto' [249] */
23ae95f90eSrillig 	goto
24ae95f90eSrillig };
25ae95f90eSrillig 
26ae95f90eSrillig /* cover 'enum_specifier: enum error' */
27ae95f90eSrillig /* expect+1: error: syntax error 'goto' [249] */
28ae95f90eSrillig enum goto {
29ae95f90eSrillig 	A
30ae95f90eSrillig };
31ae95f90eSrillig /* expect-1: warning: empty declaration [0] */
32321092b8Srillig 
33321092b8Srillig 
34321092b8Srillig /*
35321092b8Srillig  * Ensure that nested enum declarations get the value of each enum constant
36321092b8Srillig  * right.  The variable containing the "current enum value" does not account
37321092b8Srillig  * for these nested declarations.  Such declarations don't occur in practice
38321092b8Srillig  * though.
39321092b8Srillig  */
40321092b8Srillig enum outer {
41321092b8Srillig 	o1 = sizeof(
42321092b8Srillig 	    enum inner {
43321092b8Srillig 		    i1 = 10000, i2, i3
44321092b8Srillig 	    }
45321092b8Srillig 	),
46321092b8Srillig 	/*
47321092b8Srillig 	 * The only attribute that GCC 12 allows for enum constants is
48321092b8Srillig 	 * __deprecated__, and there is no way to smuggle an integer constant
49321092b8Srillig 	 * expression into the attribute.  If there were a way, and the
50321092b8Srillig 	 * expression contained an enum declaration, the value of the outer
51321092b8Srillig 	 * enum constant would become the value of the last seen inner enum
52321092b8Srillig 	 * constant.  This is because 'enumval' is a simple scalar variable,
53321092b8Srillig 	 * not a stack.  If it should ever become necessary to account for
54c7c89cb5Srillig 	 * nested enum declarations, a field should be added in decl_level.
55321092b8Srillig 	 */
56321092b8Srillig 	o2 __attribute__((__deprecated__)),
57321092b8Srillig 	o3 = i3
58321092b8Srillig };
59321092b8Srillig 
60321092b8Srillig /* expect+1: error: negative array dimension (-10000) [20] */
61321092b8Srillig typedef int reveal_i1[-i1];
62321092b8Srillig /* expect+1: error: negative array dimension (-10001) [20] */
63321092b8Srillig typedef int reveal_i2[-i2];
64321092b8Srillig /* expect+1: error: negative array dimension (-10002) [20] */
65321092b8Srillig typedef int reveal_i3[-i3];
66321092b8Srillig 
67321092b8Srillig /* expect+1: error: negative array dimension (-4) [20] */
68321092b8Srillig typedef int reveal_o1[-o1];
69321092b8Srillig /* expect+1: error: negative array dimension (-5) [20] */
70321092b8Srillig typedef int reveal_o2[-o2];
71321092b8Srillig /* expect+1: error: negative array dimension (-10002) [20] */
72321092b8Srillig typedef int reveal_o3[-o3];
73f87e5fc9Srillig 
74f87e5fc9Srillig /* Since C99, a trailing comma is allowed in an enum declaration. */
75f87e5fc9Srillig enum trailing_comma {
76f87e5fc9Srillig 	constant,
77f87e5fc9Srillig };
78