Daniel Dunbar | a572887 | 2009-12-15 20:14:24 +0000 | [diff] [blame] | 1 | // RUN: %clang_cc1 -fsyntax-only -verify %s |
Douglas Gregor | d94546a | 2009-05-20 21:38:11 +0000 | [diff] [blame] | 2 | |
| 3 | // --------------------------------------------------------------------- |
| 4 | // C++ Functional Casts |
| 5 | // --------------------------------------------------------------------- |
| 6 | template<int N> |
| 7 | struct ValueInit0 { |
| 8 | int f() { |
| 9 | return int(); |
| 10 | } |
| 11 | }; |
| 12 | |
| 13 | template struct ValueInit0<5>; |
| 14 | |
| 15 | template<int N> |
| 16 | struct FunctionalCast0 { |
| 17 | int f() { |
| 18 | return int(N); |
| 19 | } |
| 20 | }; |
| 21 | |
| 22 | template struct FunctionalCast0<5>; |
| 23 | |
John McCall | 220ccbf | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 24 | struct X { // expected-note 3 {{candidate constructor (the implicit copy constructor)}} |
John McCall | b1622a1 | 2010-01-06 09:43:14 +0000 | [diff] [blame] | 25 | X(int, int); // expected-note 3 {{candidate constructor}} |
Douglas Gregor | d94546a | 2009-05-20 21:38:11 +0000 | [diff] [blame] | 26 | }; |
| 27 | |
| 28 | template<int N, int M> |
| 29 | struct BuildTemporary0 { |
| 30 | X f() { |
| 31 | return X(N, M); |
| 32 | } |
| 33 | }; |
| 34 | |
| 35 | template struct BuildTemporary0<5, 7>; |
Douglas Gregor | e06274d | 2009-05-20 21:51:01 +0000 | [diff] [blame] | 36 | |
| 37 | template<int N, int M> |
| 38 | struct Temporaries0 { |
| 39 | void f() { |
| 40 | (void)X(N, M); |
| 41 | } |
| 42 | }; |
| 43 | |
| 44 | template struct Temporaries0<5, 7>; |
Douglas Gregor | 3433cf7 | 2009-05-21 00:00:09 +0000 | [diff] [blame] | 45 | |
Chandler Carruth | a3ce8ae | 2010-03-31 18:34:58 +0000 | [diff] [blame] | 46 | // Ensure that both the constructor and the destructor are instantiated by |
| 47 | // checking for parse errors from each. |
| 48 | template<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 | |
| 53 | template<int N> |
| 54 | struct PR6671 { |
| 55 | void f() { (void)BadX<1>(); } // expected-note 2 {{instantiation}} |
| 56 | }; |
| 57 | template struct PR6671<1>; |
| 58 | |
Douglas Gregor | 3433cf7 | 2009-05-21 00:00:09 +0000 | [diff] [blame] | 59 | // --------------------------------------------------------------------- |
Douglas Gregor | d0c0267 | 2009-05-21 17:21:12 +0000 | [diff] [blame] | 60 | // new/delete expressions |
Douglas Gregor | 3433cf7 | 2009-05-21 00:00:09 +0000 | [diff] [blame] | 61 | // --------------------------------------------------------------------- |
| 62 | struct Y { }; |
| 63 | |
| 64 | template<typename T> |
| 65 | struct 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 | |
| 74 | template struct New0<int>; |
| 75 | template struct New0<Y>; |
| 76 | template struct New0<X>; // expected-note{{instantiation}} |
| 77 | |
| 78 | template<typename T, typename Arg1> |
| 79 | struct New1 { |
| 80 | T* f(bool x, Arg1 a1) { |
| 81 | return new T(a1); // expected-error{{no matching}} |
| 82 | } |
| 83 | }; |
| 84 | |
| 85 | template struct New1<int, float>; |
| 86 | template struct New1<Y, Y>; |
| 87 | template struct New1<X, Y>; // expected-note{{instantiation}} |
| 88 | |
| 89 | template<typename T, typename Arg1, typename Arg2> |
| 90 | struct New2 { |
| 91 | T* f(bool x, Arg1 a1, Arg2 a2) { |
| 92 | return new T(a1, a2); // expected-error{{no matching}} |
| 93 | } |
| 94 | }; |
| 95 | |
| 96 | template struct New2<X, int, float>; |
| 97 | template struct New2<X, int, int*>; // expected-note{{instantiation}} |
| 98 | // FIXME: template struct New2<int, int, float>; |
Douglas Gregor | d0c0267 | 2009-05-21 17:21:12 +0000 | [diff] [blame] | 99 | |
Douglas Gregor | 5b5ad84 | 2009-12-22 17:13:37 +0000 | [diff] [blame] | 100 | // PR5833 |
| 101 | struct New3 { |
| 102 | New3(); |
| 103 | |
| 104 | void *operator new[](__SIZE_TYPE__) __attribute__((unavailable)); // expected-note{{explicitly made unavailable}} |
| 105 | }; |
| 106 | |
| 107 | template<class C> |
| 108 | void* object_creator() { |
| 109 | return new C(); // expected-error{{call to unavailable function 'operator new[]'}} |
| 110 | } |
| 111 | |
| 112 | template void *object_creator<New3[4]>(); // expected-note{{instantiation}} |
| 113 | |
Douglas Gregor | d0c0267 | 2009-05-21 17:21:12 +0000 | [diff] [blame] | 114 | template<typename T> |
| 115 | struct Delete0 { |
| 116 | void f(T t) { |
| 117 | delete t; // expected-error{{cannot delete}} |
| 118 | ::delete [] t; |
| 119 | } |
| 120 | }; |
| 121 | |
| 122 | template struct Delete0<int*>; |
| 123 | template struct Delete0<X*>; |
| 124 | template struct Delete0<int>; // expected-note{{instantiation}} |
Douglas Gregor | 42e5b50 | 2009-05-21 17:37:52 +0000 | [diff] [blame] | 125 | |
Douglas Gregor | a88cfbf | 2009-12-12 18:16:41 +0000 | [diff] [blame] | 126 | namespace 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 Gregor | 42e5b50 | 2009-05-21 17:37:52 +0000 | [diff] [blame] | 138 | // --------------------------------------------------------------------- |
| 139 | // throw expressions |
| 140 | // --------------------------------------------------------------------- |
| 141 | template<typename T> |
| 142 | struct Throw1 { |
| 143 | void f(T t) { |
| 144 | throw; |
| 145 | throw t; // expected-error{{incomplete type}} |
| 146 | } |
| 147 | }; |
| 148 | |
Douglas Gregor | 765ccba | 2009-12-23 21:06:06 +0000 | [diff] [blame] | 149 | struct Incomplete; // expected-note 2{{forward}} |
Douglas Gregor | 42e5b50 | 2009-05-21 17:37:52 +0000 | [diff] [blame] | 150 | |
| 151 | template struct Throw1<int>; |
| 152 | template struct Throw1<int*>; |
| 153 | template struct Throw1<Incomplete*>; // expected-note{{instantiation}} |
| 154 | |
Douglas Gregor | 12d0c30 | 2009-05-21 18:34:44 +0000 | [diff] [blame] | 155 | // --------------------------------------------------------------------- |
| 156 | // typeid expressions |
| 157 | // --------------------------------------------------------------------- |
| 158 | |
Douglas Gregor | 12d0c30 | 2009-05-21 18:34:44 +0000 | [diff] [blame] | 159 | namespace std { |
| 160 | class type_info; |
| 161 | } |
| 162 | |
| 163 | template<typename T> |
| 164 | struct TypeId0 { |
| 165 | const std::type_info &f(T* ptr) { |
| 166 | if (ptr) |
| 167 | return typeid(ptr); |
| 168 | else |
John McCall | 7c2342d | 2010-03-10 11:27:22 +0000 | [diff] [blame] | 169 | return typeid(T); // expected-error{{'typeid' of incomplete type 'Incomplete'}} |
Douglas Gregor | 12d0c30 | 2009-05-21 18:34:44 +0000 | [diff] [blame] | 170 | } |
| 171 | }; |
| 172 | |
| 173 | struct Abstract { |
| 174 | virtual void f() = 0; |
| 175 | }; |
| 176 | |
| 177 | template struct TypeId0<int>; |
Douglas Gregor | 765ccba | 2009-12-23 21:06:06 +0000 | [diff] [blame] | 178 | template struct TypeId0<Incomplete>; // expected-note{{instantiation of member function}} |
Douglas Gregor | 12d0c30 | 2009-05-21 18:34:44 +0000 | [diff] [blame] | 179 | template struct TypeId0<Abstract>; |
Douglas Gregor | 36bb03b | 2009-05-21 18:55:48 +0000 | [diff] [blame] | 180 | |
| 181 | // --------------------------------------------------------------------- |
| 182 | // type traits |
| 183 | // --------------------------------------------------------------------- |
| 184 | template<typename T> |
| 185 | struct is_pod { |
| 186 | static const bool value = __is_pod(T); |
| 187 | }; |
| 188 | |
Douglas Gregor | 60c93c9 | 2010-02-09 07:26:29 +0000 | [diff] [blame] | 189 | static int is_pod0[is_pod<X>::value? -1 : 1]; |
| 190 | static int is_pod1[is_pod<Y>::value? 1 : -1]; |
Douglas Gregor | ccb97f5 | 2009-05-21 21:38:12 +0000 | [diff] [blame] | 191 | |
| 192 | // --------------------------------------------------------------------- |
| 193 | // initializer lists |
| 194 | // --------------------------------------------------------------------- |
| 195 | template<typename T, typename Val1> |
| 196 | struct InitList1 { |
| 197 | void f(Val1 val1) { |
| 198 | T x = { val1 }; |
| 199 | } |
| 200 | }; |
| 201 | |
| 202 | struct APair { |
| 203 | int *x; |
| 204 | const float *y; |
| 205 | }; |
| 206 | |
| 207 | template struct InitList1<int[1], float>; |
| 208 | template struct InitList1<APair, int*>; |
| 209 | |
| 210 | template<typename T, typename Val1, typename Val2> |
| 211 | struct InitList2 { |
| 212 | void f(Val1 val1, Val2 val2) { |
Anders Carlsson | 2bbae5d | 2010-01-23 20:20:40 +0000 | [diff] [blame] | 213 | T x = { val1, val2 }; // expected-error{{cannot initialize}} |
Douglas Gregor | ccb97f5 | 2009-05-21 21:38:12 +0000 | [diff] [blame] | 214 | } |
| 215 | }; |
| 216 | |
| 217 | template struct InitList2<APair, int*, float*>; |
| 218 | template struct InitList2<APair, int*, double*>; // expected-note{{instantiation}} |
Douglas Gregor | 1c0ca59 | 2009-05-22 21:13:27 +0000 | [diff] [blame] | 219 | |
| 220 | // --------------------------------------------------------------------- |
| 221 | // member references |
| 222 | // --------------------------------------------------------------------- |
| 223 | template<typename T, typename Result> |
| 224 | struct DotMemRef0 { |
| 225 | void f(T t) { |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 226 | Result result = t.m; // expected-error{{non-const lvalue reference to type}} |
Douglas Gregor | 1c0ca59 | 2009-05-22 21:13:27 +0000 | [diff] [blame] | 227 | } |
| 228 | }; |
| 229 | |
| 230 | struct MemInt { |
| 231 | int m; |
| 232 | }; |
| 233 | |
| 234 | struct InheritsMemInt : MemInt { }; |
| 235 | |
| 236 | struct MemIntFunc { |
| 237 | static int m(int); |
| 238 | }; |
| 239 | |
| 240 | template struct DotMemRef0<MemInt, int&>; |
| 241 | template struct DotMemRef0<InheritsMemInt, int&>; |
| 242 | template struct DotMemRef0<MemIntFunc, int (*)(int)>; |
| 243 | template struct DotMemRef0<MemInt, float&>; // expected-note{{instantiation}} |
| 244 | |
| 245 | template<typename T, typename Result> |
| 246 | struct ArrowMemRef0 { |
| 247 | void f(T t) { |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 248 | Result result = t->m; // expected-error 2{{non-const lvalue reference}} |
Douglas Gregor | 1c0ca59 | 2009-05-22 21:13:27 +0000 | [diff] [blame] | 249 | } |
| 250 | }; |
| 251 | |
| 252 | template<typename T> |
| 253 | struct ArrowWrapper { |
| 254 | T operator->(); |
| 255 | }; |
| 256 | |
| 257 | template struct ArrowMemRef0<MemInt*, int&>; |
| 258 | template struct ArrowMemRef0<InheritsMemInt*, int&>; |
| 259 | template struct ArrowMemRef0<MemIntFunc*, int (*)(int)>; |
| 260 | template struct ArrowMemRef0<MemInt*, float&>; // expected-note{{instantiation}} |
| 261 | |
| 262 | template struct ArrowMemRef0<ArrowWrapper<MemInt*>, int&>; |
| 263 | template struct ArrowMemRef0<ArrowWrapper<InheritsMemInt*>, int&>; |
| 264 | template struct ArrowMemRef0<ArrowWrapper<MemIntFunc*>, int (*)(int)>; |
| 265 | template struct ArrowMemRef0<ArrowWrapper<MemInt*>, float&>; // expected-note{{instantiation}} |
| 266 | template struct ArrowMemRef0<ArrowWrapper<ArrowWrapper<MemInt*> >, int&>; |
| 267 | |
| 268 | // FIXME: we should be able to return a MemInt without the reference! |
| 269 | MemInt &createMemInt(int); |
| 270 | |
| 271 | template<int N> |
| 272 | struct NonDepMemberExpr0 { |
| 273 | void f() { |
| 274 | createMemInt(N).m = N; |
| 275 | } |
| 276 | }; |
| 277 | |
| 278 | template struct NonDepMemberExpr0<0>; |
Douglas Gregor | 08d3e7c | 2009-05-22 21:26:58 +0000 | [diff] [blame] | 279 | |
| 280 | template<typename T, typename Result> |
| 281 | struct MemberFuncCall0 { |
| 282 | void f(T t) { |
| 283 | Result result = t.f(); |
| 284 | } |
| 285 | }; |
| 286 | |
| 287 | template<typename T> |
| 288 | struct HasMemFunc0 { |
| 289 | T f(); |
| 290 | }; |
| 291 | |
| 292 | |
| 293 | template struct MemberFuncCall0<HasMemFunc0<int&>, const int&>; |
| 294 | |
| 295 | template<typename Result> |
| 296 | struct ThisMemberFuncCall0 { |
| 297 | Result g(); |
| 298 | |
| 299 | void f() { |
| 300 | Result r1 = g(); |
| 301 | Result r2 = this->g(); |
| 302 | } |
| 303 | }; |
| 304 | |
| 305 | template struct ThisMemberFuncCall0<int&>; |
| 306 | |
| 307 | template<typename T> |
| 308 | struct NonDepMemberCall0 { |
| 309 | void foo(HasMemFunc0<int&> x) { |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 310 | T result = x.f(); // expected-error{{non-const lvalue reference}} |
Douglas Gregor | 08d3e7c | 2009-05-22 21:26:58 +0000 | [diff] [blame] | 311 | } |
| 312 | }; |
| 313 | |
| 314 | template struct NonDepMemberCall0<int&>; |
| 315 | template struct NonDepMemberCall0<const int&>; |
| 316 | template struct NonDepMemberCall0<float&>; // expected-note{{instantiation}} |
Douglas Gregor | e30d0bd | 2009-05-22 23:47:06 +0000 | [diff] [blame] | 317 | |
| 318 | |
| 319 | template<typename T> |
| 320 | struct QualifiedDeclRef0 { |
| 321 | T f() { |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 322 | 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 Gregor | e30d0bd | 2009-05-22 23:47:06 +0000 | [diff] [blame] | 323 | } |
| 324 | }; |
| 325 | |
| 326 | template struct QualifiedDeclRef0<bool>; |
| 327 | template struct QualifiedDeclRef0<int&>; // expected-note{{instantiation}} |