init.c revision 1.15
1/*	$NetBSD: init.c,v 1.15 2023/03/28 14:44:34 rillig Exp $	*/
2# 3 "init.c"
3
4/*
5 * Tests for initialization.
6 *
7 * C99 6.7.8
8 */
9
10/* lint1-extra-flags: -X 351 */
11
12/*
13 * C99 does not allow empty initializer braces syntactically.
14 * Lint allows this syntactically, it just complains if the resulting
15 * object is empty.
16 */
17/* expect+1: error: empty array declaration for 'empty_array_with_initializer' [190] */
18double empty_array_with_initializer[] = {};
19double array_with_empty_initializer[3] = {};
20
21/*
22 * C99 does not allow empty initializer braces syntactically.
23 */
24struct {
25	int member;
26} empty_struct_initializer = {};
27
28
29typedef struct {
30	const char *key;
31	int n;
32} histogram_entry;
33
34/*
35 * The C standards allow omitting braces around the structural levels.  For
36 * human readers, it is usually clearer to include them.
37 *
38 * Seen in external/ibm-public/postfix/dist/src/util/dict.c(624).
39 */
40const histogram_entry hgr[] = {
41	"odd", 5,
42	"even", 5,
43};
44
45
46/*
47 * Initialization with fewer braces than usual, must still be accepted.
48 */
49struct {
50	int x, y;
51} points[] = {
52	0, 0, 3, 0, 0, 4, 3, 4
53};
54
55
56/*
57 * Initialization with fewer braces than usual, must still be accepted.
58 */
59void do_nothing(void);
60
61struct {
62	void (*action_1) (void);
63	void (*action_2) (void);
64} actions[1] = {
65	do_nothing,
66	do_nothing,
67};
68
69
70/* expect+1: error: initialization of incomplete type 'incomplete struct incomplete_struct' [175] */
71struct incomplete_struct s1 = {
72	1,
73/* expect+1: error: 's1' has incomplete type 'incomplete struct incomplete_struct' [31] */
74};
75
76/* expect+1: error: initialization of incomplete type 'incomplete struct incomplete_struct' [175] */
77struct incomplete_struct s2 = {
78	.member = 1,
79/* expect+1: error: 's2' has incomplete type 'incomplete struct incomplete_struct' [31] */
80};
81
82struct incomplete_struct {
83	int num;
84};
85
86
87/* expect+1: error: initialization of incomplete type 'incomplete union incomplete_union' [175] */
88union incomplete_union u1 = {
89	1,
90/* expect+1: error: 'u1' has incomplete type 'incomplete union incomplete_union' [31] */
91};
92
93/* expect+1: error: initialization of incomplete type 'incomplete union incomplete_union' [175] */
94union incomplete_union u2 = {
95	.member = 1,
96/* expect+1: error: 'u2' has incomplete type 'incomplete union incomplete_union' [31] */
97};
98
99union incomplete_union {
100	int num;
101};
102
103
104/* expect+1: warning: cannot initialize extern declaration 'extern_var' [26] */
105extern int extern_var = 1;
106int defined_var = 1;
107/* expect+1: warning: static variable 'static_var' unused [226] */
108static int static_var = 1;
109/* expect+1: error: illegal storage class [8] */
110register int register_var = 1;
111/* expect+1: error: cannot initialize typedef 'typedef_var' [25] */
112typedef int typedef_var = 1;
113
114
115/*
116 * In an array of unknown size that is declared using fewer braces than
117 * recommended, ensure that the array size is updated at the end of the
118 * initializer.
119 */
120struct {
121	int x;
122	int y;
123} points_of_unknown_size[] = {
124	3, 4,
125};
126