1185380Ssam/* $NetBSD: init.c,v 1.16 2024/06/08 13:50:47 rillig Exp $ */ 2185380Ssam# 3 "init.c" 3185380Ssam 4185380Ssam/* 5185380Ssam * Tests for initialization. 6185380Ssam * 7185380Ssam * C99 6.7.8 8185380Ssam */ 9185380Ssam 10185380Ssam/* lint1-extra-flags: -X 351 */ 11185380Ssam 12185380Ssam/* 13185380Ssam * C99 does not allow empty initializer braces syntactically. 14185380Ssam * Lint allows this syntactically, it just complains if the resulting 15185380Ssam * object is empty. 16185380Ssam */ 17185380Ssam/* expect+1: error: empty array declaration for 'empty_array_with_initializer' [190] */ 18185380Ssamdouble empty_array_with_initializer[] = {}; 19185380Ssamdouble array_with_empty_initializer[3] = {}; 20185380Ssam 21185380Ssam/* 22185380Ssam * C99 does not allow empty initializer braces syntactically. 23185380Ssam */ 24185380Ssamstruct { 25185380Ssam int member; 26185380Ssam} empty_struct_initializer = {}; 27185380Ssam 28185380Ssam 29185380Ssamtypedef struct { 30185380Ssam const char *key; 31185380Ssam int n; 32185380Ssam} histogram_entry; 33185380Ssam 34185380Ssam/* 35185380Ssam * The C standards allow omitting braces around the structural levels. For 36185380Ssam * human readers, it is usually clearer to include them. 37185380Ssam * 38185380Ssam * Seen in external/ibm-public/postfix/dist/src/util/dict.c(624). 39185380Ssam */ 40185380Ssamconst histogram_entry hgr[] = { 41185380Ssam "odd", 5, 42185380Ssam "even", 5, 43185380Ssam}; 44185380Ssam 45185380Ssam 46185380Ssam/* 47185380Ssam * Initialization with fewer braces than usual, must still be accepted. 48185380Ssam */ 49185380Ssamstruct { 50185380Ssam int x, y; 51185380Ssam} points[] = { 52185380Ssam 0, 0, 3, 0, 0, 4, 3, 4 53185380Ssam}; 54185380Ssam 55185380Ssam 56185380Ssam/* 57185380Ssam * Initialization with fewer braces than usual, must still be accepted. 58185380Ssam */ 59185380Ssamvoid do_nothing(void); 60185380Ssam 61185380Ssamstruct { 62185380Ssam void (*action_1) (void); 63185380Ssam void (*action_2) (void); 64185380Ssam} actions[1] = { 65185380Ssam do_nothing, 66185380Ssam do_nothing, 67185380Ssam}; 68185380Ssam 69185380Ssam 70185380Ssam/* expect+1: error: initialization of incomplete type 'incomplete struct incomplete_struct' [175] */ 71185380Ssamstruct incomplete_struct s1 = { 72185380Ssam 1, 73185380Ssam/* expect+1: error: 's1' has incomplete type 'incomplete struct incomplete_struct' [31] */ 74185380Ssam}; 75185380Ssam 76185380Ssam/* expect+1: error: initialization of incomplete type 'incomplete struct incomplete_struct' [175] */ 77185380Ssamstruct incomplete_struct s2 = { 78185380Ssam .member = 1, 79185380Ssam/* expect+1: error: 's2' has incomplete type 'incomplete struct incomplete_struct' [31] */ 80185380Ssam}; 81185380Ssam 82185380Ssamstruct incomplete_struct { 83185380Ssam int num; 84185380Ssam}; 85185380Ssam 86185380Ssam 87185380Ssam/* expect+1: error: initialization of incomplete type 'incomplete union incomplete_union' [175] */ 88185380Ssamunion incomplete_union u1 = { 89185380Ssam 1, 90185380Ssam/* expect+1: error: 'u1' has incomplete type 'incomplete union incomplete_union' [31] */ 91185380Ssam}; 92185380Ssam 93185380Ssam/* expect+1: error: initialization of incomplete type 'incomplete union incomplete_union' [175] */ 94185380Ssamunion incomplete_union u2 = { 95185380Ssam .member = 1, 96185380Ssam/* expect+1: error: 'u2' has incomplete type 'incomplete union incomplete_union' [31] */ 97185380Ssam}; 98185380Ssam 99185380Ssamunion incomplete_union { 100185380Ssam int num; 101185380Ssam}; 102185380Ssam 103185380Ssam 104185380Ssam/* expect+1: warning: cannot initialize extern declaration 'extern_var' [26] */ 105185380Ssamextern int extern_var = 1; 106185380Ssamint defined_var = 1; 107185380Ssam/* expect+1: warning: static variable 'static_var' unused [226] */ 108185380Ssamstatic int static_var = 1; 109185380Ssam/* expect+1: error: illegal storage class [8] */ 110185380Ssamregister int register_var = 1; 111185380Ssam/* expect+1: error: cannot initialize typedef 'typedef_var' [25] */ 112185380Ssamtypedef int typedef_var = 1; 113185380Ssam 114185380Ssam 115185380Ssam/* 116185380Ssam * In an array of unknown size that is declared using fewer braces than 117185380Ssam * recommended, ensure that the array size is updated at the end of the 118185380Ssam * initializer. 119185380Ssam */ 120struct { 121 int x; 122 int y; 123} points_of_unknown_size[] = { 124 3, 4, 125}; 126 127void 128init_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 139void 140init_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 166void 167init_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