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