init.c revision 1.8
1/*	$NetBSD: init.c,v 1.8 2021/12/21 21:16:08 rillig Exp $	*/
2# 3 "init.c"
3
4/*
5 * Tests for initialization.
6 *
7 * C99 6.7.8
8 */
9
10/*
11 * C99 does not allow empty initializer braces syntactically.
12 * Lint allows this syntactically, it just complains if the resulting
13 * object is empty.
14 */
15/* expect+1: error: empty array declaration: empty_array_with_initializer [190] */
16double empty_array_with_initializer[] = {};
17double array_with_empty_initializer[3] = {};
18
19/*
20 * C99 does not allow empty initializer braces syntactically.
21 */
22struct {
23	int member;
24} empty_struct_initializer = {};
25
26
27typedef struct {
28	const char *key;
29	int n;
30} histogram_entry;
31
32/*
33 * The C standards allow omitting braces around the structural levels.  For
34 * human readers, it is usually clearer to include them.
35 *
36 * Seen in external/ibm-public/postfix/dist/src/util/dict.c(624).
37 *
38 * TODO: Properly handle this situation; as of init.c 1.215 from 2021-12-17,
39 *  the below initialization sets in->in_err but shouldn't.
40 */
41const histogram_entry hgr[] = {
42	"odd", 5,
43	"even", 5,
44};
45
46
47/*
48 * Initialization with fewer braces than usual, must still be accepted.
49 *
50 * TODO: Properly handle this situation; as of init.c 1.215 from 2021-12-17,
51 *  the below initialization sets in->in_err but shouldn't.
52 */
53struct {
54	int x, y;
55} points[] = {
56	0, 0, 3, 0, 0, 4, 3, 4
57};
58
59
60/*
61 * Initialization with fewer braces than usual, must still be accepted.
62 *
63 * TODO: Properly handle this situation; as of init.c 1.215 from 2021-12-17,
64 *  the below initialization sets in->in_err but shouldn't.
65 */
66void do_nothing(void);
67
68struct {
69	void (*action_1) (void);
70	void (*action_2) (void);
71} actions[1] = {
72	do_nothing,
73	do_nothing,
74};
75
76union incomplete_union u1 = {
77	/* expect+1: error: too many struct/union initializers [172] */
78	1,
79/* expect+1: error: 'u1' has incomplete type 'incomplete union incomplete_union' [31] */
80};
81
82union incomplete_union u2 = {
83	/* expect+1: error: type 'incomplete union incomplete_union' does not have member 'member' [101] */
84	.member = 1,
85/* expect+1: error: 'u2' has incomplete type 'incomplete union incomplete_union' [31] */
86};
87
88union incomplete_union {
89	int num;
90};
91