decl_enum.c revision 1.6
11.6Srillig/*	$NetBSD: decl_enum.c,v 1.6 2024/10/29 20:48:31 rillig Exp $	*/
21.1Srillig# 3 "decl_enum.c"
31.1Srillig
41.1Srillig/*
51.1Srillig * Tests for enum declarations.
61.1Srillig */
71.1Srillig
81.5Srillig
91.5Srillig// Initializing an enum from a 64-bit value cuts off the upper bits.
101.5Srillig// TIME_MIN thus gets truncated from 0x8000_0000_0000_0000 to 0.
111.5Srillig// TIME_MAX thus gets truncated from 0x7fff_ffff_ffff_ffff to -1.
121.5Srilligenum {
131.6Srillig	/* expect+1: warning: constant -0x8000000000000000 too large for 'int' [56] */
141.5Srillig	TIME_MIN = (long long)(1ULL << 63),
151.6Srillig	/* expect+1: warning: constant 0x7fffffffffffffff too large for 'int' [56] */
161.5Srillig	TIME_MAX = (long long)~(1ULL << 63),
171.5Srillig};
181.5Srillig
191.5Srillig
201.1Srillig/* cover 'enumerator_list: error' */
211.1Srilligenum {
221.1Srillig	/* expect+1: error: syntax error 'goto' [249] */
231.1Srillig	goto
241.1Srillig};
251.1Srillig
261.1Srillig/* cover 'enum_specifier: enum error' */
271.1Srillig/* expect+1: error: syntax error 'goto' [249] */
281.1Srilligenum goto {
291.1Srillig	A
301.1Srillig};
311.1Srillig/* expect-1: warning: empty declaration [0] */
321.2Srillig
331.2Srillig
341.2Srillig/*
351.2Srillig * Ensure that nested enum declarations get the value of each enum constant
361.2Srillig * right.  The variable containing the "current enum value" does not account
371.2Srillig * for these nested declarations.  Such declarations don't occur in practice
381.2Srillig * though.
391.2Srillig */
401.2Srilligenum outer {
411.2Srillig	o1 = sizeof(
421.2Srillig	    enum inner {
431.2Srillig		    i1 = 10000, i2, i3
441.2Srillig	    }
451.2Srillig	),
461.2Srillig	/*
471.2Srillig	 * The only attribute that GCC 12 allows for enum constants is
481.2Srillig	 * __deprecated__, and there is no way to smuggle an integer constant
491.2Srillig	 * expression into the attribute.  If there were a way, and the
501.2Srillig	 * expression contained an enum declaration, the value of the outer
511.2Srillig	 * enum constant would become the value of the last seen inner enum
521.2Srillig	 * constant.  This is because 'enumval' is a simple scalar variable,
531.2Srillig	 * not a stack.  If it should ever become necessary to account for
541.4Srillig	 * nested enum declarations, a field should be added in decl_level.
551.2Srillig	 */
561.2Srillig	o2 __attribute__((__deprecated__)),
571.2Srillig	o3 = i3
581.2Srillig};
591.2Srillig
601.2Srillig/* expect+1: error: negative array dimension (-10000) [20] */
611.2Srilligtypedef int reveal_i1[-i1];
621.2Srillig/* expect+1: error: negative array dimension (-10001) [20] */
631.2Srilligtypedef int reveal_i2[-i2];
641.2Srillig/* expect+1: error: negative array dimension (-10002) [20] */
651.2Srilligtypedef int reveal_i3[-i3];
661.2Srillig
671.2Srillig/* expect+1: error: negative array dimension (-4) [20] */
681.2Srilligtypedef int reveal_o1[-o1];
691.2Srillig/* expect+1: error: negative array dimension (-5) [20] */
701.2Srilligtypedef int reveal_o2[-o2];
711.2Srillig/* expect+1: error: negative array dimension (-10002) [20] */
721.2Srilligtypedef int reveal_o3[-o3];
731.3Srillig
741.3Srillig/* Since C99, a trailing comma is allowed in an enum declaration. */
751.3Srilligenum trailing_comma {
761.3Srillig	constant,
771.3Srillig};
78