Run DCE after a LoopFlatten test to reduce spurious output [nfc]
[llvm-project.git] / clang / test / SemaCXX / new-delete.cpp
blob0270e42b7389fec1984c003a6ac93690c578c9b9
1 // RUN: %clang_cc1 -fsyntax-only -verify %s -triple=i686-pc-linux-gnu -Wno-new-returns-null -std=c++98
2 // RUN: %clang_cc1 -fsyntax-only -verify %s -triple=i686-pc-linux-gnu -Wno-new-returns-null -std=c++11
3 // RUN: %clang_cc1 -fsyntax-only -verify %s -triple=i686-pc-linux-gnu -Wno-new-returns-null -std=c++14
4 // RUN: %clang_cc1 -fsyntax-only -verify=expected,cxx17 %s -triple=i686-pc-linux-gnu -Wno-new-returns-null %std_cxx17-
6 // FIXME Location is (frontend)
7 // cxx17-note@*:* {{candidate function not viable: requires 2 arguments, but 3 were provided}}
9 #include <stddef.h>
11 #if __cplusplus >= 201103L
12 // expected-note@+2 {{candidate constructor (the implicit move constructor) not viable}}
13 #endif
14 struct S // expected-note {{candidate}}
16 S(int, int, double); // expected-note {{candidate}}
17 S(double, int); // expected-note 2 {{candidate}}
18 S(float, int); // expected-note 2 {{candidate}}
20 struct T; // expected-note{{forward declaration of 'T'}}
21 struct U
23 // A special new, to verify that the global version isn't used.
24 void* operator new(size_t, S*); // expected-note {{candidate}}
26 struct V : U
30 inline void operator delete(void *); // expected-warning {{replacement function 'operator delete' cannot be declared 'inline'}}
32 __attribute__((used))
33 inline void *operator new(size_t) { // no warning, due to __attribute__((used))
34 return 0; // expected-warning {{null returned from function that requires a non-null return value}}
37 // PR5823
38 void* operator new(const size_t); // expected-note {{candidate}}
39 void* operator new(size_t, int*); // expected-note 2{{candidate}}
40 void* operator new(size_t, float*); // expected-note 2{{candidate}}
41 void* operator new(size_t, S); // expected-note {{candidate}}
43 struct foo { };
45 void good_news()
47 int *pi = new int;
48 float *pf = new (pi) float();
49 pi = new int(1);
50 pi = new int('c');
51 const int *pci = new const int();
52 S *ps = new S(1, 2, 3.4);
53 ps = new (pf) (S)(1, 2, 3.4);
54 S *(*paps)[2] = new S*[*pi][2];
55 typedef int ia4[4];
56 ia4 *pai = new (int[3][4]);
57 pi = ::new int;
58 U *pu = new (ps) U;
59 V *pv = new (ps) V;
61 pi = new (S(1.0f, 2)) int;
63 (void)new int[true];
65 // PR7147
66 typedef int a[2];
67 foo* f1 = new foo;
68 foo* f2 = new foo[2];
69 typedef foo x[2];
70 typedef foo y[2][2];
71 x* f3 = new y;
73 #if __cplusplus >= 201103L
74 (void)new int[]{};
75 (void)new int[]{1, 2, 3};
76 (void)new char[]{"hello"};
77 #endif
80 struct abstract {
81 virtual ~abstract() = 0;
84 void bad_news(int *ip)
86 int i = 1; // expected-note 2{{here}}
87 (void)new; // expected-error {{expected a type}}
88 (void)new 4; // expected-error {{expected a type}}
89 (void)new () int; // expected-error {{expected expression}}
90 (void)new int[1.1];
91 #if __cplusplus <= 199711L
92 // expected-error@-2 {{array size expression must have integral or enumeration type, not 'double'}}
93 #elif __cplusplus <= 201103L
94 // expected-error@-4 {{array size expression must have integral or unscoped enumeration type, not 'double'}}
95 #else
96 // expected-warning@-6 {{implicit conversion from 'double' to 'unsigned int' changes value from 1.1 to 1}}
97 #endif
99 (void)new int[1][i]; // expected-note {{read of non-const variable 'i' is not allowed in a constant expression}}
100 (void)new (int[1][i]); // expected-note {{read of non-const variable 'i' is not allowed in a constant expression}}
101 #if __cplusplus <= 201103L
102 // expected-error@-3 {{only the first dimension}}
103 // expected-error@-3 {{only the first dimension}}
104 #else
105 // expected-error@-6 {{array size is not a constant expression}}
106 // expected-error@-6 {{array size is not a constant expression}}
107 #endif
108 (void)new (int[i]); // expected-warning {{when type is in parentheses}}
109 (void)new int(*(S*)0); // expected-error {{no viable conversion from 'S' to 'int'}}
110 (void)new int(1, 2); // expected-error {{excess elements in scalar initializer}}
111 (void)new S(1); // expected-error {{no matching constructor}}
112 (void)new S(1, 1); // expected-error {{call to constructor of 'S' is ambiguous}}
113 (void)new const int; // expected-error {{default initialization of an object of const type 'const int'}}
114 (void)new float*(ip); // expected-error {{cannot initialize a new value of type 'float *' with an lvalue of type 'int *'}}
115 // Undefined, but clang should reject it directly.
116 (void)new int[-1];
117 #if __cplusplus <= 201103L
118 // expected-error@-2 {{array size is negative}}
119 #else
120 // expected-error@-4 {{array is too large}}
121 #endif
122 (void)new int[2000000000]; // expected-error {{array is too large}}
123 (void)new int[*(S*)0];
124 #if __cplusplus <= 199711L
125 // expected-error@-2 {{array size expression must have integral or enumeration type, not 'S'}}
126 #elif __cplusplus <= 201103L
127 // expected-error@-4 {{array size expression must have integral or unscoped enumeration type, not 'S'}}
128 #else
129 // expected-error@-6 {{converting 'S' to incompatible type}}
130 #endif
132 (void)::S::new int; // expected-error {{expected unqualified-id}}
133 (void)new (0, 0) int; // expected-error {{no matching function for call to 'operator new'}}
134 (void)new (0L) int; // expected-error {{call to 'operator new' is ambiguous}}
135 // This must fail, because the member version shouldn't be found.
136 (void)::new ((S*)0) U; // expected-error {{no matching 'operator new' function for non-allocating placement new expression; include <new>}}
137 // This must fail, because any member version hides all global versions.
138 (void)new U; // expected-error {{no matching function for call to 'operator new'}}
139 (void)new (int[]); // expected-error {{array size must be specified in new expression with no initializer}}
140 (void)new int&; // expected-error {{cannot allocate reference type 'int &' with new}}
141 (void)new int[]; // expected-error {{array size must be specified in new expression with no initializer}}
142 (void)new int[](); // expected-error {{cannot determine allocated array size from initializer}}
143 // FIXME: This is a terrible diagnostic.
144 #if __cplusplus < 201103L
145 (void)new int[]{}; // expected-error {{array size must be specified in new expression with no initializer}}
146 #endif
149 void no_matching_placement_new() {
150 struct X { int n; };
151 __attribute__((aligned(__alignof(X)))) unsigned char buffer[sizeof(X)];
152 (void)new(buffer) X; // expected-error {{no matching 'operator new' function for non-allocating placement new expression; include <new>}}
153 (void)new(+buffer) X; // expected-error {{no matching 'operator new' function for non-allocating placement new expression; include <new>}}
154 (void)new(&buffer) X; // expected-error {{no matching 'operator new' function for non-allocating placement new expression; include <new>}}
157 void good_deletes()
159 delete (int*)0;
160 delete [](int*)0;
161 delete (S*)0;
162 ::delete (int*)0;
165 void bad_deletes()
167 delete 0; // expected-error {{cannot delete expression of type 'int'}}
168 delete [0] (int*)0;
169 #if __cplusplus <= 199711L
170 // expected-error@-2 {{expected expression}}
171 #else
172 // expected-error@-4 {{expected variable name or 'this' in lambda capture list}}
173 #endif
174 delete (void*)0; // expected-warning {{cannot delete expression with pointer-to-'void' type 'void *'}}
175 delete (T*)0; // expected-warning {{deleting pointer to incomplete type}}
176 ::S::delete (int*)0; // expected-error {{expected unqualified-id}}
179 struct X0 { };
181 struct X1 {
182 operator int*();
183 operator float();
186 struct X2 {
187 operator int*(); // expected-note {{conversion}}
188 operator float*(); // expected-note {{conversion}}
191 void test_delete_conv(X0 x0, X1 x1, X2 x2) {
192 delete x0; // expected-error{{cannot delete}}
193 delete x1;
194 delete x2; // expected-error{{ambiguous conversion of delete expression of type 'X2' to a pointer}}
197 // PR4782
198 class X3 {
199 public:
200 static void operator delete(void * mem, size_t size);
203 class X4 {
204 public:
205 static void release(X3 *x);
206 static void operator delete(void * mem, size_t size);
210 void X4::release(X3 *x) {
211 delete x;
214 class X5 {
215 public:
216 void Destroy() const { delete this; }
219 class Base {
220 public:
221 static void *operator new(signed char) throw(); // expected-error {{'operator new' takes type size_t}}
222 static int operator new[] (size_t) throw(); // expected-error {{operator new[]' must return type 'void *'}}
225 class Tier {};
226 class Comp : public Tier {};
228 class Thai : public Base {
229 public:
230 Thai(const Tier *adoptDictionary);
233 void loadEngineFor() {
234 const Comp *dict;
235 new Thai(dict);
238 template <class T> struct TBase {
239 void* operator new(T size, int); // expected-error {{'operator new' cannot take a dependent type as first parameter; use size_t}}
242 TBase<int> t1;
244 class X6 {
245 public:
246 static void operator delete(void*, int); // expected-note {{member found by ambiguous name lookup}}
249 class X7 {
250 public:
251 static void operator delete(void*, int); // expected-note {{member found by ambiguous name lookup}}
254 class X8 : public X6, public X7 {
257 void f(X8 *x8) {
258 delete x8; // expected-error {{member 'operator delete' found in multiple base classes of different types}}
261 class X9 {
262 public:
263 static void operator delete(void*, int); // expected-note {{'operator delete' declared here}}
264 static void operator delete(void*, float); // expected-note {{'operator delete' declared here}}
267 void f(X9 *x9) {
268 delete x9; // expected-error {{no suitable member 'operator delete' in 'X9'}}
271 struct X10 {
272 virtual ~X10();
273 #if __cplusplus >= 201103L
274 // expected-note@-2 {{overridden virtual function is here}}
275 #endif
278 struct X11 : X10 {
279 #if __cplusplus <= 199711L
280 // expected-error@-2 {{no suitable member 'operator delete' in 'X11'}}
281 #else
282 // expected-error@-4 {{deleted function '~X11' cannot override a non-deleted function}}
283 // expected-note@-5 2 {{virtual destructor requires an unambiguous, accessible 'operator delete'}}
284 #endif
285 void operator delete(void*, int);
286 #if __cplusplus <= 199711L
287 // expected-note@-2 {{'operator delete' declared here}}
288 #endif
291 void f() {
292 X11 x11;
293 #if __cplusplus <= 199711L
294 // expected-note@-2 {{implicit destructor for 'X11' first required here}}
295 #else
296 // expected-error@-4 {{attempt to use a deleted function}}
297 #endif
300 struct X12 {
301 void* operator new(size_t, void*);
304 struct X13 : X12 {
305 using X12::operator new;
308 static void* f(void* g)
310 return new (g) X13();
313 class X14 {
314 public:
315 static void operator delete(void*, const size_t);
318 void f(X14 *x14a, X14 *x14b) {
319 delete x14a;
322 class X15 {
323 private:
324 X15(); // expected-note {{declared private here}}
325 ~X15(); // expected-note {{declared private here}}
328 void f(X15* x) {
329 new X15(); // expected-error {{calling a private constructor}}
330 delete x; // expected-error {{calling a private destructor}}
333 namespace PR5918 { // Look for template operator new overloads.
334 struct S { template<typename T> static void* operator new(size_t, T); };
335 void test() {
336 (void)new(0) S;
340 namespace Test1 {
342 void f() {
343 (void)new int[10](1, 2); // expected-error {{array 'new' cannot have initialization arguments}}
345 typedef int T[10];
346 (void)new T(1, 2); // expected-error {{array 'new' cannot have initialization arguments}}
349 template<typename T>
350 void g(unsigned i) {
351 (void)new T[1](i); // expected-error {{array 'new' cannot have initialization arguments}}
354 template<typename T>
355 void h(unsigned i) {
356 (void)new T(i); // expected-error {{array 'new' cannot have initialization arguments}}
358 template void h<unsigned>(unsigned);
359 template void h<unsigned[10]>(unsigned); // expected-note {{in instantiation of function template specialization 'Test1::h<unsigned int[10]>' requested here}}
363 // Don't diagnose access for overload candidates that aren't selected.
364 namespace PR7436 {
365 struct S1 {
366 void* operator new(size_t);
367 void operator delete(void* p);
369 private:
370 void* operator new(size_t, void*); // expected-note {{declared private here}}
371 void operator delete(void*, void*);
373 class S2 {
374 void* operator new(size_t); // expected-note {{declared private here}}
375 void operator delete(void* p); // expected-note {{declared private here}}
378 void test(S1* s1, S2* s2) {
379 delete s1;
380 delete s2; // expected-error {{is a private member}}
381 (void)new S1();
382 (void)new (0L) S1(); // expected-error {{is a private member}}
383 (void)new S2(); // expected-error {{is a private member}}
387 namespace rdar8018245 {
388 struct X0 {
389 static const int value = 17;
392 const int X0::value;
394 struct X1 {
395 static int value;
398 int X1::value;
400 template<typename T>
401 int *f() {
402 return new (int[T::value]); // expected-warning{{when type is in parentheses, array cannot have dynamic size}}
405 template int *f<X0>();
406 template int *f<X1>(); // expected-note{{in instantiation of}}
410 namespace Instantiate {
411 template<typename T> struct X {
412 operator T*();
415 void f(X<int> &xi) {
416 delete xi;
420 namespace PR7810 {
421 struct X {
422 // cv is ignored in arguments
423 static void operator delete(void *const);
425 struct Y {
426 // cv is ignored in arguments
427 #if __cplusplus < 202002L
428 static void operator delete(void *volatile);
429 #else
430 static void operator delete(void *);
431 #endif
435 // Don't crash on template delete operators
436 namespace TemplateDestructors {
437 struct S {
438 virtual ~S() {}
440 void* operator new(const size_t size);
441 template<class T> void* operator new(const size_t, const int, T*);
442 void operator delete(void*, const size_t);
443 template<class T> void operator delete(void*, const size_t, const int, T*);
447 namespace DeleteParam {
448 struct X {
449 void operator delete(X*); // expected-error{{first parameter of 'operator delete' must have type 'void *'}}
452 struct Y {
453 void operator delete(void* const);
457 // Test that the correct 'operator delete' is selected to pair with
458 // the unexpected placement 'operator new'.
459 namespace PairedDelete {
460 template <class T> struct A {
461 A();
462 void *operator new(size_t s, double d = 0);
463 void operator delete(void *p, double d);
464 void operator delete(void *p) {
465 T::dealloc(p);
469 A<int> *test() {
470 return new A<int>();
474 namespace PR7702 {
475 void test1() {
476 new DoesNotExist; // expected-error {{unknown type name 'DoesNotExist'}}
480 namespace ArrayNewNeedsDtor {
481 struct A { A(); private: ~A(); };
482 #if __cplusplus <= 199711L
483 // expected-note@-2 {{declared private here}}
484 #endif
485 struct B { B(); A a; };
486 #if __cplusplus <= 199711L
487 // expected-error@-2 {{field of type 'A' has private destructor}}
488 #else
489 // expected-note@-4 {{destructor of 'B' is implicitly deleted because field 'a' has an inaccessible destructor}}
490 #endif
492 B *test9() {
493 return new B[5];
494 #if __cplusplus <= 199711L
495 // expected-note@-2 {{implicit destructor for 'ArrayNewNeedsDtor::B' first required here}}
496 #else
497 // expected-error@-4 {{attempt to use a deleted function}}
498 #endif
502 namespace DeleteIncompleteClass {
503 struct A; // expected-note {{forward declaration}}
504 extern A x;
505 void f() { delete x; } // expected-error {{deleting incomplete class type}}
508 namespace DeleteIncompleteClassPointerError {
509 struct A; // expected-note {{forward declaration}}
510 void f(A *x) { 1+delete x; } // expected-warning {{deleting pointer to incomplete type}} \
511 // expected-error {{invalid operands to binary expression}}
514 namespace PR10504 {
515 struct A {
516 virtual void foo() = 0;
518 void f(A *x) { delete x; } // expected-warning {{delete called on 'PR10504::A' that is abstract but has non-virtual destructor}}
521 struct PlacementArg {};
522 inline void *operator new[](size_t, const PlacementArg &) throw () {
523 return 0;
525 inline void operator delete[](void *, const PlacementArg &) throw () {
528 namespace r150682 {
530 template <typename X>
531 struct S {
532 struct Inner {};
533 S() { new Inner[1]; }
536 struct T {
539 template<typename X>
540 void tfn() {
541 new (*(PlacementArg*)0) T[1]; // expected-warning 2 {{binding dereferenced null pointer to reference has undefined behavior}}
544 void fn() {
545 tfn<int>(); // expected-note {{in instantiation of function template specialization 'r150682::tfn<int>' requested here}}
550 namespace P12023 {
551 struct CopyCounter
553 CopyCounter();
554 CopyCounter(const CopyCounter&);
557 int main()
559 CopyCounter* f = new CopyCounter[10](CopyCounter()); // expected-error {{cannot have initialization arguments}}
560 return 0;
564 namespace PR12061 {
565 template <class C> struct scoped_array {
566 scoped_array(C* p = __null);
568 template <class Payload> struct Foo {
569 Foo() : a_(new scoped_array<int>[5]) { }
570 scoped_array< scoped_array<int> > a_;
572 class Bar {};
573 Foo<Bar> x;
575 template <class C> struct scoped_array2 {
576 scoped_array2(C* p = __null, C* q = __null);
578 template <class Payload> struct Foo2 {
579 Foo2() : a_(new scoped_array2<int>[5]) { }
580 scoped_array2< scoped_array2<int> > a_;
582 class Bar2 {};
583 Foo2<Bar2> x2;
585 class MessageLoop {
586 public:
587 explicit MessageLoop(int type = 0);
589 template <class CookieStoreTestTraits>
590 class CookieStoreTest {
591 protected:
592 CookieStoreTest() {
593 new MessageLoop;
596 struct CookieMonsterTestTraits {
598 class DeferredCookieTaskTest : public CookieStoreTest<CookieMonsterTestTraits>
600 DeferredCookieTaskTest() {}
604 class DeletingPlaceholder {
605 int* f() {
606 delete f; // expected-error {{reference to non-static member function must be called; did you mean to call it with no arguments?}}
607 return 0;
609 int* g(int, int) {
610 delete g; // expected-error {{reference to non-static member function must be called}}
611 return 0;
615 namespace PR18544 {
616 inline void *operator new(size_t); // expected-error {{'operator new' cannot be declared inside a namespace}}
619 // PR19968
620 inline void* operator new(); // expected-error {{'operator new' must have at least one parameter}}
622 namespace {
623 template <class C>
624 struct A {
625 void f() { this->::new; } // expected-error {{expected unqualified-id}}
626 void g() { this->::delete; } // expected-error {{expected unqualified-id}}
630 #if __cplusplus >= 201103L
631 template<typename ...T> int *dependent_array_size(T ...v) {
632 return new int[]{v...}; // expected-error {{cannot initialize}}
634 int *p0 = dependent_array_size();
635 int *p3 = dependent_array_size(1, 2, 3);
636 int *fail = dependent_array_size("hello"); // expected-note {{instantiation of}}
637 #endif
639 // FIXME: Our behavior here is incredibly inconsistent. GCC allows
640 // constant-folding in array bounds in new-expressions.
641 int (*const_fold)[12] = new int[3][&const_fold + 12 - &const_fold];
642 #if __cplusplus >= 201402L
643 // expected-error@-2 {{array size is not a constant expression}}
644 // expected-note@-3 {{cannot refer to element 12 of non-array}}
645 #elif __cplusplus < 201103L
646 // expected-error@-5 {{cannot allocate object of variably modified type}}
647 // expected-warning@-6 {{variable length arrays in C++ are a Clang extension}}
648 #endif