blob: dd28413855bd27b7dd4d65440e9f75fccc53d6fe [file] [log] [blame]
Daniel Dunbard7d5f022009-03-24 02:24:46 +00001// RUN: clang-cc -fsyntax-only -verify %s
Douglas Gregorc83ed042008-11-25 04:08:05 +00002
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}}
Sebastian Redl3cb06922009-02-07 19:52:04 +00008 S(double, int); // expected-note 2 {{candidate}}
9 S(float, int); // expected-note 2 {{candidate}}
Sebastian Redl4c5d3202008-11-21 19:14:01 +000010};
Douglas Gregor4ec339f2009-01-19 19:26:10 +000011struct T; // expected-note{{forward declaration of 'struct T'}}
Sebastian Redl636a7c42008-12-04 17:24:46 +000012struct U
13{
14 // A special new, to verify that the global version isn't used.
Sebastian Redl9afe1302009-05-14 18:11:41 +000015 void* operator new(size_t, S*); // expected-note {{candidate}}
Sebastian Redl636a7c42008-12-04 17:24:46 +000016};
Sebastian Redl7f662392008-12-04 22:20:51 +000017struct V : U
18{
19};
Sebastian Redl4c5d3202008-11-21 19:14:01 +000020
Sebastian Redl3cb06922009-02-07 19:52:04 +000021void* operator new(size_t); // expected-note 2 {{candidate}}
22void* operator new(size_t, int*); // expected-note 3 {{candidate}}
23void* operator new(size_t, float*); // expected-note 3 {{candidate}}
Anders Carlssonfc27d262009-05-31 19:49:47 +000024void* operator new(size_t, S); // expected-note 2 {{candidate}}
Sebastian Redlb5a57a62008-12-03 20:26:15 +000025
Sebastian Redl4c5d3202008-11-21 19:14:01 +000026void good_news()
27{
28 int *pi = new int;
29 float *pf = new (pi) float();
30 pi = new int(1);
31 pi = new int('c');
32 const int *pci = new const int();
33 S *ps = new S(1, 2, 3.4);
Sebastian Redlcee63fb2008-12-02 14:43:59 +000034 ps = new (pf) (S)(1, 2, 3.4);
Sebastian Redl4c5d3202008-11-21 19:14:01 +000035 S *(*paps)[2] = new S*[*pi][2];
36 ps = new (S[3])(1, 2, 3.4);
37 typedef int ia4[4];
38 ia4 *pai = new (int[3][4]);
Sebastian Redlfb4ccd72008-12-02 16:35:44 +000039 pi = ::new int;
Sebastian Redl636a7c42008-12-04 17:24:46 +000040 U *pu = new (ps) U;
Douglas Gregor5d64e5b2009-09-30 00:03:47 +000041 V *pv = new (ps) V;
Anders Carlssonfc27d262009-05-31 19:49:47 +000042
43 pi = new (S(1.0f, 2)) int;
Anders Carlssonac18b2e2009-09-23 00:37:25 +000044
45 (void)new int[true];
Sebastian Redl4c5d3202008-11-21 19:14:01 +000046}
47
Douglas Gregore7450f52009-03-24 19:52:54 +000048struct abstract {
49 virtual ~abstract() = 0;
50};
51
Douglas Gregorc83ed042008-11-25 04:08:05 +000052void bad_news(int *ip)
Sebastian Redl4c5d3202008-11-21 19:14:01 +000053{
54 int i = 1;
55 (void)new; // expected-error {{missing type specifier}}
56 (void)new 4; // expected-error {{missing type specifier}}
57 (void)new () int; // expected-error {{expected expression}}
Sebastian Redlcee63fb2008-12-02 14:43:59 +000058 (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 +000059 (void)new int[1][i]; // expected-error {{only the first dimension}}
60 (void)new (int[1][i]); // expected-error {{only the first dimension}}
Sebastian Redl8ce35b02009-10-25 21:45:37 +000061 (void)new (int[i]); // expected-error {{when type is in parentheses}}
Sebastian Redl4c5d3202008-11-21 19:14:01 +000062 (void)new int(*(S*)0); // expected-error {{incompatible type initializing}}
63 (void)new int(1, 2); // expected-error {{initializer of a builtin type can only take one argument}}
64 (void)new S(1); // expected-error {{no matching constructor}}
Douglas Gregorc83ed042008-11-25 04:08:05 +000065 (void)new S(1, 1); // expected-error {{call to constructor of 'S' is ambiguous}}
Sebastian Redl4c5d3202008-11-21 19:14:01 +000066 (void)new const int; // expected-error {{must provide an initializer}}
Douglas Gregorc83ed042008-11-25 04:08:05 +000067 (void)new float*(ip); // expected-error {{incompatible type initializing 'int *', expected 'float *'}}
Sebastian Redlcee63fb2008-12-02 14:43:59 +000068 // Undefined, but clang should reject it directly.
69 (void)new int[-1]; // expected-error {{array size is negative}}
70 (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 +000071 (void)::S::new int; // expected-error {{expected unqualified-id}}
Sebastian Redlb5a57a62008-12-03 20:26:15 +000072 (void)new (0, 0) int; // expected-error {{no matching function for call to 'operator new'}}
Sebastian Redl7f662392008-12-04 22:20:51 +000073 (void)new (0L) int; // expected-error {{call to 'operator new' is ambiguous}}
Sebastian Redl636a7c42008-12-04 17:24:46 +000074 // This must fail, because the member version shouldn't be found.
75 (void)::new ((S*)0) U; // expected-error {{no matching function for call to 'operator new'}}
Sebastian Redl9afe1302009-05-14 18:11:41 +000076 // This must fail, because any member version hides all global versions.
77 (void)new U; // expected-error {{no matching function for call to 'operator new'}}
Sebastian Redl00e68e22009-02-09 18:24:27 +000078 (void)new (int[]); // expected-error {{array size must be specified in new expressions}}
79 (void)new int&; // expected-error {{cannot allocate reference type 'int &' with new}}
Sebastian Redl4c5d3202008-11-21 19:14:01 +000080 // Some lacking cases due to lack of sema support.
81}
82
83void good_deletes()
84{
85 delete (int*)0;
86 delete [](int*)0;
87 delete (S*)0;
Sebastian Redlfb4ccd72008-12-02 16:35:44 +000088 ::delete (int*)0;
Sebastian Redl4c5d3202008-11-21 19:14:01 +000089}
90
91void bad_deletes()
92{
93 delete 0; // expected-error {{cannot delete expression of type 'int'}}
94 delete [0] (int*)0; // expected-error {{expected ']'}} \
Chris Lattner28eb7e92008-11-23 23:17:07 +000095 // expected-note {{to match this '['}}
Sebastian Redl4c5d3202008-11-21 19:14:01 +000096 delete (void*)0; // expected-error {{cannot delete expression}}
97 delete (T*)0; // expected-warning {{deleting pointer to incomplete type}}
Sebastian Redlfb4ccd72008-12-02 16:35:44 +000098 ::S::delete (int*)0; // expected-error {{expected unqualified-id}}
Sebastian Redl4c5d3202008-11-21 19:14:01 +000099}
Douglas Gregor9cd9f3f2009-09-09 23:39:55 +0000100
101struct X0 { };
102
103struct X1 {
104 operator int*();
105 operator float();
106};
107
108struct X2 {
Fariborz Jahanianf6527932009-09-15 17:21:47 +0000109 operator int*(); // expected-note {{candidate function}}
110 operator float*(); // expected-note {{candidate function}}
Douglas Gregor9cd9f3f2009-09-09 23:39:55 +0000111};
112
113void test_delete_conv(X0 x0, X1 x1, X2 x2) {
114 delete x0; // expected-error{{cannot delete}}
115 delete x1;
Fariborz Jahanianf6527932009-09-15 17:21:47 +0000116 delete x2; // expected-error{{ambiguous conversion of delete expression of type 'struct X2' to a pointer}}
117}
Douglas Gregor90916562009-09-29 18:16:17 +0000118
119// PR4782
120class X3 {
121public:
Eli Friedman5e6214b2009-11-16 05:14:40 +0000122 static void operator delete(void * mem, size_t size);
Douglas Gregor90916562009-09-29 18:16:17 +0000123};
124
125class X4 {
126public:
127 static void release(X3 *x);
Eli Friedman5e6214b2009-11-16 05:14:40 +0000128 static void operator delete(void * mem, size_t size);
Douglas Gregor90916562009-09-29 18:16:17 +0000129};
130
131
132void X4::release(X3 *x) {
133 delete x;
134}
Douglas Gregor1070c9f2009-09-29 21:38:53 +0000135
Douglas Gregor5d64e5b2009-09-30 00:03:47 +0000136class X5 {
Douglas Gregor1070c9f2009-09-29 21:38:53 +0000137public:
138 void Destroy() const { delete this; }
139};
Fariborz Jahanianb03bfa52009-11-10 23:47:18 +0000140
141class Base {
142public:
Fariborz Jahanian1f2fcee2009-11-11 22:45:41 +0000143 static int operator new(signed char) throw(); // expected-error {{'operator new' takes type size_t}} \
Fariborz Jahanianb03bfa52009-11-10 23:47:18 +0000144 // expected-error {{operator new' must return type 'void *'}}
Fariborz Jahanian1f2fcee2009-11-11 22:45:41 +0000145 static int operator new[] (signed char) throw(); // expected-error {{'operator new[]' takes type size_t}} \
Fariborz Jahanianb03bfa52009-11-10 23:47:18 +0000146 // expected-error {{operator new[]' must return type 'void *'}}
147};
148
149class Tier {};
150class Comp : public Tier {};
151
152class Thai : public Base {
153public:
154 Thai(const Tier *adoptDictionary);
155};
156
157void loadEngineFor() {
158 const Comp *dict;
159 new Thai(dict);
160}
161
162template <class T> struct TBase {
163 void* operator new(T size, int); // expected-error {{'operator new' takes type size_t}}
164};
165
166TBase<int> t1; // expected-note {{in instantiation of template class 'struct TBase<int>' requested here}}
167
Anders Carlsson0ba63ea2009-11-14 03:17:38 +0000168class X6 {
169public:
170 static void operator delete(void*, int); // expected-note {{member found by ambiguous name lookup}}
171};
172
173class X7 {
174public:
175 static void operator delete(void*, int); // expected-note {{member found by ambiguous name lookup}}
176};
177
178class X8 : public X6, public X7 {
179};
180
Anders Carlsson50724302009-11-15 16:43:15 +0000181void f(X8 *x8) {
Anders Carlsson0ba63ea2009-11-14 03:17:38 +0000182 delete x8; // expected-error {{member 'operator delete' found in multiple base classes of different types}}
183}
184
Anders Carlsson50724302009-11-15 16:43:15 +0000185class X9 {
186 static void operator delete(void*, int); // expected-note {{'operator delete' declared here}}
187 static void operator delete(void*, float); // expected-note {{'operator delete' declared here}}
188};
189
190void f(X9 *x9) {
191 delete x9; // expected-error {{no suitable member 'operator delete' in 'X9'}}
192}
Anders Carlsson37909802009-11-30 21:24:50 +0000193
194struct X10 {
195 virtual ~X10();
196};
197
198struct X11 : X10 { // expected-error {{no suitable member 'operator delete' in 'X11'}}
199 void operator delete(void*, int); // expected-note {{'operator delete' declared here}}
200};
201
202void f() {
203 X11 x11; // expected-note {{implicit default destructor for 'struct X11' first required here}}
204}
Anders Carlssoneac81392009-12-09 07:39:44 +0000205
206struct X12 {
207 void* operator new(size_t, void*);
208};
209
210struct X13 : X12 {
211 using X12::operator new;
212};
213
214static void* f(void* g)
215{
216 return new (g) X13();
217}
218