xref: /netbsd-src/tests/usr.bin/xlint/lint1/init.c (revision 360641f995d7499cd0671fd1b81a69e4a9f1f07a)
1 /*	$NetBSD: init.c,v 1.16 2024/06/08 13:50:47 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] */
18 double empty_array_with_initializer[] = {};
19 double array_with_empty_initializer[3] = {};
20 
21 /*
22  * C99 does not allow empty initializer braces syntactically.
23  */
24 struct {
25 	int member;
26 } empty_struct_initializer = {};
27 
28 
29 typedef 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  */
40 const 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  */
49 struct {
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  */
59 void do_nothing(void);
60 
61 struct {
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] */
71 struct 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] */
77 struct incomplete_struct s2 = {
78 	.member = 1,
79 /* expect+1: error: 's2' has incomplete type 'incomplete struct incomplete_struct' [31] */
80 };
81 
82 struct incomplete_struct {
83 	int num;
84 };
85 
86 
87 /* expect+1: error: initialization of incomplete type 'incomplete union incomplete_union' [175] */
88 union 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] */
94 union incomplete_union u2 = {
95 	.member = 1,
96 /* expect+1: error: 'u2' has incomplete type 'incomplete union incomplete_union' [31] */
97 };
98 
99 union incomplete_union {
100 	int num;
101 };
102 
103 
104 /* expect+1: warning: cannot initialize extern declaration 'extern_var' [26] */
105 extern int extern_var = 1;
106 int defined_var = 1;
107 /* expect+1: warning: static variable 'static_var' unused [226] */
108 static int static_var = 1;
109 /* expect+1: error: illegal storage class [8] */
110 register int register_var = 1;
111 /* expect+1: error: cannot initialize typedef 'typedef_var' [25] */
112 typedef 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  */
120 struct {
121 	int x;
122 	int y;
123 } points_of_unknown_size[] = {
124 	3, 4,
125 };
126 
127 void
init_string_via_assignment(void)128 init_string_via_assignment(void)
129 {
130 	const char *cs_match = "";
131 	const int *ws_match = L"";
132 
133 	/* expect+1: warning: illegal combination of 'pointer to const char' and 'pointer to int', op 'init' [124] */
134 	const char *cs_mismatch = L"";
135 	/* expect+1: warning: illegal combination of 'pointer to const int' and 'pointer to char', op 'init' [124] */
136 	const int *ws_mismatch = "";
137 }
138 
139 void
init_pointer_in_struct(void)140 init_pointer_in_struct(void)
141 {
142 	struct cs_ws {
143 		const char *cs;
144 		const int *ws;
145 	};
146 
147 	struct cs_ws type_match = {
148 		"",
149 		L"",
150 	};
151 
152 	struct cs_ws type_mismatch = {
153 		/* expect+1: warning: illegal combination of 'pointer to const char' and 'pointer to int', op 'init' [124] */
154 		L"",
155 		/* expect+1: warning: illegal combination of 'pointer to const int' and 'pointer to char', op 'init' [124] */
156 		"",
157 	};
158 
159 	struct cs_ws extra_braces = {
160 		{ "" },
161 		{ L"" },
162 	};
163 }
164 
165 
166 void
init_array_in_struct(void)167 init_array_in_struct(void)
168 {
169 	struct cs_ws {
170 		const char cs[10];
171 		const int ws[10];
172 	};
173 
174 	struct cs_ws type_match = {
175 		"",
176 		L"",
177 	};
178 
179 	struct cs_ws type_mismatch = {
180 		/* expect+1: warning: illegal combination of integer 'char' and pointer 'pointer to int' [183] */
181 		L"",
182 		/* expect+1: warning: illegal combination of integer 'char' and pointer 'pointer to char' [183] */
183 		"",
184 	};
185 
186 	struct cs_ws no_terminating_null = {
187 		"0123456789",
188 		L"0123456789",
189 	};
190 
191 	struct cs_ws too_many_characters = {
192 		/* expect+1: warning: string literal too long (11) for target array (10) [187] */
193 		"0123456789X",
194 		/* expect+1: warning: string literal too long (11) for target array (10) [187] */
195 		L"0123456789X",
196 	};
197 
198 	struct cs_ws extra_braces = {
199 		{ "" },
200 		{ L"" },
201 	};
202 }
203