new-delete.cpp revision fc27d268cb34cbb8d186c6ad7cc043d41581ce71
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;
454c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl}
464c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl
47e7450f5dbd5bed63b8ef9db86350a8fc3db011e8Douglas Gregorstruct abstract {
48e7450f5dbd5bed63b8ef9db86350a8fc3db011e8Douglas Gregor  virtual ~abstract() = 0;
49e7450f5dbd5bed63b8ef9db86350a8fc3db011e8Douglas Gregor};
50e7450f5dbd5bed63b8ef9db86350a8fc3db011e8Douglas Gregor
51c83ed049af2a2ed7ab94b8206fc0fec4da7e26dbDouglas Gregorvoid bad_news(int *ip)
524c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl{
534c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl  int i = 1;
544c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl  (void)new; // expected-error {{missing type specifier}}
554c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl  (void)new 4; // expected-error {{missing type specifier}}
564c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl  (void)new () int; // expected-error {{expected expression}}
57cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl  (void)new int[1.1]; // expected-error {{array size expression must have integral or enumerated type, not 'double'}}
584c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl  (void)new int[1][i]; // expected-error {{only the first dimension}}
594c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl  (void)new (int[1][i]); // expected-error {{only the first dimension}}
604c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl  (void)new int(*(S*)0); // expected-error {{incompatible type initializing}}
614c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl  (void)new int(1, 2); // expected-error {{initializer of a builtin type can only take one argument}}
624c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl  (void)new S(1); // expected-error {{no matching constructor}}
63c83ed049af2a2ed7ab94b8206fc0fec4da7e26dbDouglas Gregor  (void)new S(1, 1); // expected-error {{call to constructor of 'S' is ambiguous}}
644c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl  (void)new const int; // expected-error {{must provide an initializer}}
65c83ed049af2a2ed7ab94b8206fc0fec4da7e26dbDouglas Gregor  (void)new float*(ip); // expected-error {{incompatible type initializing 'int *', expected 'float *'}}
66cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl  // Undefined, but clang should reject it directly.
67cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl  (void)new int[-1]; // expected-error {{array size is negative}}
68cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl  (void)new int[*(S*)0]; // expected-error {{array size expression must have integral or enumerated type, not 'struct S'}}
69fb4ccd7152723ac6190eb379250cfe7516cfd1b8Sebastian Redl  (void)::S::new int; // expected-error {{expected unqualified-id}}
70b5a57a69e5fdac6dd9a92be717e279486c4a0128Sebastian Redl  (void)new (0, 0) int; // expected-error {{no matching function for call to 'operator new'}}
717f6623914e779e41eb3d85f9a2dc3affea5de1e8Sebastian Redl  (void)new (0L) int; // expected-error {{call to 'operator new' is ambiguous}}
72636a7c42d42800f69caadcdea433312fd642a4b3Sebastian Redl  // This must fail, because the member version shouldn't be found.
73636a7c42d42800f69caadcdea433312fd642a4b3Sebastian Redl  (void)::new ((S*)0) U; // expected-error {{no matching function for call to 'operator new'}}
749afe1308ed19dffc281dca5cfbe521826754980fSebastian Redl  // This must fail, because any member version hides all global versions.
759afe1308ed19dffc281dca5cfbe521826754980fSebastian Redl  (void)new U; // expected-error {{no matching function for call to 'operator new'}}
7600e68e2cc5ce37cb95beb801cae73c0d1e9dda37Sebastian Redl  (void)new (int[]); // expected-error {{array size must be specified in new expressions}}
7700e68e2cc5ce37cb95beb801cae73c0d1e9dda37Sebastian Redl  (void)new int&; // expected-error {{cannot allocate reference type 'int &' with new}}
784c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl  // Some lacking cases due to lack of sema support.
794c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl}
804c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl
814c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redlvoid good_deletes()
824c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl{
834c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl  delete (int*)0;
844c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl  delete [](int*)0;
854c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl  delete (S*)0;
86fb4ccd7152723ac6190eb379250cfe7516cfd1b8Sebastian Redl  ::delete (int*)0;
874c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl}
884c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl
894c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redlvoid bad_deletes()
904c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl{
914c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl  delete 0; // expected-error {{cannot delete expression of type 'int'}}
924c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl  delete [0] (int*)0; // expected-error {{expected ']'}} \
9328eb7e992b9a266abb300da25b6d3c1557cec361Chris Lattner                      // expected-note {{to match this '['}}
944c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl  delete (void*)0; // expected-error {{cannot delete expression}}
954c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl  delete (T*)0; // expected-warning {{deleting pointer to incomplete type}}
96fb4ccd7152723ac6190eb379250cfe7516cfd1b8Sebastian Redl  ::S::delete (int*)0; // expected-error {{expected unqualified-id}}
974c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl}
98