new-delete.cpp revision cee63fbf0e64ac526582312bf8cf33263fc5c16e
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]); 244c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl} 254c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl 26c83ed049af2a2ed7ab94b8206fc0fec4da7e26dbDouglas Gregorvoid bad_news(int *ip) 274c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl{ 284c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl int i = 1; 294c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl (void)new; // expected-error {{missing type specifier}} 304c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl (void)new 4; // expected-error {{missing type specifier}} 314c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl (void)new () int; // expected-error {{expected expression}} 32cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl (void)new int[1.1]; // expected-error {{array size expression must have integral or enumerated type, not 'double'}} 334c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl (void)new int[1][i]; // expected-error {{only the first dimension}} 344c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl (void)new (int[1][i]); // expected-error {{only the first dimension}} 354c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl (void)new int(*(S*)0); // expected-error {{incompatible type initializing}} 364c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl (void)new int(1, 2); // expected-error {{initializer of a builtin type can only take one argument}} 374c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl (void)new S(1); // expected-error {{no matching constructor}} 38c83ed049af2a2ed7ab94b8206fc0fec4da7e26dbDouglas Gregor (void)new S(1, 1); // expected-error {{call to constructor of 'S' is ambiguous}} 394c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl (void)new const int; // expected-error {{must provide an initializer}} 40c83ed049af2a2ed7ab94b8206fc0fec4da7e26dbDouglas Gregor (void)new float*(ip); // expected-error {{incompatible type initializing 'int *', expected 'float *'}} 41cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl // Undefined, but clang should reject it directly. 42cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl (void)new int[-1]; // expected-error {{array size is negative}} 43cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl (void)new int[*(S*)0]; // expected-error {{array size expression must have integral or enumerated type, not 'struct S'}} 444c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl // Some lacking cases due to lack of sema support. 454c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl} 464c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl 474c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redlvoid good_deletes() 484c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl{ 494c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl delete (int*)0; 504c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl delete [](int*)0; 514c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl delete (S*)0; 524c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl} 534c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl 544c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redlvoid bad_deletes() 554c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl{ 564c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl delete 0; // expected-error {{cannot delete expression of type 'int'}} 574c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl delete [0] (int*)0; // expected-error {{expected ']'}} \ 5828eb7e992b9a266abb300da25b6d3c1557cec361Chris Lattner // expected-note {{to match this '['}} 594c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl delete (void*)0; // expected-error {{cannot delete expression}} 604c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl delete (T*)0; // expected-warning {{deleting pointer to incomplete type}} 614c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl} 62