Daniel Dunbar | 8fbe78f | 2009-12-15 20:14:24 +0000 | [diff] [blame] | 1 | // RUN: %clang_cc1 -fsyntax-only -verify %s |
Charles Li | e7cbb3e | 2015-11-17 20:25:05 +0000 | [diff] [blame] | 2 | // RUN: %clang_cc1 -fsyntax-only -verify -std=c++98 %s |
| 3 | // RUN: %clang_cc1 -fsyntax-only -verify -std=c++11 %s |
| 4 | |
Chris Lattner | aa9c7ae | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 5 | void f(int i); |
Chris Lattner | 0369c57 | 2008-11-23 23:12:31 +0000 | [diff] [blame] | 6 | void f(int i = 0); // expected-note {{previous definition is here}} |
Chris Lattner | aa9c7ae | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 7 | void f(int i = 17); // expected-error {{redefinition of default argument}} |
| 8 | |
| 9 | |
| 10 | void g(int i, int j, int k = 3); |
| 11 | void g(int i, int j = 2, int k); |
| 12 | void g(int i = 1, int j, int k); |
| 13 | |
| 14 | void h(int i, int j = 2, int k = 3, |
| 15 | int l, // expected-error {{missing default argument on parameter 'l'}} |
| 16 | int, // expected-error {{missing default argument on parameter}} |
| 17 | int n);// expected-error {{missing default argument on parameter 'n'}} |
| 18 | |
| 19 | struct S { } s; |
Douglas Gregor | 4f4946a | 2010-04-22 00:20:18 +0000 | [diff] [blame] | 20 | void i(int = s) { } // expected-error {{no viable conversion}} \ |
| 21 | // expected-note{{passing argument to parameter here}} |
Douglas Gregor | 5496d4c | 2008-11-04 13:57:51 +0000 | [diff] [blame] | 22 | |
| 23 | struct X { |
| 24 | X(int); |
| 25 | }; |
| 26 | |
Kaelyn Uhrain | 476c823 | 2013-07-08 23:13:44 +0000 | [diff] [blame] | 27 | void j(X x = 17); // expected-note{{'::j' declared here}} |
Douglas Gregor | 5496d4c | 2008-11-04 13:57:51 +0000 | [diff] [blame] | 28 | |
Charles Li | e7cbb3e | 2015-11-17 20:25:05 +0000 | [diff] [blame] | 29 | struct Y { // expected-note 2{{candidate constructor (the implicit copy constructor) not viable}} |
| 30 | #if __cplusplus >= 201103L // C++11 or later |
| 31 | // expected-note@-2 2 {{candidate constructor (the implicit move constructor) not viable}} |
| 32 | #endif |
| 33 | |
Richard Smith | 2519554 | 2020-01-09 12:27:48 -0800 | [diff] [blame] | 34 | explicit Y(int); // expected-note 2{{explicit constructor is not a candidate}} |
Douglas Gregor | 5496d4c | 2008-11-04 13:57:51 +0000 | [diff] [blame] | 35 | }; |
| 36 | |
Douglas Gregor | 4f4946a | 2010-04-22 00:20:18 +0000 | [diff] [blame] | 37 | void k(Y y = 17); // expected-error{{no viable conversion}} \ |
| 38 | // expected-note{{passing argument to parameter 'y' here}} |
Douglas Gregor | 5835403 | 2008-12-24 00:01:03 +0000 | [diff] [blame] | 39 | |
Douglas Gregor | 4f4946a | 2010-04-22 00:20:18 +0000 | [diff] [blame] | 40 | void kk(Y = 17); // expected-error{{no viable conversion}} \ |
| 41 | // expected-note{{passing argument to parameter here}} |
James Molloy | e943003 | 2012-03-13 08:55:35 +0000 | [diff] [blame] | 42 | |
| 43 | int l () { |
| 44 | int m(int i, int j, int k = 3); |
| 45 | if (1) |
| 46 | { |
| 47 | int m(int i, int j = 2, int k = 4); |
| 48 | m(8); |
| 49 | } |
| 50 | return 0; |
| 51 | } |
| 52 | |
| 53 | int i () { |
| 54 | void j (int f = 4); |
| 55 | { |
Kaelyn Uhrain | 476c823 | 2013-07-08 23:13:44 +0000 | [diff] [blame] | 56 | void j (int f); |
| 57 | j(); // expected-error{{too few arguments to function call, expected 1, have 0; did you mean '::j'?}} |
| 58 | } |
| 59 | void jj (int f = 4); |
| 60 | { |
| 61 | void jj (int f); // expected-note{{'jj' declared here}} |
| 62 | jj(); // expected-error{{too few arguments to function call, single argument 'f' was not specified}} |
Richard Smith | d72da15 | 2012-05-15 06:21:54 +0000 | [diff] [blame] | 63 | } |
| 64 | } |
| 65 | |
| 66 | int i2() { |
| 67 | void j(int f = 4); // expected-note{{'j' declared here}} |
| 68 | { |
| 69 | j(2, 3); // expected-error{{too many arguments to function call, expected at most single argument 'f', have 2}} |
James Molloy | e943003 | 2012-03-13 08:55:35 +0000 | [diff] [blame] | 70 | } |
| 71 | } |
Serge Pavlov | b4b3578 | 2014-07-22 01:54:49 +0000 | [diff] [blame] | 72 | |
| 73 | int pr20055_f(int x = 0, int y = UNDEFINED); // expected-error{{use of undeclared identifier}} |
| 74 | int pr20055_v = pr20055_f(0); |
Richard Smith | 5971e8c | 2014-08-27 22:31:34 +0000 | [diff] [blame] | 75 | |
| 76 | void PR20769() { void PR20769(int = 1); } |
| 77 | void PR20769(int = 2); |
| 78 | |
| 79 | void PR20769_b(int = 1); |
| 80 | void PR20769_b() { void PR20769_b(int = 2); } |
Richard Smith | de47d66 | 2019-05-24 21:08:12 +0000 | [diff] [blame] | 81 | |
| 82 | #if __cplusplus >= 201103L |
| 83 | template<typename T> constexpr int f1() { return 0; } |
| 84 | // This is OK, but in order to see that we must instantiate f<int>, despite it |
| 85 | // being in an unused default argument. |
| 86 | void g1(char c = {f1<int>()}) {} // expected-warning {{braces around scalar}} |
| 87 | |
| 88 | // This is formally ill-formed, but we choose to not trigger instantiation here |
| 89 | // (at least, not until g2 is actually called in a way that uses the default |
| 90 | // argument). |
| 91 | template<typename T> int f2() { return T::error; } |
| 92 | void g2(int c = f2<int>()) {} |
| 93 | |
| 94 | // FIXME: Provide a note pointing at the first use of the default argument? |
| 95 | template<typename T> int f3() { return T::error; } // expected-error {{no members}} |
| 96 | void g3(int c = f3<int>()) {} // expected-note {{in instantiation of}} |
| 97 | void use_g3() { g3(); } |
Richard Smith | e92be7c | 2021-01-22 15:43:47 -0800 | [diff] [blame] | 98 | |
| 99 | namespace PR47682 { |
| 100 | inline namespace A { |
| 101 | void f(int = 0); |
| 102 | } |
| 103 | } |
| 104 | void PR47682::f(int) {} |
| 105 | void PR47682_test() { PR47682::f(); } |
Richard Smith | de47d66 | 2019-05-24 21:08:12 +0000 | [diff] [blame] | 106 | #endif |