designated-initializers.c 8.9 KB

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