xref: /netbsd-src/tests/usr.bin/xlint/lint1/init.c (revision 7d62b00eb9ad855ffcd7da46b41e23feb5476fac)
1 /*	$NetBSD: init.c,v 1.14 2023/01/04 06:04:07 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 for 'empty_array_with_initializer' [190] */
16 double empty_array_with_initializer[] = {};
17 double array_with_empty_initializer[3] = {};
18 
19 /*
20  * C99 does not allow empty initializer braces syntactically.
21  */
22 struct {
23 	int member;
24 } empty_struct_initializer = {};
25 
26 
27 typedef 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 const histogram_entry hgr[] = {
39 	"odd", 5,
40 	"even", 5,
41 };
42 
43 
44 /*
45  * Initialization with fewer braces than usual, must still be accepted.
46  */
47 struct {
48 	int x, y;
49 } points[] = {
50 	0, 0, 3, 0, 0, 4, 3, 4
51 };
52 
53 
54 /*
55  * Initialization with fewer braces than usual, must still be accepted.
56  */
57 void do_nothing(void);
58 
59 struct {
60 	void (*action_1) (void);
61 	void (*action_2) (void);
62 } actions[1] = {
63 	do_nothing,
64 	do_nothing,
65 };
66 
67 
68 /* expect+1: error: initialization of incomplete type 'incomplete struct incomplete_struct' [175] */
69 struct incomplete_struct s1 = {
70 	1,
71 /* expect+1: error: 's1' has incomplete type 'incomplete struct incomplete_struct' [31] */
72 };
73 
74 /* expect+1: error: initialization of incomplete type 'incomplete struct incomplete_struct' [175] */
75 struct incomplete_struct s2 = {
76 	.member = 1,
77 /* expect+1: error: 's2' has incomplete type 'incomplete struct incomplete_struct' [31] */
78 };
79 
80 struct incomplete_struct {
81 	int num;
82 };
83 
84 
85 /* expect+1: error: initialization of incomplete type 'incomplete union incomplete_union' [175] */
86 union incomplete_union u1 = {
87 	1,
88 /* expect+1: error: 'u1' has incomplete type 'incomplete union incomplete_union' [31] */
89 };
90 
91 /* expect+1: error: initialization of incomplete type 'incomplete union incomplete_union' [175] */
92 union incomplete_union u2 = {
93 	.member = 1,
94 /* expect+1: error: 'u2' has incomplete type 'incomplete union incomplete_union' [31] */
95 };
96 
97 union incomplete_union {
98 	int num;
99 };
100 
101 
102 /* expect+1: warning: cannot initialize extern declaration 'extern_var' [26] */
103 extern int extern_var = 1;
104 int defined_var = 1;
105 /* expect+1: warning: static variable 'static_var' unused [226] */
106 static int static_var = 1;
107 /* expect+1: error: illegal storage class [8] */
108 register int register_var = 1;
109 /* expect+1: error: cannot initialize typedef 'typedef_var' [25] */
110 typedef int typedef_var = 1;
111 
112 
113 /*
114  * In an array of unknown size that is declared using fewer braces than
115  * recommended, ensure that the array size is updated at the end of the
116  * initializer.
117  */
118 struct {
119 	int x;
120 	int y;
121 } points_of_unknown_size[] = {
122 	3, 4,
123 };
124