John McCall | 1e7fe75 | 2010-09-02 09:58:18 +0000 | [diff] [blame] | 1 | // RUN: %clang_cc1 -triple x86_64-apple-darwin10 %s -emit-llvm -o - | FileCheck %s |
Anders Carlsson | 60e282c | 2009-08-16 21:13:42 +0000 | [diff] [blame] | 2 | |
| 3 | void t1(int *a) { |
| 4 | delete a; |
| 5 | } |
| 6 | |
| 7 | struct S { |
| 8 | int a; |
| 9 | }; |
| 10 | |
| 11 | // POD types. |
| 12 | void t3(S *s) { |
| 13 | delete s; |
| 14 | } |
| 15 | |
| 16 | // Non-POD |
| 17 | struct T { |
| 18 | ~T(); |
| 19 | int a; |
| 20 | }; |
| 21 | |
John McCall | 9c82afc | 2010-04-20 02:18:25 +0000 | [diff] [blame] | 22 | // CHECK: define void @_Z2t4P1T |
Anders Carlsson | 60e282c | 2009-08-16 21:13:42 +0000 | [diff] [blame] | 23 | void t4(T *t) { |
John McCall | 9c82afc | 2010-04-20 02:18:25 +0000 | [diff] [blame] | 24 | // CHECK: call void @_ZN1TD1Ev |
| 25 | // CHECK-NEXT: bitcast |
| 26 | // CHECK-NEXT: call void @_ZdlPv |
Anders Carlsson | 60e282c | 2009-08-16 21:13:42 +0000 | [diff] [blame] | 27 | delete t; |
| 28 | } |
Douglas Gregor | d69dd78 | 2009-10-01 05:49:51 +0000 | [diff] [blame] | 29 | |
| 30 | // PR5102 |
| 31 | template <typename T> |
| 32 | class A { |
John McCall | ca82a82 | 2011-09-21 08:36:56 +0000 | [diff] [blame] | 33 | public: operator T *() const; |
Douglas Gregor | d69dd78 | 2009-10-01 05:49:51 +0000 | [diff] [blame] | 34 | }; |
| 35 | |
| 36 | void f() { |
| 37 | A<char*> a; |
| 38 | |
| 39 | delete a; |
| 40 | } |
John McCall | 9c82afc | 2010-04-20 02:18:25 +0000 | [diff] [blame] | 41 | |
| 42 | namespace test0 { |
| 43 | struct A { |
| 44 | void *operator new(__SIZE_TYPE__ sz); |
| 45 | void operator delete(void *p) { ::operator delete(p); } |
| 46 | ~A() {} |
| 47 | }; |
| 48 | |
| 49 | // CHECK: define void @_ZN5test04testEPNS_1AE( |
| 50 | void test(A *a) { |
| 51 | // CHECK: call void @_ZN5test01AD1Ev |
| 52 | // CHECK-NEXT: bitcast |
| 53 | // CHECK-NEXT: call void @_ZN5test01AdlEPv |
| 54 | delete a; |
| 55 | } |
| 56 | |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 57 | // CHECK: define linkonce_odr void @_ZN5test01AD1Ev(%"struct.test0::A"* %this) unnamed_addr |
John McCall | 9c82afc | 2010-04-20 02:18:25 +0000 | [diff] [blame] | 58 | // CHECK: define linkonce_odr void @_ZN5test01AdlEPv |
| 59 | } |
John McCall | 1e7fe75 | 2010-09-02 09:58:18 +0000 | [diff] [blame] | 60 | |
| 61 | namespace test1 { |
| 62 | struct A { |
| 63 | int x; |
| 64 | ~A(); |
| 65 | }; |
| 66 | |
| 67 | // CHECK: define void @_ZN5test14testEPA10_A20_NS_1AE( |
| 68 | void test(A (*arr)[10][20]) { |
| 69 | delete [] arr; |
John McCall | 7cfd76c | 2011-07-13 01:41:37 +0000 | [diff] [blame] | 70 | // CHECK: icmp eq [10 x [20 x [[A:%.*]]]]* [[PTR:%.*]], null |
John McCall | 1e7fe75 | 2010-09-02 09:58:18 +0000 | [diff] [blame] | 71 | // CHECK-NEXT: br i1 |
| 72 | |
John McCall | 7cfd76c | 2011-07-13 01:41:37 +0000 | [diff] [blame] | 73 | // CHECK: [[BEGIN:%.*]] = getelementptr inbounds [10 x [20 x [[A]]]]* [[PTR]], i32 0, i32 0, i32 0 |
| 74 | // CHECK-NEXT: [[T0:%.*]] = bitcast [[A]]* [[BEGIN]] to i8* |
| 75 | // CHECK-NEXT: [[ALLOC:%.*]] = getelementptr inbounds i8* [[T0]], i64 -8 |
| 76 | // CHECK-NEXT: [[T1:%.*]] = bitcast i8* [[ALLOC]] to i64* |
| 77 | // CHECK-NEXT: [[COUNT:%.*]] = load i64* [[T1]] |
John McCall | 7cfd76c | 2011-07-13 01:41:37 +0000 | [diff] [blame] | 78 | // CHECK: [[END:%.*]] = getelementptr inbounds [[A]]* [[BEGIN]], i64 [[COUNT]] |
John McCall | fbf780a | 2011-07-13 08:09:46 +0000 | [diff] [blame] | 79 | // CHECK-NEXT: [[ISEMPTY:%.*]] = icmp eq [[A]]* [[BEGIN]], [[END]] |
| 80 | // CHECK-NEXT: br i1 [[ISEMPTY]], |
John McCall | 7cfd76c | 2011-07-13 01:41:37 +0000 | [diff] [blame] | 81 | // CHECK: [[PAST:%.*]] = phi [[A]]* [ [[END]], {{%.*}} ], [ [[CUR:%.*]], {{%.*}} ] |
| 82 | // CHECK-NEXT: [[CUR:%.*]] = getelementptr inbounds [[A]]* [[PAST]], i64 -1 |
| 83 | // CHECK-NEXT: call void @_ZN5test11AD1Ev([[A]]* [[CUR]]) |
| 84 | // CHECK-NEXT: [[ISDONE:%.*]] = icmp eq [[A]]* [[CUR]], [[BEGIN]] |
| 85 | // CHECK-NEXT: br i1 [[ISDONE]] |
John McCall | 1e7fe75 | 2010-09-02 09:58:18 +0000 | [diff] [blame] | 86 | // CHECK: call void @_ZdaPv(i8* [[ALLOC]]) |
| 87 | } |
| 88 | } |
Douglas Gregor | f9cce31 | 2010-09-02 15:34:35 +0000 | [diff] [blame] | 89 | |
| 90 | namespace test2 { |
| 91 | // CHECK: define void @_ZN5test21fEPb |
| 92 | void f(bool *b) { |
| 93 | // CHECK: call void @_ZdlPv(i8* |
| 94 | delete b; |
| 95 | // CHECK: call void @_ZdaPv(i8* |
| 96 | delete [] b; |
| 97 | } |
| 98 | } |
Argyrios Kyrtzidis | 4076dac | 2010-09-13 20:15:54 +0000 | [diff] [blame] | 99 | |
| 100 | namespace test3 { |
| 101 | void f(int a[10][20]) { |
| 102 | // CHECK: call void @_ZdaPv(i8* |
| 103 | delete a; |
| 104 | } |
| 105 | } |
Douglas Gregor | a8b20f7 | 2011-07-13 00:54:47 +0000 | [diff] [blame] | 106 | |
| 107 | namespace test4 { |
| 108 | // PR10341: ::delete with a virtual destructor |
| 109 | struct X { |
| 110 | virtual ~X(); |
| 111 | void operator delete (void *); |
| 112 | }; |
| 113 | |
| 114 | // CHECK: define void @_ZN5test421global_delete_virtualEPNS_1XE |
| 115 | void global_delete_virtual(X *xp) { |
John McCall | ecd03b4 | 2012-09-25 10:10:39 +0000 | [diff] [blame] | 116 | // Load the offset-to-top from the vtable and apply it. |
| 117 | // This has to be done first because the dtor can mess it up. |
| 118 | // CHECK: [[T0:%.*]] = bitcast [[X:%.*]]* [[XP:%.*]] to i64** |
| 119 | // CHECK-NEXT: [[VTABLE:%.*]] = load i64** [[T0]] |
| 120 | // CHECK-NEXT: [[T0:%.*]] = getelementptr inbounds i64* [[VTABLE]], i64 -2 |
| 121 | // CHECK-NEXT: [[OFFSET:%.*]] = load i64* [[T0]], align 8 |
| 122 | // CHECK-NEXT: [[T0:%.*]] = bitcast [[X]]* [[XP]] to i8* |
| 123 | // CHECK-NEXT: [[ALLOCATED:%.*]] = getelementptr inbounds i8* [[T0]], i64 [[OFFSET]] |
| 124 | // Load the complete-object destructor (not the deleting destructor) |
| 125 | // and call it. |
| 126 | // CHECK-NEXT: [[T0:%.*]] = bitcast [[X:%.*]]* [[XP:%.*]] to void ([[X]]*)*** |
| 127 | // CHECK-NEXT: [[VTABLE:%.*]] = load void ([[X]]*)*** [[T0]] |
| 128 | // CHECK-NEXT: [[T0:%.*]] = getelementptr inbounds void ([[X]]*)** [[VTABLE]], i64 0 |
| 129 | // CHECK-NEXT: [[DTOR:%.*]] = load void ([[X]]*)** [[T0]] |
| 130 | // CHECK-NEXT: call void [[DTOR]]([[X]]* [[OBJ:%.*]]) |
| 131 | // Call the global operator delete. |
Bill Wendling | 4e1125f | 2013-02-22 09:10:20 +0000 | [diff] [blame] | 132 | // CHECK-NEXT: call void @_ZdlPv(i8* [[ALLOCATED]]) [[NUW:#[0-9]+]] |
Douglas Gregor | a8b20f7 | 2011-07-13 00:54:47 +0000 | [diff] [blame] | 133 | ::delete xp; |
| 134 | } |
| 135 | } |
Eli Friedman | 91873b7 | 2011-07-27 18:54:57 +0000 | [diff] [blame] | 136 | |
| 137 | namespace test5 { |
| 138 | struct Incomplete; |
Eli Friedman | aebab72 | 2011-08-02 18:05:30 +0000 | [diff] [blame] | 139 | // CHECK: define void @_ZN5test523array_delete_incompleteEPNS_10IncompleteES1_ |
| 140 | void array_delete_incomplete(Incomplete *p1, Incomplete *p2) { |
| 141 | // CHECK: call void @_ZdlPv |
| 142 | delete p1; |
Eli Friedman | 8c83031 | 2011-07-27 20:09:36 +0000 | [diff] [blame] | 143 | // CHECK: call void @_ZdaPv |
Eli Friedman | aebab72 | 2011-08-02 18:05:30 +0000 | [diff] [blame] | 144 | delete [] p2; |
Eli Friedman | 91873b7 | 2011-07-27 18:54:57 +0000 | [diff] [blame] | 145 | } |
| 146 | } |
Bill Wendling | 4e1125f | 2013-02-22 09:10:20 +0000 | [diff] [blame] | 147 | |
| 148 | // CHECK: attributes [[NUW]] = { nounwind{{.*}} } |