destructors.cpp revision 9a70846c5ffd5ff5cce60de49cd7b312146bf502
1// RUN: %clang_cc1 %s -emit-llvm -o - -mconstructor-aliases | FileCheck %s
2
3// CHECK: @_ZN5test01AD1Ev = alias {{.*}} @_ZN5test01AD2Ev
4// CHECK: @_ZN5test11MD2Ev = alias {{.*}} @_ZN5test11AD2Ev
5// CHECK: @_ZN5test11ND2Ev = alias {{.*}} @_ZN5test11AD2Ev
6// CHECK: @_ZN5test11OD2Ev = alias {{.*}} @_ZN5test11AD2Ev
7// CHECK: @_ZN5test11SD2Ev = alias bitcast {{.*}} @_ZN5test11AD2Ev
8
9struct A {
10  int a;
11
12  ~A();
13};
14
15// Base with non-trivial destructor
16struct B : A {
17  ~B();
18};
19
20B::~B() { }
21
22// Field with non-trivial destructor
23struct C {
24  A a;
25
26  ~C();
27};
28
29C::~C() { }
30
31// PR5084
32template<typename T>
33class A1 {
34  ~A1();
35};
36
37template<> A1<char>::~A1();
38
39// PR5529
40namespace PR5529 {
41  struct A {
42    ~A();
43  };
44
45  A::~A() { }
46  struct B : A {
47    virtual ~B();
48  };
49
50  B::~B()  {}
51}
52
53// FIXME: there's a known problem in the codegen here where, if one
54// destructor throws, the remaining destructors aren't run.  Fix it,
55// then make this code check for it.
56namespace test0 {
57  void foo();
58  struct VBase { ~VBase(); };
59  struct Base { ~Base(); };
60  struct Member { ~Member(); };
61
62  struct A : Base {
63    Member M;
64    ~A();
65  };
66
67  // The function-try-block won't suppress -mconstructor-aliases here.
68  A::~A() try { } catch (int i) {}
69
70// complete destructor alias tested above
71
72// CHECK: define void @_ZN5test01AD2Ev
73// CHECK: invoke void @_ZN5test06MemberD1Ev
74// CHECK:   unwind label [[MEM_UNWIND:%[a-zA-Z0-9.]+]]
75// CHECK: invoke void @_ZN5test04BaseD2Ev
76// CHECK:   unwind label [[BASE_UNWIND:%[a-zA-Z0-9.]+]]
77
78  struct B : Base, virtual VBase {
79    Member M;
80    ~B();
81  };
82  B::~B() try { } catch (int i) {}
83  // It will suppress the delegation optimization here, though.
84
85// CHECK: define void @_ZN5test01BD1Ev
86// CHECK: invoke void @_ZN5test06MemberD1Ev
87// CHECK:   unwind label [[MEM_UNWIND:%[a-zA-Z0-9.]+]]
88// CHECK: invoke void @_ZN5test04BaseD2Ev
89// CHECK:   unwind label [[BASE_UNWIND:%[a-zA-Z0-9.]+]]
90// CHECK: invoke void @_ZN5test05VBaseD2Ev
91// CHECK:   unwind label [[VBASE_UNWIND:%[a-zA-Z0-9.]+]]
92
93// CHECK: define void @_ZN5test01BD2Ev
94// CHECK: invoke void @_ZN5test06MemberD1Ev
95// CHECK:   unwind label [[MEM_UNWIND:%[a-zA-Z0-9.]+]]
96// CHECK: invoke void @_ZN5test04BaseD2Ev
97// CHECK:   unwind label [[BASE_UNWIND:%[a-zA-Z0-9.]+]]
98}
99
100// Test base-class aliasing.
101namespace test1 {
102  struct A { ~A(); char ***m; }; // non-trivial destructor
103  struct B { ~B(); }; // non-trivial destructor
104  struct Empty { }; // trivial destructor, empty
105  struct NonEmpty { int x; }; // trivial destructor, non-empty
106
107  // There must be a definition in this translation unit for the alias
108  // optimization to apply.
109  A::~A() { delete m; }
110
111  struct M : A { ~M(); };
112  M::~M() {} // alias tested above
113
114  struct N : A, Empty { ~N(); };
115  N::~N() {} // alias tested above
116
117  struct O : Empty, A { ~O(); };
118  O::~O() {} // alias tested above
119
120  struct P : NonEmpty, A { ~P(); };
121  P::~P() {} // CHECK: define void @_ZN5test11PD2Ev
122
123  struct Q : A, B { ~Q(); };
124  Q::~Q() {} // CHECK: define void @_ZN5test11QD2Ev
125
126  struct R : A { ~R(); };
127  R::~R() { A a; } // CHECK: define void @_ZN5test11RD2Ev
128
129  struct S : A { ~S(); int x; };
130  S::~S() {} // alias tested above
131
132  struct T : A { ~T(); B x; };
133  T::~T() {} // CHECK: define void @_ZN5test11TD2Ev
134
135  // The VTT parameter prevents this.  We could still make this work
136  // for calling conventions that are safe against extra parameters.
137  struct U : A, virtual B { ~U(); };
138  U::~U() {} // CHECK: define void @_ZN5test11UD2Ev
139}
140
141// PR6471
142namespace test2 {
143  struct A { ~A(); char ***m; };
144  struct B : A { ~B(); };
145
146  B::~B() {}
147  // CHECK: define void @_ZN5test21BD2Ev
148  // CHECK: call void @_ZN5test21AD2Ev
149}
150