2018-11-07 Richard Biener <rguenther@suse.de>
[official-gcc.git] / gcc / testsuite / g++.dg / torture / pr40389.C
blobe3ceb1238b6b5c8f81918580963ed11f7390603f
1 /* { dg-do run } */
3 template <typename V> struct S
5   V *f, *l;
6   __attribute__ ((noinline)) S (void) { f = 0, l = 0; }
7   void foo (V *x)
8   {
9     if (x->p != 0)
10       x->p->n = x->n;
11     else
12       f = x->n;
13     if (x->n != 0)
14       x->n->p = x->p;
15     else
16       l = x->p;
17   }
18   __attribute__ ((noinline)) void bar (V *x)
19   {
20     x->n = 0;
21     x->p = l;
22     if (l != 0)
23       l->n = x;
24     else
25       f = x;
26     l = x;
27   }
30 struct H;
32 struct A
34   S <H> k;
37 struct H
39   A *a;
40   H *p, *n;
41   __attribute__ ((noinline)) H (void) { p = 0, n = 0, a = 0; }
42   __attribute__ ((noinline)) H (A *b) : a (b)
43   {
44     p = 0;
45     n = 0;
46     if (a != 0)
47       a->k.bar (this);
48   }
49   __attribute__ ((noinline)) H (const H &h) : a (h.a)
50   {
51     p = 0;
52     n = 0;
53     if (a != 0)
54       a->k.bar (this);
55   }
56   ~H (void) { if (a != 0) a->k.foo (this); }
57   H &operator= (const H &o)
58   {
59     if (a != 0 || &o == this)
60       __builtin_abort ();
61     a = o.a;
62     if (a != 0)
63       a->k.bar (this);
64     return *this;
65   }
68 __attribute__ ((noinline))
69 H baz (void)
71   return H (new A);
74 H g;
76 int
77 main (void)
79   g = baz ();
80   if (g.a->k.f != &g)
81     __builtin_abort ();
82   return 0;