2003-12-26 Guilhem Lavaux <guilhem@kaffe.org>
[official-gcc.git] / gcc / testsuite / g++.dg / abi / empty4.C
blobd20a55cf7fcaa4c72156c251973d43c49d922a8f
1 // { dg-do run }
3 // Copyright (C) 2001 Free Software Foundation, Inc.
4 // Contributed by Nathan Sidwell 31 Jul 2001 <nathan@codesourcery.com>
6 // Bug 3820. We were bit copying empty bases including the
7 // padding. Which clobbers whatever they overlay.
9 struct Empty {};
11 struct Inter : Empty {};
13 long now = 0;
15 struct NonPod
17   long m;
19   NonPod () {m = 0x12345678;}
20   NonPod (long m_) {m = m_;}
21   NonPod &operator= (NonPod const &src) {now = m; m = src.m; return *this;}
22   NonPod (NonPod const &src) {m = src.m;}
25 struct A : Inter
27   A (long c) {m = c;}
28   
29   NonPod m;
32 struct B
34   Inter empty;
35   NonPod m;
37   B (long c) {m = c;}
40 struct C : NonPod, Inter
42   C (long c) : NonPod (c), Inter () {}
45 int main ()
47   A a (0x12131415);
48   
49   long was = a.m.m;
50   
51   a = 0x22232425;
53   if (was != now)
54     return 1;   // we copied the empty base which clobbered a.m.m's
55                 // original value.
56   
57   A b (0x32333435);
58   *(Inter *)&a = *(Inter *)&b;
59   
60   if (a.m.m != 0x22232425)
61     return 2;   // we copied padding, which clobbered a.m.m
63   A b2 (0x32333435);
64   (Inter &)b2 = Inter ();
65   if (b2.m.m != 0x32333435)
66     return 2;   // we copied padding, which clobbered b2.m.m
67   
68   B c (0x12131415);
69   was = c.m.m;
70   c = 0x22232425;
71   if (was != now)
72     return 3;
73   
74   B d (0x32333435);
75   c.empty = d.empty;
77   if (c.m.m != 0x22232425)
78     return 4;
80   C e (0x32333435);
82   if (e.m != 0x32333435)
83     return 2;   // we copied padding
84   
85   return 0;