new-delete.cpp revision fb4ccd7152723ac6190eb379250cfe7516cfd1b8
1c83ed049af2a2ed7ab94b8206fc0fec4da7e26dbDouglas Gregor// RUN: clang -fsyntax-only -verify %s
2c83ed049af2a2ed7ab94b8206fc0fec4da7e26dbDouglas Gregor
34c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redlstruct S // expected-note {{candidate}}
44c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl{
54c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl  S(int, int, double); // expected-note {{candidate}}
64c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl  S(double, int); // expected-note {{candidate}} expected-note {{candidate}}
74c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl  S(float, int); // expected-note {{candidate}} expected-note {{candidate}}
84c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl};
94c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redlstruct T;
104c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl
114c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redlvoid good_news()
124c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl{
134c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl  int *pi = new int;
144c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl  float *pf = new (pi) float();
154c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl  pi = new int(1);
164c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl  pi = new int('c');
174c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl  const int *pci = new const int();
184c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl  S *ps = new S(1, 2, 3.4);
19cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl  ps = new (pf) (S)(1, 2, 3.4);
204c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl  S *(*paps)[2] = new S*[*pi][2];
214c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl  ps = new (S[3])(1, 2, 3.4);
224c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl  typedef int ia4[4];
234c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl  ia4 *pai = new (int[3][4]);
24fb4ccd7152723ac6190eb379250cfe7516cfd1b8Sebastian Redl  pi = ::new int;
254c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl}
264c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl
27c83ed049af2a2ed7ab94b8206fc0fec4da7e26dbDouglas Gregorvoid bad_news(int *ip)
284c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl{
294c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl  int i = 1;
304c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl  (void)new; // expected-error {{missing type specifier}}
314c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl  (void)new 4; // expected-error {{missing type specifier}}
324c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl  (void)new () int; // expected-error {{expected expression}}
33cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl  (void)new int[1.1]; // expected-error {{array size expression must have integral or enumerated type, not 'double'}}
344c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl  (void)new int[1][i]; // expected-error {{only the first dimension}}
354c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl  (void)new (int[1][i]); // expected-error {{only the first dimension}}
364c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl  (void)new int(*(S*)0); // expected-error {{incompatible type initializing}}
374c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl  (void)new int(1, 2); // expected-error {{initializer of a builtin type can only take one argument}}
384c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl  (void)new S(1); // expected-error {{no matching constructor}}
39c83ed049af2a2ed7ab94b8206fc0fec4da7e26dbDouglas Gregor  (void)new S(1, 1); // expected-error {{call to constructor of 'S' is ambiguous}}
404c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl  (void)new const int; // expected-error {{must provide an initializer}}
41c83ed049af2a2ed7ab94b8206fc0fec4da7e26dbDouglas Gregor  (void)new float*(ip); // expected-error {{incompatible type initializing 'int *', expected 'float *'}}
42cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl  // Undefined, but clang should reject it directly.
43cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl  (void)new int[-1]; // expected-error {{array size is negative}}
44cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl  (void)new int[*(S*)0]; // expected-error {{array size expression must have integral or enumerated type, not 'struct S'}}
45fb4ccd7152723ac6190eb379250cfe7516cfd1b8Sebastian Redl  (void)::S::new int; // expected-error {{expected unqualified-id}}
464c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl  // Some lacking cases due to lack of sema support.
474c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl}
484c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl
494c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redlvoid good_deletes()
504c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl{
514c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl  delete (int*)0;
524c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl  delete [](int*)0;
534c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl  delete (S*)0;
54fb4ccd7152723ac6190eb379250cfe7516cfd1b8Sebastian Redl  ::delete (int*)0;
554c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl}
564c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl
574c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redlvoid bad_deletes()
584c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl{
594c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl  delete 0; // expected-error {{cannot delete expression of type 'int'}}
604c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl  delete [0] (int*)0; // expected-error {{expected ']'}} \
6128eb7e992b9a266abb300da25b6d3c1557cec361Chris Lattner                      // expected-note {{to match this '['}}
624c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl  delete (void*)0; // expected-error {{cannot delete expression}}
634c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl  delete (T*)0; // expected-warning {{deleting pointer to incomplete type}}
64fb4ccd7152723ac6190eb379250cfe7516cfd1b8Sebastian Redl  ::S::delete (int*)0; // expected-error {{expected unqualified-id}}
654c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl}
66