1 // RUN: %clang_cc1 -fcxx-exceptions -fexceptions -fsyntax-only -verify %s
2 // RUN: %clang_cc1 -fcxx-exceptions -fexceptions -fsyntax-only -verify -std=c++98 %s
3 // RUN: %clang_cc1 -fcxx-exceptions -fexceptions -fsyntax-only -verify -std=c++11 %s
4 
5 // ---------------------------------------------------------------------
6 // C++ Functional Casts
7 // ---------------------------------------------------------------------
8 template<int N>
9 struct ValueInit0 {
fValueInit010   int f() {
11     return int();
12   }
13 };
14 
15 template struct ValueInit0<5>;
16 
17 template<int N>
18 struct FunctionalCast0 {
fFunctionalCast019   int f() {
20     return int(N);
21   }
22 };
23 
24 template struct FunctionalCast0<5>;
25 
26 struct X { // expected-note 3 {{candidate constructor (the implicit copy constructor)}}
27 #if __cplusplus >= 201103L
28 // expected-note@-2 3 {{candidate constructor (the implicit move constructor) not viable}}
29 #endif
30   X(int, int); // expected-note 3 {{candidate constructor}}
31 };
32 
33 template<int N, int M>
34 struct BuildTemporary0 {
fBuildTemporary035   X f() {
36     return X(N, M);
37   }
38 };
39 
40 template struct BuildTemporary0<5, 7>;
41 
42 template<int N, int M>
43 struct Temporaries0 {
fTemporaries044   void f() {
45     (void)X(N, M);
46   }
47 };
48 
49 template struct Temporaries0<5, 7>;
50 
51 // Ensure that both the constructor and the destructor are instantiated by
52 // checking for parse errors from each.
53 template<int N> struct BadX {
BadXBadX54   BadX() { int a[-N]; } // expected-error {{array with a negative size}}
~BadXBadX55   ~BadX() { int a[-N]; } // expected-error {{array with a negative size}}
56 };
57 
58 template<int N>
59 struct PR6671 {
fPR667160   void f() { (void)BadX<1>(); } // expected-note 2 {{instantiation}}
61 };
62 template struct PR6671<1>;
63 
64 // ---------------------------------------------------------------------
65 // new/delete expressions
66 // ---------------------------------------------------------------------
67 struct Y { };
68 
69 template<typename T>
70 struct New0 {
fNew071   T* f(bool x) {
72     if (x)
73       return new T; // expected-error{{no matching}}
74     else
75       return new T();
76   }
77 };
78 
79 template struct New0<int>;
80 template struct New0<Y>;
81 template struct New0<X>; // expected-note{{instantiation}}
82 
83 template<typename T, typename Arg1>
84 struct New1 {
fNew185   T* f(bool x, Arg1 a1) {
86     return new T(a1); // expected-error{{no matching}}
87   }
88 };
89 
90 template struct New1<int, float>;
91 template struct New1<Y, Y>;
92 template struct New1<X, Y>; // expected-note{{instantiation}}
93 
94 template<typename T, typename Arg1, typename Arg2>
95 struct New2 {
fNew296   T* f(bool x, Arg1 a1, Arg2 a2) {
97     return new T(a1, a2); // expected-error{{no matching}}
98   }
99 };
100 
101 template struct New2<X, int, float>;
102 template struct New2<X, int, int*>; // expected-note{{instantiation}}
103 // FIXME: template struct New2<int, int, float>;
104 
105 // PR5833
106 struct New3 {
107   New3();
108 
109   void *operator new[](__SIZE_TYPE__) __attribute__((unavailable)); // expected-note{{explicitly marked unavailable here}}
110 };
111 
112 template<class C>
object_creator()113 void* object_creator() {
114   return new C(); // expected-error{{'operator new[]' is unavailable}}
115 }
116 
117 template void *object_creator<New3[4]>(); // expected-note{{instantiation}}
118 
119 template<typename T>
120 struct Delete0 {
fDelete0121   void f(T t) {
122     delete t; // expected-error{{cannot delete}}
123     ::delete [] t; // expected-error{{cannot delete}}
124   }
125 };
126 
127 template struct Delete0<int*>;
128 template struct Delete0<X*>;
129 template struct Delete0<int>; // expected-note{{instantiation}}
130 
131 namespace PR5755 {
132   template <class T>
Foo()133   void Foo() {
134     char* p = 0;
135     delete[] p;
136   }
137 
Test()138   void Test() {
139     Foo<int>();
140   }
141 }
142 
143 namespace PR10480 {
144   template<typename T>
145   struct X {
146     X();
~XPR10480::X147     ~X() {
148       T *ptr = 1; // expected-error{{cannot initialize a variable of type 'int *' with an rvalue of type 'int'}}
149     }
150   };
151 
152   template<typename T>
f()153   void f() {
154     new X<int>[1]; // expected-note{{in instantiation of member function 'PR10480::X<int>::~X' requested here}}
155   }
156 
157   template void f<int>();
158 }
159 
160 // ---------------------------------------------------------------------
161 // throw expressions
162 // ---------------------------------------------------------------------
163 template<typename T>
164 struct Throw1 {
fThrow1165   void f(T t) {
166     throw;
167     throw t; // expected-error{{incomplete type}}
168   }
169 };
170 
171 struct Incomplete; // expected-note 2{{forward}}
172 
173 template struct Throw1<int>;
174 template struct Throw1<int*>;
175 template struct Throw1<Incomplete*>; // expected-note{{instantiation}}
176 
177 // ---------------------------------------------------------------------
178 // typeid expressions
179 // ---------------------------------------------------------------------
180 
181 namespace std {
182   class type_info;
183 }
184 
185 template<typename T>
186 struct TypeId0 {
fTypeId0187   const std::type_info &f(T* ptr) {
188     if (ptr)
189       return typeid(ptr);
190     else
191       return typeid(T); // expected-error{{'typeid' of incomplete type 'Incomplete'}}
192   }
193 };
194 
195 template<typename T>
196 struct TypeId1 {
fTypeId1197   const std::type_info &f() {
198     return typeid(T); // expected-error-re 2{{type operand 'void () {{const|&}}' of 'typeid' cannot have '{{const|&}}' qualifier}}
199   }
200 };
201 
202 struct Abstract {
203   virtual void f() = 0;
204 };
205 
206 template struct TypeId0<int>;
207 template struct TypeId0<Incomplete>; // expected-note{{instantiation of member function}}
208 template struct TypeId0<Abstract>;
209 template struct TypeId1<void() const>; // expected-note{{instantiation of}}
210 template struct TypeId1<void() &>; // expected-warning 0-1{{C++11}} expected-note{{instantiation of}}
211 
212 // ---------------------------------------------------------------------
213 // type traits
214 // ---------------------------------------------------------------------
215 template<typename T>
216 struct is_pod {
217   static const bool value = __is_pod(T);
218 };
219 
220 static int is_pod0[is_pod<X>::value? -1 : 1];
221 static int is_pod1[is_pod<Y>::value? 1 : -1];
222 
223 // ---------------------------------------------------------------------
224 // initializer lists
225 // ---------------------------------------------------------------------
226 template<typename T, typename Val1>
227 struct InitList1 {
fInitList1228   void f(Val1 val1) {
229     T x = { val1 };
230 #if __cplusplus >= 201103L
231     // expected-error@-2 {{type 'float' cannot be narrowed to 'int' in initializer list}}
232     // expected-note@-3 {{insert an explicit cast to silence this issue}}
233 #endif
234   }
235 };
236 
237 struct APair {
238   int *x;
239   const float *y;
240 };
241 
242 template struct InitList1<int[1], float>;
243 #if __cplusplus >= 201103L
244 // expected-note@-2 {{instantiation of member function}}
245 #endif
246 template struct InitList1<APair, int*>;
247 
248 template<typename T, typename Val1, typename Val2>
249 struct InitList2 {
fInitList2250   void f(Val1 val1, Val2 val2) {
251     T x = { val1, val2 }; // expected-error{{cannot initialize}}
252   }
253 };
254 
255 template struct InitList2<APair, int*, float*>;
256 template struct InitList2<APair, int*, double*>; // expected-note{{instantiation}}
257 
258 // ---------------------------------------------------------------------
259 // member references
260 // ---------------------------------------------------------------------
261 template<typename T, typename Result>
262 struct DotMemRef0 {
fDotMemRef0263   void f(T t) {
264     Result result = t.m; // expected-error{{non-const lvalue reference to type}}
265   }
266 };
267 
268 struct MemInt {
269   int m;
270 };
271 
272 struct InheritsMemInt : MemInt { };
273 
274 struct MemIntFunc {
275   static int m(int);
276 };
277 
278 template struct DotMemRef0<MemInt, int&>;
279 template struct DotMemRef0<InheritsMemInt, int&>;
280 template struct DotMemRef0<MemIntFunc, int (*)(int)>;
281 template struct DotMemRef0<MemInt, float&>; // expected-note{{instantiation}}
282 
283 template<typename T, typename Result>
284 struct ArrowMemRef0 {
fArrowMemRef0285   void f(T t) {
286     Result result = t->m; // expected-error 2{{non-const lvalue reference}}
287   }
288 };
289 
290 template<typename T>
291 struct ArrowWrapper {
292   T operator->();
293 };
294 
295 template struct ArrowMemRef0<MemInt*, int&>;
296 template struct ArrowMemRef0<InheritsMemInt*, int&>;
297 template struct ArrowMemRef0<MemIntFunc*, int (*)(int)>;
298 template struct ArrowMemRef0<MemInt*, float&>; // expected-note{{instantiation}}
299 
300 template struct ArrowMemRef0<ArrowWrapper<MemInt*>, int&>;
301 template struct ArrowMemRef0<ArrowWrapper<InheritsMemInt*>, int&>;
302 template struct ArrowMemRef0<ArrowWrapper<MemIntFunc*>, int (*)(int)>;
303 template struct ArrowMemRef0<ArrowWrapper<MemInt*>, float&>; // expected-note{{instantiation}}
304 template struct ArrowMemRef0<ArrowWrapper<ArrowWrapper<MemInt*> >, int&>;
305 
306 struct UnresolvedMemRefArray {
307   int f(int);
308   int f(char);
309 };
310 UnresolvedMemRefArray Arr[10];
UnresolvedMemRefArrayT(U u)311 template<typename U> int UnresolvedMemRefArrayT(U u) {
312   return Arr->f(u);
313 }
314 template int UnresolvedMemRefArrayT<int>(int);
315 
316 // FIXME: we should be able to return a MemInt without the reference!
317 MemInt &createMemInt(int);
318 
319 template<int N>
320 struct NonDepMemberExpr0 {
fNonDepMemberExpr0321   void f() {
322     createMemInt(N).m = N;
323   }
324 };
325 
326 template struct NonDepMemberExpr0<0>;
327 
328 template<typename T, typename Result>
329 struct MemberFuncCall0 {
fMemberFuncCall0330   void f(T t) {
331     Result result = t.f();
332   }
333 };
334 
335 template<typename T>
336 struct HasMemFunc0 {
337   T f();
338 };
339 
340 
341 template struct MemberFuncCall0<HasMemFunc0<int&>, const int&>;
342 
343 template<typename Result>
344 struct ThisMemberFuncCall0 {
345   Result g();
346 
fThisMemberFuncCall0347   void f() {
348     Result r1 = g();
349     Result r2 = this->g();
350   }
351 };
352 
353 template struct ThisMemberFuncCall0<int&>;
354 
355 template<typename T>
356 struct NonDepMemberCall0 {
fooNonDepMemberCall0357   void foo(HasMemFunc0<int&> x) {
358     T result = x.f(); // expected-error{{non-const lvalue reference}}
359   }
360 };
361 
362 template struct NonDepMemberCall0<int&>;
363 template struct NonDepMemberCall0<const int&>;
364 template struct NonDepMemberCall0<float&>; // expected-note{{instantiation}}
365 
366 
367 template<typename T>
368 struct QualifiedDeclRef0 {
fQualifiedDeclRef0369   T f() {
370     return is_pod<X>::value; // expected-error{{non-const lvalue reference to type 'int' cannot bind to a value of unrelated type 'const bool'}}
371   }
372 };
373 
374 template struct QualifiedDeclRef0<bool>;
375 template struct QualifiedDeclRef0<int&>; // expected-note{{instantiation}}
376