90_struct-init.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. typedef unsigned char u8;
  2. typedef struct {} empty_s;
  3. struct contains_empty {
  4. u8 a;
  5. empty_s empty;
  6. u8 b;
  7. };
  8. struct contains_empty ce = { { (1) }, (empty_s){}, 022, };
  9. /* The following decl of 'q' would demonstrate the TCC bug in init_putv when
  10. handling copying compound literals. (Compound literals
  11. aren't acceptable constant initializers in isoc99, but
  12. we accept them like gcc, except for this case)
  13. //char *q = (char *){ "trara" }; */
  14. struct SS {u8 a[3], b; };
  15. struct SS sinit16[] = { { 1 }, 2 };
  16. struct S
  17. {
  18. u8 a,b;
  19. u8 c[2];
  20. };
  21. struct T
  22. {
  23. u8 s[16];
  24. u8 a;
  25. };
  26. struct U
  27. {
  28. u8 a;
  29. struct S s;
  30. u8 b;
  31. struct T t;
  32. };
  33. struct V
  34. {
  35. struct S s;
  36. struct T t;
  37. u8 a;
  38. };
  39. struct W
  40. {
  41. struct V t;
  42. struct S s[];
  43. };
  44. struct S gs = ((struct S){1, 2, 3, 4});
  45. struct S gs2 = {1, 2, {3, 4}};
  46. struct T gt = {"hello", 42};
  47. struct U gu = {3, 5,6,7,8, 4, "huhu", 43};
  48. struct U gu2 = {3, {5,6,7,8}, 4, {"huhu", 43}};
  49. /* Optional braces around scalar initializers. Accepted, but with
  50. a warning. */
  51. struct U gu3 = { {3}, {5,6,7,8,}, 4, {"huhu", 43}};
  52. /* Many superfluous braces and leaving out one initializer for U.s.c[1] */
  53. struct U gu4 = { 3, {5,6,7,}, 5, { "bla", {44}} };
  54. /* Superfluous braces and useless parens around values */
  55. struct S gs3 = { (1), {(2)}, {(((3))), {4}}};
  56. /* Superfluous braces, and leaving out braces for V.t, plus cast */
  57. struct V gv = {{{3},4,{5,6}}, "haha", (u8)45, 46};
  58. /* Compound literal */
  59. struct V gv2 = {(struct S){7,8,{9,10}}, {"hihi", 47}, 48};
  60. /* Parens around compound literal */
  61. struct V gv3 = {((struct S){7,8,{9,10}}), {"hoho", 49}, 50};
  62. /* Initialization of a flex array member (warns in GCC) */
  63. struct W gw = {{1,2,3,4}, {1,2,3,4,5}};
  64. union UU {
  65. u8 a;
  66. u8 b;
  67. };
  68. struct SU {
  69. union UU u;
  70. u8 c;
  71. };
  72. struct SU gsu = {5,6};
  73. /* Unnamed struct/union members aren't ISO C, but it's a widely accepted
  74. extension. See below for further extensions to that under -fms-extension.*/
  75. union UV {
  76. struct {u8 a,b;};
  77. struct S s;
  78. };
  79. union UV guv = {{6,5}};
  80. union UV guv2 = {{.b = 7, .a = 8}};
  81. union UV guv3 = {.b = 8, .a = 7};
  82. /* Under -fms-extensions also the following is valid:
  83. union UV2 {
  84. struct Anon {u8 a,b;}; // unnamed member, but tagged struct, ...
  85. struct S s;
  86. };
  87. struct Anon gan = { 10, 11 }; // ... which makes it available here.
  88. union UV2 guv4 = {{4,3}}; // and the other inits from above as well
  89. */
  90. struct in6_addr {
  91. union {
  92. u8 u6_addr8[16];
  93. unsigned short u6_addr16[8];
  94. } u;
  95. };
  96. struct flowi6 {
  97. struct in6_addr saddr, daddr;
  98. };
  99. struct pkthdr {
  100. struct in6_addr daddr, saddr;
  101. };
  102. struct pkthdr phdr = { { { 6,5,4,3 } }, { { 9,8,7,6 } } };
  103. struct Wrap {
  104. void *func;
  105. };
  106. int global;
  107. void inc_global (void)
  108. {
  109. global++;
  110. }
  111. struct Wrap global_wrap[] = {
  112. ((struct Wrap) {inc_global}),
  113. inc_global,
  114. };
  115. #include <stdio.h>
  116. void print_ (const char *name, const u8 *p, long size)
  117. {
  118. printf ("%s:", name);
  119. while (size--) {
  120. printf (" %x", *p++);
  121. }
  122. printf ("\n");
  123. }
  124. #define print(x) print_(#x, (u8*)&x, sizeof (x))
  125. #if 1
  126. void foo (struct W *w, struct pkthdr *phdr_)
  127. {
  128. struct S ls = {1, 2, 3, 4};
  129. struct S ls2 = {1, 2, {3, 4}};
  130. struct T lt = {"hello", 42};
  131. struct U lu = {3, 5,6,7,8, 4, "huhu", 43};
  132. struct U lu1 = {3, ls, 4, {"huhu", 43}};
  133. struct U lu2 = {3, (ls), 4, {"huhu", 43}};
  134. const struct S *pls = &ls;
  135. struct S ls21 = *pls;
  136. struct U lu22 = {3, *pls, 4, {"huhu", 43}};
  137. /* Incomplete bracing. */
  138. struct U lu21 = {3, ls, 4, "huhu", 43};
  139. /* Optional braces around scalar initializers. Accepted, but with
  140. a warning. */
  141. struct U lu3 = { 3, {5,6,7,8,}, 4, {"huhu", 43}};
  142. /* Many superfluous braces and leaving out one initializer for U.s.c[1] */
  143. struct U lu4 = { 3, {5,6,7,}, 5, { "bla", 44} };
  144. /* Superfluous braces and useless parens around values */
  145. struct S ls3 = { (1), (2), {(((3))), 4}};
  146. /* Superfluous braces, and leaving out braces for V.t, plus cast */
  147. struct V lv = {{3,4,{5,6}}, "haha", (u8)45, 46};
  148. /* Compound literal */
  149. struct V lv2 = {(struct S)w->t.s, {"hihi", 47}, 48};
  150. /* Parens around compound literal */
  151. struct V lv3 = {((struct S){7,8,{9,10}}), ((const struct W *)w)->t.t, 50};
  152. const struct pkthdr *phdr = phdr_;
  153. struct flowi6 flow = { .daddr = phdr->daddr, .saddr = phdr->saddr };
  154. int elt = 0x42;
  155. /* Range init, overlapping */
  156. struct T lt2 = { { [1 ... 5] = 9, [6 ... 10] = elt, [4 ... 7] = elt+1 }, 1 };
  157. print(ls);
  158. print(ls2);
  159. print(lt);
  160. print(lu);
  161. print(lu1);
  162. print(lu2);
  163. print(ls21);
  164. print(lu21);
  165. print(lu22);
  166. print(lu3);
  167. print(lu4);
  168. print(ls3);
  169. print(lv);
  170. print(lv2);
  171. print(lv3);
  172. print(lt2);
  173. print(flow);
  174. }
  175. #endif
  176. void test_compound_with_relocs (void)
  177. {
  178. struct Wrap local_wrap[] = {
  179. ((struct Wrap) {inc_global}),
  180. inc_global,
  181. };
  182. void (*p)(void);
  183. p = global_wrap[0].func; p();
  184. p = global_wrap[1].func; p();
  185. p = local_wrap[0].func; p();
  186. p = local_wrap[1].func; p();
  187. }
  188. void sys_ni(void) { printf("ni\n"); }
  189. void sys_one(void) { printf("one\n"); }
  190. void sys_two(void) { printf("two\n"); }
  191. void sys_three(void) { printf("three\n"); }
  192. typedef void (*fptr)(void);
  193. const fptr table[3] = {
  194. [0 ... 2] = &sys_ni,
  195. [0] = sys_one,
  196. [1] = sys_two,
  197. [2] = sys_three,
  198. };
  199. void test_multi_relocs(void)
  200. {
  201. int i;
  202. for (i = 0; i < sizeof(table)/sizeof(table[0]); i++)
  203. table[i]();
  204. }
  205. /* Following is from GCC gcc.c-torture/execute/20050613-1.c. */
  206. struct SEA { int i; int j; int k; int l; };
  207. struct SEB { struct SEA a; int r[1]; };
  208. struct SEC { struct SEA a; int r[0]; };
  209. struct SED { struct SEA a; int r[]; };
  210. static void
  211. test_correct_filling (struct SEA *x)
  212. {
  213. static int i;
  214. if (x->i != 0 || x->j != 5 || x->k != 0 || x->l != 0)
  215. printf("sea_fill%d: wrong\n", i);
  216. else
  217. printf("sea_fill%d: okay\n", i);
  218. i++;
  219. }
  220. int
  221. test_zero_init (void)
  222. {
  223. /* The peculiarity here is that only a.j is initialized. That
  224. means that all other members must be zero initialized. TCC
  225. once didn't do that for sub-level designators. */
  226. struct SEB b = { .a.j = 5 };
  227. struct SEC c = { .a.j = 5 };
  228. struct SED d = { .a.j = 5 };
  229. test_correct_filling (&b.a);
  230. test_correct_filling (&c.a);
  231. test_correct_filling (&d.a);
  232. return 0;
  233. }
  234. int main()
  235. {
  236. print(ce);
  237. print(gs);
  238. print(gs2);
  239. print(gt);
  240. print(gu);
  241. print(gu2);
  242. print(gu3);
  243. print(gu4);
  244. print(gs3);
  245. print(gv);
  246. print(gv2);
  247. print(gv3);
  248. print(sinit16);
  249. print(gw);
  250. print(gsu);
  251. print(guv);
  252. print(guv.b);
  253. print(guv2);
  254. print(guv3);
  255. print(phdr);
  256. foo(&gw, &phdr);
  257. //printf("q: %s\n", q);
  258. test_compound_with_relocs();
  259. test_multi_relocs();
  260. test_zero_init();
  261. return 0;
  262. }