blob: 29133c7c7ff100c6d13ae6dc3b9d69bc8dd997d4 [file] [log] [blame]
Daniel Dunbara5728872009-12-15 20:14:24 +00001// RUN: %clang_cc1 -fsyntax-only -pedantic -verify %s
Douglas Gregor6fc17ff2008-10-29 15:10:40 +00002int* f(int) { return 0; }
3float* f(float) { return 0; }
Douglas Gregor8e9bebd2008-10-21 16:13:35 +00004void f();
5
6void test_f(int iv, float fv) {
7 float* fp = f(fv);
8 int* ip = f(iv);
9}
10
11int* g(int, float, int); // expected-note {{ candidate function }}
12float* g(int, int, int); // expected-note {{ candidate function }}
13double* g(int, float, float); // expected-note {{ candidate function }}
14char* g(int, float, ...); // expected-note {{ candidate function }}
15void g();
16
17void test_g(int iv, float fv) {
18 int* ip1 = g(iv, fv, 0);
19 float* fp1 = g(iv, iv, 0);
20 double* dp1 = g(iv, fv, fv);
21 char* cp1 = g(0, 0);
22 char* cp2 = g(0, 0, 0, iv, fv);
23
24 double* dp2 = g(0, fv, 1.5); // expected-error {{ call to 'g' is ambiguous; candidates are: }}
25}
26
27double* h(double f);
28int* h(int);
29
30void test_h(float fv, unsigned char cv) {
31 double* dp = h(fv);
32 int* ip = h(cv);
33}
34
35int* i(int);
36double* i(long);
37
38void test_i(short sv, int iv, long lv, unsigned char ucv) {
39 int* ip1 = i(sv);
40 int* ip2 = i(iv);
41 int* ip3 = i(ucv);
42 double* dp1 = i(lv);
43}
44
45int* j(void*);
46double* j(bool);
47
48void test_j(int* ip) {
49 int* ip1 = j(ip);
50}
51
52int* k(char*);
53double* k(bool);
54
55void test_k() {
Douglas Gregora9bff302010-02-28 18:30:25 +000056 int* ip1 = k("foo"); // expected-warning{{conversion from string literal to 'char *' is deprecated}}
Douglas Gregor8e9bebd2008-10-21 16:13:35 +000057 double* dp1 = k(L"foo");
58}
59
60int* l(wchar_t*);
61double* l(bool);
62
63void test_l() {
Douglas Gregora9bff302010-02-28 18:30:25 +000064 int* ip1 = l(L"foo"); // expected-warning{{conversion from string literal to 'wchar_t *' is deprecated}}
Douglas Gregor8e9bebd2008-10-21 16:13:35 +000065 double* dp1 = l("foo");
66}
67
68int* m(const char*);
69double* m(char*);
70
71void test_m() {
72 int* ip = m("foo");
73}
74
75int* n(char*);
76double* n(void*);
Douglas Gregor0f669f52008-11-26 23:33:36 +000077class E;
Douglas Gregor8e9bebd2008-10-21 16:13:35 +000078
Douglas Gregor0f669f52008-11-26 23:33:36 +000079void test_n(E* e) {
Douglas Gregor8e9bebd2008-10-21 16:13:35 +000080 char ca[7];
81 int* ip1 = n(ca);
Douglas Gregora9bff302010-02-28 18:30:25 +000082 int* ip2 = n("foo"); // expected-warning{{conversion from string literal to 'char *' is deprecated}}
Douglas Gregor8e9bebd2008-10-21 16:13:35 +000083
84 float fa[7];
85 double* dp1 = n(fa);
Douglas Gregor0f669f52008-11-26 23:33:36 +000086
87 double* dp2 = n(e);
Douglas Gregor8e9bebd2008-10-21 16:13:35 +000088}
89
90enum PromotesToInt {
Douglas Gregorc9467cf2008-12-12 02:00:36 +000091 PromotesToIntValue = -1
Douglas Gregor8e9bebd2008-10-21 16:13:35 +000092};
93
94enum PromotesToUnsignedInt {
John McCall842aef82009-12-09 09:09:27 +000095 PromotesToUnsignedIntValue = __INT_MAX__ * 2U
Douglas Gregor8e9bebd2008-10-21 16:13:35 +000096};
97
98int* o(int);
99double* o(unsigned int);
100float* o(long);
101
102void test_o() {
103 int* ip1 = o(PromotesToIntValue);
104 double* dp1 = o(PromotesToUnsignedIntValue);
105}
106
107int* p(int);
108double* p(double);
109
110void test_p() {
111 int* ip = p((short)1);
112 double* dp = p(1.0f);
113}
114
115struct Bits {
116 signed short int_bitfield : 5;
117 unsigned int uint_bitfield : 8;
118};
119
120int* bitfields(int, int);
121float* bitfields(unsigned int, int);
122
123void test_bitfield(Bits bits, int x) {
124 int* ip = bitfields(bits.int_bitfield, 0);
125 float* fp = bitfields(bits.uint_bitfield, 0u);
126}
127
128int* multiparm(long, int, long); // expected-note {{ candidate function }}
129float* multiparm(int, int, int); // expected-note {{ candidate function }}
130double* multiparm(int, int, short); // expected-note {{ candidate function }}
131
132void test_multiparm(long lv, short sv, int iv) {
133 int* ip1 = multiparm(lv, iv, lv);
134 int* ip2 = multiparm(lv, sv, lv);
135 float* fp1 = multiparm(iv, iv, iv);
136 float* fp2 = multiparm(sv, iv, iv);
137 double* dp1 = multiparm(sv, sv, sv);
138 double* dp2 = multiparm(iv, sv, sv);
139 multiparm(sv, sv, lv); // expected-error {{ call to 'multiparm' is ambiguous; candidates are: }}
140}
Douglas Gregor9b6e2d22008-10-22 00:38:21 +0000141
142// Test overloading based on qualification vs. no qualification
143// conversion.
144int* quals1(int const * p);
145char* quals1(int * p);
146
147int* quals2(int const * const * pp);
148char* quals2(int * * pp);
149
150int* quals3(int const * * const * ppp);
151char* quals3(int *** ppp);
152
153void test_quals(int * p, int * * pp, int * * * ppp) {
154 char* q1 = quals1(p);
155 char* q2 = quals2(pp);
156 char* q3 = quals3(ppp);
157}
158
159// Test overloading based on qualification ranking (C++ 13.3.2)p3.
160int* quals_rank1(int const * p);
161float* quals_rank1(int const volatile *p);
Douglas Gregor57373262008-10-22 14:17:15 +0000162char* quals_rank1(char*);
163double* quals_rank1(const char*);
Douglas Gregor9b6e2d22008-10-22 00:38:21 +0000164
165int* quals_rank2(int const * const * pp);
166float* quals_rank2(int * const * pp);
167
Douglas Gregor57373262008-10-22 14:17:15 +0000168void quals_rank3(int const * const * const volatile * p); // expected-note{{candidate function}}
169void quals_rank3(int const * const volatile * const * p); // expected-note{{candidate function}}
170
171void quals_rank3(int const *); // expected-note{{candidate function}}
172void quals_rank3(int volatile *); // expected-note{{candidate function}}
173
Douglas Gregor9b6e2d22008-10-22 00:38:21 +0000174void test_quals_ranking(int * p, int volatile *pq, int * * pp, int * * * ppp) {
Douglas Gregor57373262008-10-22 14:17:15 +0000175 int* q1 = quals_rank1(p);
Douglas Gregor9b6e2d22008-10-22 00:38:21 +0000176 float* q2 = quals_rank1(pq);
Douglas Gregor57373262008-10-22 14:17:15 +0000177 double* q3 = quals_rank1("string literal");
178 char a[17];
179 const char* ap = a;
180 char* q4 = quals_rank1(a);
181 double* q5 = quals_rank1(ap);
182
183 float* q6 = quals_rank2(pp);
184
185 quals_rank3(ppp); // expected-error {{call to 'quals_rank3' is ambiguous; candidates are:}}
186
187 quals_rank3(p); // expected-error {{call to 'quals_rank3' is ambiguous; candidates are:}}
188 quals_rank3(pq);
Douglas Gregor9b6e2d22008-10-22 00:38:21 +0000189}
Douglas Gregor57373262008-10-22 14:17:15 +0000190
Douglas Gregorbc0805a2008-10-23 00:40:37 +0000191// Test overloading based on derived-to-base conversions
192class A { };
193class B : public A { };
194class C : public B { };
195class D : public C { };
196
197int* derived1(A*);
198char* derived1(const A*);
199float* derived1(void*);
200
201int* derived2(A*);
202float* derived2(B*);
203
204int* derived3(A*);
205float* derived3(const B*);
206char* derived3(C*);
207
208void test_derived(B* b, B const* bc, C* c, const C* cc, void* v, D* d) {
209 int* d1 = derived1(b);
210 char* d2 = derived1(bc);
211 int* d3 = derived1(c);
212 char* d4 = derived1(cc);
213 float* d5 = derived1(v);
214
215 float* d6 = derived2(b);
216 float* d7 = derived2(c);
217
218 char* d8 = derived3(d);
219}
Douglas Gregor27c8dc02008-10-29 00:13:59 +0000220
221// Test overloading of references.
222// (FIXME: tests binding to determine candidate sets, not overload
223// resolution per se).
224int* intref(int&);
225float* intref(const int&);
226
227void intref_test() {
228 float* ir1 = intref(5);
229 float* ir2 = intref(5.5);
230}
Douglas Gregor15da57e2008-10-29 02:00:59 +0000231
232// Test reference binding vs. standard conversions.
233int& bind_vs_conv(const double&);
234float& bind_vs_conv(int);
235
236void bind_vs_conv_test()
237{
238 int& i1 = bind_vs_conv(1.0f);
239 float& f1 = bind_vs_conv((short)1);
240}
241
242// Test that cv-qualifiers get subsumed in the reference binding.
243struct X { };
244struct Y { };
245struct Z : X, Y { };
246
247int& cvqual_subsume(X&); // expected-note{{candidate function}}
248float& cvqual_subsume(const Y&); // expected-note{{candidate function}}
249
Douglas Gregor66821b52010-04-18 09:22:00 +0000250int& cvqual_subsume2(const X&); // expected-note{{candidate function}}
251float& cvqual_subsume2(const volatile Y&); // expected-note{{candidate function}}
Douglas Gregor15da57e2008-10-29 02:00:59 +0000252
253Z get_Z();
254
255void cvqual_subsume_test(Z z) {
256 cvqual_subsume(z); // expected-error{{call to 'cvqual_subsume' is ambiguous; candidates are:}}
Douglas Gregor66821b52010-04-18 09:22:00 +0000257 int& x = cvqual_subsume2(get_Z()); // expected-error{{call to 'cvqual_subsume2' is ambiguous; candidates are:}}
Douglas Gregor15da57e2008-10-29 02:00:59 +0000258}
Douglas Gregorf70bdb92008-10-29 14:50:44 +0000259
260// Test overloading with cv-qualification differences in reference
261// binding.
262int& cvqual_diff(X&);
263float& cvqual_diff(const X&);
264
265void cvqual_diff_test(X x, Z z) {
266 int& i1 = cvqual_diff(x);
267 int& i2 = cvqual_diff(z);
268}
269
270// Test overloading with derived-to-base differences in reference
271// binding.
272struct Z2 : Z { };
273
274int& db_rebind(X&);
275long& db_rebind(Y&);
276float& db_rebind(Z&);
277
278void db_rebind_test(Z2 z2) {
279 float& f1 = db_rebind(z2);
280}
Douglas Gregorb7a86f52009-11-06 01:02:41 +0000281
282class string { };
283class opt : public string { };
284
285struct SR {
286 SR(const string&);
287};
288
289void f(SR) { }
290
291void g(opt o) {
292 f(o);
293}
Douglas Gregor335b07a2009-12-13 21:29:20 +0000294
295
296namespace PR5756 {
297 int &a(void*, int);
298 float &a(void*, float);
299 void b() {
300 int &ir = a(0,0);
301 (void)ir;
302 }
303}
John McCall3c80f572010-01-12 02:15:36 +0000304
305// Tests the exact text used to note the candidates
306namespace test1 {
John McCalle81e15e2010-01-14 00:56:20 +0000307 template <class T> void foo(T t, unsigned N); // expected-note {{candidate function [with T = int] not viable: no known conversion from 'char const [6]' to 'unsigned int' for 2nd argument}}
308 void foo(int n, char N); // expected-note {{candidate function not viable: no known conversion from 'char const [6]' to 'char' for 2nd argument}}
John McCalladbb8f82010-01-13 09:16:55 +0000309 void foo(int n); // expected-note {{candidate function not viable: requires 1 argument, but 2 were provided}}
310 void foo(unsigned n = 10); // expected-note {{candidate function not viable: requires at most 1 argument, but 2 were provided}}
311 void foo(int n, const char *s, int t); // expected-note {{candidate function not viable: requires 3 arguments, but 2 were provided}}
312 void foo(int n, const char *s, int t, ...); // expected-note {{candidate function not viable: requires at least 3 arguments, but 2 were provided}}
313 void foo(int n, const char *s, int t, int u = 0); // expected-note {{candidate function not viable: requires at least 3 arguments, but 2 were provided}}
John McCall3c80f572010-01-12 02:15:36 +0000314
315 void test() {
316 foo(4, "hello"); //expected-error {{no matching function for call to 'foo'}}
317 }
318}
John McCalladbb8f82010-01-13 09:16:55 +0000319
John McCallcefd3ad2010-01-13 22:30:33 +0000320// PR 6014
321namespace test2 {
322 struct QFixed {
323 QFixed(int i);
324 QFixed(long i);
325 };
326
327 bool operator==(const QFixed &f, int i);
328
329 class qrgb666 {
330 inline operator unsigned int () const;
331
332 inline bool operator==(const qrgb666 &v) const;
333 inline bool operator!=(const qrgb666 &v) const { return !(*this == v); }
334 };
335}
John McCall258b2032010-01-23 08:10:49 +0000336
337// PR 6117
338namespace test3 {
339 struct Base {};
340 struct Incomplete;
341
342 void foo(Base *); // expected-note 2 {{cannot convert argument of incomplete type}}
343 void foo(Base &); // expected-note 2 {{cannot convert argument of incomplete type}}
344
345 void test(Incomplete *P) {
346 foo(P); // expected-error {{no matching function for call to 'foo'}}
347 foo(*P); // expected-error {{no matching function for call to 'foo'}}
348 }
349}
Douglas Gregorad323a82010-01-27 03:51:04 +0000350
351namespace DerivedToBaseVsVoid {
352 struct A { };
353 struct B : A { };
354
355 float &f(void *);
356 int &f(const A*);
357
358 void g(B *b) {
359 int &ir = f(b);
360 }
361}
John McCallb1bdc622010-02-25 01:37:24 +0000362
John McCall3a813372010-02-25 10:46:05 +0000363// PR 6398 + PR 6421
John McCallb1bdc622010-02-25 01:37:24 +0000364namespace test4 {
365 class A;
366 class B {
367 static void foo(); // expected-note {{not viable}}
368 static void foo(int*); // expected-note {{not viable}}
John McCall3a813372010-02-25 10:46:05 +0000369 static void foo(long*); // expected-note {{not viable}}
John McCallb1bdc622010-02-25 01:37:24 +0000370
371 void bar(A *a) {
372 foo(a); // expected-error {{no matching function for call}}
373 }
374 };
375}
Douglas Gregor9e239322010-02-25 19:01:05 +0000376
377namespace DerivedToBase {
378 struct A { };
379 struct B : A { };
380 struct C : B { };
381
382 int &f0(const A&);
383 float &f0(B);
384
385 void g() {
386 float &fr = f0(C());
387 }
388}
Douglas Gregora1a9f032010-03-07 23:17:44 +0000389
390namespace PR6483 {
391 struct X0 {
392 operator const unsigned int & () const;
393 };
394
395 struct X1 {
396 operator unsigned int & () const;
397 };
398
399 void f0(const bool &);
400 void f1(bool &); // expected-note 2{{not viable}}
401
402 void g(X0 x0, X1 x1) {
403 f0(x0);
404 f1(x0); // expected-error{{no matching function for call}}
405 f0(x1);
406 f1(x1); // expected-error{{no matching function for call}}
407 }
408}
Douglas Gregoraa0be172010-04-13 15:50:39 +0000409
410namespace PR6078 {
Douglas Gregord1a27222010-04-24 20:54:38 +0000411 struct A {
Douglas Gregoraa0be172010-04-13 15:50:39 +0000412 A(short); // expected-note{{candidate constructor}}
413 A(long); // expected-note{{candidate constructor}}
414 };
415 struct S {
416 typedef void ft(A);
417 operator ft*();
418 };
419
420 void f() {
421 S()(0); // expected-error{{conversion from 'int' to 'PR6078::A' is ambiguous}}
422 }
423}
Douglas Gregor66821b52010-04-18 09:22:00 +0000424
425namespace PR6177 {
426 struct String { String(char const*); };
427
Douglas Gregora41a8c52010-04-22 00:20:18 +0000428 void f(bool const volatile&); // expected-note{{passing argument to parameter here}}
Douglas Gregor66821b52010-04-18 09:22:00 +0000429 void f(String);
430
431 void g() { f(""); } // expected-error{{volatile lvalue reference to type 'bool const volatile' cannot bind to a value of unrelated type 'char const [1]'}}
432}
Douglas Gregorae65f4b2010-05-23 22:10:15 +0000433
434namespace PR7095 {
435 struct X { };
436
437 struct Y {
438 operator const X*();
439
440 private:
441 operator X*();
442 };
443
444 void f(const X *);
445 void g(Y y) { f(y); }
446}
Douglas Gregoraf7bea52010-05-25 15:31:05 +0000447
448namespace PR7224 {
449 class A {};
450 class B : public A {};
451
452 int &foo(A *const d);
453 float &foo(const A *const d);
454
455 void bar()
456 {
457 B *const d = 0;
458 B const *const d2 = 0;
459 int &ir = foo(d);
460 float &fr = foo(d2);
461 }
462}