Daniel Dunbar | d7d5f02 | 2009-03-24 02:24:46 +0000 | [diff] [blame] | 1 | // RUN: clang-cc -fsyntax-only -verify %s |
Sebastian Redl | ddf7e99 | 2009-02-08 10:28:44 +0000 | [diff] [blame] | 2 | |
Chris Lattner | 0442108 | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 3 | void f(int i, int j, int k = 3); |
Douglas Gregor | 6d6eb57 | 2008-05-07 04:49:29 +0000 | [diff] [blame] | 4 | void f(int i, int j, int k); |
Chris Lattner | 0442108 | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 5 | void f(int i, int j = 2, int k); |
Douglas Gregor | 6d6eb57 | 2008-05-07 04:49:29 +0000 | [diff] [blame] | 6 | void f(int i, int j, int k); |
Chris Lattner | 0442108 | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 7 | void f(int i = 1, int j, int k); |
Douglas Gregor | 6d6eb57 | 2008-05-07 04:49:29 +0000 | [diff] [blame] | 8 | void f(int i, int j, int k); |
Chris Lattner | 0442108 | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 9 | |
| 10 | void i() |
| 11 | { |
| 12 | f(); |
| 13 | f(0); |
| 14 | f(0, 1); |
| 15 | f(0, 1, 2); |
| 16 | } |
Chris Lattner | 8123a95 | 2008-04-10 02:22:51 +0000 | [diff] [blame] | 17 | |
| 18 | |
| 19 | int f1(int i, int i, int j) { // expected-error {{redefinition of parameter 'i'}} |
| 20 | i = 17; |
| 21 | return j; |
| 22 | } |
| 23 | |
| 24 | int x; |
| 25 | void g(int x, int y = x); // expected-error {{default argument references parameter 'x'}} |
| 26 | |
Chris Lattner | 9e97955 | 2008-04-12 23:52:44 +0000 | [diff] [blame] | 27 | void g2(int x, int y, int z = x + y); // expected-error {{default argument references parameter 'x'}} expected-error {{default argument references parameter 'y'}} |
Douglas Gregor | 6d6eb57 | 2008-05-07 04:49:29 +0000 | [diff] [blame] | 28 | |
Douglas Gregor | 30c5436 | 2008-11-03 22:47:57 +0000 | [diff] [blame] | 29 | class X { |
| 30 | void f(X* x = this); // expected-error{{invalid use of 'this' outside of a nonstatic member function}} |
Douglas Gregor | 3996f23 | 2008-11-04 13:41:56 +0000 | [diff] [blame] | 31 | |
| 32 | void g() { |
| 33 | int f(X* x = this); // expected-error{{default argument references 'this'}} |
| 34 | } |
Douglas Gregor | 30c5436 | 2008-11-03 22:47:57 +0000 | [diff] [blame] | 35 | }; |
Douglas Gregor | 69497c3 | 2008-12-16 00:08:34 +0000 | [diff] [blame] | 36 | |
| 37 | // C++ [dcl.fct.default]p6 |
| 38 | class 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 | }; |
| 45 | void C::f(int i = 3) // expected-error{{redefinition of default argument}} |
| 46 | { } |
| 47 | |
| 48 | void C::g(int i = 88, int j) {} |
| 49 | |
| 50 | void C::h() { |
| 51 | g(); // okay |
| 52 | } |
| 53 | |
| 54 | // C++ [dcl.fct.default]p9 |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 55 | struct Y { |
Douglas Gregor | 69497c3 | 2008-12-16 00:08:34 +0000 | [diff] [blame] | 56 | int a; |
| 57 | int mem1(int i = a); // expected-error{{invalid use of nonstatic data member 'a'}} |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 58 | int mem2(int i = b); // OK; use Y::b |
Douglas Gregor | 0a59acb | 2008-12-16 00:38:16 +0000 | [diff] [blame] | 59 | int mem3(int i); |
| 60 | int mem4(int i); |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 61 | |
| 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 Gregor | 69497c3 | 2008-12-16 00:08:34 +0000 | [diff] [blame] | 72 | static int b; |
| 73 | }; |
Douglas Gregor | 0a59acb | 2008-12-16 00:38:16 +0000 | [diff] [blame] | 74 | |
| 75 | int Y::mem3(int i = b) { return i; } // OK; use X::b |
| 76 | |
| 77 | int Y::mem4(int i = a) // expected-error{{invalid use of nonstatic data member 'a'}} |
| 78 | { return i; } |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 79 | |
| 80 | |
| 81 | // Try to verify that default arguments interact properly with copy |
| 82 | // constructors. |
| 83 | class Z { |
| 84 | public: |
Sebastian Redl | 00d5074 | 2009-02-08 14:56:26 +0000 | [diff] [blame] | 85 | Z(Z&, int i = 17); // expected-note 2 {{candidate function}} |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 86 | |
| 87 | void f(Z& z) { |
| 88 | Z z2; // expected-error{{no matching constructor for initialization}} |
| 89 | Z z3(z); |
| 90 | } |
Douglas Gregor | 61366e9 | 2008-12-24 00:01:03 +0000 | [diff] [blame] | 91 | |
| 92 | void test_Z(const Z& z) { |
| 93 | Z z2(z); // expected-error{{no matching constructor for initialization of 'z2'}} |
| 94 | } |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 95 | }; |
| 96 | |
| 97 | void test_Z(const Z& z) { |
| 98 | Z z2(z); // expected-error{{no matching constructor for initialization of 'z2'}} |
| 99 | } |
Douglas Gregor | 61366e9 | 2008-12-24 00:01:03 +0000 | [diff] [blame] | 100 | |
| 101 | struct ZZ { |
Douglas Gregor | 61366e9 | 2008-12-24 00:01:03 +0000 | [diff] [blame] | 102 | static ZZ g(int = 17); |
| 103 | |
Anders Carlsson | 5e300d1 | 2009-06-12 16:51:40 +0000 | [diff] [blame] | 104 | void f(ZZ z = g()); // expected-error{{no matching constructor for initialization}} |
| 105 | |
Douglas Gregor | 61366e9 | 2008-12-24 00:01:03 +0000 | [diff] [blame] | 106 | ZZ(ZZ&, int = 17); // expected-note{{candidate function}} |
| 107 | }; |
Anders Carlsson | 5e300d1 | 2009-06-12 16:51:40 +0000 | [diff] [blame] | 108 | |
| 109 | // http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_active.html#325 |
| 110 | class 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 Friedman | d33133c | 2009-07-22 21:45:50 +0000 | [diff] [blame] | 114 | |
| 115 | // Make sure we actually parse the default argument for an inline definition |
| 116 | class XX { |
| 117 | void A(int length = -1 ) { } |
| 118 | void B() { A(); } |
| 119 | }; |