Daniel Dunbar | a572887 | 2009-12-15 20:14:24 +0000 | [diff] [blame] | 1 | // RUN: %clang_cc1 -fsyntax-only -verify %s |
Bill Wendling | 08ad47c | 2007-07-17 03:52:31 +0000 | [diff] [blame] | 2 | int g(int); |
| 3 | |
| 4 | void f() { |
| 5 | int i; |
| 6 | int &r = i; |
| 7 | r = 1; |
| 8 | int *p = &r; |
| 9 | int &rr = r; |
Douglas Gregor | 3eb1c54 | 2008-12-17 16:19:15 +0000 | [diff] [blame] | 10 | int (&rg)(int) = g; |
Bill Wendling | 08ad47c | 2007-07-17 03:52:31 +0000 | [diff] [blame] | 11 | rg(i); |
| 12 | int a[3]; |
Douglas Gregor | 3eb1c54 | 2008-12-17 16:19:15 +0000 | [diff] [blame] | 13 | int (&ra)[3] = a; |
Bill Wendling | 08ad47c | 2007-07-17 03:52:31 +0000 | [diff] [blame] | 14 | ra[1] = i; |
| 15 | int *Q; |
| 16 | int *& P = Q; |
| 17 | P[1] = 1; |
| 18 | } |
Chris Lattner | e39245b | 2007-09-04 16:49:09 +0000 | [diff] [blame] | 19 | |
Chris Lattner | 943140e | 2007-10-16 02:55:40 +0000 | [diff] [blame] | 20 | typedef int t[1]; |
| 21 | void test2() { |
| 22 | t a; |
| 23 | t& b = a; |
| 24 | |
| 25 | |
| 26 | int c[3]; |
Douglas Gregor | 3eb1c54 | 2008-12-17 16:19:15 +0000 | [diff] [blame] | 27 | int (&rc)[3] = c; |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 28 | } |
| 29 | |
| 30 | // C++ [dcl.init.ref]p5b1 |
| 31 | struct A { }; |
| 32 | struct B : A { } b; |
| 33 | |
| 34 | void test3() { |
| 35 | double d = 2.0; |
| 36 | double& rd = d; // rd refers to d |
| 37 | const double& rcd = d; // rcd refers to d |
| 38 | |
| 39 | A& ra = b; // ra refers to A subobject in b |
| 40 | const A& rca = b; // rca refers to A subobject in b |
| 41 | } |
| 42 | |
| 43 | B fB(); |
| 44 | |
| 45 | // C++ [dcl.init.ref]p5b2 |
| 46 | void test4() { |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 47 | double& rd2 = 2.0; // expected-error{{non-const lvalue reference to type 'double' cannot bind to a temporary of type 'double'}} |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 48 | int i = 2; |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 49 | double& rd3 = i; // expected-error{{non-const lvalue reference to type 'double' cannot bind to a value of unrelated type 'int'}} |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 50 | |
| 51 | const A& rca = fB(); |
| 52 | } |
| 53 | |
| 54 | void test5() { |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 55 | // const double& rcd2 = 2; // rcd2 refers to temporary with value 2.0 |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 56 | const volatile int cvi = 1; |
Chris Lattner | 58f9e13 | 2010-09-05 00:04:01 +0000 | [diff] [blame] | 57 | const int& r = cvi; // expected-error{{binding of reference to type 'const int' to a value of type 'const volatile int' drops qualifiers}} |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | // C++ [dcl.init.ref]p3 |
| 61 | int& test6(int& x) { |
| 62 | int& yo; // expected-error{{declaration of reference variable 'yo' requires an initializer}} |
| 63 | |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 64 | return x; |
| 65 | } |
| 66 | int& not_initialized_error; // expected-error{{declaration of reference variable 'not_initialized_error' requires an initializer}} |
| 67 | extern int& not_initialized_okay; |
| 68 | |
Douglas Gregor | 325e593 | 2010-04-15 00:00:53 +0000 | [diff] [blame] | 69 | class Test6 { // expected-warning{{class 'Test6' does not declare any constructor to initialize its non-modifiable members}} |
| 70 | int& okay; // expected-note{{reference member 'okay' will never be initialized}} |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 71 | }; |
| 72 | |
| 73 | struct C : B, A { }; |
| 74 | |
| 75 | void test7(C& c) { |
John McCall | 7c2342d | 2010-03-10 11:27:22 +0000 | [diff] [blame] | 76 | A& a1 = c; // expected-error {{ambiguous conversion from derived class 'C' to base class 'A':}} |
Chris Lattner | 943140e | 2007-10-16 02:55:40 +0000 | [diff] [blame] | 77 | } |
Douglas Gregor | f1f9b4e | 2008-11-03 15:51:28 +0000 | [diff] [blame] | 78 | |
| 79 | // C++ [dcl.ref]p1, C++ [dcl.ref]p4 |
| 80 | void test8(int& const,// expected-error{{'const' qualifier may not be applied to a reference}} |
| 81 | |
| 82 | void&, // expected-error{{cannot form a reference to 'void'}} |
Chris Lattner | 08631c5 | 2008-11-23 21:45:46 +0000 | [diff] [blame] | 83 | int& &) // expected-error{{type name declared as a reference to a reference}} |
Douglas Gregor | f1f9b4e | 2008-11-03 15:51:28 +0000 | [diff] [blame] | 84 | { |
| 85 | typedef int& intref; |
| 86 | typedef intref& intrefref; // C++ DR 106: reference collapsing |
| 87 | |
| 88 | typedef intref const intref_c; // okay. FIXME: how do we verify that this is the same type as intref? |
| 89 | } |
Douglas Gregor | 96be691 | 2009-09-23 22:51:26 +0000 | [diff] [blame] | 90 | |
| 91 | |
| 92 | class string { |
| 93 | char *Data; |
| 94 | unsigned Length; |
| 95 | public: |
| 96 | string(); |
| 97 | ~string(); |
| 98 | }; |
| 99 | |
| 100 | string getInput(); |
| 101 | |
| 102 | void test9() { |
| 103 | string &s = getInput(); // expected-error{{lvalue reference}} |
| 104 | } |
Anders Carlsson | 0938026 | 2010-01-31 17:18:49 +0000 | [diff] [blame] | 105 | |
| 106 | void test10() { |
| 107 | __attribute((vector_size(16))) typedef int vec4; |
| 108 | typedef __attribute__(( ext_vector_type(4) )) int ext_vec4; |
| 109 | |
| 110 | vec4 v; |
| 111 | int &a = v[0]; // expected-error{{non-const reference cannot bind to vector element}} |
| 112 | const int &b = v[0]; |
| 113 | |
| 114 | ext_vec4 ev; |
| 115 | int &c = ev.x; // expected-error{{non-const reference cannot bind to vector element}} |
| 116 | const int &d = ev.x; |
| 117 | } |
Douglas Gregor | 9dadd94 | 2010-05-17 18:45:21 +0000 | [diff] [blame] | 118 | |
| 119 | namespace PR7149 { |
| 120 | template<typename T> struct X0 |
| 121 | { |
| 122 | T& first; |
| 123 | X0(T& p1) : first(p1) { } |
| 124 | }; |
| 125 | |
| 126 | |
| 127 | void f() |
| 128 | { |
| 129 | int p1[1]; |
| 130 | X0< const int[1]> c(p1); |
| 131 | } |
| 132 | } |