blob: 183452070da434faa1be0b462457ae19a6c2bb75 [file] [log] [blame]
Daniel Dunbard7d5f022009-03-24 02:24:46 +00001// RUN: clang-cc -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
19int f1(int i, int i, int j) { // expected-error {{redefinition of parameter 'i'}}
20 i = 17;
21 return j;
22}
23
24int x;
25void g(int x, int y = x); // expected-error {{default argument references parameter 'x'}}
26
Chris Lattner9e979552008-04-12 23:52:44 +000027void 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 +000028
Douglas Gregor30c54362008-11-03 22:47:57 +000029class X {
30 void f(X* x = this); // expected-error{{invalid use of 'this' outside of a nonstatic member function}}
Douglas Gregor3996f232008-11-04 13:41:56 +000031
32 void g() {
33 int f(X* x = this); // expected-error{{default argument references 'this'}}
34 }
Douglas Gregor30c54362008-11-03 22:47:57 +000035};
Douglas Gregor69497c32008-12-16 00:08:34 +000036
37// C++ [dcl.fct.default]p6
38class C {
39 static int x;
40 void f(int i = 3); // expected-note{{previous definition is here}}
41 void g(int i, int j = x);
42
43 void h();
44};
45void C::f(int i = 3) // expected-error{{redefinition of default argument}}
46{ }
47
48void C::g(int i = 88, int j) {}
49
50void C::h() {
51 g(); // okay
52}
53
54// C++ [dcl.fct.default]p9
Douglas Gregor72b505b2008-12-16 21:30:33 +000055struct Y {
Douglas Gregor69497c32008-12-16 00:08:34 +000056 int a;
57 int mem1(int i = a); // expected-error{{invalid use of nonstatic data member 'a'}}
Douglas Gregor72b505b2008-12-16 21:30:33 +000058 int mem2(int i = b); // OK; use Y::b
Douglas Gregor0a59acb2008-12-16 00:38:16 +000059 int mem3(int i);
60 int mem4(int i);
Douglas Gregor72b505b2008-12-16 21:30:33 +000061
62 struct Nested {
63 int mem5(int i = b, // OK; use Y::b
64 int j = c, // OK; use Y::Nested::c
65 int k = j, // expected-error{{default argument references parameter 'j'}}
66 int l = a, // expected-error{{invalid use of nonstatic data member 'a'}}
67 Nested* self = this, // expected-error{{invalid use of 'this' outside of a nonstatic member function}}
68 int m); // expected-error{{missing default argument on parameter 'm'}}
69 static int c;
70 };
71
Douglas Gregor69497c32008-12-16 00:08:34 +000072 static int b;
73};
Douglas Gregor0a59acb2008-12-16 00:38:16 +000074
75int Y::mem3(int i = b) { return i; } // OK; use X::b
76
77int Y::mem4(int i = a) // expected-error{{invalid use of nonstatic data member 'a'}}
78{ return i; }
Douglas Gregor72b505b2008-12-16 21:30:33 +000079
80
81// Try to verify that default arguments interact properly with copy
82// constructors.
83class Z {
84public:
Sebastian Redl00d50742009-02-08 14:56:26 +000085 Z(Z&, int i = 17); // expected-note 2 {{candidate function}}
Douglas Gregor72b505b2008-12-16 21:30:33 +000086
87 void f(Z& z) {
88 Z z2; // expected-error{{no matching constructor for initialization}}
89 Z z3(z);
90 }
Douglas Gregor61366e92008-12-24 00:01:03 +000091
92 void test_Z(const Z& z) {
93 Z z2(z); // expected-error{{no matching constructor for initialization of 'z2'}}
94 }
Douglas Gregor72b505b2008-12-16 21:30:33 +000095};
96
97void test_Z(const Z& z) {
98 Z z2(z); // expected-error{{no matching constructor for initialization of 'z2'}}
99}
Douglas Gregor61366e92008-12-24 00:01:03 +0000100
101struct ZZ {
Douglas Gregor61366e92008-12-24 00:01:03 +0000102 static ZZ g(int = 17);
103
Anders Carlsson5e300d12009-06-12 16:51:40 +0000104 void f(ZZ z = g()); // expected-error{{no matching constructor for initialization}}
105
Douglas Gregor61366e92008-12-24 00:01:03 +0000106 ZZ(ZZ&, int = 17); // expected-note{{candidate function}}
107};
Anders Carlsson5e300d12009-06-12 16:51:40 +0000108
109// http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_active.html#325
110class C2 {
111 static void g(int = f()); // expected-error{{use of default argument to function 'f' that is declared later in class 'C2'}}
112 static int f(int = 10); // expected-note{{default argument declared here}}
113};
Eli Friedmand33133c2009-07-22 21:45:50 +0000114
115// Make sure we actually parse the default argument for an inline definition
116class XX {
117 void A(int length = -1 ) { }
118 void B() { A(); }
119};