Douglas Gregor | c83ed04 | 2008-11-25 04:08:05 +0000 | [diff] [blame] | 1 | // RUN: clang -fsyntax-only -verify %s |
| 2 | |
Sebastian Redl | b5a57a6 | 2008-12-03 20:26:15 +0000 | [diff] [blame] | 3 | #include <stddef.h> |
| 4 | |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 5 | struct S // expected-note {{candidate}} |
| 6 | { |
| 7 | S(int, int, double); // expected-note {{candidate}} |
| 8 | S(double, int); // expected-note {{candidate}} expected-note {{candidate}} |
| 9 | S(float, int); // expected-note {{candidate}} expected-note {{candidate}} |
| 10 | }; |
Douglas Gregor | 4ec339f | 2009-01-19 19:26:10 +0000 | [diff] [blame^] | 11 | struct T; // expected-note{{forward declaration of 'struct T'}} |
Sebastian Redl | 636a7c4 | 2008-12-04 17:24:46 +0000 | [diff] [blame] | 12 | struct U |
| 13 | { |
| 14 | // A special new, to verify that the global version isn't used. |
| 15 | void* operator new(size_t, S*); |
| 16 | }; |
Sebastian Redl | 7f66239 | 2008-12-04 22:20:51 +0000 | [diff] [blame] | 17 | struct V : U |
| 18 | { |
| 19 | }; |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 20 | |
Sebastian Redl | b5a57a6 | 2008-12-03 20:26:15 +0000 | [diff] [blame] | 21 | void* operator new(size_t); // expected-note {{candidate}} |
| 22 | void* operator new(size_t, int*); // expected-note {{candidate}} |
| 23 | void* operator new(size_t, float*); // expected-note {{candidate}} |
| 24 | |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 25 | void good_news() |
| 26 | { |
| 27 | int *pi = new int; |
| 28 | float *pf = new (pi) float(); |
| 29 | pi = new int(1); |
| 30 | pi = new int('c'); |
| 31 | const int *pci = new const int(); |
| 32 | S *ps = new S(1, 2, 3.4); |
Sebastian Redl | cee63fb | 2008-12-02 14:43:59 +0000 | [diff] [blame] | 33 | ps = new (pf) (S)(1, 2, 3.4); |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 34 | S *(*paps)[2] = new S*[*pi][2]; |
| 35 | ps = new (S[3])(1, 2, 3.4); |
| 36 | typedef int ia4[4]; |
| 37 | ia4 *pai = new (int[3][4]); |
Sebastian Redl | fb4ccd7 | 2008-12-02 16:35:44 +0000 | [diff] [blame] | 38 | pi = ::new int; |
Sebastian Redl | 636a7c4 | 2008-12-04 17:24:46 +0000 | [diff] [blame] | 39 | U *pu = new (ps) U; |
Sebastian Redl | 7f66239 | 2008-12-04 22:20:51 +0000 | [diff] [blame] | 40 | // This is xfail. Inherited functions are not looked up currently. |
| 41 | //V *pv = new (ps) V; |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 42 | } |
| 43 | |
Douglas Gregor | c83ed04 | 2008-11-25 04:08:05 +0000 | [diff] [blame] | 44 | void bad_news(int *ip) |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 45 | { |
| 46 | int i = 1; |
| 47 | (void)new; // expected-error {{missing type specifier}} |
| 48 | (void)new 4; // expected-error {{missing type specifier}} |
| 49 | (void)new () int; // expected-error {{expected expression}} |
Sebastian Redl | cee63fb | 2008-12-02 14:43:59 +0000 | [diff] [blame] | 50 | (void)new int[1.1]; // expected-error {{array size expression must have integral or enumerated type, not 'double'}} |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 51 | (void)new int[1][i]; // expected-error {{only the first dimension}} |
| 52 | (void)new (int[1][i]); // expected-error {{only the first dimension}} |
| 53 | (void)new int(*(S*)0); // expected-error {{incompatible type initializing}} |
| 54 | (void)new int(1, 2); // expected-error {{initializer of a builtin type can only take one argument}} |
| 55 | (void)new S(1); // expected-error {{no matching constructor}} |
Douglas Gregor | c83ed04 | 2008-11-25 04:08:05 +0000 | [diff] [blame] | 56 | (void)new S(1, 1); // expected-error {{call to constructor of 'S' is ambiguous}} |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 57 | (void)new const int; // expected-error {{must provide an initializer}} |
Douglas Gregor | c83ed04 | 2008-11-25 04:08:05 +0000 | [diff] [blame] | 58 | (void)new float*(ip); // expected-error {{incompatible type initializing 'int *', expected 'float *'}} |
Sebastian Redl | cee63fb | 2008-12-02 14:43:59 +0000 | [diff] [blame] | 59 | // Undefined, but clang should reject it directly. |
| 60 | (void)new int[-1]; // expected-error {{array size is negative}} |
| 61 | (void)new int[*(S*)0]; // expected-error {{array size expression must have integral or enumerated type, not 'struct S'}} |
Sebastian Redl | fb4ccd7 | 2008-12-02 16:35:44 +0000 | [diff] [blame] | 62 | (void)::S::new int; // expected-error {{expected unqualified-id}} |
Sebastian Redl | b5a57a6 | 2008-12-03 20:26:15 +0000 | [diff] [blame] | 63 | (void)new (0, 0) int; // expected-error {{no matching function for call to 'operator new'}} |
Sebastian Redl | 7f66239 | 2008-12-04 22:20:51 +0000 | [diff] [blame] | 64 | (void)new (0L) int; // expected-error {{call to 'operator new' is ambiguous}} |
Sebastian Redl | 636a7c4 | 2008-12-04 17:24:46 +0000 | [diff] [blame] | 65 | // This must fail, because the member version shouldn't be found. |
| 66 | (void)::new ((S*)0) U; // expected-error {{no matching function for call to 'operator new'}} |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 67 | // Some lacking cases due to lack of sema support. |
| 68 | } |
| 69 | |
| 70 | void good_deletes() |
| 71 | { |
| 72 | delete (int*)0; |
| 73 | delete [](int*)0; |
| 74 | delete (S*)0; |
Sebastian Redl | fb4ccd7 | 2008-12-02 16:35:44 +0000 | [diff] [blame] | 75 | ::delete (int*)0; |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 76 | } |
| 77 | |
| 78 | void bad_deletes() |
| 79 | { |
| 80 | delete 0; // expected-error {{cannot delete expression of type 'int'}} |
| 81 | delete [0] (int*)0; // expected-error {{expected ']'}} \ |
Chris Lattner | 28eb7e9 | 2008-11-23 23:17:07 +0000 | [diff] [blame] | 82 | // expected-note {{to match this '['}} |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 83 | delete (void*)0; // expected-error {{cannot delete expression}} |
| 84 | delete (T*)0; // expected-warning {{deleting pointer to incomplete type}} |
Sebastian Redl | fb4ccd7 | 2008-12-02 16:35:44 +0000 | [diff] [blame] | 85 | ::S::delete (int*)0; // expected-error {{expected unqualified-id}} |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 86 | } |