new-delete.cpp revision 1070c9f7acc889336be6f80c70dc1b32622cc83d
1d7d5f0223bd30dfd618762349c6209dd1d5ea3e6Daniel Dunbar// RUN: clang-cc -fsyntax-only -verify %s
2c83ed049af2a2ed7ab94b8206fc0fec4da7e26dbDouglas Gregor
3b5a57a69e5fdac6dd9a92be717e279486c4a0128Sebastian Redl#include <stddef.h>
4b5a57a69e5fdac6dd9a92be717e279486c4a0128Sebastian Redl
54c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redlstruct S // expected-note {{candidate}}
64c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl{
74c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl  S(int, int, double); // expected-note {{candidate}}
83cb069213c8502dbb7a67860d40122d869ed8fd6Sebastian Redl  S(double, int); // expected-note 2 {{candidate}}
93cb069213c8502dbb7a67860d40122d869ed8fd6Sebastian Redl  S(float, int); // expected-note 2 {{candidate}}
104c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl};
114ec339f43c0cae2678334850c90926bea10999c7Douglas Gregorstruct T; // expected-note{{forward declaration of 'struct T'}}
12636a7c42d42800f69caadcdea433312fd642a4b3Sebastian Redlstruct U
13636a7c42d42800f69caadcdea433312fd642a4b3Sebastian Redl{
14636a7c42d42800f69caadcdea433312fd642a4b3Sebastian Redl  // A special new, to verify that the global version isn't used.
159afe1308ed19dffc281dca5cfbe521826754980fSebastian Redl  void* operator new(size_t, S*); // expected-note {{candidate}}
16636a7c42d42800f69caadcdea433312fd642a4b3Sebastian Redl};
177f6623914e779e41eb3d85f9a2dc3affea5de1e8Sebastian Redlstruct V : U
187f6623914e779e41eb3d85f9a2dc3affea5de1e8Sebastian Redl{
197f6623914e779e41eb3d85f9a2dc3affea5de1e8Sebastian Redl};
204c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl
213cb069213c8502dbb7a67860d40122d869ed8fd6Sebastian Redlvoid* operator new(size_t); // expected-note 2 {{candidate}}
223cb069213c8502dbb7a67860d40122d869ed8fd6Sebastian Redlvoid* operator new(size_t, int*); // expected-note 3 {{candidate}}
233cb069213c8502dbb7a67860d40122d869ed8fd6Sebastian Redlvoid* operator new(size_t, float*); // expected-note 3 {{candidate}}
24fc27d268cb34cbb8d186c6ad7cc043d41581ce71Anders Carlssonvoid* operator new(size_t, S); // expected-note 2 {{candidate}}
25b5a57a69e5fdac6dd9a92be717e279486c4a0128Sebastian Redl
264c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redlvoid good_news()
274c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl{
284c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl  int *pi = new int;
294c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl  float *pf = new (pi) float();
304c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl  pi = new int(1);
314c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl  pi = new int('c');
324c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl  const int *pci = new const int();
334c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl  S *ps = new S(1, 2, 3.4);
34cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl  ps = new (pf) (S)(1, 2, 3.4);
354c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl  S *(*paps)[2] = new S*[*pi][2];
364c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl  ps = new (S[3])(1, 2, 3.4);
374c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl  typedef int ia4[4];
384c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl  ia4 *pai = new (int[3][4]);
39fb4ccd7152723ac6190eb379250cfe7516cfd1b8Sebastian Redl  pi = ::new int;
40636a7c42d42800f69caadcdea433312fd642a4b3Sebastian Redl  U *pu = new (ps) U;
419afe1308ed19dffc281dca5cfbe521826754980fSebastian Redl  // FIXME: Inherited functions are not looked up currently.
427f6623914e779e41eb3d85f9a2dc3affea5de1e8Sebastian Redl  //V *pv = new (ps) V;
43fc27d268cb34cbb8d186c6ad7cc043d41581ce71Anders Carlsson
44fc27d268cb34cbb8d186c6ad7cc043d41581ce71Anders Carlsson  pi = new (S(1.0f, 2)) int;
45ac18b2e3af8d0c5304f74e362a1d4207363e4c94Anders Carlsson
46ac18b2e3af8d0c5304f74e362a1d4207363e4c94Anders Carlsson  (void)new int[true];
474c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl}
484c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl
49e7450f5dbd5bed63b8ef9db86350a8fc3db011e8Douglas Gregorstruct abstract {
50e7450f5dbd5bed63b8ef9db86350a8fc3db011e8Douglas Gregor  virtual ~abstract() = 0;
51e7450f5dbd5bed63b8ef9db86350a8fc3db011e8Douglas Gregor};
52e7450f5dbd5bed63b8ef9db86350a8fc3db011e8Douglas Gregor
53c83ed049af2a2ed7ab94b8206fc0fec4da7e26dbDouglas Gregorvoid bad_news(int *ip)
544c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl{
554c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl  int i = 1;
564c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl  (void)new; // expected-error {{missing type specifier}}
574c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl  (void)new 4; // expected-error {{missing type specifier}}
584c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl  (void)new () int; // expected-error {{expected expression}}
59cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl  (void)new int[1.1]; // expected-error {{array size expression must have integral or enumerated type, not 'double'}}
604c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl  (void)new int[1][i]; // expected-error {{only the first dimension}}
614c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl  (void)new (int[1][i]); // expected-error {{only the first dimension}}
624c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl  (void)new int(*(S*)0); // expected-error {{incompatible type initializing}}
634c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl  (void)new int(1, 2); // expected-error {{initializer of a builtin type can only take one argument}}
644c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl  (void)new S(1); // expected-error {{no matching constructor}}
65c83ed049af2a2ed7ab94b8206fc0fec4da7e26dbDouglas Gregor  (void)new S(1, 1); // expected-error {{call to constructor of 'S' is ambiguous}}
664c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl  (void)new const int; // expected-error {{must provide an initializer}}
67c83ed049af2a2ed7ab94b8206fc0fec4da7e26dbDouglas Gregor  (void)new float*(ip); // expected-error {{incompatible type initializing 'int *', expected 'float *'}}
68cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl  // Undefined, but clang should reject it directly.
69cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl  (void)new int[-1]; // expected-error {{array size is negative}}
70cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl  (void)new int[*(S*)0]; // expected-error {{array size expression must have integral or enumerated type, not 'struct S'}}
71fb4ccd7152723ac6190eb379250cfe7516cfd1b8Sebastian Redl  (void)::S::new int; // expected-error {{expected unqualified-id}}
72b5a57a69e5fdac6dd9a92be717e279486c4a0128Sebastian Redl  (void)new (0, 0) int; // expected-error {{no matching function for call to 'operator new'}}
737f6623914e779e41eb3d85f9a2dc3affea5de1e8Sebastian Redl  (void)new (0L) int; // expected-error {{call to 'operator new' is ambiguous}}
74636a7c42d42800f69caadcdea433312fd642a4b3Sebastian Redl  // This must fail, because the member version shouldn't be found.
75636a7c42d42800f69caadcdea433312fd642a4b3Sebastian Redl  (void)::new ((S*)0) U; // expected-error {{no matching function for call to 'operator new'}}
769afe1308ed19dffc281dca5cfbe521826754980fSebastian Redl  // This must fail, because any member version hides all global versions.
779afe1308ed19dffc281dca5cfbe521826754980fSebastian Redl  (void)new U; // expected-error {{no matching function for call to 'operator new'}}
7800e68e2cc5ce37cb95beb801cae73c0d1e9dda37Sebastian Redl  (void)new (int[]); // expected-error {{array size must be specified in new expressions}}
7900e68e2cc5ce37cb95beb801cae73c0d1e9dda37Sebastian Redl  (void)new int&; // expected-error {{cannot allocate reference type 'int &' with new}}
804c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl  // Some lacking cases due to lack of sema support.
814c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl}
824c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl
834c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redlvoid good_deletes()
844c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl{
854c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl  delete (int*)0;
864c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl  delete [](int*)0;
874c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl  delete (S*)0;
88fb4ccd7152723ac6190eb379250cfe7516cfd1b8Sebastian Redl  ::delete (int*)0;
894c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl}
904c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl
914c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redlvoid bad_deletes()
924c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl{
934c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl  delete 0; // expected-error {{cannot delete expression of type 'int'}}
944c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl  delete [0] (int*)0; // expected-error {{expected ']'}} \
9528eb7e992b9a266abb300da25b6d3c1557cec361Chris Lattner                      // expected-note {{to match this '['}}
964c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl  delete (void*)0; // expected-error {{cannot delete expression}}
974c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl  delete (T*)0; // expected-warning {{deleting pointer to incomplete type}}
98fb4ccd7152723ac6190eb379250cfe7516cfd1b8Sebastian Redl  ::S::delete (int*)0; // expected-error {{expected unqualified-id}}
994c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl}
1009cd9f3f55d22f34f1d69db8bfc2735c4e1e082c3Douglas Gregor
1019cd9f3f55d22f34f1d69db8bfc2735c4e1e082c3Douglas Gregorstruct X0 { };
1029cd9f3f55d22f34f1d69db8bfc2735c4e1e082c3Douglas Gregor
1039cd9f3f55d22f34f1d69db8bfc2735c4e1e082c3Douglas Gregorstruct X1 {
1049cd9f3f55d22f34f1d69db8bfc2735c4e1e082c3Douglas Gregor  operator int*();
1059cd9f3f55d22f34f1d69db8bfc2735c4e1e082c3Douglas Gregor  operator float();
1069cd9f3f55d22f34f1d69db8bfc2735c4e1e082c3Douglas Gregor};
1079cd9f3f55d22f34f1d69db8bfc2735c4e1e082c3Douglas Gregor
1089cd9f3f55d22f34f1d69db8bfc2735c4e1e082c3Douglas Gregorstruct X2 {
109f652793d4d32cc71b5ee2167069cbd363baa75deFariborz Jahanian  operator int*(); // expected-note {{candidate function}}
110f652793d4d32cc71b5ee2167069cbd363baa75deFariborz Jahanian  operator float*(); // expected-note {{candidate function}}
1119cd9f3f55d22f34f1d69db8bfc2735c4e1e082c3Douglas Gregor};
1129cd9f3f55d22f34f1d69db8bfc2735c4e1e082c3Douglas Gregor
1139cd9f3f55d22f34f1d69db8bfc2735c4e1e082c3Douglas Gregorvoid test_delete_conv(X0 x0, X1 x1, X2 x2) {
1149cd9f3f55d22f34f1d69db8bfc2735c4e1e082c3Douglas Gregor  delete x0; // expected-error{{cannot delete}}
1159cd9f3f55d22f34f1d69db8bfc2735c4e1e082c3Douglas Gregor  delete x1;
116f652793d4d32cc71b5ee2167069cbd363baa75deFariborz Jahanian  delete x2; // expected-error{{ambiguous conversion of delete expression of type 'struct X2' to a pointer}}
117f652793d4d32cc71b5ee2167069cbd363baa75deFariborz Jahanian}
1189091656e423f2354e53b2b3baa95b3eb5510badcDouglas Gregor
1199091656e423f2354e53b2b3baa95b3eb5510badcDouglas Gregor// PR4782
1209091656e423f2354e53b2b3baa95b3eb5510badcDouglas Gregorclass X3 {
1219091656e423f2354e53b2b3baa95b3eb5510badcDouglas Gregorpublic:
1229091656e423f2354e53b2b3baa95b3eb5510badcDouglas Gregor  static void operator delete(void * mem, unsigned long size);
1239091656e423f2354e53b2b3baa95b3eb5510badcDouglas Gregor};
1249091656e423f2354e53b2b3baa95b3eb5510badcDouglas Gregor
1259091656e423f2354e53b2b3baa95b3eb5510badcDouglas Gregorclass X4 {
1269091656e423f2354e53b2b3baa95b3eb5510badcDouglas Gregorpublic:
1279091656e423f2354e53b2b3baa95b3eb5510badcDouglas Gregor  static void release(X3 *x);
1289091656e423f2354e53b2b3baa95b3eb5510badcDouglas Gregor  static void operator delete(void * mem, unsigned long size);
1299091656e423f2354e53b2b3baa95b3eb5510badcDouglas Gregor};
1309091656e423f2354e53b2b3baa95b3eb5510badcDouglas Gregor
1319091656e423f2354e53b2b3baa95b3eb5510badcDouglas Gregor
1329091656e423f2354e53b2b3baa95b3eb5510badcDouglas Gregorvoid X4::release(X3 *x) {
1339091656e423f2354e53b2b3baa95b3eb5510badcDouglas Gregor  delete x;
1349091656e423f2354e53b2b3baa95b3eb5510badcDouglas Gregor}
1351070c9f7acc889336be6f80c70dc1b32622cc83dDouglas Gregor
1361070c9f7acc889336be6f80c70dc1b32622cc83dDouglas Gregorclass X6 {
1371070c9f7acc889336be6f80c70dc1b32622cc83dDouglas Gregorpublic:
1381070c9f7acc889336be6f80c70dc1b32622cc83dDouglas Gregor  void Destroy() const { delete this; }
1391070c9f7acc889336be6f80c70dc1b32622cc83dDouglas Gregor};
140