blob: c90cd2f314c63a8af6a7853636be13ed4d05363d [file] [log] [blame]
Douglas Gregorc83ed042008-11-25 04:08:05 +00001// RUN: clang -fsyntax-only -verify %s
2
Sebastian Redlb5a57a62008-12-03 20:26:15 +00003#include <stddef.h>
4
Sebastian Redl4c5d3202008-11-21 19:14:01 +00005struct 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};
11struct T;
Sebastian Redl636a7c42008-12-04 17:24:46 +000012struct U
13{
14 // A special new, to verify that the global version isn't used.
15 void* operator new(size_t, S*);
16};
Sebastian Redl4c5d3202008-11-21 19:14:01 +000017
Sebastian Redlb5a57a62008-12-03 20:26:15 +000018void* operator new(size_t); // expected-note {{candidate}}
19void* operator new(size_t, int*); // expected-note {{candidate}}
20void* operator new(size_t, float*); // expected-note {{candidate}}
21
Sebastian Redl4c5d3202008-11-21 19:14:01 +000022void good_news()
23{
24 int *pi = new int;
25 float *pf = new (pi) float();
26 pi = new int(1);
27 pi = new int('c');
28 const int *pci = new const int();
29 S *ps = new S(1, 2, 3.4);
Sebastian Redlcee63fb2008-12-02 14:43:59 +000030 ps = new (pf) (S)(1, 2, 3.4);
Sebastian Redl4c5d3202008-11-21 19:14:01 +000031 S *(*paps)[2] = new S*[*pi][2];
32 ps = new (S[3])(1, 2, 3.4);
33 typedef int ia4[4];
34 ia4 *pai = new (int[3][4]);
Sebastian Redlfb4ccd72008-12-02 16:35:44 +000035 pi = ::new int;
Sebastian Redl636a7c42008-12-04 17:24:46 +000036 U *pu = new (ps) U;
Sebastian Redl4c5d3202008-11-21 19:14:01 +000037}
38
Douglas Gregorc83ed042008-11-25 04:08:05 +000039void bad_news(int *ip)
Sebastian Redl4c5d3202008-11-21 19:14:01 +000040{
41 int i = 1;
42 (void)new; // expected-error {{missing type specifier}}
43 (void)new 4; // expected-error {{missing type specifier}}
44 (void)new () int; // expected-error {{expected expression}}
Sebastian Redlcee63fb2008-12-02 14:43:59 +000045 (void)new int[1.1]; // expected-error {{array size expression must have integral or enumerated type, not 'double'}}
Sebastian Redl4c5d3202008-11-21 19:14:01 +000046 (void)new int[1][i]; // expected-error {{only the first dimension}}
47 (void)new (int[1][i]); // expected-error {{only the first dimension}}
48 (void)new int(*(S*)0); // expected-error {{incompatible type initializing}}
49 (void)new int(1, 2); // expected-error {{initializer of a builtin type can only take one argument}}
50 (void)new S(1); // expected-error {{no matching constructor}}
Douglas Gregorc83ed042008-11-25 04:08:05 +000051 (void)new S(1, 1); // expected-error {{call to constructor of 'S' is ambiguous}}
Sebastian Redl4c5d3202008-11-21 19:14:01 +000052 (void)new const int; // expected-error {{must provide an initializer}}
Douglas Gregorc83ed042008-11-25 04:08:05 +000053 (void)new float*(ip); // expected-error {{incompatible type initializing 'int *', expected 'float *'}}
Sebastian Redlcee63fb2008-12-02 14:43:59 +000054 // Undefined, but clang should reject it directly.
55 (void)new int[-1]; // expected-error {{array size is negative}}
56 (void)new int[*(S*)0]; // expected-error {{array size expression must have integral or enumerated type, not 'struct S'}}
Sebastian Redlfb4ccd72008-12-02 16:35:44 +000057 (void)::S::new int; // expected-error {{expected unqualified-id}}
Sebastian Redlb5a57a62008-12-03 20:26:15 +000058 (void)new (0, 0) int; // expected-error {{no matching function for call to 'operator new'}}
Sebastian Redl636a7c42008-12-04 17:24:46 +000059 (void)new (0L) int; // expected-error {{use of overloaded operator 'new' is ambiguous}}
60 // This must fail, because the member version shouldn't be found.
61 (void)::new ((S*)0) U; // expected-error {{no matching function for call to 'operator new'}}
Sebastian Redl4c5d3202008-11-21 19:14:01 +000062 // Some lacking cases due to lack of sema support.
63}
64
65void good_deletes()
66{
67 delete (int*)0;
68 delete [](int*)0;
69 delete (S*)0;
Sebastian Redlfb4ccd72008-12-02 16:35:44 +000070 ::delete (int*)0;
Sebastian Redl4c5d3202008-11-21 19:14:01 +000071}
72
73void bad_deletes()
74{
75 delete 0; // expected-error {{cannot delete expression of type 'int'}}
76 delete [0] (int*)0; // expected-error {{expected ']'}} \
Chris Lattner28eb7e92008-11-23 23:17:07 +000077 // expected-note {{to match this '['}}
Sebastian Redl4c5d3202008-11-21 19:14:01 +000078 delete (void*)0; // expected-error {{cannot delete expression}}
79 delete (T*)0; // expected-warning {{deleting pointer to incomplete type}}
Sebastian Redlfb4ccd72008-12-02 16:35:44 +000080 ::S::delete (int*)0; // expected-error {{expected unqualified-id}}
Sebastian Redl4c5d3202008-11-21 19:14:01 +000081}