1*f4a2713aSLionel Sambuc // RUN: %clang_cc1 -fsyntax-only -verify %s 2*f4a2713aSLionel Sambuc 3*f4a2713aSLionel Sambuc void f(int i, int j, int k = 3); 4*f4a2713aSLionel Sambuc void f(int i, int j, int k); 5*f4a2713aSLionel Sambuc void f(int i, int j = 2, int k); 6*f4a2713aSLionel Sambuc void f(int i, int j, int k); 7*f4a2713aSLionel Sambuc void f(int i = 1, int j, int k); 8*f4a2713aSLionel Sambuc void f(int i, int j, int k); 9*f4a2713aSLionel Sambuc 10*f4a2713aSLionel Sambuc void i() 11*f4a2713aSLionel Sambuc { 12*f4a2713aSLionel Sambuc f(); 13*f4a2713aSLionel Sambuc f(0); 14*f4a2713aSLionel Sambuc f(0, 1); 15*f4a2713aSLionel Sambuc f(0, 1, 2); 16*f4a2713aSLionel Sambuc } 17*f4a2713aSLionel Sambuc 18*f4a2713aSLionel Sambuc 19*f4a2713aSLionel Sambuc int f1(int i, // expected-note {{previous declaration is here}} 20*f4a2713aSLionel Sambuc int i, int j) { // expected-error {{redefinition of parameter 'i'}} 21*f4a2713aSLionel Sambuc i = 17; 22*f4a2713aSLionel Sambuc return j; 23*f4a2713aSLionel Sambuc } 24*f4a2713aSLionel Sambuc 25*f4a2713aSLionel Sambuc int x; 26*f4a2713aSLionel Sambuc void g(int x, int y = x); // expected-error {{default argument references parameter 'x'}} 27*f4a2713aSLionel Sambuc 28*f4a2713aSLionel Sambuc void g2(int x, int y, int z = x + y); // expected-error {{default argument references parameter 'x'}} expected-error {{default argument references parameter 'y'}} 29*f4a2713aSLionel Sambuc 30*f4a2713aSLionel Sambuc class X { 31*f4a2713aSLionel Sambuc void f(X* x = this); // expected-error{{invalid use of 'this' outside of a non-static member function}} 32*f4a2713aSLionel Sambuc 33*f4a2713aSLionel Sambuc void g() { 34*f4a2713aSLionel Sambuc int f(X* x = this); // expected-error{{default argument references 'this'}} 35*f4a2713aSLionel Sambuc } 36*f4a2713aSLionel Sambuc }; 37*f4a2713aSLionel Sambuc 38*f4a2713aSLionel Sambuc // C++ [dcl.fct.default]p6 39*f4a2713aSLionel Sambuc class C { 40*f4a2713aSLionel Sambuc static int x; 41*f4a2713aSLionel Sambuc void f(int i = 3); // expected-note{{previous definition is here}} 42*f4a2713aSLionel Sambuc void g(int i, int j = x); 43*f4a2713aSLionel Sambuc 44*f4a2713aSLionel Sambuc void h(); 45*f4a2713aSLionel Sambuc }; 46*f4a2713aSLionel Sambuc void C::f(int i = 3) // expected-error{{redefinition of default argument}} 47*f4a2713aSLionel Sambuc { } 48*f4a2713aSLionel Sambuc 49*f4a2713aSLionel Sambuc void C::g(int i = 88, int j) {} 50*f4a2713aSLionel Sambuc 51*f4a2713aSLionel Sambuc void C::h() { 52*f4a2713aSLionel Sambuc g(); // okay 53*f4a2713aSLionel Sambuc } 54*f4a2713aSLionel Sambuc 55*f4a2713aSLionel Sambuc // C++ [dcl.fct.default]p9 56*f4a2713aSLionel Sambuc struct Y { 57*f4a2713aSLionel Sambuc int a; 58*f4a2713aSLionel Sambuc int mem1(int i = a); // expected-error{{invalid use of non-static data member 'a'}} 59*f4a2713aSLionel Sambuc int mem2(int i = b); // OK; use Y::b 60*f4a2713aSLionel Sambuc int mem3(int i); 61*f4a2713aSLionel Sambuc int mem4(int i); 62*f4a2713aSLionel Sambuc 63*f4a2713aSLionel Sambuc struct Nested { 64*f4a2713aSLionel Sambuc int mem5(int i = b, // OK; use Y::b 65*f4a2713aSLionel Sambuc int j = c, // OK; use Y::Nested::c 66*f4a2713aSLionel Sambuc int k = j, // expected-error{{default argument references parameter 'j'}} 67*f4a2713aSLionel Sambuc int l = a, // expected-error{{invalid use of non-static data member 'a'}} 68*f4a2713aSLionel Sambuc Nested* self = this, // expected-error{{invalid use of 'this' outside of a non-static member function}} 69*f4a2713aSLionel Sambuc int m); // expected-error{{missing default argument on parameter 'm'}} 70*f4a2713aSLionel Sambuc static int c; 71*f4a2713aSLionel Sambuc Nested(int i = 42); 72*f4a2713aSLionel Sambuc }; 73*f4a2713aSLionel Sambuc 74*f4a2713aSLionel Sambuc int mem7(Nested n = Nested()); 75*f4a2713aSLionel Sambuc 76*f4a2713aSLionel Sambuc static int b; 77*f4a2713aSLionel Sambuc }; 78*f4a2713aSLionel Sambuc 79*f4a2713aSLionel Sambuc int Y::mem3(int i = b) { return i; } // OK; use X::b 80*f4a2713aSLionel Sambuc 81*f4a2713aSLionel Sambuc int Y::mem4(int i = a) // expected-error{{invalid use of non-static data member 'a'}} 82*f4a2713aSLionel Sambuc { return i; } 83*f4a2713aSLionel Sambuc 84*f4a2713aSLionel Sambuc 85*f4a2713aSLionel Sambuc // Try to verify that default arguments interact properly with copy 86*f4a2713aSLionel Sambuc // constructors. 87*f4a2713aSLionel Sambuc class Z { 88*f4a2713aSLionel Sambuc public: 89*f4a2713aSLionel Sambuc Z(Z&, int i = 17); // expected-note 3 {{candidate constructor}} 90*f4a2713aSLionel Sambuc 91*f4a2713aSLionel Sambuc void f(Z& z) { 92*f4a2713aSLionel Sambuc Z z2; // expected-error{{no matching constructor for initialization}} 93*f4a2713aSLionel Sambuc Z z3(z); 94*f4a2713aSLionel Sambuc } 95*f4a2713aSLionel Sambuc 96*f4a2713aSLionel Sambuc void test_Z(const Z& z) { 97*f4a2713aSLionel Sambuc Z z2(z); // expected-error{{no matching constructor for initialization of 'Z'}} 98*f4a2713aSLionel Sambuc } 99*f4a2713aSLionel Sambuc }; 100*f4a2713aSLionel Sambuc 101*f4a2713aSLionel Sambuc void test_Z(const Z& z) { 102*f4a2713aSLionel Sambuc Z z2(z); // expected-error{{no matching constructor for initialization of 'Z'}} 103*f4a2713aSLionel Sambuc } 104*f4a2713aSLionel Sambuc 105*f4a2713aSLionel Sambuc struct ZZ { 106*f4a2713aSLionel Sambuc static ZZ g(int = 17); 107*f4a2713aSLionel Sambuc 108*f4a2713aSLionel Sambuc void f(ZZ z = g()); // expected-error{{no matching constructor for initialization}} \ 109*f4a2713aSLionel Sambuc // expected-note{{passing argument to parameter 'z' here}} 110*f4a2713aSLionel Sambuc 111*f4a2713aSLionel Sambuc ZZ(ZZ&, int = 17); // expected-note{{candidate constructor}} 112*f4a2713aSLionel Sambuc }; 113*f4a2713aSLionel Sambuc 114*f4a2713aSLionel Sambuc // http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_active.html#325 115*f4a2713aSLionel Sambuc class C2 { 116*f4a2713aSLionel Sambuc static void g(int = f()); // expected-error{{use of default argument to function 'f' that is declared later in class 'C2'}} 117*f4a2713aSLionel Sambuc static int f(int = 10); // expected-note{{default argument declared here}} 118*f4a2713aSLionel Sambuc }; 119*f4a2713aSLionel Sambuc 120*f4a2713aSLionel Sambuc // Make sure we actually parse the default argument for an inline definition 121*f4a2713aSLionel Sambuc class XX { 122*f4a2713aSLionel Sambuc void A(int length = -1 ) { } 123*f4a2713aSLionel Sambuc void B() { A(); } 124*f4a2713aSLionel Sambuc }; 125