2009-12-16 04:14:24 +08:00
|
|
|
// RUN: %clang_cc1 -triple x86_64-unknown-unknown %s -emit-llvm -o - | FileCheck %s
|
2011-05-16 09:05:12 +08:00
|
|
|
|
|
|
|
typedef __typeof__(sizeof(0)) size_t;
|
2009-06-01 04:21:44 +08:00
|
|
|
|
|
|
|
void t1() {
|
|
|
|
int* a = new int;
|
|
|
|
}
|
2009-06-01 04:26:12 +08:00
|
|
|
|
2011-05-16 09:05:12 +08:00
|
|
|
// Declare the reserved placement operators.
|
|
|
|
void *operator new(size_t, void*) throw();
|
|
|
|
void operator delete(void*, void*) throw();
|
|
|
|
void *operator new[](size_t, void*) throw();
|
|
|
|
void operator delete[](void*, void*) throw();
|
2009-06-01 04:26:12 +08:00
|
|
|
|
|
|
|
void t2(int* a) {
|
|
|
|
int* b = new (a) int;
|
|
|
|
}
|
2009-06-01 04:56:36 +08:00
|
|
|
|
2009-06-01 05:12:26 +08:00
|
|
|
struct S {
|
|
|
|
int a;
|
|
|
|
};
|
|
|
|
|
2009-06-01 05:53:59 +08:00
|
|
|
// POD types.
|
2009-06-01 04:56:36 +08:00
|
|
|
void t3() {
|
|
|
|
int *a = new int(10);
|
2009-06-01 05:07:58 +08:00
|
|
|
_Complex int* b = new _Complex int(10i);
|
|
|
|
|
2009-06-01 05:12:26 +08:00
|
|
|
S s;
|
|
|
|
s.a = 10;
|
|
|
|
S *sp = new S(s);
|
2009-06-01 04:56:36 +08:00
|
|
|
}
|
2009-06-01 05:53:59 +08:00
|
|
|
|
|
|
|
// Non-POD
|
|
|
|
struct T {
|
|
|
|
T();
|
|
|
|
int a;
|
|
|
|
};
|
|
|
|
|
|
|
|
void t4() {
|
2009-09-24 00:07:23 +08:00
|
|
|
// CHECK: call void @_ZN1TC1Ev
|
2009-06-01 05:53:59 +08:00
|
|
|
T *t = new T;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct T2 {
|
|
|
|
int a;
|
|
|
|
T2(int, int);
|
|
|
|
};
|
|
|
|
|
|
|
|
void t5() {
|
2009-09-24 00:07:23 +08:00
|
|
|
// CHECK: call void @_ZN2T2C1Eii
|
2009-06-01 05:53:59 +08:00
|
|
|
T2 *t2 = new T2(10, 10);
|
|
|
|
}
|
2009-06-01 08:05:16 +08:00
|
|
|
|
|
|
|
int *t6() {
|
|
|
|
// Null check.
|
|
|
|
return new (0) int(10);
|
|
|
|
}
|
2009-06-01 08:26:14 +08:00
|
|
|
|
|
|
|
void t7() {
|
|
|
|
new int();
|
|
|
|
}
|
2009-09-24 00:07:23 +08:00
|
|
|
|
2009-09-24 02:59:48 +08:00
|
|
|
struct U {
|
|
|
|
~U();
|
|
|
|
};
|
|
|
|
|
2009-09-24 00:07:23 +08:00
|
|
|
void t8(int n) {
|
|
|
|
new int[10];
|
|
|
|
new int[n];
|
|
|
|
|
|
|
|
// Non-POD
|
|
|
|
new T[10];
|
|
|
|
new T[n];
|
2009-09-24 02:59:48 +08:00
|
|
|
|
|
|
|
// Cookie required
|
|
|
|
new U[10];
|
|
|
|
new U[n];
|
2009-09-24 00:07:23 +08:00
|
|
|
}
|
2009-11-23 03:25:33 +08:00
|
|
|
|
2010-08-18 23:06:25 +08:00
|
|
|
// noalias
|
|
|
|
// CHECK: declare noalias i8* @_Znam
|
|
|
|
void *operator new[](size_t);
|
|
|
|
|
2009-11-23 03:25:33 +08:00
|
|
|
void t9() {
|
|
|
|
bool b;
|
|
|
|
|
|
|
|
new bool(true);
|
|
|
|
new (&b) bool(true);
|
|
|
|
}
|
2009-11-25 02:29:37 +08:00
|
|
|
|
|
|
|
struct A {
|
|
|
|
void* operator new(__typeof(sizeof(int)), int, float, ...);
|
|
|
|
A();
|
|
|
|
};
|
|
|
|
|
|
|
|
A* t10() {
|
|
|
|
// CHECK: @_ZN1AnwEmifz
|
|
|
|
return new(1, 2, 3.45, 100) A;
|
|
|
|
}
|
|
|
|
|
2010-07-21 09:10:17 +08:00
|
|
|
// CHECK: define void @_Z3t11i
|
2010-05-03 09:20:20 +08:00
|
|
|
struct B { int a; };
|
2010-07-21 09:10:17 +08:00
|
|
|
struct Bmemptr { int Bmemptr::* memptr; int a; };
|
|
|
|
|
|
|
|
void t11(int n) {
|
2009-12-17 00:59:22 +08:00
|
|
|
// CHECK: call noalias i8* @_Znwm
|
2010-04-04 11:10:52 +08:00
|
|
|
// CHECK: call void @llvm.memset.p0i8.i64(
|
2009-12-16 14:57:54 +08:00
|
|
|
B* b = new B();
|
2010-07-21 09:10:17 +08:00
|
|
|
|
|
|
|
// CHECK: call noalias i8* @_Znam
|
|
|
|
// CHECK: {{call void.*llvm.memset.p0i8.i64.*i8 0, i64 %}}
|
|
|
|
B *b2 = new B[n]();
|
|
|
|
|
|
|
|
// CHECK: call noalias i8* @_Znam
|
|
|
|
// CHECK: call void @llvm.memcpy.p0i8.p0i8.i64
|
|
|
|
// CHECK: br
|
|
|
|
Bmemptr *b_memptr = new Bmemptr[n]();
|
|
|
|
|
|
|
|
// CHECK: ret void
|
2009-12-16 14:57:54 +08:00
|
|
|
}
|
2010-05-03 23:09:17 +08:00
|
|
|
|
|
|
|
struct Empty { };
|
|
|
|
|
|
|
|
// We don't need to initialize an empty class.
|
2010-07-21 09:10:17 +08:00
|
|
|
// CHECK: define void @_Z3t12v
|
2010-05-03 23:09:17 +08:00
|
|
|
void t12() {
|
2010-07-21 09:10:17 +08:00
|
|
|
// CHECK: call noalias i8* @_Znam
|
|
|
|
// CHECK-NOT: br
|
2010-05-03 23:09:17 +08:00
|
|
|
(void)new Empty[10];
|
2010-07-21 09:10:17 +08:00
|
|
|
|
|
|
|
// CHECK: call noalias i8* @_Znam
|
|
|
|
// CHECK-NOT: br
|
|
|
|
(void)new Empty[10]();
|
|
|
|
|
|
|
|
// CHECK: ret void
|
|
|
|
}
|
|
|
|
|
|
|
|
// Zero-initialization
|
|
|
|
// CHECK: define void @_Z3t13i
|
|
|
|
void t13(int n) {
|
|
|
|
// CHECK: call noalias i8* @_Znwm
|
|
|
|
// CHECK: store i32 0, i32*
|
|
|
|
(void)new int();
|
|
|
|
|
|
|
|
// CHECK: call noalias i8* @_Znam
|
|
|
|
// CHECK: {{call void.*llvm.memset.p0i8.i64.*i8 0, i64 %}}
|
|
|
|
(void)new int[n]();
|
|
|
|
|
|
|
|
// CHECK-NEXT: ret void
|
2010-05-03 23:09:17 +08:00
|
|
|
}
|
2010-08-26 07:14:56 +08:00
|
|
|
|
|
|
|
struct Alloc{
|
2010-08-26 23:23:38 +08:00
|
|
|
int x;
|
2010-08-26 07:14:56 +08:00
|
|
|
void* operator new[](size_t size);
|
|
|
|
void operator delete[](void* p);
|
2010-08-26 23:23:38 +08:00
|
|
|
~Alloc();
|
2010-08-26 07:14:56 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
void f() {
|
2010-08-26 23:23:38 +08:00
|
|
|
// CHECK: call i8* @_ZN5AllocnaEm(i64 808)
|
|
|
|
// CHECK: store i64 200
|
2010-09-02 17:58:18 +08:00
|
|
|
// CHECK: call void @_ZN5AllocD1Ev(
|
2010-08-26 07:14:56 +08:00
|
|
|
// CHECK: call void @_ZN5AllocdaEPv(i8*
|
|
|
|
delete[] new Alloc[10][20];
|
2010-09-03 07:24:14 +08:00
|
|
|
// CHECK: call noalias i8* @_Znwm
|
|
|
|
// CHECK: call void @_ZdlPv(i8*
|
|
|
|
delete new bool;
|
|
|
|
// CHECK: ret void
|
2010-08-26 07:14:56 +08:00
|
|
|
}
|
2011-05-16 09:05:12 +08:00
|
|
|
|
|
|
|
namespace test15 {
|
|
|
|
struct A { A(); ~A(); };
|
|
|
|
|
|
|
|
// CHECK: define void @_ZN6test155test0EPv(
|
|
|
|
// CHECK: [[P:%.*]] = load i8*
|
|
|
|
// CHECK-NEXT: icmp eq i8* [[P]], null
|
|
|
|
// CHECK-NEXT: br i1
|
|
|
|
// CHECK: [[T0:%.*]] = bitcast i8* [[P]] to [[A:%.*]]*
|
|
|
|
// CHECK-NEXT: call void @_ZN6test151AC1Ev([[A]]* [[T0]])
|
|
|
|
void test0(void *p) {
|
|
|
|
new (p) A();
|
|
|
|
}
|
|
|
|
|
|
|
|
// CHECK: define void @_ZN6test155test1EPv(
|
2011-07-13 14:10:41 +08:00
|
|
|
// CHECK: [[P:%.*]] = load i8**
|
2011-05-16 09:05:12 +08:00
|
|
|
// CHECK-NEXT: icmp eq i8* [[P]], null
|
|
|
|
// CHECK-NEXT: br i1
|
2011-07-13 14:10:41 +08:00
|
|
|
// CHECK: [[BEGIN:%.*]] = bitcast i8* [[P]] to [[A:%.*]]*
|
|
|
|
// CHECK-NEXT: [[END:%.*]] = getelementptr inbounds [[A]]* [[BEGIN]], i64 5
|
|
|
|
// CHECK-NEXT: br label
|
|
|
|
// CHECK: [[CUR:%.*]] = phi [[A]]* [ [[BEGIN]], {{%.*}} ], [ [[NEXT:%.*]], {{%.*}} ]
|
|
|
|
// CHECK-NEXT: call void @_ZN6test151AC1Ev([[A]]* [[CUR]])
|
|
|
|
// CHECK-NEXT: [[NEXT]] = getelementptr inbounds [[A]]* [[CUR]], i64 1
|
|
|
|
// CHECK-NEXT: [[DONE:%.*]] = icmp eq [[A]]* [[NEXT]], [[END]]
|
|
|
|
// CHECK-NEXT: br i1 [[DONE]]
|
2011-05-16 09:05:12 +08:00
|
|
|
void test1(void *p) {
|
|
|
|
new (p) A[5];
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: it's okay if all these size calculations get dropped.
|
|
|
|
// FIXME: maybe we should try to throw on overflow?
|
|
|
|
// CHECK: define void @_ZN6test155test2EPvi(
|
|
|
|
// CHECK: [[N:%.*]] = load i32*
|
|
|
|
// CHECK-NEXT: [[T0:%.*]] = sext i32 [[N]] to i64
|
|
|
|
// CHECK-NEXT: [[T1:%.*]] = icmp slt i64 [[T0]], 0
|
|
|
|
// CHECK-NEXT: [[T2:%.*]] = select i1 [[T1]], i64 -1, i64 [[T0]]
|
|
|
|
// CHECK-NEXT: [[P:%.*]] = load i8*
|
|
|
|
// CHECK-NEXT: icmp eq i8* [[P]], null
|
|
|
|
// CHECK-NEXT: br i1
|
2011-07-13 14:10:41 +08:00
|
|
|
// CHECK: [[BEGIN:%.*]] = bitcast i8* [[P]] to [[A:%.*]]*
|
|
|
|
// CHECK-NEXT: [[ISEMPTY:%.*]] = icmp eq i64 [[T0]], 0
|
|
|
|
// CHECK-NEXT: br i1 [[ISEMPTY]],
|
|
|
|
// CHECK: [[END:%.*]] = getelementptr inbounds [[A]]* [[BEGIN]], i64 [[T0]]
|
|
|
|
// CHECK-NEXT: br label
|
|
|
|
// CHECK: [[CUR:%.*]] = phi [[A]]* [ [[BEGIN]],
|
|
|
|
// CHECK-NEXT: call void @_ZN6test151AC1Ev([[A]]* [[CUR]])
|
2011-05-16 09:05:12 +08:00
|
|
|
void test2(void *p, int n) {
|
|
|
|
new (p) A[n];
|
|
|
|
}
|
|
|
|
}
|
2011-06-28 00:55:54 +08:00
|
|
|
|
|
|
|
namespace PR10197 {
|
|
|
|
// CHECK: define weak_odr void @_ZN7PR101971fIiEEvv()
|
|
|
|
template<typename T>
|
|
|
|
void f() {
|
|
|
|
// CHECK: [[CALL:%.*]] = call noalias i8* @_Znwm
|
|
|
|
// CHECK-NEXT: [[CASTED:%.*]] = bitcast i8* [[CALL]] to
|
|
|
|
new T;
|
|
|
|
// CHECK-NEXT: ret void
|
|
|
|
}
|
|
|
|
|
|
|
|
template void f<int>();
|
|
|
|
}
|