new-delete.cpp revision 9afe1308ed19dffc281dca5cfbe521826754980f
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}}
24b5a57a69e5fdac6dd9a92be717e279486c4a0128Sebastian Redl
254c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redlvoid good_news()
264c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl{
274c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl  int *pi = new int;
284c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl  float *pf = new (pi) float();
294c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl  pi = new int(1);
304c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl  pi = new int('c');
314c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl  const int *pci = new const int();
324c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl  S *ps = new S(1, 2, 3.4);
33cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl  ps = new (pf) (S)(1, 2, 3.4);
344c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl  S *(*paps)[2] = new S*[*pi][2];
354c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl  ps = new (S[3])(1, 2, 3.4);
364c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl  typedef int ia4[4];
374c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl  ia4 *pai = new (int[3][4]);
38fb4ccd7152723ac6190eb379250cfe7516cfd1b8Sebastian Redl  pi = ::new int;
39636a7c42d42800f69caadcdea433312fd642a4b3Sebastian Redl  U *pu = new (ps) U;
409afe1308ed19dffc281dca5cfbe521826754980fSebastian Redl  // FIXME: Inherited functions are not looked up currently.
417f6623914e779e41eb3d85f9a2dc3affea5de1e8Sebastian Redl  //V *pv = new (ps) V;
424c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl}
434c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl
44e7450f5dbd5bed63b8ef9db86350a8fc3db011e8Douglas Gregorstruct abstract {
45e7450f5dbd5bed63b8ef9db86350a8fc3db011e8Douglas Gregor  virtual ~abstract() = 0;
46e7450f5dbd5bed63b8ef9db86350a8fc3db011e8Douglas Gregor};
47e7450f5dbd5bed63b8ef9db86350a8fc3db011e8Douglas Gregor
48c83ed049af2a2ed7ab94b8206fc0fec4da7e26dbDouglas Gregorvoid bad_news(int *ip)
494c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl{
504c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl  int i = 1;
514c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl  (void)new; // expected-error {{missing type specifier}}
524c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl  (void)new 4; // expected-error {{missing type specifier}}
534c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl  (void)new () int; // expected-error {{expected expression}}
54cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl  (void)new int[1.1]; // expected-error {{array size expression must have integral or enumerated type, not 'double'}}
554c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl  (void)new int[1][i]; // expected-error {{only the first dimension}}
564c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl  (void)new (int[1][i]); // expected-error {{only the first dimension}}
574c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl  (void)new int(*(S*)0); // expected-error {{incompatible type initializing}}
584c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl  (void)new int(1, 2); // expected-error {{initializer of a builtin type can only take one argument}}
594c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl  (void)new S(1); // expected-error {{no matching constructor}}
60c83ed049af2a2ed7ab94b8206fc0fec4da7e26dbDouglas Gregor  (void)new S(1, 1); // expected-error {{call to constructor of 'S' is ambiguous}}
614c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl  (void)new const int; // expected-error {{must provide an initializer}}
62c83ed049af2a2ed7ab94b8206fc0fec4da7e26dbDouglas Gregor  (void)new float*(ip); // expected-error {{incompatible type initializing 'int *', expected 'float *'}}
63cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl  // Undefined, but clang should reject it directly.
64cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl  (void)new int[-1]; // expected-error {{array size is negative}}
65cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl  (void)new int[*(S*)0]; // expected-error {{array size expression must have integral or enumerated type, not 'struct S'}}
66fb4ccd7152723ac6190eb379250cfe7516cfd1b8Sebastian Redl  (void)::S::new int; // expected-error {{expected unqualified-id}}
67b5a57a69e5fdac6dd9a92be717e279486c4a0128Sebastian Redl  (void)new (0, 0) int; // expected-error {{no matching function for call to 'operator new'}}
687f6623914e779e41eb3d85f9a2dc3affea5de1e8Sebastian Redl  (void)new (0L) int; // expected-error {{call to 'operator new' is ambiguous}}
69636a7c42d42800f69caadcdea433312fd642a4b3Sebastian Redl  // This must fail, because the member version shouldn't be found.
70636a7c42d42800f69caadcdea433312fd642a4b3Sebastian Redl  (void)::new ((S*)0) U; // expected-error {{no matching function for call to 'operator new'}}
719afe1308ed19dffc281dca5cfbe521826754980fSebastian Redl  // This must fail, because any member version hides all global versions.
729afe1308ed19dffc281dca5cfbe521826754980fSebastian Redl  (void)new U; // expected-error {{no matching function for call to 'operator new'}}
7300e68e2cc5ce37cb95beb801cae73c0d1e9dda37Sebastian Redl  (void)new (int[]); // expected-error {{array size must be specified in new expressions}}
7400e68e2cc5ce37cb95beb801cae73c0d1e9dda37Sebastian Redl  (void)new int&; // expected-error {{cannot allocate reference type 'int &' with new}}
754c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl  // Some lacking cases due to lack of sema support.
764c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl}
774c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl
784c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redlvoid good_deletes()
794c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl{
804c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl  delete (int*)0;
814c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl  delete [](int*)0;
824c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl  delete (S*)0;
83fb4ccd7152723ac6190eb379250cfe7516cfd1b8Sebastian Redl  ::delete (int*)0;
844c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl}
854c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl
864c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redlvoid bad_deletes()
874c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl{
884c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl  delete 0; // expected-error {{cannot delete expression of type 'int'}}
894c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl  delete [0] (int*)0; // expected-error {{expected ']'}} \
9028eb7e992b9a266abb300da25b6d3c1557cec361Chris Lattner                      // expected-note {{to match this '['}}
914c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl  delete (void*)0; // expected-error {{cannot delete expression}}
924c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl  delete (T*)0; // expected-warning {{deleting pointer to incomplete type}}
93fb4ccd7152723ac6190eb379250cfe7516cfd1b8Sebastian Redl  ::S::delete (int*)0; // expected-error {{expected unqualified-id}}
944c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl}
95