blob: 16260449d4be674a90924c44e884f7472cfb6db4 [file] [log] [blame]
Daniel Dunbara5728872009-12-15 20:14:24 +00001// RUN: %clang_cc1 -fsyntax-only -verify %s
Sebastian Redlddf7e992009-02-08 10:28:44 +00002
Chris Lattner04421082008-04-08 04:40:51 +00003void f(int i, int j, int k = 3);
Douglas Gregor6d6eb572008-05-07 04:49:29 +00004void f(int i, int j, int k);
Chris Lattner04421082008-04-08 04:40:51 +00005void f(int i, int j = 2, int k);
Douglas Gregor6d6eb572008-05-07 04:49:29 +00006void f(int i, int j, int k);
Chris Lattner04421082008-04-08 04:40:51 +00007void f(int i = 1, int j, int k);
Douglas Gregor6d6eb572008-05-07 04:49:29 +00008void f(int i, int j, int k);
Chris Lattner04421082008-04-08 04:40:51 +00009
10void i()
11{
12 f();
13 f(0);
14 f(0, 1);
15 f(0, 1, 2);
16}
Chris Lattner8123a952008-04-10 02:22:51 +000017
18
Chris Lattnerd84aac12010-02-22 00:40:25 +000019int f1(int i, // expected-note {{previous declaration is here}}
20 int i, int j) { // expected-error {{redefinition of parameter 'i'}}
Chris Lattner8123a952008-04-10 02:22:51 +000021 i = 17;
22 return j;
23}
24
25int x;
26void g(int x, int y = x); // expected-error {{default argument references parameter 'x'}}
27
Chris Lattner9e979552008-04-12 23:52:44 +000028void g2(int x, int y, int z = x + y); // expected-error {{default argument references parameter 'x'}} expected-error {{default argument references parameter 'y'}}
Douglas Gregor6d6eb572008-05-07 04:49:29 +000029
Douglas Gregor30c54362008-11-03 22:47:57 +000030class X {
Richard Smitha85cf392012-04-05 01:13:04 +000031 void f(X* x = this); // expected-error{{invalid use of 'this' outside of a non-static member function}}
Douglas Gregor3996f232008-11-04 13:41:56 +000032
33 void g() {
34 int f(X* x = this); // expected-error{{default argument references 'this'}}
35 }
Douglas Gregor30c54362008-11-03 22:47:57 +000036};
Douglas Gregor69497c32008-12-16 00:08:34 +000037
38// C++ [dcl.fct.default]p6
39class C {
40 static int x;
41 void f(int i = 3); // expected-note{{previous definition is here}}
42 void g(int i, int j = x);
43
44 void h();
45};
46void C::f(int i = 3) // expected-error{{redefinition of default argument}}
47{ }
48
49void C::g(int i = 88, int j) {}
50
51void C::h() {
52 g(); // okay
53}
54
55// C++ [dcl.fct.default]p9
Douglas Gregor72b505b2008-12-16 21:30:33 +000056struct Y {
Douglas Gregor69497c32008-12-16 00:08:34 +000057 int a;
Richard Smitha85cf392012-04-05 01:13:04 +000058 int mem1(int i = a); // expected-error{{invalid use of non-static data member 'a'}}
Douglas Gregor72b505b2008-12-16 21:30:33 +000059 int mem2(int i = b); // OK; use Y::b
Douglas Gregor0a59acb2008-12-16 00:38:16 +000060 int mem3(int i);
61 int mem4(int i);
Douglas Gregor72b505b2008-12-16 21:30:33 +000062
63 struct Nested {
64 int mem5(int i = b, // OK; use Y::b
65 int j = c, // OK; use Y::Nested::c
66 int k = j, // expected-error{{default argument references parameter 'j'}}
Richard Smitha85cf392012-04-05 01:13:04 +000067 int l = a, // expected-error{{invalid use of non-static data member 'a'}}
68 Nested* self = this, // expected-error{{invalid use of 'this' outside of a non-static member function}}
Douglas Gregor72b505b2008-12-16 21:30:33 +000069 int m); // expected-error{{missing default argument on parameter 'm'}}
70 static int c;
Douglas Gregord54eb442010-10-12 16:25:54 +000071 Nested(int i = 42);
Douglas Gregor72b505b2008-12-16 21:30:33 +000072 };
73
Douglas Gregord54eb442010-10-12 16:25:54 +000074 int mem7(Nested n = Nested());
75
Douglas Gregor69497c32008-12-16 00:08:34 +000076 static int b;
77};
Douglas Gregor0a59acb2008-12-16 00:38:16 +000078
79int Y::mem3(int i = b) { return i; } // OK; use X::b
80
Richard Smitha85cf392012-04-05 01:13:04 +000081int Y::mem4(int i = a) // expected-error{{invalid use of non-static data member 'a'}}
Douglas Gregor0a59acb2008-12-16 00:38:16 +000082{ return i; }
Douglas Gregor72b505b2008-12-16 21:30:33 +000083
84
85// Try to verify that default arguments interact properly with copy
86// constructors.
87class Z {
88public:
John McCallb1622a12010-01-06 09:43:14 +000089 Z(Z&, int i = 17); // expected-note 3 {{candidate constructor}}
Douglas Gregor72b505b2008-12-16 21:30:33 +000090
91 void f(Z& z) {
92 Z z2; // expected-error{{no matching constructor for initialization}}
93 Z z3(z);
94 }
Douglas Gregor61366e92008-12-24 00:01:03 +000095
96 void test_Z(const Z& z) {
John McCall7c2342d2010-03-10 11:27:22 +000097 Z z2(z); // expected-error{{no matching constructor for initialization of 'Z'}}
Douglas Gregor61366e92008-12-24 00:01:03 +000098 }
Douglas Gregor72b505b2008-12-16 21:30:33 +000099};
100
101void test_Z(const Z& z) {
John McCall7c2342d2010-03-10 11:27:22 +0000102 Z z2(z); // expected-error{{no matching constructor for initialization of 'Z'}}
Douglas Gregor72b505b2008-12-16 21:30:33 +0000103}
Douglas Gregor61366e92008-12-24 00:01:03 +0000104
105struct ZZ {
Douglas Gregor61366e92008-12-24 00:01:03 +0000106 static ZZ g(int = 17);
107
Douglas Gregora41a8c52010-04-22 00:20:18 +0000108 void f(ZZ z = g()); // expected-error{{no matching constructor for initialization}} \
109 // expected-note{{passing argument to parameter 'z' here}}
Anders Carlsson5e300d12009-06-12 16:51:40 +0000110
John McCallb1622a12010-01-06 09:43:14 +0000111 ZZ(ZZ&, int = 17); // expected-note{{candidate constructor}}
Douglas Gregor61366e92008-12-24 00:01:03 +0000112};
Anders Carlsson5e300d12009-06-12 16:51:40 +0000113
114// http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_active.html#325
115class C2 {
116 static void g(int = f()); // expected-error{{use of default argument to function 'f' that is declared later in class 'C2'}}
117 static int f(int = 10); // expected-note{{default argument declared here}}
118};
Eli Friedmand33133c2009-07-22 21:45:50 +0000119
120// Make sure we actually parse the default argument for an inline definition
121class XX {
122 void A(int length = -1 ) { }
123 void B() { A(); }
124};