blob: a0999c0c4c62a114b578eb7aa79616e821a9f368 [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 {
31 void f(X* x = this); // expected-error{{invalid use of 'this' outside of a nonstatic 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;
58 int mem1(int i = a); // expected-error{{invalid use of nonstatic 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'}}
67 int l = a, // expected-error{{invalid use of nonstatic data member 'a'}}
68 Nested* self = this, // expected-error{{invalid use of 'this' outside of a nonstatic member function}}
69 int m); // expected-error{{missing default argument on parameter 'm'}}
70 static int c;
71 };
72
Douglas Gregor69497c32008-12-16 00:08:34 +000073 static int b;
74};
Douglas Gregor0a59acb2008-12-16 00:38:16 +000075
76int Y::mem3(int i = b) { return i; } // OK; use X::b
77
78int Y::mem4(int i = a) // expected-error{{invalid use of nonstatic data member 'a'}}
79{ return i; }
Douglas Gregor72b505b2008-12-16 21:30:33 +000080
81
82// Try to verify that default arguments interact properly with copy
83// constructors.
84class Z {
85public:
John McCallb1622a12010-01-06 09:43:14 +000086 Z(Z&, int i = 17); // expected-note 3 {{candidate constructor}}
Douglas Gregor72b505b2008-12-16 21:30:33 +000087
88 void f(Z& z) {
89 Z z2; // expected-error{{no matching constructor for initialization}}
90 Z z3(z);
91 }
Douglas Gregor61366e92008-12-24 00:01:03 +000092
93 void test_Z(const Z& z) {
John McCall7c2342d2010-03-10 11:27:22 +000094 Z z2(z); // expected-error{{no matching constructor for initialization of 'Z'}}
Douglas Gregor61366e92008-12-24 00:01:03 +000095 }
Douglas Gregor72b505b2008-12-16 21:30:33 +000096};
97
98void test_Z(const Z& z) {
John McCall7c2342d2010-03-10 11:27:22 +000099 Z z2(z); // expected-error{{no matching constructor for initialization of 'Z'}}
Douglas Gregor72b505b2008-12-16 21:30:33 +0000100}
Douglas Gregor61366e92008-12-24 00:01:03 +0000101
102struct ZZ {
Douglas Gregor61366e92008-12-24 00:01:03 +0000103 static ZZ g(int = 17);
104
Anders Carlsson5e300d12009-06-12 16:51:40 +0000105 void f(ZZ z = g()); // expected-error{{no matching constructor for initialization}}
106
John McCallb1622a12010-01-06 09:43:14 +0000107 ZZ(ZZ&, int = 17); // expected-note{{candidate constructor}}
Douglas Gregor61366e92008-12-24 00:01:03 +0000108};
Anders Carlsson5e300d12009-06-12 16:51:40 +0000109
110// http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_active.html#325
111class C2 {
112 static void g(int = f()); // expected-error{{use of default argument to function 'f' that is declared later in class 'C2'}}
113 static int f(int = 10); // expected-note{{default argument declared here}}
114};
Eli Friedmand33133c2009-07-22 21:45:50 +0000115
116// Make sure we actually parse the default argument for an inline definition
117class XX {
118 void A(int length = -1 ) { }
119 void B() { A(); }
120};