Chris Lattner | 0442108 | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 1 | // RUN: clang -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 | |
| 27 | void h() |
| 28 | { |
| 29 | int i; |
| 30 | extern void h2(int x = sizeof(i)); // expected-error {{default argument references local variable 'i' of enclosing function}} |
| 31 | } |
Chris Lattner | 9e97955 | 2008-04-12 23:52:44 +0000 | [diff] [blame] | 32 | |
| 33 | 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] | 34 | |
| 35 | void nondecl(int (*f)(int x = 5)) // {expected-error {{default arguments can only be specified}}} |
| 36 | { |
| 37 | void (*f2)(int = 17) // {expected-error {{default arguments can only be specified}}} |
| 38 | = (void (*)(int = 42))f; // {expected-error {{default arguments can only be specified}}} |
| 39 | } |
Douglas Gregor | 30c5436 | 2008-11-03 22:47:57 +0000 | [diff] [blame] | 40 | |
| 41 | class X { |
| 42 | 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] | 43 | |
| 44 | void g() { |
| 45 | int f(X* x = this); // expected-error{{default argument references 'this'}} |
| 46 | } |
Douglas Gregor | 30c5436 | 2008-11-03 22:47:57 +0000 | [diff] [blame] | 47 | }; |
Douglas Gregor | 69497c3 | 2008-12-16 00:08:34 +0000 | [diff] [blame] | 48 | |
| 49 | // C++ [dcl.fct.default]p6 |
| 50 | class C { |
| 51 | static int x; |
| 52 | void f(int i = 3); // expected-note{{previous definition is here}} |
| 53 | void g(int i, int j = x); |
| 54 | |
| 55 | void h(); |
| 56 | }; |
| 57 | void C::f(int i = 3) // expected-error{{redefinition of default argument}} |
| 58 | { } |
| 59 | |
| 60 | void C::g(int i = 88, int j) {} |
| 61 | |
| 62 | void C::h() { |
| 63 | g(); // okay |
| 64 | } |
| 65 | |
| 66 | // C++ [dcl.fct.default]p9 |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 67 | struct Y { |
Douglas Gregor | 69497c3 | 2008-12-16 00:08:34 +0000 | [diff] [blame] | 68 | int a; |
| 69 | 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] | 70 | int mem2(int i = b); // OK; use Y::b |
Douglas Gregor | 0a59acb | 2008-12-16 00:38:16 +0000 | [diff] [blame] | 71 | int mem3(int i); |
| 72 | int mem4(int i); |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 73 | |
| 74 | struct Nested { |
| 75 | int mem5(int i = b, // OK; use Y::b |
| 76 | int j = c, // OK; use Y::Nested::c |
| 77 | int k = j, // expected-error{{default argument references parameter 'j'}} |
| 78 | int l = a, // expected-error{{invalid use of nonstatic data member 'a'}} |
| 79 | Nested* self = this, // expected-error{{invalid use of 'this' outside of a nonstatic member function}} |
| 80 | int m); // expected-error{{missing default argument on parameter 'm'}} |
| 81 | static int c; |
| 82 | }; |
| 83 | |
Douglas Gregor | 69497c3 | 2008-12-16 00:08:34 +0000 | [diff] [blame] | 84 | static int b; |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 85 | |
| 86 | int (*f)(int = 17); // expected-error{{default arguments can only be specified for parameters in a function declaration}} |
| 87 | |
| 88 | void mem8(int (*fp)(int) = (int (*)(int = 17))0); // expected-error{{default arguments can only be specified for parameters in a function declaration}} |
Douglas Gregor | 69497c3 | 2008-12-16 00:08:34 +0000 | [diff] [blame] | 89 | }; |
Douglas Gregor | 0a59acb | 2008-12-16 00:38:16 +0000 | [diff] [blame] | 90 | |
| 91 | int Y::mem3(int i = b) { return i; } // OK; use X::b |
| 92 | |
| 93 | int Y::mem4(int i = a) // expected-error{{invalid use of nonstatic data member 'a'}} |
| 94 | { return i; } |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 95 | |
| 96 | |
| 97 | // Try to verify that default arguments interact properly with copy |
| 98 | // constructors. |
| 99 | class Z { |
| 100 | public: |
Sebastian Redl | 00d5074 | 2009-02-08 14:56:26 +0000 | [diff] [blame^] | 101 | Z(Z&, int i = 17); // expected-note 2 {{candidate function}} |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 102 | |
| 103 | void f(Z& z) { |
| 104 | Z z2; // expected-error{{no matching constructor for initialization}} |
| 105 | Z z3(z); |
| 106 | } |
Douglas Gregor | 61366e9 | 2008-12-24 00:01:03 +0000 | [diff] [blame] | 107 | |
| 108 | void test_Z(const Z& z) { |
| 109 | Z z2(z); // expected-error{{no matching constructor for initialization of 'z2'}} |
| 110 | } |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 111 | }; |
| 112 | |
| 113 | void test_Z(const Z& z) { |
| 114 | Z z2(z); // expected-error{{no matching constructor for initialization of 'z2'}} |
| 115 | } |
Douglas Gregor | 61366e9 | 2008-12-24 00:01:03 +0000 | [diff] [blame] | 116 | |
| 117 | struct ZZ { |
| 118 | void f(ZZ z = g()); // expected-error{{no matching constructor for initialization}} |
| 119 | |
| 120 | static ZZ g(int = 17); |
| 121 | |
| 122 | ZZ(ZZ&, int = 17); // expected-note{{candidate function}} |
| 123 | }; |