aboutsummaryrefslogtreecommitdiff
path: root/clang/test/CodeGenCXX/new.cpp
blob: af225529c494e625c408fed5255c980dfd384dfa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
// RUN: %clang_cc1 -std=c++14 -triple x86_64-unknown-unknown %s -emit-llvm -o - | FileCheck %s

typedef __typeof__(sizeof(0)) size_t;

// Declare an 'operator new' template to tickle a bug in __builtin_operator_new.
template<typename T> void *operator new(size_t, int (*)(T));

// Ensure that this declaration doesn't cause operator new to lose its
// 'noalias' attribute.
void *operator new[](size_t);

void t1() {
  delete new int;
  delete [] new int [3];
}

// CHECK: declare noundef nonnull ptr @_Znwm(i64 noundef) [[ATTR_NOBUILTIN:#[^ ]*]]
// CHECK: declare void @_ZdlPvm(ptr noundef, i64 noundef) [[ATTR_NOBUILTIN_NOUNWIND:#[^ ]*]]
// CHECK: declare noundef nonnull ptr @_Znam(i64 noundef) [[ATTR_NOBUILTIN]]
// CHECK: declare void @_ZdaPv(ptr noundef) [[ATTR_NOBUILTIN_NOUNWIND]]

namespace std {
  struct nothrow_t {};
}
std::nothrow_t nothrow;

// 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();

// Declare the replaceable global allocation operators.
void *operator new(size_t, const std::nothrow_t &) throw();
void *operator new[](size_t, const std::nothrow_t &) throw();
void operator delete(void *, const std::nothrow_t &) throw();
void operator delete[](void *, const std::nothrow_t &) throw();

// Declare some other placemenet operators.
void *operator new(size_t, void*, bool) throw();
void *operator new[](size_t, void*, bool) throw();

void t2(int* a) {
  int* b = new (a) int;
}

struct S {
  int a;
};

// POD types.
void t3() {
  int *a = new int(10);
  _Complex int* b = new _Complex int(10i);

  S s;
  s.a = 10;
  S *sp = new S(s);
}

// Non-POD
struct T {
  T();
  int a;
};

void t4() {
  // CHECK: call void @_ZN1TC1Ev
  T *t = new T;
}

struct T2 {
  int a;
  T2(int, int);
};

void t5() {
  // CHECK: call void @_ZN2T2C1Eii
  T2 *t2 = new T2(10, 10);
}

int *t6() {
  // Null check.
  return new (0) int(10);
}

void t7() {
  new int();
}

struct U {
  ~U();
};

void t8(int n) {
  new int[10];
  new int[n];

  // Non-POD
  new T[10];
  new T[n];

  // Cookie required
  new U[10];
  new U[n];
}

void t9() {
  bool b;

  new bool(true);
  new (&b) bool(true);
}

struct A {
  void* operator new(__typeof(sizeof(int)), int, float, ...);
  A();
};

A* t10() {
   // CHECK: @_ZN1AnwEmifz
  return new(1, 2, 3.45, 100) A;
}

// CHECK-LABEL: define{{.*}} void @_Z3t11i
struct B { int a; };
struct Bmemptr { int Bmemptr::* memptr; int a; };

void t11(int n) {
  // CHECK: call noalias noundef nonnull ptr @_Znwm
  // CHECK: call void @llvm.memset.p0.i64(
  B* b = new B();

  // CHECK: call noalias noundef nonnull ptr @_Znam
  // CHECK: {{call void.*llvm.memset.p0.i64.*i8 0, i64 %}}
  B *b2 = new B[n]();

  // CHECK: call noalias noundef nonnull ptr @_Znam
  // CHECK: call void @llvm.memcpy.p0.p0.i64
  // CHECK: br
  Bmemptr *b_memptr = new Bmemptr[n]();

  // CHECK: ret void
}

struct Empty { };

// We don't need to initialize an empty class.
// CHECK-LABEL: define{{.*}} void @_Z3t12v
void t12() {
  // CHECK: call noalias noundef nonnull ptr @_Znam
  // CHECK-NOT: br
  (void)new Empty[10];

  // CHECK: call noalias noundef nonnull ptr @_Znam
  // CHECK-NOT: br
  (void)new Empty[10]();

  // CHECK: ret void
}

// Zero-initialization
// CHECK-LABEL: define{{.*}} void @_Z3t13i
void t13(int n) {
  // CHECK: call noalias noundef nonnull ptr @_Znwm
  // CHECK: store i32 0, ptr
  (void)new int();

  // CHECK: call noalias noundef nonnull ptr @_Znam
  // CHECK: {{call void.*llvm.memset.p0.i64.*i8 0, i64 %}}
  (void)new int[n]();

  // CHECK-NEXT: ret void
}

struct Alloc{
  int x;
  void* operator new[](size_t size);
  __attribute__((returns_nonnull)) void *operator new[](size_t size, const std::nothrow_t &) throw();
  void operator delete[](void* p);
  ~Alloc();
};

void f() {
  // CHECK: call noundef ptr @_ZN5AllocnaEm(i64 noundef 808)
  // CHECK: store i64 200
  // CHECK: call void @_ZN5AllocD1Ev(
  // CHECK: call void @_ZN5AllocdaEPv(ptr
  delete[] new Alloc[10][20];
  // CHECK: [[P:%.*]] = call noundef nonnull ptr @_ZN5AllocnaEmRKSt9nothrow_t(i64 noundef 808, {{.*}}) [[ATTR_NOUNWIND:#[^ ]*]]
  // CHECK-NOT: icmp eq ptr [[P]], null
  // CHECK: store i64 200
  delete[] new (nothrow) Alloc[10][20];
  // CHECK: call noalias noundef nonnull ptr @_Znwm
  // CHECK: call void @_ZdlPvm(ptr noundef {{%.*}}, i64 noundef 1)
  delete new bool;
  // CHECK: ret void
}

namespace test15 {
  struct A { A(); ~A(); };

  // CHECK-LABEL:    define{{.*}} void @_ZN6test156test0aEPv(
  // CHECK:      [[P:%.*]] = load ptr, ptr
  // CHECK-NOT:  icmp eq ptr [[P]], null
  // CHECK-NOT:  br i1
  // CHECK-NEXT: call void @_ZN6test151AC1Ev(ptr {{[^,]*}} [[P]])
  void test0a(void *p) {
    new (p) A();
  }

  // CHECK-LABEL:    define{{.*}} void @_ZN6test156test0bEPv(
  // CHECK:      [[P0:%.*]] = load ptr, ptr
  // CHECK:      [[P:%.*]] = call noundef ptr @_ZnwmPvb(i64 noundef 1, ptr noundef [[P0]]
  // CHECK-NEXT: icmp eq ptr [[P]], null
  // CHECK-NEXT: br i1
  // CHECK: call void @_ZN6test151AC1Ev(ptr {{[^,]*}} [[P]])
  void test0b(void *p) {
    new (p, true) A();
  }

  // CHECK-LABEL:    define{{.*}} void @_ZN6test156test1aEPv(
  // CHECK:      [[P:%.*]] = load ptr, ptr
  // CHECK-NOT:  icmp eq ptr [[P]], null
  // CHECK-NOT:  br i1
  // CHECK-NEXT: [[END:%.*]] = getelementptr inbounds [[A:.*]], ptr [[P]], i64 5
  // CHECK-NEXT: br label
  // CHECK:      [[CUR:%.*]] = phi ptr [ [[P]], {{%.*}} ], [ [[NEXT:%.*]], {{%.*}} ]
  // CHECK-NEXT: call void @_ZN6test151AC1Ev(ptr {{[^,]*}} [[CUR]])
  // CHECK-NEXT: [[NEXT]] = getelementptr inbounds [[A]], ptr [[CUR]], i64 1
  // CHECK-NEXT: [[DONE:%.*]] = icmp eq ptr [[NEXT]], [[END]]
  // CHECK-NEXT: br i1 [[DONE]]
  void test1a(void *p) {
    new (p) A[5];
  }

  // CHECK-LABEL:    define{{.*}} void @_ZN6test156test1bEPv(
  // CHECK:      [[P0:%.*]] = load ptr, ptr
  // CHECK:      [[P:%.*]] = call noundef ptr @_ZnamPvb(i64 noundef 13, ptr noundef [[P0]]
  // CHECK-NEXT: icmp eq ptr [[P]], null
  // CHECK-NEXT: br i1
  // CHECK:      [[AFTER_COOKIE:%.*]] = getelementptr inbounds i8, ptr [[P]], i64 8
  // CHECK-NEXT: [[END:%.*]] = getelementptr inbounds [[A]], ptr [[AFTER_COOKIE]], i64 5
  // CHECK-NEXT: br label
  // CHECK:      [[CUR:%.*]] = phi ptr [ [[AFTER_COOKIE]], {{%.*}} ], [ [[NEXT:%.*]], {{%.*}} ]
  // CHECK-NEXT: call void @_ZN6test151AC1Ev(ptr {{[^,]*}} [[CUR]])
  // CHECK-NEXT: [[NEXT]] = getelementptr inbounds [[A]], ptr [[CUR]], i64 1
  // CHECK-NEXT: [[DONE:%.*]] = icmp eq ptr [[NEXT]], [[END]]
  // CHECK-NEXT: br i1 [[DONE]]
  void test1b(void *p) {
    new (p, true) A[5];
  }

  // TODO: it's okay if all these size calculations get dropped.
  // FIXME: maybe we should try to throw on overflow?
  // CHECK-LABEL:    define{{.*}} void @_ZN6test155test2EPvi(
  // CHECK:      [[N:%.*]] = load i32, ptr
  // CHECK-NEXT: [[T0:%.*]] = sext i32 [[N]] to i64
  // CHECK-NEXT: [[P:%.*]] = load ptr, ptr
  // CHECK-NEXT: [[ISEMPTY:%.*]] = icmp eq i64 [[T0]], 0
  // CHECK-NEXT: br i1 [[ISEMPTY]],
  // CHECK:      [[END:%.*]] = getelementptr inbounds [[A]], ptr [[P]], i64 [[T0]]
  // CHECK-NEXT: br label
  // CHECK:      [[CUR:%.*]] = phi ptr [ [[P]],
  // CHECK-NEXT: call void @_ZN6test151AC1Ev(ptr {{[^,]*}} [[CUR]])
  void test2(void *p, int n) {
    new (p) A[n];
  }
}

namespace PR10197 {
  // CHECK-LABEL: define weak_odr void @_ZN7PR101971fIiEEvv()
  template<typename T>
  void f() {
    // CHECK: [[CALL:%.*]] = call noalias noundef nonnull ptr @_Znwm
    new T;
    // CHECK-NEXT: ret void
  }

  template void f<int>();
}

namespace PR11523 {
  class MyClass;
  typedef int MyClass::* NewTy;
  // CHECK-LABEL: define{{.*}} ptr @_ZN7PR115231fEv
  // CHECK: store i64 -1
  NewTy* f() { return new NewTy[2](); }
}

namespace PR11757 {
  // Make sure we elide the copy construction.
  struct X { X(); X(const X&); };
  X* a(X* x) { return new X(X()); }
  // CHECK: define {{.*}} @_ZN7PR117571aEPNS_1XE
  // CHECK: [[CALL:%.*]] = call noalias noundef nonnull ptr @_Znwm
  // CHECK: ret {{.*}} [[CALL]]
}

namespace PR13380 {
  struct A { A() {} };
  struct B : public A { int x; };
  // CHECK-LABEL: define{{.*}} ptr @_ZN7PR133801fEv
  // CHECK: call noalias noundef nonnull ptr @_Znam(
  // CHECK: call void @llvm.memset.p0
  // CHECK-NEXT: call void @_ZN7PR133801BC1Ev
  void* f() { return new B[2](); }
}

struct MyPlacementType {} mpt;
void *operator new(size_t, MyPlacementType);

namespace N3664 {
  struct S { S() throw(int); };

  // CHECK-LABEL: define{{.*}} void @_ZN5N36641fEv
  void f() {
    // CHECK: call noalias noundef nonnull ptr @_Znwm(i64 noundef 4) [[ATTR_BUILTIN_NEW:#[^ ]*]]
    int *p = new int; // expected-note {{allocated with 'new' here}}
    // CHECK: call void @_ZdlPvm({{.*}}) [[ATTR_BUILTIN_DELETE:#[^ ]*]]
    delete p;

    // CHECK: call noalias noundef nonnull ptr @_Znam(i64 noundef 12) [[ATTR_BUILTIN_NEW]]
    int *q = new int[3];
    // CHECK: call void @_ZdaPv({{.*}}) [[ATTR_BUILTIN_DELETE]]
    delete[] p; // expected-warning {{'delete[]' applied to a pointer that was allocated with 'new'; did you mean 'delete'?}}

    // CHECK: call noalias noundef ptr @_ZnamRKSt9nothrow_t(i64 noundef 3, {{.*}}) [[ATTR_NOBUILTIN_NOUNWIND_ALLOCSIZE:#[^ ]*]]
    (void) new (nothrow) S[3];

    // CHECK: call noundef ptr @_Znwm15MyPlacementType(i64 noundef 4){{$}}
    (void) new (mpt) int;
  }

  // CHECK: declare noundef ptr @_ZnamRKSt9nothrow_t(i64 noundef, {{.*}}) [[ATTR_NOBUILTIN_NOUNWIND_ALLOCSIZE:#[^ ]*]]

  // CHECK-LABEL: define{{.*}} void @_ZN5N36641gEv
  void g() {
    // It's OK for there to be attributes here, so long as we don't have a
    // 'builtin' attribute.
    // CHECK: call noalias noundef nonnull ptr @_Znwm(i64 noundef 4) {{#[^ ]*}}{{$}}
    int *p = (int*)operator new(4);
    // CHECK: call void @_ZdlPv({{.*}}) [[ATTR_NOUNWIND:#[^ ]*]]
    operator delete(p);

    // CHECK: call noalias noundef nonnull ptr @_Znam(i64 noundef 12) {{#[^ ]*}}{{$}}
    int *q = (int*)operator new[](12);
    // CHECK: call void @_ZdaPv({{.*}}) [[ATTR_NOUNWIND]]
    operator delete [](p);

    // CHECK: call noalias noundef ptr @_ZnamRKSt9nothrow_t(i64 noundef 3, {{.*}}) [[ATTR_NOUNWIND_ALLOCSIZE:#[^ ]*]]
    (void) operator new[](3, nothrow);
  }
}

namespace builtins {
  // CHECK-LABEL: define{{.*}} void @_ZN8builtins1fEv
  void f() {
    // CHECK: call noalias noundef nonnull ptr @_Znwm(i64 noundef 4) [[ATTR_BUILTIN_NEW]]
    // CHECK: call void @_ZdlPv({{.*}}) [[ATTR_BUILTIN_DELETE]]
    __builtin_operator_delete(__builtin_operator_new(4));
  }
}

// CHECK-DAG: attributes [[ATTR_NOBUILTIN]] = {{[{].*}} nobuiltin allocsize(0) {{.*[}]}}
// CHECK-DAG: attributes [[ATTR_NOBUILTIN_NOUNWIND]] = {{[{].*}} nobuiltin nounwind {{.*[}]}}
// CHECK-DAG: attributes [[ATTR_NOBUILTIN_NOUNWIND_ALLOCSIZE]] = {{[{].*}} nobuiltin nounwind allocsize(0) {{.*[}]}}

// CHECK-DAG: attributes [[ATTR_BUILTIN_NEW]] = {{[{].*}} builtin {{.*[}]}}
// CHECK-DAG: attributes [[ATTR_BUILTIN_DELETE]] = {{[{].*}} builtin {{.*[}]}}

// The ([^b}|...) monstrosity is matching a character that's not the start of 'builtin'.
// Add more letters if this matches some other attribute.
// CHECK-DAG: attributes [[ATTR_NOUNWIND]] = {{([^b]|b[^u]|bu[^i]|bui[^l])*}} nounwind {{([^b]|b[^u]|bu[^i]|bui[^l])*$}}
// CHECK-DAG: attributes [[ATTR_NOUNWIND_ALLOCSIZE]] = {{([^b]|b[^u]|bu[^i]|bui[^l])*}} nounwind allocsize(0) {{([^b]|b[^u]|bu[^i]|bui[^l])*$}}