new-delete.cpp revision b5a57a69e5fdac6dd9a92be717e279486c4a0128
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; 124c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl 13b5a57a69e5fdac6dd9a92be717e279486c4a0128Sebastian Redlvoid* operator new(size_t); // expected-note {{candidate}} 14b5a57a69e5fdac6dd9a92be717e279486c4a0128Sebastian Redlvoid* operator new(size_t, int*); // expected-note {{candidate}} 15b5a57a69e5fdac6dd9a92be717e279486c4a0128Sebastian Redlvoid* operator new(size_t, float*); // expected-note {{candidate}} 16b5a57a69e5fdac6dd9a92be717e279486c4a0128Sebastian Redl 174c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redlvoid good_news() 184c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl{ 194c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl int *pi = new int; 204c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl float *pf = new (pi) float(); 214c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl pi = new int(1); 224c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl pi = new int('c'); 234c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl const int *pci = new const int(); 244c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl S *ps = new S(1, 2, 3.4); 25cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl ps = new (pf) (S)(1, 2, 3.4); 264c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl S *(*paps)[2] = new S*[*pi][2]; 274c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl ps = new (S[3])(1, 2, 3.4); 284c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl typedef int ia4[4]; 294c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl ia4 *pai = new (int[3][4]); 30fb4ccd7152723ac6190eb379250cfe7516cfd1b8Sebastian Redl pi = ::new int; 314c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl} 324c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl 33c83ed049af2a2ed7ab94b8206fc0fec4da7e26dbDouglas Gregorvoid bad_news(int *ip) 344c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl{ 354c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl int i = 1; 364c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl (void)new; // expected-error {{missing type specifier}} 374c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl (void)new 4; // expected-error {{missing type specifier}} 384c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl (void)new () int; // expected-error {{expected expression}} 39cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl (void)new int[1.1]; // expected-error {{array size expression must have integral or enumerated type, not 'double'}} 404c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl (void)new int[1][i]; // expected-error {{only the first dimension}} 414c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl (void)new (int[1][i]); // expected-error {{only the first dimension}} 424c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl (void)new int(*(S*)0); // expected-error {{incompatible type initializing}} 434c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl (void)new int(1, 2); // expected-error {{initializer of a builtin type can only take one argument}} 444c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl (void)new S(1); // expected-error {{no matching constructor}} 45c83ed049af2a2ed7ab94b8206fc0fec4da7e26dbDouglas Gregor (void)new S(1, 1); // expected-error {{call to constructor of 'S' is ambiguous}} 464c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl (void)new const int; // expected-error {{must provide an initializer}} 47c83ed049af2a2ed7ab94b8206fc0fec4da7e26dbDouglas Gregor (void)new float*(ip); // expected-error {{incompatible type initializing 'int *', expected 'float *'}} 48cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl // Undefined, but clang should reject it directly. 49cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl (void)new int[-1]; // expected-error {{array size is negative}} 50cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl (void)new int[*(S*)0]; // expected-error {{array size expression must have integral or enumerated type, not 'struct S'}} 51fb4ccd7152723ac6190eb379250cfe7516cfd1b8Sebastian Redl (void)::S::new int; // expected-error {{expected unqualified-id}} 52b5a57a69e5fdac6dd9a92be717e279486c4a0128Sebastian Redl (void)new (0, 0) int; // expected-error {{no matching function for call to 'operator new'}} 534c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl // Some lacking cases due to lack of sema support. 544c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl} 554c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl 564c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redlvoid good_deletes() 574c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl{ 584c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl delete (int*)0; 594c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl delete [](int*)0; 604c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl delete (S*)0; 61fb4ccd7152723ac6190eb379250cfe7516cfd1b8Sebastian Redl ::delete (int*)0; 624c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl} 634c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl 644c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redlvoid bad_deletes() 654c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl{ 664c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl delete 0; // expected-error {{cannot delete expression of type 'int'}} 674c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl delete [0] (int*)0; // expected-error {{expected ']'}} \ 6828eb7e992b9a266abb300da25b6d3c1557cec361Chris Lattner // expected-note {{to match this '['}} 694c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl delete (void*)0; // expected-error {{cannot delete expression}} 704c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl delete (T*)0; // expected-warning {{deleting pointer to incomplete type}} 71fb4ccd7152723ac6190eb379250cfe7516cfd1b8Sebastian Redl ::S::delete (int*)0; // expected-error {{expected unqualified-id}} 724c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl} 73