Chris Lattner | 0442108 | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 1 | // RUN: clang -fsyntax-only -verify %s |
| 2 | void f(int i, int j, int k = 3); |
Douglas Gregor | 6d6eb57 | 2008-05-07 04:49:29 +0000 | [diff] [blame] | 3 | void f(int i, int j, int k); |
Chris Lattner | 0442108 | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 4 | void f(int i, int j = 2, int k); |
Douglas Gregor | 6d6eb57 | 2008-05-07 04:49:29 +0000 | [diff] [blame] | 5 | void f(int i, int j, int k); |
Chris Lattner | 0442108 | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 6 | void f(int i = 1, int j, int k); |
Douglas Gregor | 6d6eb57 | 2008-05-07 04:49:29 +0000 | [diff] [blame] | 7 | void f(int i, int j, int k); |
Chris Lattner | 0442108 | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 8 | |
| 9 | void i() |
| 10 | { |
| 11 | f(); |
| 12 | f(0); |
| 13 | f(0, 1); |
| 14 | f(0, 1, 2); |
| 15 | } |
Chris Lattner | 8123a95 | 2008-04-10 02:22:51 +0000 | [diff] [blame] | 16 | |
| 17 | |
| 18 | int f1(int i, int i, int j) { // expected-error {{redefinition of parameter 'i'}} |
| 19 | i = 17; |
| 20 | return j; |
| 21 | } |
| 22 | |
| 23 | int x; |
| 24 | void g(int x, int y = x); // expected-error {{default argument references parameter 'x'}} |
| 25 | |
| 26 | void h() |
| 27 | { |
| 28 | int i; |
| 29 | extern void h2(int x = sizeof(i)); // expected-error {{default argument references local variable 'i' of enclosing function}} |
| 30 | } |
Chris Lattner | 9e97955 | 2008-04-12 23:52:44 +0000 | [diff] [blame] | 31 | |
| 32 | 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] | 33 | |
| 34 | void nondecl(int (*f)(int x = 5)) // {expected-error {{default arguments can only be specified}}} |
| 35 | { |
| 36 | void (*f2)(int = 17) // {expected-error {{default arguments can only be specified}}} |
| 37 | = (void (*)(int = 42))f; // {expected-error {{default arguments can only be specified}}} |
| 38 | } |
Douglas Gregor | 30c5436 | 2008-11-03 22:47:57 +0000 | [diff] [blame] | 39 | |
| 40 | class X { |
| 41 | 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] | 42 | |
| 43 | void g() { |
| 44 | int f(X* x = this); // expected-error{{default argument references 'this'}} |
| 45 | } |
Douglas Gregor | 30c5436 | 2008-11-03 22:47:57 +0000 | [diff] [blame] | 46 | }; |
Douglas Gregor | 69497c3 | 2008-12-16 00:08:34 +0000 | [diff] [blame] | 47 | |
| 48 | // C++ [dcl.fct.default]p6 |
| 49 | class C { |
| 50 | static int x; |
| 51 | void f(int i = 3); // expected-note{{previous definition is here}} |
| 52 | void g(int i, int j = x); |
| 53 | |
| 54 | void h(); |
| 55 | }; |
| 56 | void C::f(int i = 3) // expected-error{{redefinition of default argument}} |
| 57 | { } |
| 58 | |
| 59 | void C::g(int i = 88, int j) {} |
| 60 | |
| 61 | void C::h() { |
| 62 | g(); // okay |
| 63 | } |
| 64 | |
| 65 | // C++ [dcl.fct.default]p9 |
| 66 | class Y { |
| 67 | int a; |
| 68 | int mem1(int i = a); // expected-error{{invalid use of nonstatic data member 'a'}} |
| 69 | // FIXME: The code below is well-formed. |
| 70 | // int mem2(int i = b); // OK; use X::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 | 69497c3 | 2008-12-16 00:08:34 +0000 | [diff] [blame] | 73 | static int b; |
| 74 | }; |
Douglas Gregor | 0a59acb | 2008-12-16 00:38:16 +0000 | [diff] [blame^] | 75 | |
| 76 | int Y::mem3(int i = b) { return i; } // OK; use X::b |
| 77 | |
| 78 | int Y::mem4(int i = a) // expected-error{{invalid use of nonstatic data member 'a'}} |
| 79 | { return i; } |