new-delete.cpp revision 636a7c42d42800f69caadcdea433312fd642a4b3
1c83ed049af2a2ed7ab94b8206fc0fec4da7e26dbDouglas Gregor// RUN: clang -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}}
84c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl  S(double, int); // expected-note {{candidate}} expected-note {{candidate}}
94c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl  S(float, int); // expected-note {{candidate}} expected-note {{candidate}}
104c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl};
114c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redlstruct T;
12636a7c42d42800f69caadcdea433312fd642a4b3Sebastian Redlstruct U
13636a7c42d42800f69caadcdea433312fd642a4b3Sebastian Redl{
14636a7c42d42800f69caadcdea433312fd642a4b3Sebastian Redl  // A special new, to verify that the global version isn't used.
15636a7c42d42800f69caadcdea433312fd642a4b3Sebastian Redl  void* operator new(size_t, S*);
16636a7c42d42800f69caadcdea433312fd642a4b3Sebastian Redl};
174c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl
18b5a57a69e5fdac6dd9a92be717e279486c4a0128Sebastian Redlvoid* operator new(size_t); // expected-note {{candidate}}
19b5a57a69e5fdac6dd9a92be717e279486c4a0128Sebastian Redlvoid* operator new(size_t, int*); // expected-note {{candidate}}
20b5a57a69e5fdac6dd9a92be717e279486c4a0128Sebastian Redlvoid* operator new(size_t, float*); // expected-note {{candidate}}
21b5a57a69e5fdac6dd9a92be717e279486c4a0128Sebastian Redl
224c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redlvoid good_news()
234c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl{
244c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl  int *pi = new int;
254c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl  float *pf = new (pi) float();
264c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl  pi = new int(1);
274c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl  pi = new int('c');
284c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl  const int *pci = new const int();
294c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl  S *ps = new S(1, 2, 3.4);
30cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl  ps = new (pf) (S)(1, 2, 3.4);
314c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl  S *(*paps)[2] = new S*[*pi][2];
324c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl  ps = new (S[3])(1, 2, 3.4);
334c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl  typedef int ia4[4];
344c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl  ia4 *pai = new (int[3][4]);
35fb4ccd7152723ac6190eb379250cfe7516cfd1b8Sebastian Redl  pi = ::new int;
36636a7c42d42800f69caadcdea433312fd642a4b3Sebastian Redl  U *pu = new (ps) U;
374c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl}
384c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl
39c83ed049af2a2ed7ab94b8206fc0fec4da7e26dbDouglas Gregorvoid bad_news(int *ip)
404c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl{
414c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl  int i = 1;
424c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl  (void)new; // expected-error {{missing type specifier}}
434c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl  (void)new 4; // expected-error {{missing type specifier}}
444c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl  (void)new () int; // expected-error {{expected expression}}
45cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl  (void)new int[1.1]; // expected-error {{array size expression must have integral or enumerated type, not 'double'}}
464c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl  (void)new int[1][i]; // expected-error {{only the first dimension}}
474c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl  (void)new (int[1][i]); // expected-error {{only the first dimension}}
484c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl  (void)new int(*(S*)0); // expected-error {{incompatible type initializing}}
494c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl  (void)new int(1, 2); // expected-error {{initializer of a builtin type can only take one argument}}
504c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl  (void)new S(1); // expected-error {{no matching constructor}}
51c83ed049af2a2ed7ab94b8206fc0fec4da7e26dbDouglas Gregor  (void)new S(1, 1); // expected-error {{call to constructor of 'S' is ambiguous}}
524c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl  (void)new const int; // expected-error {{must provide an initializer}}
53c83ed049af2a2ed7ab94b8206fc0fec4da7e26dbDouglas Gregor  (void)new float*(ip); // expected-error {{incompatible type initializing 'int *', expected 'float *'}}
54cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl  // Undefined, but clang should reject it directly.
55cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl  (void)new int[-1]; // expected-error {{array size is negative}}
56cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl  (void)new int[*(S*)0]; // expected-error {{array size expression must have integral or enumerated type, not 'struct S'}}
57fb4ccd7152723ac6190eb379250cfe7516cfd1b8Sebastian Redl  (void)::S::new int; // expected-error {{expected unqualified-id}}
58b5a57a69e5fdac6dd9a92be717e279486c4a0128Sebastian Redl  (void)new (0, 0) int; // expected-error {{no matching function for call to 'operator new'}}
59636a7c42d42800f69caadcdea433312fd642a4b3Sebastian Redl  (void)new (0L) int; // expected-error {{use of overloaded operator 'new' is ambiguous}}
60636a7c42d42800f69caadcdea433312fd642a4b3Sebastian Redl  // This must fail, because the member version shouldn't be found.
61636a7c42d42800f69caadcdea433312fd642a4b3Sebastian Redl  (void)::new ((S*)0) U; // expected-error {{no matching function for call to 'operator new'}}
624c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl  // Some lacking cases due to lack of sema support.
634c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl}
644c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl
654c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redlvoid good_deletes()
664c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl{
674c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl  delete (int*)0;
684c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl  delete [](int*)0;
694c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl  delete (S*)0;
70fb4ccd7152723ac6190eb379250cfe7516cfd1b8Sebastian Redl  ::delete (int*)0;
714c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl}
724c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl
734c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redlvoid bad_deletes()
744c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl{
754c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl  delete 0; // expected-error {{cannot delete expression of type 'int'}}
764c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl  delete [0] (int*)0; // expected-error {{expected ']'}} \
7728eb7e992b9a266abb300da25b6d3c1557cec361Chris Lattner                      // expected-note {{to match this '['}}
784c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl  delete (void*)0; // expected-error {{cannot delete expression}}
794c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl  delete (T*)0; // expected-warning {{deleting pointer to incomplete type}}
80fb4ccd7152723ac6190eb379250cfe7516cfd1b8Sebastian Redl  ::S::delete (int*)0; // expected-error {{expected unqualified-id}}
814c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl}
82