1 // RUN: %clang_cc1 -fsyntax-only -verify -triple x86_64-unknown-unknown %s 2 3 int complete_array_from_init[] = { 1, 2, [10] = 5, 1, 2, [5] = 2, 6 }; 4 5 int complete_array_from_init_check[((sizeof(complete_array_from_init) / sizeof(int)) == 13)? 1 : -1]; 6 7 int iarray[10] = { 8 [0] = 1, 9 [1 ... 5] = 2, 10 [ 6 ... 6 ] = 3, 11 [ 8 ... 7 ] = 4, // expected-error{{array designator range [8, 7] is empty}} 12 [10] = 5, 13 [-1] = 6 // expected-error{{array designator value '-1' is negative}} 14 }; 15 16 int iarray2[10] = { 17 [10] = 1, // expected-error{{array designator index (10) exceeds array bounds (10)}} 18 }; 19 20 int iarray3[10] = { 21 [3] 2, // expected-warning{{use of GNU 'missing =' extension in designator}} 22 [5 ... 12] = 2 // expected-error{{array designator index (12) exceeds array bounds (10)}} 23 }; 24 25 struct point { 26 double x; 27 double y; 28 }; 29 30 struct point p1 = { 31 .y = 1.0, 32 x: 2.0, // expected-warning{{}} 33 .a = 4.0, // expected-error{{field designator 'a' does not refer to any field in type 'struct point'}} 34 }; 35 36 struct point p2 = { 37 [1] = 1.0 // expected-error{{array designator cannot initialize non-array type}} 38 }; 39 40 struct point array[10] = { 41 [0].x = 1.0, 42 [1].y = 2.0, 43 [2].z = 3.0, // expected-error{{field designator 'z' does not refer to any field in type 'struct point'}} 44 }; 45 46 struct point array2[10] = { 47 [10].x = 2.0, // expected-error{{array designator index (10) exceeds array bounds (10)}} 48 [4 ... 5].y = 2.0, // expected-note 2 {{previous initialization is here}} 49 [4 ... 6] = { .x = 3, .y = 4.0 } // expected-warning 2 {{subobject initialization overrides initialization of other fields within its enclosing subobject}} 50 }; 51 52 struct point array3[10] = { 53 .x = 5 // expected-error{{field designator cannot initialize a non-struct, non-union type}} 54 }; 55 56 struct rect { 57 struct point top_left; 58 struct point bottom_right; 59 }; 60 61 struct rect window = { .top_left.x = 1.0 }; 62 63 struct rect windows[] = { 64 [2].top_left = { 1.0, 2.0 }, 65 [4].bottom_right = { .y = 1.0 }, 66 { { .y = 7.0, .x = 8.0 }, { .x = 5.0 } }, 67 [3] = { .top_left = { 1.1, 2.2 }, .bottom_right = { .y = 1.1 } } 68 }; 69 70 int windows_size[((sizeof(windows) / sizeof(struct rect)) == 6)? 1 : -1]; 71 72 struct rect windows_bad[3] = { 73 [2].top_left = { { .x = 1.1 } }, // expected-error{{designator in initializer for scalar type}} 74 [1].top_left = { .x = 1.1 } 75 }; 76 77 struct gui { 78 struct rect windows[10]; 79 }; 80 81 struct gui gui[] = { 82 [5].windows[3].top_left.x = { 7.0 } // expected-warning{{braces around scalar initializer}} 83 }; 84 85 struct translator { 86 struct wonky { int * ptr; } wonky ; 87 struct rect window; 88 struct point offset; 89 } tran = { 90 .window = { .top_left = { 1.0, 2.0 } }, 91 { .x = 5.0, .y = 6.0 }, 92 .wonky = { 0 } 93 }; 94 95 int anint; 96 struct {int x,*y;} z[] = {[0].x = 2, &z[0].x}; 97 98 struct outer { struct inner { int x, *y; } in, *inp; } zz[] = { 99 [0].in.x = 2, &zz[0].in.x, &zz[0].in, 100 0, &anint, &zz[1].in, 101 [3].in = { .y = &anint, .x = 17 }, 102 [7].in.y = &anint, &zz[0].in, 103 [4].in.y = &anint, [5].in.x = 12 104 }; 105 106 int zz_sizecheck[sizeof(zz) / sizeof(struct outer) == 8? 1 : -1 ]; 107 108 struct disklabel_ops { 109 struct {} type; 110 int labelsize; 111 }; 112 113 struct disklabel_ops disklabel64_ops = { 114 .labelsize = sizeof(struct disklabel_ops) 115 }; 116 117 // PR clang/3378 118 int bitwidth[] = { [(long long int)1] = 5, [(short int)2] = 2 }; 119 int a[]= { [sizeof(int)] = 0 }; 120 int a2[]= { [0 ... sizeof(int)] = 0 }; 121 122 // Test warnings about initializers overriding previous initializers 123 struct X { 124 int a, b, c; 125 }; 126 127 int counter = 0; 128 int get8() { ++counter; return 8; } 129 130 void test() { 131 struct X xs[] = { 132 [0] = (struct X){1, 2}, // expected-note 2 {{previous initialization is here}} 133 [0].c = 3, // expected-warning{{subobject initialization overrides initialization of other fields within its enclosing subobject}} 134 (struct X) {4, 5, 6}, // expected-note{{previous initialization is here}} 135 [1].b = get8(), // expected-warning{{subobject initialization overrides initialization of other fields within its enclosing subobject}} 136 [0].b = 8 // expected-warning{{subobject initialization overrides initialization of other fields within its enclosing subobject}} 137 }; 138 } 139 140 union { char c; long l; } u1 = { .l = 0xFFFF }; 141 142 extern float global_float; 143 144 struct XX { int a, *b; }; 145 struct XY { int before; struct XX xx, *xp; float* after; } xy[] = { 146 0, 0, &xy[0].xx.a, &xy[0].xx, &global_float, 147 [1].xx = 0, &xy[1].xx.a, &xy[1].xx, &global_float, 148 0, // expected-note{{previous initialization is here}} 149 0, // expected-note{{previous initialization is here}} 150 [2].before = 0, // expected-warning{{initializer overrides prior initialization of this subobject}} 151 0, // expected-warning{{initializer overrides prior initialization of this subobject}} 152 &xy[2].xx.a, &xy[2].xx, &global_float 153 }; 154 155 // PR3519 156 struct foo { 157 int arr[10]; 158 }; 159 160 struct foo Y[10] = { 161 [1] .arr [1] = 2, 162 [4] .arr [2] = 4 163 }; 164 165 struct bar { 166 struct foo f; 167 float *arr[10]; 168 }; 169 170 extern float f; 171 struct bar saloon = { 172 .f.arr[3] = 1, 173 .arr = { &f } 174 }; 175 176 typedef unsigned char u_char; 177 typedef unsigned short u_short; 178 179 union wibble { 180 u_char arr1[6]; 181 u_short arr2[3]; 182 }; 183 184 const union wibble wobble = { .arr2[0] = 0xffff, 185 .arr2[1] = 0xffff, 186 .arr2[2] = 0xffff }; 187 188 const union wibble wobble2 = { .arr2 = {4, 5, 6}, 7 }; // expected-warning{{excess elements in union initializer}} 189 190 // PR3778 191 struct s { 192 union { int i; }; 193 }; 194 struct s si = { 195 { .i = 1 } 196 }; 197 198 double d0; 199 char c0; 200 float f0; 201 int i0; 202 203 struct Enigma { 204 union { 205 struct { 206 struct { 207 double *double_ptr; 208 char *string; 209 }; 210 float *float_ptr; 211 }; 212 int *int_ptr; 213 }; 214 char *string2; 215 }; 216 217 struct Enigma enigma = { 218 .double_ptr = &d0, &c0, 219 &f0, // expected-note{{previous}} 220 &c0, 221 .float_ptr = &f0 // expected-warning{{overrides}} 222 }; 223 224 225 /// PR16644 226 typedef union { 227 struct { 228 int zero; 229 int one; 230 int two; 231 int three; 232 } a; 233 int b[4]; 234 } union_16644_t; 235 236 union_16644_t union_16644_instance_0 = 237 { 238 .b[0] = 0, // expected-note{{previous}} 239 .a.one = 1, // expected-warning{{overrides}} expected-note{{previous}} 240 .b[2] = 2, // expected-warning{{overrides}} expected-note{{previous}} 241 .a.three = 3, // expected-warning{{overrides}} 242 }; 243 244 union_16644_t union_16644_instance_1 = 245 { 246 .a.three = 13, // expected-note{{previous}} 247 .b[2] = 12, // expected-warning{{overrides}} expected-note{{previous}} 248 .a.one = 11, // expected-warning{{overrides}} expected-note{{previous}} 249 .b[0] = 10, // expected-warning{{overrides}} 250 }; 251 252 union_16644_t union_16644_instance_2 = 253 { 254 .a.one = 21, // expected-note{{previous}} 255 .b[1] = 20, // expected-warning{{overrides}} 256 }; 257 258 union_16644_t union_16644_instance_3 = 259 { 260 .b[1] = 30, // expected-note{{previous}} 261 .a = { // expected-warning{{overrides}} 262 .one = 31 263 } 264 }; 265 266 union_16644_t union_16644_instance_4[2] = 267 { 268 [0].a.one = 2, 269 [1].a.zero = 3,// expected-note{{previous}} 270 [0].a.zero = 5, 271 [1].b[1] = 4 // expected-warning{{overrides}} 272 }; 273 274 /// PR4073 275 /// Should use evaluate to fold aggressively and emit a warning if not an ice. 276 extern int crazy_x; 277 278 int crazy_Y[] = { 279 [ 0 ? crazy_x : 4] = 1 280 }; 281 282 // PR5843 283 struct expr { 284 int nargs; 285 union { 286 unsigned long int num; 287 struct expr *args[3]; 288 } val; 289 }; 290 291 struct expr expr0 = { 292 .nargs = 2, 293 .val = { 294 .args = { 295 [0] = (struct expr *)0, 296 [1] = (struct expr *)0 297 } 298 } 299 }; 300 301 // PR6955 302 303 struct ds { 304 struct { 305 struct { 306 unsigned int a; 307 }; 308 unsigned int b; 309 struct { 310 unsigned int c; 311 }; 312 }; 313 }; 314 315 // C1X lookup-based anonymous member init cases 316 struct ds ds0 = { 317 { { 318 .a = 1 // expected-note{{previous initialization is here}} 319 } }, 320 .a = 2, // expected-warning{{initializer overrides prior initialization of this subobject}} 321 .b = 3 322 }; 323 struct ds ds1 = { .c = 0 }; 324 struct ds ds2 = { { { 325 .a = 0, 326 .b = 1 // expected-error{{field designator 'b' does not refer to any field}} 327 } } }; 328 329 // Check initializer override warnings overriding a character in a string 330 struct overwrite_string_struct { 331 char L[6]; 332 int M; 333 } overwrite_string[] = { 334 { { "foo" }, 1 }, // expected-note {{previous initialization is here}} 335 [0].L[2] = 'x' // expected-warning{{subobject initialization overrides initialization of other fields}} 336 }; 337 struct overwrite_string_struct2 { 338 char L[6]; 339 int M; 340 } overwrite_string2[] = { 341 { { "foo" }, 1 }, // expected-note{{previous initialization is here}} 342 [0].L[4] = 'x' // expected-warning{{subobject initialization overrides initialization of other fields}} 343 }; 344 struct overwrite_string_struct 345 overwrite_string3[] = { 346 "foo", 1, // expected-note{{previous initialization is here}} 347 [0].L[4] = 'x' // expected-warning{{subobject initialization overrides initialization of other fields}} 348 }; 349 struct overwrite_string_struct 350 overwrite_string4[] = { 351 { { 'f', 'o', 'o' }, 1 }, 352 [0].L[4] = 'x' // no-warning 353 }; 354