new-delete.cpp revision 6ee326af4e77e6f05973486097884d7431f2108d
18e610ed585685c55e2cfd010b4233eafc7d568c2Thanh Le// RUN: %clang_cc1 -fsyntax-only -verify %s -triple=i686-pc-linux-gnu
21194ec356a16f3c6dcf408289e36e42c149d6dc8Kevin Jin
38e610ed585685c55e2cfd010b4233eafc7d568c2Thanh Le#include <stddef.h>
48e610ed585685c55e2cfd010b4233eafc7d568c2Thanh Le
58e610ed585685c55e2cfd010b4233eafc7d568c2Thanh Lestruct S // expected-note {{candidate}}
68e610ed585685c55e2cfd010b4233eafc7d568c2Thanh Le{
78e610ed585685c55e2cfd010b4233eafc7d568c2Thanh Le  S(int, int, double); // expected-note {{candidate}}
88e610ed585685c55e2cfd010b4233eafc7d568c2Thanh Le  S(double, int); // expected-note 2 {{candidate}}
98e610ed585685c55e2cfd010b4233eafc7d568c2Thanh Le  S(float, int); // expected-note 2 {{candidate}}
108e610ed585685c55e2cfd010b4233eafc7d568c2Thanh Le};
118e610ed585685c55e2cfd010b4233eafc7d568c2Thanh Lestruct T; // expected-note{{forward declaration of 'T'}}
128e610ed585685c55e2cfd010b4233eafc7d568c2Thanh Lestruct U
138e610ed585685c55e2cfd010b4233eafc7d568c2Thanh Le{
148e610ed585685c55e2cfd010b4233eafc7d568c2Thanh Le  // A special new, to verify that the global version isn't used.
158e610ed585685c55e2cfd010b4233eafc7d568c2Thanh Le  void* operator new(size_t, S*); // expected-note {{candidate}}
168e610ed585685c55e2cfd010b4233eafc7d568c2Thanh Le};
171194ec356a16f3c6dcf408289e36e42c149d6dc8Kevin Jinstruct V : U
188e610ed585685c55e2cfd010b4233eafc7d568c2Thanh Le{
19f9c2a591497874769b87bf492a0666cf853e0ae5Kevin Jin};
20f9c2a591497874769b87bf492a0666cf853e0ae5Kevin Jin
21f9c2a591497874769b87bf492a0666cf853e0ae5Kevin Jin// PR5823
22f9c2a591497874769b87bf492a0666cf853e0ae5Kevin Jinvoid* operator new(const size_t); // expected-note 2 {{candidate}}
231194ec356a16f3c6dcf408289e36e42c149d6dc8Kevin Jinvoid* operator new(size_t, int*); // expected-note 3 {{candidate}}
241194ec356a16f3c6dcf408289e36e42c149d6dc8Kevin Jinvoid* operator new(size_t, float*); // expected-note 3 {{candidate}}
258e610ed585685c55e2cfd010b4233eafc7d568c2Thanh Levoid* operator new(size_t, S); // expected-note 2 {{candidate}}
268e610ed585685c55e2cfd010b4233eafc7d568c2Thanh Le
27f9c2a591497874769b87bf492a0666cf853e0ae5Kevin Jinstruct foo { };
288e610ed585685c55e2cfd010b4233eafc7d568c2Thanh Le
29a738fe74f57f48dde2dd7a28479bab3f5441dadbKevin Jinvoid good_news()
3021a0001e2426644dd68e6140b5873ebaeafcc3dcKevin Jin{
3121a0001e2426644dd68e6140b5873ebaeafcc3dcKevin Jin  int *pi = new int;
3221a0001e2426644dd68e6140b5873ebaeafcc3dcKevin Jin  float *pf = new (pi) float();
3321a0001e2426644dd68e6140b5873ebaeafcc3dcKevin Jin  pi = new int(1);
3421a0001e2426644dd68e6140b5873ebaeafcc3dcKevin Jin  pi = new int('c');
3521a0001e2426644dd68e6140b5873ebaeafcc3dcKevin Jin  const int *pci = new const int();
3621a0001e2426644dd68e6140b5873ebaeafcc3dcKevin Jin  S *ps = new S(1, 2, 3.4);
3721a0001e2426644dd68e6140b5873ebaeafcc3dcKevin Jin  ps = new (pf) (S)(1, 2, 3.4);
3821a0001e2426644dd68e6140b5873ebaeafcc3dcKevin Jin  S *(*paps)[2] = new S*[*pi][2];
3921a0001e2426644dd68e6140b5873ebaeafcc3dcKevin Jin  typedef int ia4[4];
4021a0001e2426644dd68e6140b5873ebaeafcc3dcKevin Jin  ia4 *pai = new (int[3][4]);
4121a0001e2426644dd68e6140b5873ebaeafcc3dcKevin Jin  pi = ::new int;
42f9c2a591497874769b87bf492a0666cf853e0ae5Kevin Jin  U *pu = new (ps) U;
43f9c2a591497874769b87bf492a0666cf853e0ae5Kevin Jin  V *pv = new (ps) V;
44a738fe74f57f48dde2dd7a28479bab3f5441dadbKevin Jin
45a738fe74f57f48dde2dd7a28479bab3f5441dadbKevin Jin  pi = new (S(1.0f, 2)) int;
46f9c2a591497874769b87bf492a0666cf853e0ae5Kevin Jin
47f9c2a591497874769b87bf492a0666cf853e0ae5Kevin Jin  (void)new int[true];
4821a0001e2426644dd68e6140b5873ebaeafcc3dcKevin Jin
4921a0001e2426644dd68e6140b5873ebaeafcc3dcKevin Jin  // PR7147
500319e7c14a536a11851cc30cfa57241ce90fec11Kevin Jin  typedef int a[2];
5121a0001e2426644dd68e6140b5873ebaeafcc3dcKevin Jin  foo* f1 = new foo;
5221a0001e2426644dd68e6140b5873ebaeafcc3dcKevin Jin  foo* f2 = new foo[2];
5321a0001e2426644dd68e6140b5873ebaeafcc3dcKevin Jin  typedef foo x[2];
5421a0001e2426644dd68e6140b5873ebaeafcc3dcKevin Jin  typedef foo y[2][2];
55f9c2a591497874769b87bf492a0666cf853e0ae5Kevin Jin  x* f3 = new y;
56f9c2a591497874769b87bf492a0666cf853e0ae5Kevin Jin}
577dde4b200c490587409e0301e58261210e7a5896Tony Wickham
58f9c6c5063b38b623679e47d7095cccddb0481319Kevin Jinstruct abstract {
59f9c2a591497874769b87bf492a0666cf853e0ae5Kevin Jin  virtual ~abstract() = 0;
60f9c2a591497874769b87bf492a0666cf853e0ae5Kevin Jin};
61f9c6c5063b38b623679e47d7095cccddb0481319Kevin Jin
62f9c2a591497874769b87bf492a0666cf853e0ae5Kevin Jinvoid bad_news(int *ip)
63f9c2a591497874769b87bf492a0666cf853e0ae5Kevin Jin{
6421a0001e2426644dd68e6140b5873ebaeafcc3dcKevin Jin  int i = 1; // expected-note 2{{here}}
6521a0001e2426644dd68e6140b5873ebaeafcc3dcKevin Jin  (void)new; // expected-error {{expected a type}}
6621a0001e2426644dd68e6140b5873ebaeafcc3dcKevin Jin  (void)new 4; // expected-error {{expected a type}}
6721a0001e2426644dd68e6140b5873ebaeafcc3dcKevin Jin  (void)new () int; // expected-error {{expected expression}}
6821a0001e2426644dd68e6140b5873ebaeafcc3dcKevin Jin  (void)new int[1.1]; // expected-error {{array size expression must have integral or enumeration type, not 'double'}}
6921a0001e2426644dd68e6140b5873ebaeafcc3dcKevin Jin  (void)new int[1][i]; // expected-error {{only the first dimension}} expected-note {{read of non-const variable 'i' is not allowed in a constant expression}}
7021a0001e2426644dd68e6140b5873ebaeafcc3dcKevin Jin  (void)new (int[1][i]); // expected-error {{only the first dimension}} expected-note {{read of non-const variable 'i' is not allowed in a constant expression}}
71f9c2a591497874769b87bf492a0666cf853e0ae5Kevin Jin  (void)new (int[i]); // expected-warning {{when type is in parentheses}}
72f9c2a591497874769b87bf492a0666cf853e0ae5Kevin Jin  (void)new int(*(S*)0); // expected-error {{no viable conversion from 'S' to 'int'}}
7321a0001e2426644dd68e6140b5873ebaeafcc3dcKevin Jin  (void)new int(1, 2); // expected-error {{excess elements in scalar initializer}}
7421a0001e2426644dd68e6140b5873ebaeafcc3dcKevin Jin  (void)new S(1); // expected-error {{no matching constructor}}
7521a0001e2426644dd68e6140b5873ebaeafcc3dcKevin Jin  (void)new S(1, 1); // expected-error {{call to constructor of 'S' is ambiguous}}
7621a0001e2426644dd68e6140b5873ebaeafcc3dcKevin Jin  (void)new const int; // expected-error {{default initialization of an object of const type 'const int'}}
7721a0001e2426644dd68e6140b5873ebaeafcc3dcKevin Jin  (void)new float*(ip); // expected-error {{cannot initialize a new value of type 'float *' with an lvalue of type 'int *'}}
7821a0001e2426644dd68e6140b5873ebaeafcc3dcKevin Jin  // Undefined, but clang should reject it directly.
79f9c2a591497874769b87bf492a0666cf853e0ae5Kevin Jin  (void)new int[-1]; // expected-error {{array size is negative}}
80f9c2a591497874769b87bf492a0666cf853e0ae5Kevin Jin  (void)new int[2000000000]; // expected-error {{array is too large}}
8121a0001e2426644dd68e6140b5873ebaeafcc3dcKevin Jin  (void)new int[*(S*)0]; // expected-error {{array size expression must have integral or enumeration type, not 'S'}}
8221a0001e2426644dd68e6140b5873ebaeafcc3dcKevin Jin  (void)::S::new int; // expected-error {{expected unqualified-id}}
8321a0001e2426644dd68e6140b5873ebaeafcc3dcKevin Jin  (void)new (0, 0) int; // expected-error {{no matching function for call to 'operator new'}}
8421a0001e2426644dd68e6140b5873ebaeafcc3dcKevin Jin  (void)new (0L) int; // expected-error {{call to 'operator new' is ambiguous}}
8521a0001e2426644dd68e6140b5873ebaeafcc3dcKevin Jin  // This must fail, because the member version shouldn't be found.
868e610ed585685c55e2cfd010b4233eafc7d568c2Thanh Le  (void)::new ((S*)0) U; // expected-error {{no matching function for call to 'operator new'}}
8721a0001e2426644dd68e6140b5873ebaeafcc3dcKevin Jin  // This must fail, because any member version hides all global versions.
8821a0001e2426644dd68e6140b5873ebaeafcc3dcKevin Jin  (void)new U; // expected-error {{no matching function for call to 'operator new'}}
8921a0001e2426644dd68e6140b5873ebaeafcc3dcKevin Jin  (void)new (int[]); // expected-error {{array size must be specified in new expressions}}
9021a0001e2426644dd68e6140b5873ebaeafcc3dcKevin Jin  (void)new int&; // expected-error {{cannot allocate reference type 'int &' with new}}
918e610ed585685c55e2cfd010b4233eafc7d568c2Thanh Le  // Some lacking cases due to lack of sema support.
92}
93
94void good_deletes()
95{
96  delete (int*)0;
97  delete [](int*)0;
98  delete (S*)0;
99  ::delete (int*)0;
100}
101
102void bad_deletes()
103{
104  delete 0; // expected-error {{cannot delete expression of type 'int'}}
105  delete [0] (int*)0; // expected-error {{expected expression}}
106  delete (void*)0; // expected-warning {{cannot delete expression with pointer-to-'void' type 'void *'}}
107  delete (T*)0; // expected-warning {{deleting pointer to incomplete type}}
108  ::S::delete (int*)0; // expected-error {{expected unqualified-id}}
109}
110
111struct X0 { };
112
113struct X1 {
114  operator int*();
115  operator float();
116};
117
118struct X2 {
119  operator int*(); // expected-note {{candidate function}}
120  operator float*(); // expected-note {{candidate function}}
121};
122
123void test_delete_conv(X0 x0, X1 x1, X2 x2) {
124  delete x0; // expected-error{{cannot delete}}
125  delete x1;
126  delete x2; // expected-error{{ambiguous conversion of delete expression of type 'X2' to a pointer}}
127}
128
129// PR4782
130class X3 {
131public:
132  static void operator delete(void * mem, size_t size);
133};
134
135class X4 {
136public:
137  static void release(X3 *x);
138  static void operator delete(void * mem, size_t size);
139};
140
141
142void X4::release(X3 *x) {
143  delete x;
144}
145
146class X5 {
147public:
148  void Destroy() const { delete this; }
149};
150
151class Base {
152public:
153  static void *operator new(signed char) throw(); // expected-error {{'operator new' takes type size_t}}
154  static int operator new[] (size_t) throw(); // expected-error {{operator new[]' must return type 'void *'}}
155};
156
157class Tier {};
158class Comp : public Tier {};
159
160class Thai : public Base {
161public:
162  Thai(const Tier *adoptDictionary);
163};
164
165void loadEngineFor() {
166  const Comp *dict;
167  new Thai(dict);
168}
169
170template <class T> struct TBase {
171  void* operator new(T size, int); // expected-error {{'operator new' cannot take a dependent type as first parameter; use size_t}}
172};
173
174TBase<int> t1;
175
176class X6 {
177public:
178  static void operator delete(void*, int); // expected-note {{member found by ambiguous name lookup}}
179};
180
181class X7 {
182public:
183  static void operator delete(void*, int); // expected-note {{member found by ambiguous name lookup}}
184};
185
186class X8 : public X6, public X7 {
187};
188
189void f(X8 *x8) {
190  delete x8; // expected-error {{member 'operator delete' found in multiple base classes of different types}}
191}
192
193class X9 {
194public:
195  static void operator delete(void*, int); // expected-note {{'operator delete' declared here}}
196  static void operator delete(void*, float); // expected-note {{'operator delete' declared here}}
197};
198
199void f(X9 *x9) {
200  delete x9; // expected-error {{no suitable member 'operator delete' in 'X9'}}
201}
202
203struct X10 {
204  virtual ~X10();
205};
206
207struct X11 : X10 { // expected-error {{no suitable member 'operator delete' in 'X11'}}
208  void operator delete(void*, int); // expected-note {{'operator delete' declared here}}
209};
210
211void f() {
212  X11 x11; // expected-note {{implicit default destructor for 'X11' first required here}}
213}
214
215struct X12 {
216  void* operator new(size_t, void*);
217};
218
219struct X13 : X12 {
220  using X12::operator new;
221};
222
223static void* f(void* g)
224{
225    return new (g) X13();
226}
227
228class X14 {
229public:
230  static void operator delete(void*, const size_t);
231};
232
233void f(X14 *x14a, X14 *x14b) {
234  delete x14a;
235}
236
237class X15 {
238private:
239  X15(); // expected-note {{declared private here}}
240  ~X15(); // expected-note {{declared private here}}
241};
242
243void f(X15* x) {
244  new X15(); // expected-error {{calling a private constructor}}
245  delete x; // expected-error {{calling a private destructor}}
246}
247
248namespace PR5918 { // Look for template operator new overloads.
249  struct S { template<typename T> static void* operator new(size_t, T); };
250  void test() {
251    (void)new(0) S;
252  }
253}
254
255namespace Test1 {
256
257void f() {
258  (void)new int[10](1, 2); // expected-error {{array 'new' cannot have initialization arguments}}
259
260  typedef int T[10];
261  (void)new T(1, 2); // expected-error {{array 'new' cannot have initialization arguments}}
262}
263
264template<typename T>
265void g(unsigned i) {
266  (void)new T[1](i); // expected-error {{array 'new' cannot have initialization arguments}}
267}
268
269template<typename T>
270void h(unsigned i) {
271  (void)new T(i); // expected-error {{array 'new' cannot have initialization arguments}}
272}
273template void h<unsigned>(unsigned);
274template void h<unsigned[10]>(unsigned); // expected-note {{in instantiation of function template specialization 'Test1::h<unsigned int [10]>' requested here}}
275
276}
277
278// Don't diagnose access for overload candidates that aren't selected.
279namespace PR7436 {
280struct S1 {
281  void* operator new(size_t);
282  void operator delete(void* p);
283
284private:
285  void* operator new(size_t, void*); // expected-note {{declared private here}}
286  void operator delete(void*, void*);
287};
288class S2 {
289  void* operator new(size_t); // expected-note {{declared private here}}
290  void operator delete(void* p); // expected-note {{declared private here}}
291};
292
293void test(S1* s1, S2* s2) {
294  delete s1;
295  delete s2; // expected-error {{is a private member}}
296  (void)new S1();
297  (void)new (0L) S1(); // expected-error {{is a private member}}
298  (void)new S2(); // expected-error {{is a private member}}
299}
300}
301
302namespace rdar8018245 {
303  struct X0 {
304    static const int value = 17;
305  };
306
307  const int X0::value;
308
309  struct X1 {
310    static int value;
311  };
312
313  int X1::value;
314
315  template<typename T>
316  int *f() {
317    return new (int[T::value]); // expected-warning{{when type is in parentheses, array cannot have dynamic size}}
318  }
319
320  template int *f<X0>();
321  template int *f<X1>(); // expected-note{{in instantiation of}}
322
323}
324
325// <rdar://problem/8248780>
326namespace Instantiate {
327  template<typename T> struct X {
328    operator T*();
329  };
330
331  void f(X<int> &xi) {
332    delete xi;
333  }
334}
335
336namespace PR7810 {
337  struct X {
338    // cv is ignored in arguments
339    static void operator delete(void *const);
340  };
341  struct Y {
342    // cv is ignored in arguments
343    static void operator delete(void *volatile);
344  };
345}
346
347// Don't crash on template delete operators
348namespace TemplateDestructors {
349  struct S {
350    virtual ~S() {}
351
352    void* operator new(const size_t size);
353    template<class T> void* operator new(const size_t, const int, T*);
354    void operator delete(void*, const size_t);
355    template<class T> void operator delete(void*, const size_t, const int, T*);
356  };
357}
358
359namespace DeleteParam {
360  struct X {
361    void operator delete(X*); // expected-error{{first parameter of 'operator delete' must have type 'void *'}}
362  };
363
364  struct Y {
365    void operator delete(void* const);
366  };
367}
368
369// <rdar://problem/8427878>
370// Test that the correct 'operator delete' is selected to pair with
371// the unexpected placement 'operator new'.
372namespace PairedDelete {
373  template <class T> struct A {
374    A();
375    void *operator new(size_t s, double d = 0);
376    void operator delete(void *p, double d);
377    void operator delete(void *p) {
378      T::dealloc(p);
379    }
380  };
381
382  A<int> *test() {
383    return new A<int>();
384  }
385}
386
387namespace PR7702 {
388  void test1() {
389    new DoesNotExist; // expected-error {{unknown type name 'DoesNotExist'}}
390  }
391}
392
393namespace ArrayNewNeedsDtor {
394  struct A { A(); private: ~A(); }; // expected-note {{declared private here}}
395  struct B { B(); A a; }; // expected-error {{field of type 'ArrayNewNeedsDtor::A' has private destructor}}
396  B *test9() {
397    return new B[5]; // expected-note {{implicit default destructor for 'ArrayNewNeedsDtor::B' first required here}}
398  }
399}
400
401namespace DeleteIncompleteClass {
402  struct A; // expected-note {{forward declaration}}
403  extern A x;
404  void f() { delete x; } // expected-error {{deleting incomplete class type}}
405}
406
407namespace DeleteIncompleteClassPointerError {
408  struct A; // expected-note {{forward declaration}}
409  void f(A *x) { 1+delete x; } // expected-warning {{deleting pointer to incomplete type}} \
410                               // expected-error {{invalid operands to binary expression}}
411}
412
413namespace PR10504 {
414  struct A {
415    virtual void foo() = 0;
416  };
417  void f(A *x) { delete x; } // expected-warning {{delete called on 'PR10504::A' that is abstract but has non-virtual destructor}}
418}
419
420struct PlacementArg {};
421inline void *operator new[](size_t, const PlacementArg &) throw () {
422  return 0;
423}
424inline void operator delete[](void *, const PlacementArg &) throw () {
425}
426
427namespace r150682 {
428
429  template <typename X>
430  struct S {
431    struct Inner {};
432    S() { new Inner[1]; }
433  };
434
435  struct T {
436  };
437
438  template<typename X>
439  void tfn() {
440    new (*(PlacementArg*)0) T[1];
441  }
442
443  void fn() {
444    tfn<int>();
445  }
446
447}
448
449namespace P12023 {
450  struct CopyCounter
451  {
452      CopyCounter();
453      CopyCounter(const CopyCounter&);
454  };
455
456  int main()
457  {
458    CopyCounter* f = new CopyCounter[10](CopyCounter()); // expected-error {{cannot have initialization arguments}}
459      return 0;
460  }
461}
462
463namespace PR12061 {
464  template <class C> struct scoped_array {
465    scoped_array(C* p = __null);
466  };
467  template <class Payload> struct Foo {
468    Foo() : a_(new scoped_array<int>[5]) { }
469    scoped_array< scoped_array<int> > a_;
470  };
471  class Bar {};
472  Foo<Bar> x;
473
474  template <class C> struct scoped_array2 {
475    scoped_array2(C* p = __null, C* q = __null);
476  };
477  template <class Payload> struct Foo2 {
478    Foo2() : a_(new scoped_array2<int>[5]) { }
479    scoped_array2< scoped_array2<int> > a_;
480  };
481  class Bar2 {};
482  Foo2<Bar2> x2;
483
484  class MessageLoop {
485  public:
486    explicit MessageLoop(int type = 0);
487  };
488  template <class CookieStoreTestTraits>
489  class CookieStoreTest {
490  protected:
491    CookieStoreTest() {
492      new MessageLoop;
493    }
494  };
495  struct CookieMonsterTestTraits {
496  };
497  class DeferredCookieTaskTest : public CookieStoreTest<CookieMonsterTestTraits>
498  {
499    DeferredCookieTaskTest() {}
500  };
501}
502