1  /* { dg-do run } */
       2  /* { dg-options "-O2" } */
       3  
       4  #include "builtin-object-size-common.h"
       5  
       6  union A
       7  {
       8    int a1;
       9    char a2[3];
      10  };
      11  
      12  union B
      13  {
      14    long long b1;
      15    union A b2;
      16  };
      17  
      18  struct C
      19  {
      20    int c1;
      21    union A c2;
      22  };
      23  
      24  struct D
      25  {
      26    int d1;
      27    union B d2;
      28  };
      29  
      30  union E
      31  {
      32    struct C e1;
      33    char e2[3];
      34  };
      35  
      36  union F
      37  {
      38    int f1;
      39    struct D f2;
      40  };
      41  
      42  struct G
      43  {
      44    union A g1;
      45    char g2;
      46  };
      47  
      48  struct H
      49  {
      50    int h1;
      51    union E h2;
      52  };
      53  
      54  #define T(X, S0, S1) \
      55    if (__builtin_object_size (X, 0) != (S0))	\
      56      FAIL ();					\
      57    if (__builtin_object_size (X, 1) != (S1))	\
      58      FAIL ();					\
      59    if (__builtin_object_size (X, 2) != (S0))	\
      60      FAIL ();					\
      61    if (__builtin_object_size (X, 3) != (S1))	\
      62      FAIL ()
      63  #define TS(X, S0) T(&X, S0, sizeof (X))
      64  #define TA(X, S0, S1) \
      65    T(X, S0, S1); T(&X[0], S0, S1); T(&X[1], (S0) - 1, (S1) - 1)
      66  #define TF(X, S0) TA(X, S0, S0)
      67  
      68  int
      69  main (void)
      70  {
      71    size_t s, o, o2;
      72  
      73    s = sizeof (union A);
      74    o = 0;
      75    union A *a1 = malloc (s);
      76    union A *a2 = malloc (o + 212);
      77    TS (a1->a1, s);
      78    s = o + 212;
      79    TS (a2->a1, s);
      80    free (a2);
      81    free (a1);
      82    s = sizeof (union A);
      83    o = 0;
      84    a1 = malloc (s);
      85    a2 = malloc (o + 212);
      86    TF (a1->a2, s);
      87    s = o + 212;
      88    TF (a2->a2, s);
      89    free (a2);
      90    free (a1);
      91  
      92    s = sizeof (union B);
      93    o = 0;
      94    union B *b1 = malloc (s);
      95    union B *b2 = malloc (o + 212);
      96    TS (b1->b1, s);
      97    s = o + 212;
      98    TS (b2->b1, s);
      99    free (b2);
     100    free (b1);
     101    s = sizeof (union B);
     102    o = 0;
     103    b1 = malloc (s);
     104    b2 = malloc (o + 212);
     105    TS (b1->b2.a1, s);
     106    s = o + 212;
     107    TS (b2->b2.a1, s);
     108    free (b2);
     109    free (b1);
     110    s = sizeof (union B);
     111    o = 0;
     112    b1 = malloc (s);
     113    b2 = malloc (o + 212);
     114    TF (b1->b2.a2, s);
     115    s = o + 212;
     116    TF (b2->b2.a2, s);
     117    free (b2);
     118    free (b1);
     119  
     120    s = sizeof (struct C);
     121    o = __builtin_offsetof (struct C, c2);
     122    struct C *c1 = malloc (s);
     123    struct C *c2 = malloc (o + 212);
     124    TS (c1->c1, s);
     125    s = o + 212;
     126    TS (c2->c1, s);
     127    free (c2);
     128    free (c1);
     129    s = sizeof (struct C);
     130    o = __builtin_offsetof (struct C, c2);
     131    c1 = malloc (s);
     132    c2 = malloc (o + 212);
     133    TS (c1->c2.a1, s - o);
     134    s = o + 212;
     135    TS (c2->c2.a1, s - o);
     136    free (c2);
     137    free (c1);
     138    s = sizeof (struct C);
     139    o = __builtin_offsetof (struct C, c2);
     140    c1 = malloc (s);
     141    c2 = malloc (o + 212);
     142    TF (c1->c2.a2, s - o);
     143    s = o + 212;
     144    TF (c2->c2.a2, s - o);
     145    free (c2);
     146    free (c1);
     147  
     148    s = sizeof (struct D);
     149    o = __builtin_offsetof (struct D, d2);
     150    struct D *d1 = malloc (s);
     151    struct D *d2 = malloc (o + 212);
     152    TS (d1->d1, s);
     153    s = o + 212;
     154    TS (d2->d1, s);
     155    free (d2);
     156    free (d1);
     157    s = sizeof (struct D);
     158    o = __builtin_offsetof (struct D, d2);
     159    d1 = malloc (s);
     160    d2 = malloc (o + 212);
     161    TS (d1->d2.b1, s - o);
     162    s = o + 212;
     163    TS (d2->d2.b1, s - o);
     164    free (d2);
     165    free (d1);
     166    s = sizeof (struct D);
     167    o = __builtin_offsetof (struct D, d2);
     168    d1 = malloc (s);
     169    d2 = malloc (o + 212);
     170    TS (d1->d2.b2.a1, s - o);
     171    s = o + 212;
     172    TS (d2->d2.b2.a1, s - o);
     173    free (d2);
     174    free (d1);
     175    s = sizeof (struct D);
     176    o = __builtin_offsetof (struct D, d2);
     177    d1 = malloc (s);
     178    d2 = malloc (o + 212);
     179    TF (d1->d2.b2.a2, s - o);
     180    s = o + 212;
     181    TF (d2->d2.b2.a2, s - o);
     182    free (d2);
     183    free (d1);
     184  
     185    s = sizeof (union E);
     186    o = __builtin_offsetof (union E, e1.c2);
     187    union E *e1 = malloc (s);
     188    union E *e2 = malloc (o + 212);
     189    TS (e1->e1.c1, s);
     190    s = o + 212;
     191    TS (e2->e1.c1, s);
     192    free (e2);
     193    free (e1);
     194    s = sizeof (union E);
     195    o = __builtin_offsetof (union E, e1.c2);
     196    e1 = malloc (s);
     197    e2 = malloc (o + 212);
     198    TS (e1->e1.c2.a1, s - o);
     199    s = o + 212;
     200    TS (e2->e1.c2.a1, s - o);
     201    free (e2);
     202    free (e1);
     203    s = sizeof (union E);
     204    o = __builtin_offsetof (union E, e1.c2);
     205    e1 = malloc (s);
     206    e2 = malloc (o + 212);
     207    TF (e1->e1.c2.a2, s - o);
     208    s = o + 212;
     209    TF (e2->e1.c2.a2, s - o);
     210    free (e2);
     211    free (e1);
     212    s = sizeof (union E);
     213    o = __builtin_offsetof (union E, e1.c2);
     214    e1 = malloc (s);
     215    e2 = malloc (o + 212);
     216    TF (e1->e2, s);
     217    s = o + 212;
     218    TF (e2->e2, s);
     219    free (e2);
     220    free (e1);
     221  
     222    s = sizeof (union F);
     223    o = __builtin_offsetof (union F, f2.d2);
     224    union F *f1 = malloc (s);
     225    union F *f2 = malloc (o + 212);
     226    TS (f1->f1, s);
     227    s = o + 212;
     228    TS (f2->f1, s);
     229    free (f2);
     230    free (f1);
     231    s = sizeof (union F);
     232    o = __builtin_offsetof (union F, f2.d2);
     233    f1 = malloc (s);
     234    f2 = malloc (o + 212);
     235    TS (f1->f2.d1, s);
     236    s = o + 212;
     237    TS (f2->f2.d1, s);
     238    free (f2);
     239    free (f1);
     240    s = sizeof (union F);
     241    o = __builtin_offsetof (union F, f2.d2);
     242    f1 = malloc (s);
     243    f2 = malloc (o + 212);
     244    TS (f1->f2.d2.b1, s - o);
     245    s = o + 212;
     246    TS (f2->f2.d2.b1, s - o);
     247    free (f2);
     248    free (f1);
     249    s = sizeof (union F);
     250    o = __builtin_offsetof (union F, f2.d2);
     251    f1 = malloc (s);
     252    f2 = malloc (o + 212);
     253    TS (f1->f2.d2.b2.a1, s - o);
     254    s = o + 212;
     255    TS (f2->f2.d2.b2.a1, s - o);
     256    free (f2);
     257    free (f1);
     258    s = sizeof (union F);
     259    o = __builtin_offsetof (union F, f2.d2);
     260    f1 = malloc (s);
     261    f2 = malloc (o + 212);
     262    TF (f1->f2.d2.b2.a2, s - o);
     263    s = o + 212;
     264    TF (f2->f2.d2.b2.a2, s - o);
     265    free (f2);
     266    free (f1);
     267  
     268    s = sizeof (struct G);
     269    o = __builtin_offsetof (struct G, g2);
     270    struct G *g1 = malloc (s);
     271    struct G *g2 = malloc (o + 212);
     272    TS (g1->g1.a1, s);
     273    s = o + 212;
     274    TS (g2->g1.a1, s);
     275    free (g2);
     276    free (g1);
     277    s = sizeof (struct G);
     278    o = __builtin_offsetof (struct G, g2);
     279    g1 = malloc (s);
     280    g2 = malloc (o + 212);
     281    TA (g1->g1.a2, s, sizeof (g1->g1.a2));
     282    s = o + 212;
     283    TA (g2->g1.a2, s, sizeof (g1->g1.a2));
     284    free (g2);
     285    free (g1);
     286    s = sizeof (struct G);
     287    o = __builtin_offsetof (struct G, g2);
     288    g1 = malloc (s);
     289    g2 = malloc (o + 212);
     290    TS (g1->g2, s - o);
     291    s = o + 212;
     292    TS (g2->g2, s - o);
     293    free (g2);
     294    free (g1);
     295  
     296    s = sizeof (struct H);
     297    o = __builtin_offsetof (struct H, h2);
     298    o2 = __builtin_offsetof (struct H, h2.e1.c2);
     299    struct H *h1 = malloc (s);
     300    struct H *h2 = malloc (o2 + 212);
     301    TS (h1->h1, s);
     302    s = o2 + 212;
     303    TS (h2->h1, s);
     304    free (h2);
     305    free (h1);
     306    s = sizeof (struct H);
     307    o = __builtin_offsetof (struct H, h2);
     308    o2 = __builtin_offsetof (struct H, h2.e1.c2);
     309    h1 = malloc (s);
     310    h2 = malloc (o2 + 212);
     311    TS (h1->h2.e1.c1, s - o);
     312    s = o2 + 212;
     313    TS (h2->h2.e1.c1, s - o);
     314    free (h2);
     315    free (h1);
     316    s = sizeof (struct H);
     317    o = __builtin_offsetof (struct H, h2);
     318    o2 = __builtin_offsetof (struct H, h2.e1.c2);
     319    h1 = malloc (s);
     320    h2 = malloc (o2 + 212);
     321    TS (h1->h2.e1.c2.a1, s - o2);
     322    s = o2 + 212;
     323    TS (h2->h2.e1.c2.a1, s - o2);
     324    free (h2);
     325    free (h1);
     326    s = sizeof (struct H);
     327    o = __builtin_offsetof (struct H, h2);
     328    o2 = __builtin_offsetof (struct H, h2.e1.c2);
     329    h1 = malloc (s);
     330    h2 = malloc (o2 + 212);
     331    TA (h1->h2.e1.c2.a2, s - o2, sizeof (h1->h2.e1.c2.a2));
     332    s = o2 + 212;
     333    TA (h2->h2.e1.c2.a2, s - o2, sizeof (h2->h2.e1.c2.a2));
     334    free (h2);
     335    free (h1);
     336    s = sizeof (struct H);
     337    o = __builtin_offsetof (struct H, h2);
     338    o2 = __builtin_offsetof (struct H, h2.e1.c2);
     339    h1 = malloc (s);
     340    h2 = malloc (o2 + 212);
     341    TF (h1->h2.e2, s - o);
     342    s = o2 + 212;
     343    TF (h2->h2.e2, s - o);
     344    free (h2);
     345    free (h1);
     346  
     347    DONE ();
     348  }