new-delete.cpp revision 00e68e2cc5ce37cb95beb801cae73c0d1e9dda37
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}} 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. 15636a7c42d42800f69caadcdea433312fd642a4b3Sebastian Redl void* operator new(size_t, S*); 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}} 24b5a57a69e5fdac6dd9a92be717e279486c4a0128Sebastian Redl 254c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redlvoid good_news() 264c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl{ 274c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl int *pi = new int; 284c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl float *pf = new (pi) float(); 294c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl pi = new int(1); 304c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl pi = new int('c'); 314c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl const int *pci = new const int(); 324c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl S *ps = new S(1, 2, 3.4); 33cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl ps = new (pf) (S)(1, 2, 3.4); 344c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl S *(*paps)[2] = new S*[*pi][2]; 354c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl ps = new (S[3])(1, 2, 3.4); 364c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl typedef int ia4[4]; 374c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl ia4 *pai = new (int[3][4]); 38fb4ccd7152723ac6190eb379250cfe7516cfd1b8Sebastian Redl pi = ::new int; 39636a7c42d42800f69caadcdea433312fd642a4b3Sebastian Redl U *pu = new (ps) U; 407f6623914e779e41eb3d85f9a2dc3affea5de1e8Sebastian Redl // This is xfail. Inherited functions are not looked up currently. 417f6623914e779e41eb3d85f9a2dc3affea5de1e8Sebastian Redl //V *pv = new (ps) V; 424c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl} 434c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl 44c83ed049af2a2ed7ab94b8206fc0fec4da7e26dbDouglas Gregorvoid bad_news(int *ip) 454c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl{ 464c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl int i = 1; 474c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl (void)new; // expected-error {{missing type specifier}} 484c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl (void)new 4; // expected-error {{missing type specifier}} 494c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl (void)new () int; // expected-error {{expected expression}} 50cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl (void)new int[1.1]; // expected-error {{array size expression must have integral or enumerated type, not 'double'}} 514c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl (void)new int[1][i]; // expected-error {{only the first dimension}} 524c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl (void)new (int[1][i]); // expected-error {{only the first dimension}} 534c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl (void)new int(*(S*)0); // expected-error {{incompatible type initializing}} 544c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl (void)new int(1, 2); // expected-error {{initializer of a builtin type can only take one argument}} 554c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl (void)new S(1); // expected-error {{no matching constructor}} 56c83ed049af2a2ed7ab94b8206fc0fec4da7e26dbDouglas Gregor (void)new S(1, 1); // expected-error {{call to constructor of 'S' is ambiguous}} 574c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl (void)new const int; // expected-error {{must provide an initializer}} 58c83ed049af2a2ed7ab94b8206fc0fec4da7e26dbDouglas Gregor (void)new float*(ip); // expected-error {{incompatible type initializing 'int *', expected 'float *'}} 59cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl // Undefined, but clang should reject it directly. 60cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl (void)new int[-1]; // expected-error {{array size is negative}} 61cee63fbf0e64ac526582312bf8cf33263fc5c16eSebastian Redl (void)new int[*(S*)0]; // expected-error {{array size expression must have integral or enumerated type, not 'struct S'}} 62fb4ccd7152723ac6190eb379250cfe7516cfd1b8Sebastian Redl (void)::S::new int; // expected-error {{expected unqualified-id}} 63b5a57a69e5fdac6dd9a92be717e279486c4a0128Sebastian Redl (void)new (0, 0) int; // expected-error {{no matching function for call to 'operator new'}} 647f6623914e779e41eb3d85f9a2dc3affea5de1e8Sebastian Redl (void)new (0L) int; // expected-error {{call to 'operator new' is ambiguous}} 65636a7c42d42800f69caadcdea433312fd642a4b3Sebastian Redl // This must fail, because the member version shouldn't be found. 66636a7c42d42800f69caadcdea433312fd642a4b3Sebastian Redl (void)::new ((S*)0) U; // expected-error {{no matching function for call to 'operator new'}} 6700e68e2cc5ce37cb95beb801cae73c0d1e9dda37Sebastian Redl (void)new (int[]); // expected-error {{array size must be specified in new expressions}} 6800e68e2cc5ce37cb95beb801cae73c0d1e9dda37Sebastian Redl (void)new int&; // expected-error {{cannot allocate reference type 'int &' with new}} 6900e68e2cc5ce37cb95beb801cae73c0d1e9dda37Sebastian Redl (void)new (void ()); // expected-error {{cannot allocate function type 'void (void)' with new}} 704c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl // Some lacking cases due to lack of sema support. 714c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl} 724c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl 734c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redlvoid good_deletes() 744c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl{ 754c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl delete (int*)0; 764c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl delete [](int*)0; 774c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl delete (S*)0; 78fb4ccd7152723ac6190eb379250cfe7516cfd1b8Sebastian Redl ::delete (int*)0; 794c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl} 804c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl 814c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redlvoid bad_deletes() 824c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl{ 834c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl delete 0; // expected-error {{cannot delete expression of type 'int'}} 844c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl delete [0] (int*)0; // expected-error {{expected ']'}} \ 8528eb7e992b9a266abb300da25b6d3c1557cec361Chris Lattner // expected-note {{to match this '['}} 864c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl delete (void*)0; // expected-error {{cannot delete expression}} 874c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl delete (T*)0; // expected-warning {{deleting pointer to incomplete type}} 88fb4ccd7152723ac6190eb379250cfe7516cfd1b8Sebastian Redl ::S::delete (int*)0; // expected-error {{expected unqualified-id}} 894c5d320a7581f4b80b151630c91cea5727fa9923Sebastian Redl} 90