init.c revision 1.9
11.9Srillig/*	$NetBSD: init.c,v 1.9 2021/12/21 21:42:21 rillig Exp $	*/
21.1Srillig# 3 "init.c"
31.1Srillig
41.1Srillig/*
51.1Srillig * Tests for initialization.
61.1Srillig *
71.1Srillig * C99 6.7.8
81.1Srillig */
91.1Srillig
101.1Srillig/*
111.1Srillig * C99 does not allow empty initializer braces syntactically.
121.1Srillig * Lint allows this syntactically, it just complains if the resulting
131.1Srillig * object is empty.
141.1Srillig */
151.1Srillig/* expect+1: error: empty array declaration: empty_array_with_initializer [190] */
161.1Srilligdouble empty_array_with_initializer[] = {};
171.1Srilligdouble array_with_empty_initializer[3] = {};
181.1Srillig
191.1Srillig/*
201.1Srillig * C99 does not allow empty initializer braces syntactically.
211.1Srillig */
221.1Srilligstruct {
231.1Srillig	int member;
241.1Srillig} empty_struct_initializer = {};
251.2Srillig
261.2Srillig
271.2Srilligtypedef struct {
281.2Srillig	const char *key;
291.2Srillig	int n;
301.2Srillig} histogram_entry;
311.2Srillig
321.2Srillig/*
331.2Srillig * The C standards allow omitting braces around the structural levels.  For
341.2Srillig * human readers, it is usually clearer to include them.
351.2Srillig *
361.2Srillig * Seen in external/ibm-public/postfix/dist/src/util/dict.c(624).
371.3Srillig *
381.7Srillig * TODO: Properly handle this situation; as of init.c 1.215 from 2021-12-17,
391.3Srillig *  the below initialization sets in->in_err but shouldn't.
401.2Srillig */
411.2Srilligconst histogram_entry hgr[] = {
421.2Srillig	"odd", 5,
431.2Srillig	"even", 5,
441.2Srillig};
451.4Srillig
461.4Srillig
471.5Srillig/*
481.5Srillig * Initialization with fewer braces than usual, must still be accepted.
491.5Srillig *
501.7Srillig * TODO: Properly handle this situation; as of init.c 1.215 from 2021-12-17,
511.5Srillig *  the below initialization sets in->in_err but shouldn't.
521.5Srillig */
531.4Srilligstruct {
541.4Srillig	int x, y;
551.4Srillig} points[] = {
561.4Srillig	0, 0, 3, 0, 0, 4, 3, 4
571.4Srillig};
581.6Srillig
591.6Srillig
601.6Srillig/*
611.6Srillig * Initialization with fewer braces than usual, must still be accepted.
621.6Srillig *
631.7Srillig * TODO: Properly handle this situation; as of init.c 1.215 from 2021-12-17,
641.6Srillig *  the below initialization sets in->in_err but shouldn't.
651.6Srillig */
661.6Srilligvoid do_nothing(void);
671.6Srillig
681.6Srilligstruct {
691.6Srillig	void (*action_1) (void);
701.6Srillig	void (*action_2) (void);
711.6Srillig} actions[1] = {
721.6Srillig	do_nothing,
731.6Srillig	do_nothing,
741.6Srillig};
751.8Srillig
761.9Srillig
771.9Srillig/* expect+1: error: initialization of incomplete type 'incomplete struct incomplete_struct' [175] */
781.9Srilligstruct incomplete_struct s1 = {
791.9Srillig	1,
801.9Srillig/* expect+1: error: 's1' has incomplete type 'incomplete struct incomplete_struct' [31] */
811.9Srillig};
821.9Srillig
831.9Srillig/* expect+1: error: initialization of incomplete type 'incomplete struct incomplete_struct' [175] */
841.9Srilligstruct incomplete_struct s2 = {
851.9Srillig	.member = 1,
861.9Srillig/* expect+1: error: 's2' has incomplete type 'incomplete struct incomplete_struct' [31] */
871.9Srillig};
881.9Srillig
891.9Srilligstruct incomplete_struct {
901.9Srillig	int num;
911.9Srillig};
921.9Srillig
931.9Srillig
941.9Srillig/* expect+1: error: initialization of incomplete type 'incomplete union incomplete_union' [175] */
951.8Srilligunion incomplete_union u1 = {
961.8Srillig	1,
971.8Srillig/* expect+1: error: 'u1' has incomplete type 'incomplete union incomplete_union' [31] */
981.8Srillig};
991.8Srillig
1001.9Srillig/* expect+1: error: initialization of incomplete type 'incomplete union incomplete_union' [175] */
1011.8Srilligunion incomplete_union u2 = {
1021.8Srillig	.member = 1,
1031.8Srillig/* expect+1: error: 'u2' has incomplete type 'incomplete union incomplete_union' [31] */
1041.8Srillig};
1051.8Srillig
1061.8Srilligunion incomplete_union {
1071.8Srillig	int num;
1081.8Srillig};
109