blob: 53da306c4c320dec3b90d6d550e8d48a83368313 [file] [log] [blame]
Douglas Gregorea0528d2009-01-23 22:22:29 +00001// RUN: clang -fsyntax-only -verify -arch x86_64 %s
Douglas Gregorc5a6bdc2009-01-22 00:58:24 +00002
Douglas Gregor710f6d42009-01-22 23:26:18 +00003int complete_array_from_init[] = { 1, 2, [10] = 5, 1, 2, [5] = 2, 6 };
4
5int complete_array_from_init_check[((sizeof(complete_array_from_init) / sizeof(int)) == 13)? 1 : -1];
6
Douglas Gregorc5a6bdc2009-01-22 00:58:24 +00007int 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
16int iarray2[10] = {
17 [10] = 1, // expected-error{{array designator index (10) exceeds array bounds (10)}}
Douglas Gregor710f6d42009-01-22 23:26:18 +000018};
19
20int iarray3[10] = {
Douglas Gregor9fddded2009-01-29 19:42:23 +000021 [5 ... 12] = 2 // expected-error{{array designator index (12) exceeds array bounds (10)}}
Douglas Gregorc5a6bdc2009-01-22 00:58:24 +000022};
23
24struct point {
25 double x;
26 double y;
27};
28
29struct point p1 = {
30 .y = 1.0,
31 x: 2.0,
32 .a = 4.0, // expected-error{{field designator 'a' does not refer to any field in type 'struct point'}}
Douglas Gregor710f6d42009-01-22 23:26:18 +000033};
Douglas Gregorc5a6bdc2009-01-22 00:58:24 +000034
Douglas Gregor710f6d42009-01-22 23:26:18 +000035struct point p2 = {
Douglas Gregorc5a6bdc2009-01-22 00:58:24 +000036 [1] = 1.0 // expected-error{{array designator cannot initialize non-array type}}
37};
38
39struct point array[10] = {
40 [0].x = 1.0,
41 [1].y = 2.0,
42 [2].z = 3.0, // expected-error{{field designator 'z' does not refer to any field in type 'struct point'}}
Douglas Gregor710f6d42009-01-22 23:26:18 +000043};
44
45struct point array2[10] = {
Douglas Gregorc5a6bdc2009-01-22 00:58:24 +000046 [10].x = 2.0, // expected-error{{array designator index (10) exceeds array bounds (10)}}
Douglas Gregor9fddded2009-01-29 19:42:23 +000047 [4 ... 5].y = 2.0,
48 [4 ... 6] = { .x = 3, .y = 4.0 }
Douglas Gregor710f6d42009-01-22 23:26:18 +000049};
50
51struct point array3[10] = {
Douglas Gregorc5a6bdc2009-01-22 00:58:24 +000052 .x = 5 // expected-error{{field designator cannot initialize a non-struct, non-union type}}
53};
54
55struct rect {
56 struct point top_left;
57 struct point bottom_right;
58};
59
60struct rect window = { .top_left.x = 1.0 };
61
62struct rect windows[] = {
63 [2].top_left = { 1.0, 2.0 },
64 [4].bottom_right = { .y = 1.0 },
65 { { .y = 7.0, .x = 8.0 }, { .x = 5.0 } },
66 [3] = { .top_left = { 1.1, 2.2 }, .bottom_right = { .y = 1.1 } }
67};
68
69int windows_size[((sizeof(windows) / sizeof(struct rect)) == 6)? 1 : -1];
70
71struct rect windows_bad[3] = {
72 [2].top_left = { { .x = 1.1 } }, // expected-error{{designator in initializer for scalar type}}
73 [1].top_left = { .x = 1.1 }
74};
75
76struct gui {
77 struct rect windows[10];
78};
79
80struct gui gui[] = {
81 [5].windows[3].top_left.x = { 7.0 } // expected-warning{{braces around scalar initializer}}
82};
83
84struct translator {
85 struct wonky { int * ptr; } wonky ;
86 struct rect window;
87 struct point offset;
88} tran = {
89 .window = { .top_left = { 1.0, 2.0 } },
90 { .x = 5.0, .y = 6.0 },
91 .wonky = { 0 }
92};
93
Douglas Gregor710f6d42009-01-22 23:26:18 +000094int anint;
95struct {int x,*y;} z[] = {[0].x = 2, &z[0].x};
96
97struct outer { struct inner { int x, *y; } in, *inp; } zz[] = {
98 [0].in.x = 2, &zz[0].in.x, &zz[0].in,
99 0, &anint, &zz[1].in,
100 [3].in = { .y = &anint, .x = 17 },
101 [7].in.y = &anint, &zz[0].in,
102 [4].in.y = &anint, [5].in.x = 12
103};
104
105int zz_sizecheck[sizeof(zz) / sizeof(struct outer) == 8? 1 : -1 ];
106
107struct disklabel_ops {
108 struct {} type;
109 int labelsize;
110};
111
112struct disklabel_ops disklabel64_ops = {
113 .labelsize = sizeof(struct disklabel_ops)
114};
Douglas Gregor5a203a62009-01-23 16:54:12 +0000115
Douglas Gregore498e372009-01-23 21:04:18 +0000116// PR clang/3378
Douglas Gregor5a203a62009-01-23 16:54:12 +0000117int bitwidth[] = { [(long long int)1] = 5, [(short int)2] = 2 };
Douglas Gregor69722702009-01-23 18:58:42 +0000118int a[]= { [sizeof(int)] = 0 };
Douglas Gregor9fddded2009-01-29 19:42:23 +0000119int a2[]= { [0 ... sizeof(int)] = 0 };
Douglas Gregorf603b472009-01-28 21:54:33 +0000120
121// Test warnings about initializers overriding previous initializers
122struct X {
123 int a, b, c;
124};
125
126int counter = 0;
127int get8() { ++counter; return 8; }
128
129void test() {
130 struct X xs[] = {
131 [0] = (struct X){1, 2}, // expected-note{{previous initialization is here}}
132 [0].c = 3, // expected-warning{{subobject initialization overrides initialization of other fields within its enclosing subobject}}
133 (struct X) {4, 5, 6}, // expected-note{{previous initialization is here}}
134 [1].b = get8(), // expected-warning{{subobject initialization overrides initialization of other fields within its enclosing subobject}}
135 [0].b = 8
136 };
137}
138
Douglas Gregor36859eb2009-01-29 00:39:20 +0000139// FIXME: How do we test that this initializes the long properly?
Douglas Gregor36dd0c52009-01-28 23:36:17 +0000140union { char c; long l; } u1 = { .l = 0xFFFF };
Douglas Gregor36859eb2009-01-29 00:39:20 +0000141
142extern float global_float;
143
144struct XX { int a, *b; };
145struct XY { int before; struct XX xx, *xp; float* after; } xy[] = {
146 0, 0, &xy[0].xx.a, &xy[0].xx, &global_float,
Douglas Gregorf36b4932009-01-29 01:10:11 +0000147 [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
Douglas Gregor36859eb2009-01-29 00:39:20 +0000153};