blob: 8cd7342e98ebc6e9b46eeef05e1a2dd7239e39bd [file] [log] [blame]
Daniel Dunbara5728872009-12-15 20:14:24 +00001// RUN: %clang_cc1 -fsyntax-only -verify %s
Douglas Gregord94546a2009-05-20 21:38:11 +00002
3// ---------------------------------------------------------------------
4// C++ Functional Casts
5// ---------------------------------------------------------------------
6template<int N>
7struct ValueInit0 {
8 int f() {
9 return int();
10 }
11};
12
13template struct ValueInit0<5>;
14
15template<int N>
16struct FunctionalCast0 {
17 int f() {
18 return int(N);
19 }
20};
21
22template struct FunctionalCast0<5>;
23
John McCall220ccbf2010-01-13 00:25:19 +000024struct X { // expected-note 3 {{candidate constructor (the implicit copy constructor)}}
John McCallb1622a12010-01-06 09:43:14 +000025 X(int, int); // expected-note 3 {{candidate constructor}}
Douglas Gregord94546a2009-05-20 21:38:11 +000026};
27
28template<int N, int M>
29struct BuildTemporary0 {
30 X f() {
31 return X(N, M);
32 }
33};
34
35template struct BuildTemporary0<5, 7>;
Douglas Gregore06274d2009-05-20 21:51:01 +000036
37template<int N, int M>
38struct Temporaries0 {
39 void f() {
40 (void)X(N, M);
41 }
42};
43
44template struct Temporaries0<5, 7>;
Douglas Gregor3433cf72009-05-21 00:00:09 +000045
Chandler Carrutha3ce8ae2010-03-31 18:34:58 +000046// Ensure that both the constructor and the destructor are instantiated by
47// checking for parse errors from each.
48template<int N> struct BadX {
49 BadX() { int a[-N]; } // expected-error {{array size is negative}}
50 ~BadX() { int a[-N]; } // expected-error {{array size is negative}}
51};
52
53template<int N>
54struct PR6671 {
55 void f() { (void)BadX<1>(); } // expected-note 2 {{instantiation}}
56};
57template struct PR6671<1>;
58
Douglas Gregor3433cf72009-05-21 00:00:09 +000059// ---------------------------------------------------------------------
Douglas Gregord0c02672009-05-21 17:21:12 +000060// new/delete expressions
Douglas Gregor3433cf72009-05-21 00:00:09 +000061// ---------------------------------------------------------------------
62struct Y { };
63
64template<typename T>
65struct New0 {
66 T* f(bool x) {
67 if (x)
68 return new T; // expected-error{{no matching}}
69 else
70 return new T();
71 }
72};
73
74template struct New0<int>;
75template struct New0<Y>;
76template struct New0<X>; // expected-note{{instantiation}}
77
78template<typename T, typename Arg1>
79struct New1 {
80 T* f(bool x, Arg1 a1) {
81 return new T(a1); // expected-error{{no matching}}
82 }
83};
84
85template struct New1<int, float>;
86template struct New1<Y, Y>;
87template struct New1<X, Y>; // expected-note{{instantiation}}
88
89template<typename T, typename Arg1, typename Arg2>
90struct New2 {
91 T* f(bool x, Arg1 a1, Arg2 a2) {
92 return new T(a1, a2); // expected-error{{no matching}}
93 }
94};
95
96template struct New2<X, int, float>;
97template struct New2<X, int, int*>; // expected-note{{instantiation}}
98// FIXME: template struct New2<int, int, float>;
Douglas Gregord0c02672009-05-21 17:21:12 +000099
Douglas Gregor5b5ad842009-12-22 17:13:37 +0000100// PR5833
101struct New3 {
102 New3();
103
104 void *operator new[](__SIZE_TYPE__) __attribute__((unavailable)); // expected-note{{explicitly made unavailable}}
105};
106
107template<class C>
108void* object_creator() {
109 return new C(); // expected-error{{call to unavailable function 'operator new[]'}}
110}
111
112template void *object_creator<New3[4]>(); // expected-note{{instantiation}}
113
Douglas Gregord0c02672009-05-21 17:21:12 +0000114template<typename T>
115struct Delete0 {
116 void f(T t) {
117 delete t; // expected-error{{cannot delete}}
118 ::delete [] t;
119 }
120};
121
122template struct Delete0<int*>;
123template struct Delete0<X*>;
124template struct Delete0<int>; // expected-note{{instantiation}}
Douglas Gregor42e5b502009-05-21 17:37:52 +0000125
Douglas Gregora88cfbf2009-12-12 18:16:41 +0000126namespace PR5755 {
127 template <class T>
128 void Foo() {
129 char* p = 0;
130 delete[] p;
131 }
132
133 void Test() {
134 Foo<int>();
135 }
136}
137
Douglas Gregor42e5b502009-05-21 17:37:52 +0000138// ---------------------------------------------------------------------
139// throw expressions
140// ---------------------------------------------------------------------
141template<typename T>
142struct Throw1 {
143 void f(T t) {
144 throw;
145 throw t; // expected-error{{incomplete type}}
146 }
147};
148
Douglas Gregor765ccba2009-12-23 21:06:06 +0000149struct Incomplete; // expected-note 2{{forward}}
Douglas Gregor42e5b502009-05-21 17:37:52 +0000150
151template struct Throw1<int>;
152template struct Throw1<int*>;
153template struct Throw1<Incomplete*>; // expected-note{{instantiation}}
154
Douglas Gregor12d0c302009-05-21 18:34:44 +0000155// ---------------------------------------------------------------------
156// typeid expressions
157// ---------------------------------------------------------------------
158
Douglas Gregor12d0c302009-05-21 18:34:44 +0000159namespace std {
160 class type_info;
161}
162
163template<typename T>
164struct TypeId0 {
165 const std::type_info &f(T* ptr) {
166 if (ptr)
167 return typeid(ptr);
168 else
John McCall7c2342d2010-03-10 11:27:22 +0000169 return typeid(T); // expected-error{{'typeid' of incomplete type 'Incomplete'}}
Douglas Gregor12d0c302009-05-21 18:34:44 +0000170 }
171};
172
173struct Abstract {
174 virtual void f() = 0;
175};
176
177template struct TypeId0<int>;
Douglas Gregor765ccba2009-12-23 21:06:06 +0000178template struct TypeId0<Incomplete>; // expected-note{{instantiation of member function}}
Douglas Gregor12d0c302009-05-21 18:34:44 +0000179template struct TypeId0<Abstract>;
Douglas Gregor36bb03b2009-05-21 18:55:48 +0000180
181// ---------------------------------------------------------------------
182// type traits
183// ---------------------------------------------------------------------
184template<typename T>
185struct is_pod {
186 static const bool value = __is_pod(T);
187};
188
Douglas Gregor60c93c92010-02-09 07:26:29 +0000189static int is_pod0[is_pod<X>::value? -1 : 1];
190static int is_pod1[is_pod<Y>::value? 1 : -1];
Douglas Gregorccb97f52009-05-21 21:38:12 +0000191
192// ---------------------------------------------------------------------
193// initializer lists
194// ---------------------------------------------------------------------
195template<typename T, typename Val1>
196struct InitList1 {
197 void f(Val1 val1) {
198 T x = { val1 };
199 }
200};
201
202struct APair {
203 int *x;
204 const float *y;
205};
206
207template struct InitList1<int[1], float>;
208template struct InitList1<APair, int*>;
209
210template<typename T, typename Val1, typename Val2>
211struct InitList2 {
212 void f(Val1 val1, Val2 val2) {
Anders Carlsson2bbae5d2010-01-23 20:20:40 +0000213 T x = { val1, val2 }; // expected-error{{cannot initialize}}
Douglas Gregorccb97f52009-05-21 21:38:12 +0000214 }
215};
216
217template struct InitList2<APair, int*, float*>;
218template struct InitList2<APair, int*, double*>; // expected-note{{instantiation}}
Douglas Gregor1c0ca592009-05-22 21:13:27 +0000219
220// ---------------------------------------------------------------------
221// member references
222// ---------------------------------------------------------------------
223template<typename T, typename Result>
224struct DotMemRef0 {
225 void f(T t) {
Douglas Gregor20093b42009-12-09 23:02:17 +0000226 Result result = t.m; // expected-error{{non-const lvalue reference to type}}
Douglas Gregor1c0ca592009-05-22 21:13:27 +0000227 }
228};
229
230struct MemInt {
231 int m;
232};
233
234struct InheritsMemInt : MemInt { };
235
236struct MemIntFunc {
237 static int m(int);
238};
239
240template struct DotMemRef0<MemInt, int&>;
241template struct DotMemRef0<InheritsMemInt, int&>;
242template struct DotMemRef0<MemIntFunc, int (*)(int)>;
243template struct DotMemRef0<MemInt, float&>; // expected-note{{instantiation}}
244
245template<typename T, typename Result>
246struct ArrowMemRef0 {
247 void f(T t) {
Douglas Gregor20093b42009-12-09 23:02:17 +0000248 Result result = t->m; // expected-error 2{{non-const lvalue reference}}
Douglas Gregor1c0ca592009-05-22 21:13:27 +0000249 }
250};
251
252template<typename T>
253struct ArrowWrapper {
254 T operator->();
255};
256
257template struct ArrowMemRef0<MemInt*, int&>;
258template struct ArrowMemRef0<InheritsMemInt*, int&>;
259template struct ArrowMemRef0<MemIntFunc*, int (*)(int)>;
260template struct ArrowMemRef0<MemInt*, float&>; // expected-note{{instantiation}}
261
262template struct ArrowMemRef0<ArrowWrapper<MemInt*>, int&>;
263template struct ArrowMemRef0<ArrowWrapper<InheritsMemInt*>, int&>;
264template struct ArrowMemRef0<ArrowWrapper<MemIntFunc*>, int (*)(int)>;
265template struct ArrowMemRef0<ArrowWrapper<MemInt*>, float&>; // expected-note{{instantiation}}
266template struct ArrowMemRef0<ArrowWrapper<ArrowWrapper<MemInt*> >, int&>;
267
268// FIXME: we should be able to return a MemInt without the reference!
269MemInt &createMemInt(int);
270
271template<int N>
272struct NonDepMemberExpr0 {
273 void f() {
274 createMemInt(N).m = N;
275 }
276};
277
278template struct NonDepMemberExpr0<0>;
Douglas Gregor08d3e7c2009-05-22 21:26:58 +0000279
280template<typename T, typename Result>
281struct MemberFuncCall0 {
282 void f(T t) {
283 Result result = t.f();
284 }
285};
286
287template<typename T>
288struct HasMemFunc0 {
289 T f();
290};
291
292
293template struct MemberFuncCall0<HasMemFunc0<int&>, const int&>;
294
295template<typename Result>
296struct ThisMemberFuncCall0 {
297 Result g();
298
299 void f() {
300 Result r1 = g();
301 Result r2 = this->g();
302 }
303};
304
305template struct ThisMemberFuncCall0<int&>;
306
307template<typename T>
308struct NonDepMemberCall0 {
309 void foo(HasMemFunc0<int&> x) {
Douglas Gregor20093b42009-12-09 23:02:17 +0000310 T result = x.f(); // expected-error{{non-const lvalue reference}}
Douglas Gregor08d3e7c2009-05-22 21:26:58 +0000311 }
312};
313
314template struct NonDepMemberCall0<int&>;
315template struct NonDepMemberCall0<const int&>;
316template struct NonDepMemberCall0<float&>; // expected-note{{instantiation}}
Douglas Gregore30d0bd2009-05-22 23:47:06 +0000317
318
319template<typename T>
320struct QualifiedDeclRef0 {
321 T f() {
Douglas Gregor18ef5e22009-12-18 05:02:21 +0000322 return is_pod<X>::value; // expected-error{{non-const lvalue reference to type 'int' cannot bind to a value of unrelated type 'bool const'}}
Douglas Gregore30d0bd2009-05-22 23:47:06 +0000323 }
324};
325
326template struct QualifiedDeclRef0<bool>;
327template struct QualifiedDeclRef0<int&>; // expected-note{{instantiation}}