init.c revision 1.8
11.8Srillig/*	$NetBSD: init.c,v 1.8 2021/12/21 21:16:08 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.8Srilligunion incomplete_union u1 = {
771.8Srillig	/* expect+1: error: too many struct/union initializers [172] */
781.8Srillig	1,
791.8Srillig/* expect+1: error: 'u1' has incomplete type 'incomplete union incomplete_union' [31] */
801.8Srillig};
811.8Srillig
821.8Srilligunion incomplete_union u2 = {
831.8Srillig	/* expect+1: error: type 'incomplete union incomplete_union' does not have member 'member' [101] */
841.8Srillig	.member = 1,
851.8Srillig/* expect+1: error: 'u2' has incomplete type 'incomplete union incomplete_union' [31] */
861.8Srillig};
871.8Srillig
881.8Srilligunion incomplete_union {
891.8Srillig	int num;
901.8Srillig};
91