blob: 604354b9af2d2089a728a0aeb11777e7e1c01276 [file] [log] [blame]
Chris Lattner04421082008-04-08 04:40:51 +00001// RUN: clang -fsyntax-only -verify %s
Sebastian Redlddf7e992009-02-08 10:28:44 +00002// XFAIL
3// fails due to exact diagnostic matching
4
Chris Lattner04421082008-04-08 04:40:51 +00005void f(int i, int j, int k = 3);
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, int j = 2, int k);
Douglas Gregor6d6eb572008-05-07 04:49:29 +00008void f(int i, int j, int k);
Chris Lattner04421082008-04-08 04:40:51 +00009void f(int i = 1, int j, int k);
Douglas Gregor6d6eb572008-05-07 04:49:29 +000010void f(int i, int j, int k);
Chris Lattner04421082008-04-08 04:40:51 +000011
12void i()
13{
14 f();
15 f(0);
16 f(0, 1);
17 f(0, 1, 2);
18}
Chris Lattner8123a952008-04-10 02:22:51 +000019
20
21int f1(int i, int i, int j) { // expected-error {{redefinition of parameter 'i'}}
22 i = 17;
23 return j;
24}
25
26int x;
27void g(int x, int y = x); // expected-error {{default argument references parameter 'x'}}
28
29void h()
30{
31 int i;
32 extern void h2(int x = sizeof(i)); // expected-error {{default argument references local variable 'i' of enclosing function}}
33}
Chris Lattner9e979552008-04-12 23:52:44 +000034
35void 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 +000036
37void nondecl(int (*f)(int x = 5)) // {expected-error {{default arguments can only be specified}}}
38{
39 void (*f2)(int = 17) // {expected-error {{default arguments can only be specified}}}
40 = (void (*)(int = 42))f; // {expected-error {{default arguments can only be specified}}}
41}
Douglas Gregor30c54362008-11-03 22:47:57 +000042
43class X {
44 void f(X* x = this); // expected-error{{invalid use of 'this' outside of a nonstatic member function}}
Douglas Gregor3996f232008-11-04 13:41:56 +000045
46 void g() {
47 int f(X* x = this); // expected-error{{default argument references 'this'}}
48 }
Douglas Gregor30c54362008-11-03 22:47:57 +000049};
Douglas Gregor69497c32008-12-16 00:08:34 +000050
51// C++ [dcl.fct.default]p6
52class C {
53 static int x;
54 void f(int i = 3); // expected-note{{previous definition is here}}
55 void g(int i, int j = x);
56
57 void h();
58};
59void C::f(int i = 3) // expected-error{{redefinition of default argument}}
60{ }
61
62void C::g(int i = 88, int j) {}
63
64void C::h() {
65 g(); // okay
66}
67
68// C++ [dcl.fct.default]p9
Douglas Gregor72b505b2008-12-16 21:30:33 +000069struct Y {
Douglas Gregor69497c32008-12-16 00:08:34 +000070 int a;
71 int mem1(int i = a); // expected-error{{invalid use of nonstatic data member 'a'}}
Douglas Gregor72b505b2008-12-16 21:30:33 +000072 int mem2(int i = b); // OK; use Y::b
Douglas Gregor0a59acb2008-12-16 00:38:16 +000073 int mem3(int i);
74 int mem4(int i);
Douglas Gregor72b505b2008-12-16 21:30:33 +000075
76 struct Nested {
77 int mem5(int i = b, // OK; use Y::b
78 int j = c, // OK; use Y::Nested::c
79 int k = j, // expected-error{{default argument references parameter 'j'}}
80 int l = a, // expected-error{{invalid use of nonstatic data member 'a'}}
81 Nested* self = this, // expected-error{{invalid use of 'this' outside of a nonstatic member function}}
82 int m); // expected-error{{missing default argument on parameter 'm'}}
83 static int c;
84 };
85
Douglas Gregor69497c32008-12-16 00:08:34 +000086 static int b;
Douglas Gregor72b505b2008-12-16 21:30:33 +000087
88 int (*f)(int = 17); // expected-error{{default arguments can only be specified for parameters in a function declaration}}
89
90 void mem8(int (*fp)(int) = (int (*)(int = 17))0); // expected-error{{default arguments can only be specified for parameters in a function declaration}}
Douglas Gregor69497c32008-12-16 00:08:34 +000091};
Douglas Gregor0a59acb2008-12-16 00:38:16 +000092
93int Y::mem3(int i = b) { return i; } // OK; use X::b
94
95int Y::mem4(int i = a) // expected-error{{invalid use of nonstatic data member 'a'}}
96{ return i; }
Douglas Gregor72b505b2008-12-16 21:30:33 +000097
98
99// Try to verify that default arguments interact properly with copy
100// constructors.
101class Z {
102public:
103 Z(Z&, int i = 17); // expected-note{{candidate function}}
104
105 void f(Z& z) {
106 Z z2; // expected-error{{no matching constructor for initialization}}
107 Z z3(z);
108 }
Douglas Gregor61366e92008-12-24 00:01:03 +0000109
110 void test_Z(const Z& z) {
111 Z z2(z); // expected-error{{no matching constructor for initialization of 'z2'}}
112 }
Douglas Gregor72b505b2008-12-16 21:30:33 +0000113};
114
115void test_Z(const Z& z) {
116 Z z2(z); // expected-error{{no matching constructor for initialization of 'z2'}}
117}
Douglas Gregor61366e92008-12-24 00:01:03 +0000118
119struct ZZ {
120 void f(ZZ z = g()); // expected-error{{no matching constructor for initialization}}
121
122 static ZZ g(int = 17);
123
124 ZZ(ZZ&, int = 17); // expected-note{{candidate function}}
125};