xref: /netbsd-src/tests/usr.bin/xlint/lint1/init.c (revision 867d70fc718005c0918b8b8b2f9d7f2d52d0a0db)
1 /*	$NetBSD: init.c,v 1.13 2022/06/22 19:23:18 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  * 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  */
41 const 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  */
53 struct {
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  */
66 void do_nothing(void);
67 
68 struct {
69 	void (*action_1) (void);
70 	void (*action_2) (void);
71 } actions[1] = {
72 	do_nothing,
73 	do_nothing,
74 };
75 
76 
77 /* expect+1: error: initialization of incomplete type 'incomplete struct incomplete_struct' [175] */
78 struct incomplete_struct s1 = {
79 	1,
80 /* expect+1: error: 's1' has incomplete type 'incomplete struct incomplete_struct' [31] */
81 };
82 
83 /* expect+1: error: initialization of incomplete type 'incomplete struct incomplete_struct' [175] */
84 struct incomplete_struct s2 = {
85 	.member = 1,
86 /* expect+1: error: 's2' has incomplete type 'incomplete struct incomplete_struct' [31] */
87 };
88 
89 struct incomplete_struct {
90 	int num;
91 };
92 
93 
94 /* expect+1: error: initialization of incomplete type 'incomplete union incomplete_union' [175] */
95 union incomplete_union u1 = {
96 	1,
97 /* expect+1: error: 'u1' has incomplete type 'incomplete union incomplete_union' [31] */
98 };
99 
100 /* expect+1: error: initialization of incomplete type 'incomplete union incomplete_union' [175] */
101 union incomplete_union u2 = {
102 	.member = 1,
103 /* expect+1: error: 'u2' has incomplete type 'incomplete union incomplete_union' [31] */
104 };
105 
106 union incomplete_union {
107 	int num;
108 };
109 
110 
111 /* expect+1: warning: cannot initialize extern declaration 'extern_var' [26] */
112 extern int extern_var = 1;
113 int defined_var = 1;
114 /* expect+1: warning: static variable 'static_var' unused [226] */
115 static int static_var = 1;
116 /* expect+1: error: illegal storage class [8] */
117 register int register_var = 1;
118 /* expect+1: error: cannot initialize typedef 'typedef_var' [25] */
119 typedef int typedef_var = 1;
120 
121 
122 /*
123  * In an array of unknown size that is declared using fewer braces than
124  * recommended, ensure that the array size is updated at the end of the
125  * initializer.
126  */
127 struct {
128 	int x;
129 	int y;
130 } points_of_unknown_size[] = {
131 	3, 4,
132 };
133