blob: 5406fbcd1baf7da8a6611b1cae0dbfc28029a7a9 [file] [log] [blame]
Anders Carlssonabea9512011-02-28 00:40:07 +00001// RUN: %clang_cc1 -fcxx-exceptions -fexceptions -fsyntax-only -verify %s
Douglas Gregor54dabfc2009-05-14 23:26:13 +00002template<typename T, typename U>
3struct X0 {
4 void f(T x, U y) {
Douglas Gregord7e27052009-05-20 22:33:37 +00005 (void)(x + y); // expected-error{{invalid operands}}
Douglas Gregor54dabfc2009-05-14 23:26:13 +00006 }
7};
8
9struct X1 { };
10
11template struct X0<int, float>;
12template struct X0<int*, int>;
13template struct X0<int X1::*, int>; // expected-note{{instantiation of}}
Douglas Gregore7a18c82009-05-14 23:40:54 +000014
15template<typename T>
16struct X2 {
17 void f(T);
18
19 T g(T x, T y) {
Douglas Gregorb9f1b8d2009-05-15 00:01:03 +000020 /* DeclStmt */;
21 T *xp = &x, &yr = y; // expected-error{{pointer to a reference}}
Douglas Gregore7a18c82009-05-14 23:40:54 +000022 /* NullStmt */;
23 }
24};
25
26template struct X2<int>;
Douglas Gregorb9f1b8d2009-05-15 00:01:03 +000027template struct X2<int&>; // expected-note{{instantiation of}}
Anders Carlsson137fa562009-05-15 00:15:26 +000028
29template<typename T>
30struct X3 {
31 void f(T) {
32 Label:
33 T x;
34 goto Label;
35 }
36};
37
38template struct X3<int>;
Anders Carlsson03d77762009-05-15 00:48:27 +000039
40template <typename T> struct X4 {
41 T f() const {
Chris Lattner184aa4e2010-07-11 23:34:02 +000042 return; // expected-error{{non-void function 'f' should return a value}}
Anders Carlsson03d77762009-05-15 00:48:27 +000043 }
44
45 T g() const {
Chris Lattner184aa4e2010-07-11 23:34:02 +000046 return 1; // expected-error{{void function 'g' should not return a value}}
Anders Carlsson03d77762009-05-15 00:48:27 +000047 }
48};
49
Douglas Gregorf3e7ce42009-05-18 17:01:57 +000050template struct X4<void>; // expected-note{{in instantiation of}}
51template struct X4<int>; // expected-note{{in instantiation of}}
Douglas Gregore2c31ff2009-05-15 17:59:04 +000052
Douglas Gregord308e622009-05-18 20:51:54 +000053struct Incomplete; // expected-note 2{{forward declaration}}
Douglas Gregore2c31ff2009-05-15 17:59:04 +000054
55template<typename T> struct X5 {
56 T f() { } // expected-error{{incomplete result type}}
57};
58void test_X5(X5<Incomplete> x5); // okay!
59
60template struct X5<Incomplete>; // expected-note{{instantiation}}
Douglas Gregord06f6ca2009-05-15 18:53:42 +000061
62template<typename T, typename U, typename V> struct X6 {
63 U f(T t, U u, V v) {
64 // IfStmt
65 if (t > 0)
66 return u;
Douglas Gregor49f25ec2009-05-15 21:18:27 +000067 else {
68 if (t < 0)
Douglas Gregor18ef5e22009-12-18 05:02:21 +000069 return v; // expected-error{{cannot initialize return object of type}}
Douglas Gregor49f25ec2009-05-15 21:18:27 +000070 }
71
Douglas Gregore06274d2009-05-20 21:51:01 +000072 if (T x = t) {
73 t = x;
74 }
John McCall7114cba2010-08-27 19:56:05 +000075 return v; // expected-error{{cannot initialize return object of type}}
Douglas Gregord06f6ca2009-05-15 18:53:42 +000076 }
77};
78
79struct ConvertibleToInt {
80 operator int() const;
81};
82
83template struct X6<ConvertibleToInt, float, char>;
84template struct X6<bool, int, int*>; // expected-note{{instantiation}}
Anders Carlsson0712d292009-05-15 20:26:03 +000085
86template <typename T> struct X7 {
87 void f() {
88 void *v = this;
89 }
90};
91
92template struct X7<int>;
Douglas Gregor4a2e2042009-05-15 21:45:53 +000093
94template<typename T> struct While0 {
95 void f(T t) {
96 while (t) {
97 }
98
99 while (T t2 = T()) ;
100 }
101};
102
103template struct While0<float>;
Douglas Gregor9f3ca2a2009-05-15 21:56:04 +0000104
105template<typename T> struct Do0 {
106 void f(T t) {
107 do {
108 } while (t); // expected-error{{not contextually}}
Douglas Gregor9f3ca2a2009-05-15 21:56:04 +0000109 }
110};
111
112struct NotConvertibleToBool { };
113template struct Do0<ConvertibleToInt>;
114template struct Do0<NotConvertibleToBool>; // expected-note{{instantiation}}
Douglas Gregor5831c6a2009-05-15 22:12:32 +0000115
116template<typename T> struct For0 {
117 void f(T f, T l) {
118 for (; f != l; ++f) {
Douglas Gregor861ce312009-05-15 22:32:39 +0000119 if (*f)
120 continue;
121 else if (*f == 17)
122 break;
Douglas Gregor5831c6a2009-05-15 22:12:32 +0000123 }
124 }
125};
126
127template struct For0<int*>;
Anders Carlssonffce2df2009-05-15 23:10:19 +0000128
129template<typename T> struct Member0 {
130 void f(T t) {
131 t;
132 t.f;
133 t->f;
134
135 T* tp;
136 tp.f; // expected-error{{member reference base type 'T *' is not a structure or union}}
137 tp->f;
138
139 this->f;
Anders Carlsson4e579922009-07-10 21:35:09 +0000140 this.f; // expected-error{{member reference base type 'Member0<T> *' is not a structure or union}}
Anders Carlssonffce2df2009-05-15 23:10:19 +0000141 }
142};
Douglas Gregordbb26db2009-05-15 23:57:33 +0000143
144template<typename T, typename U> struct Switch0 {
145 U f(T value, U v0, U v1, U v2) {
146 switch (value) {
147 case 0: return v0;
148
149 case 1: return v1;
150
151 case 2: // fall through
152
153 default:
154 return v2;
155 }
156 }
157};
158
159template struct Switch0<int, float>;
160
161template<typename T, int I1, int I2> struct Switch1 {
162 T f(T x, T y, T z) {
163 switch (x) {
164 case I1: return y; // expected-note{{previous}}
165 case I2: return z; // expected-error{{duplicate}}
166 default: return x;
167 }
168 }
169};
170
171template struct Switch1<int, 1, 2>;
172template struct Switch1<int, 2, 2>; // expected-note{{instantiation}}
Douglas Gregor5f1b9e62009-05-16 00:20:29 +0000173
174template<typename T> struct IndirectGoto0 {
175 void f(T x) {
176 // FIXME: crummy error message below
177 goto *x; // expected-error{{incompatible}}
Douglas Gregor76658232009-05-22 23:25:52 +0000178
179 prior:
Chris Lattner632c9d22010-03-01 20:56:44 +0000180 T prior_label;
John McCall7114cba2010-08-27 19:56:05 +0000181 prior_label = &&prior; // expected-error{{assigning to 'int'}}
Douglas Gregor76658232009-05-22 23:25:52 +0000182
Chris Lattner632c9d22010-03-01 20:56:44 +0000183 T later_label;
John McCall7114cba2010-08-27 19:56:05 +0000184 later_label = &&later; // expected-error{{assigning to 'int'}}
Douglas Gregor76658232009-05-22 23:25:52 +0000185
186 later:
187 (void)(1+1);
Douglas Gregor5f1b9e62009-05-16 00:20:29 +0000188 }
189};
190
191template struct IndirectGoto0<void*>;
192template struct IndirectGoto0<int>; // expected-note{{instantiation}}
Douglas Gregord308e622009-05-18 20:51:54 +0000193
194template<typename T> struct TryCatch0 {
195 void f() {
196 try {
Douglas Gregora2762912010-03-08 01:47:36 +0000197 } catch (T t) { // expected-warning{{incomplete type}} \
Douglas Gregord308e622009-05-18 20:51:54 +0000198 // expected-error{{abstract class}}
199 } catch (...) {
200 }
201 }
202};
203
204struct Abstract {
205 virtual void foo() = 0; // expected-note{{pure virtual}}
206};
207
208template struct TryCatch0<int>; // okay
209template struct TryCatch0<Incomplete*>; // expected-note{{instantiation}}
210template struct TryCatch0<Abstract>; // expected-note{{instantiation}}
Anders Carlsson31a08752009-06-13 02:59:33 +0000211
212// PR4383
213template<typename T> struct X;
214template<typename T> struct Y : public X<T> {
215 Y& x() { return *this; }
216};
John McCall069ace52010-03-11 09:33:17 +0000217
218// Make sure our assertions don't get too uppity.
219namespace test0 {
220 template <class T> class A { void foo(T array[10]); };
221 template class A<int>;
222}
Douglas Gregorf7d72f52010-05-03 20:22:41 +0000223
224namespace PR7016 {
225 template<typename T> void f() { T x = x; }
226 template void f<int>();
227}
Douglas Gregorc056c172011-05-09 20:45:16 +0000228
229namespace PR9880 {
230 struct lua_State;
231 struct no_tag { char a; }; // (A)
232 struct yes_tag { long a; long b; }; // (A)
233
234 template <typename T>
235 struct HasIndexMetamethod {
236 template <typename U>
237 static no_tag check(...);
238 template <typename U>
239 static yes_tag check(char[sizeof(&U::luaIndex)]);
240 enum { value = sizeof(check<T>(0)) == sizeof(yes_tag) };
241 };
242
243 class SomeClass {
244 public:
245 int luaIndex(lua_State* L);
246 };
247
248 int i = HasIndexMetamethod<SomeClass>::value;
249}