Daniel Dunbar | a572887 | 2009-12-15 20:14:24 +0000 | [diff] [blame] | 1 | // RUN: %clang_cc1 -fsyntax-only -verify -triple x86_64-unknown-unknown %s |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2 | |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 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 | |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 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)}} |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 18 | }; |
| 19 | |
| 20 | int iarray3[10] = { |
Douglas Gregor | eeae8f0 | 2009-03-28 00:41:23 +0000 | [diff] [blame] | 21 | [3] 2, // expected-warning{{use of GNU 'missing =' extension in designator}} |
Douglas Gregor | a9c8780 | 2009-01-29 19:42:23 +0000 | [diff] [blame] | 22 | [5 ... 12] = 2 // expected-error{{array designator index (12) exceeds array bounds (10)}} |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 23 | }; |
| 24 | |
| 25 | struct point { |
| 26 | double x; |
| 27 | double y; |
| 28 | }; |
| 29 | |
| 30 | struct point p1 = { |
| 31 | .y = 1.0, |
Douglas Gregor | eeae8f0 | 2009-03-28 00:41:23 +0000 | [diff] [blame] | 32 | x: 2.0, // expected-warning{{}} |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 33 | .a = 4.0, // expected-error{{field designator 'a' does not refer to any field in type 'struct point'}} |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 34 | }; |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 35 | |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 36 | struct point p2 = { |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 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'}} |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 44 | }; |
| 45 | |
| 46 | struct point array2[10] = { |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 47 | [10].x = 2.0, // expected-error{{array designator index (10) exceeds array bounds (10)}} |
Douglas Gregor | a9c8780 | 2009-01-29 19:42:23 +0000 | [diff] [blame] | 48 | [4 ... 5].y = 2.0, |
| 49 | [4 ... 6] = { .x = 3, .y = 4.0 } |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 50 | }; |
| 51 | |
| 52 | struct point array3[10] = { |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 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 | |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 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 | }; |
Douglas Gregor | f6c717c | 2009-01-23 16:54:12 +0000 | [diff] [blame] | 116 | |
Douglas Gregor | 53d3d8e | 2009-01-23 21:04:18 +0000 | [diff] [blame] | 117 | // PR clang/3378 |
Douglas Gregor | f6c717c | 2009-01-23 16:54:12 +0000 | [diff] [blame] | 118 | int bitwidth[] = { [(long long int)1] = 5, [(short int)2] = 2 }; |
Douglas Gregor | e3fa2de | 2009-01-23 18:58:42 +0000 | [diff] [blame] | 119 | int a[]= { [sizeof(int)] = 0 }; |
Douglas Gregor | a9c8780 | 2009-01-29 19:42:23 +0000 | [diff] [blame] | 120 | int a2[]= { [0 ... sizeof(int)] = 0 }; |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 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{{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 |
| 137 | }; |
| 138 | } |
| 139 | |
Douglas Gregor | 6fbdc6b | 2009-01-29 00:39:20 +0000 | [diff] [blame] | 140 | // FIXME: How do we test that this initializes the long properly? |
Douglas Gregor | 34e7946 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 141 | union { char c; long l; } u1 = { .l = 0xFFFF }; |
Douglas Gregor | 6fbdc6b | 2009-01-29 00:39:20 +0000 | [diff] [blame] | 142 | |
| 143 | extern float global_float; |
| 144 | |
| 145 | struct XX { int a, *b; }; |
| 146 | struct XY { int before; struct XX xx, *xp; float* after; } xy[] = { |
| 147 | 0, 0, &xy[0].xx.a, &xy[0].xx, &global_float, |
Douglas Gregor | 58e22b1 | 2009-01-29 01:10:11 +0000 | [diff] [blame] | 148 | [1].xx = 0, &xy[1].xx.a, &xy[1].xx, &global_float, |
| 149 | 0, // expected-note{{previous initialization is here}} |
| 150 | 0, // expected-note{{previous initialization is here}} |
| 151 | [2].before = 0, // expected-warning{{initializer overrides prior initialization of this subobject}} |
| 152 | 0, // expected-warning{{initializer overrides prior initialization of this subobject}} |
| 153 | &xy[2].xx.a, &xy[2].xx, &global_float |
Douglas Gregor | 6fbdc6b | 2009-01-29 00:39:20 +0000 | [diff] [blame] | 154 | }; |
Douglas Gregor | fdf5569 | 2009-02-09 19:45:19 +0000 | [diff] [blame] | 155 | |
| 156 | // PR3519 |
| 157 | struct foo { |
| 158 | int arr[10]; |
| 159 | }; |
| 160 | |
| 161 | struct foo Y[10] = { |
| 162 | [1] .arr [1] = 2, |
| 163 | [4] .arr [2] = 4 |
| 164 | }; |
| 165 | |
| 166 | struct bar { |
| 167 | struct foo f; |
| 168 | float *arr[10]; |
| 169 | }; |
| 170 | |
| 171 | extern float f; |
| 172 | struct bar saloon = { |
| 173 | .f.arr[3] = 1, |
| 174 | .arr = { &f } |
| 175 | }; |
Douglas Gregor | dfb5e59 | 2009-02-12 19:00:39 +0000 | [diff] [blame] | 176 | |
| 177 | typedef unsigned char u_char; |
| 178 | typedef unsigned short u_short; |
| 179 | |
| 180 | union wibble { |
| 181 | u_char arr1[6]; |
| 182 | u_short arr2[3]; |
| 183 | }; |
| 184 | |
| 185 | const union wibble wobble = { .arr2[0] = 0xffff, |
| 186 | .arr2[1] = 0xffff, |
| 187 | .arr2[2] = 0xffff }; |
| 188 | |
Douglas Gregor | 7c53ca6 | 2009-02-18 22:23:55 +0000 | [diff] [blame] | 189 | const union wibble wobble2 = { .arr2 = {4, 5, 6}, 7 }; // expected-warning{{excess elements in union initializer}} |
Douglas Gregor | ffb4b6e | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 190 | |
| 191 | // PR3778 |
| 192 | struct s { |
| 193 | union { int i; }; |
| 194 | }; |
| 195 | struct s si = { |
| 196 | { .i = 1 } |
| 197 | }; |
| 198 | |
| 199 | double d0; |
| 200 | char c0; |
| 201 | float f0; |
| 202 | int i0; |
| 203 | |
| 204 | struct Enigma { |
| 205 | union { |
| 206 | struct { |
| 207 | struct { |
| 208 | double *double_ptr; |
| 209 | char *string; |
| 210 | }; |
| 211 | float *float_ptr; |
| 212 | }; |
| 213 | int *int_ptr; |
| 214 | }; |
| 215 | char *string2; |
| 216 | }; |
| 217 | |
| 218 | struct Enigma enigma = { |
| 219 | .double_ptr = &d0, &c0, |
| 220 | &f0, // expected-note{{previous}} |
| 221 | &c0, |
| 222 | .float_ptr = &f0 // expected-warning{{overrides}} |
| 223 | }; |
Chris Lattner | 3bf6893 | 2009-04-25 21:59:05 +0000 | [diff] [blame] | 224 | |
| 225 | |
| 226 | /// PR4073 |
| 227 | /// Should use evaluate to fold aggressively and emit a warning if not an ice. |
| 228 | extern int crazy_x; |
| 229 | |
| 230 | int crazy_Y[] = { |
| 231 | [ 0 ? crazy_x : 4] = 1 |
| 232 | }; |
| 233 | |
Douglas Gregor | d6d37de | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 234 | // PR5843 |
| 235 | struct expr { |
| 236 | int nargs; |
| 237 | union { |
| 238 | unsigned long int num; |
| 239 | struct expr *args[3]; |
| 240 | } val; |
| 241 | }; |
Chris Lattner | 3bf6893 | 2009-04-25 21:59:05 +0000 | [diff] [blame] | 242 | |
Douglas Gregor | d6d37de | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 243 | struct expr expr0 = { |
| 244 | .nargs = 2, |
| 245 | .val = { |
| 246 | .args = { |
| 247 | [0] = (struct expr *)0, |
| 248 | [1] = (struct expr *)0 |
| 249 | } |
| 250 | } |
| 251 | }; |
Douglas Gregor | 022d13d | 2010-10-08 20:44:28 +0000 | [diff] [blame] | 252 | |
| 253 | // PR6955 |
| 254 | |
| 255 | struct ds { |
| 256 | struct { |
| 257 | struct { |
| 258 | unsigned int a; |
| 259 | }; |
| 260 | unsigned int b; |
| 261 | struct { |
| 262 | unsigned int c; |
| 263 | }; |
| 264 | }; |
| 265 | }; |
| 266 | |
| 267 | // C1X lookup-based anonymous member init cases |
| 268 | struct ds ds0 = { |
| 269 | { { |
| 270 | .a = 1 // expected-note{{previous initialization is here}} |
| 271 | } }, |
| 272 | .a = 2, // expected-warning{{initializer overrides prior initialization of this subobject}} |
| 273 | .b = 3 |
| 274 | }; |
| 275 | struct ds ds1 = { .c = 0 }; |
| 276 | struct ds ds2 = { { { |
| 277 | .a = 0, |
| 278 | .b = 1 // expected-error{{field designator 'b' does not refer to any field}} |
| 279 | } } }; |