1 // RUN: %clang_cc1 -fsyntax-only -verify %s
2 // RUN: %clang_cc1 -std=c++11 -fsyntax-only -verify %s
3 // RUN: %clang_cc1 -std=c++14 -fsyntax-only -verify %s
4 int g(int);
5
f()6 void f() {
7 int i;
8 int &r = i;
9 r = 1;
10 int *p = &r;
11 int &rr = r;
12 int (&rg)(int) = g;
13 rg(i);
14 int a[3];
15 int (&ra)[3] = a;
16 ra[1] = i;
17 int *Q;
18 int *& P = Q;
19 P[1] = 1;
20 }
21
22 typedef int t[1];
test2()23 void test2() {
24 t a;
25 t& b = a;
26
27
28 int c[3];
29 int (&rc)[3] = c;
30 }
31
32 // C++ [dcl.init.ref]p5b1
33 struct A { };
34 struct B : A { } b;
35
test3()36 void test3() {
37 double d = 2.0;
38 double& rd = d; // rd refers to d
39 const double& rcd = d; // rcd refers to d
40
41 A& ra = b; // ra refers to A subobject in b
42 const A& rca = b; // rca refers to A subobject in b
43 }
44
45 B fB();
46
47 // C++ [dcl.init.ref]p5b2
test4()48 void test4() {
49 double& rd2 = 2.0; // expected-error{{non-const lvalue reference to type 'double' cannot bind to a temporary of type 'double'}}
50 int i = 2;
51 double& rd3 = i; // expected-error{{non-const lvalue reference to type 'double' cannot bind to a value of unrelated type 'int'}}
52
53 const A& rca = fB();
54 }
55
test5()56 void test5() {
57 // const double& rcd2 = 2; // rcd2 refers to temporary with value 2.0
58 const volatile int cvi = 1;
59 const int& r = cvi; // expected-error{{binding reference of type 'const int' to value of type 'const volatile int' drops 'volatile' qualifier}}
60
61 #if __cplusplus >= 201103L
62 const int& r2{cvi}; // expected-error{{binding reference of type 'const int' to value of type 'const volatile int' drops 'volatile' qualifier}}
63
64 const int a = 2;
65 int& r3{a}; // expected-error{{binding reference of type 'int' to value of type 'const int' drops 'const' qualifier}}
66
67 const int&& r4{a}; // expected-error{{rvalue reference to type 'const int' cannot bind to lvalue of type 'const int'}}
68
69 void func();
70 void func(int);
71 int &ptr1 = {func}; // expected-error{{address of overloaded function 'func' does not match required type 'int'}}
72 int &&ptr2{func}; // expected-error{{address of overloaded function 'func' does not match required type 'int'}}
73 // expected-note@-4{{candidate function}}
74 // expected-note@-4{{candidate function}}
75 // expected-note@-6{{candidate function}}
76 // expected-note@-6{{candidate function}}
77 #endif
78 }
79
80 // C++ [dcl.init.ref]p3
test6(int & x)81 int& test6(int& x) {
82 int& yo; // expected-error{{declaration of reference variable 'yo' requires an initializer}}
83
84 return x;
85 }
86 int& not_initialized_error; // expected-error{{declaration of reference variable 'not_initialized_error' requires an initializer}}
87 extern int& not_initialized_okay;
88
89 class Test6 { // expected-warning{{class 'Test6' does not declare any constructor to initialize its non-modifiable members}}
90 int& okay; // expected-note{{reference member 'okay' will never be initialized}}
91 };
92
93 struct C : B, A { }; // expected-warning {{direct base 'A' is inaccessible due to ambiguity:\n struct C -> B -> A\nstruct C -> A}}
94
test7(C & c)95 void test7(C& c) {
96 A& a1 = c; // expected-error {{ambiguous conversion from derived class 'C' to base class 'A':}}
97 }
98
99 // C++ [dcl.ref]p1, C++ [dcl.ref]p4
test8(int & const,void &,int & &)100 void test8(int& const,// expected-error{{'const' qualifier may not be applied to a reference}}
101
102 void&, // expected-error{{cannot form a reference to 'void'}}
103 int& &) // expected-error{{type name declared as a reference to a reference}}
104 {
105 typedef int& intref;
106 typedef intref& intrefref; // C++ DR 106: reference collapsing
107
108 typedef intref const intref_c; // expected-warning {{'const' qualifier on reference type 'intref' (aka 'int &') has no effect}}
109 typedef intref_c intref; // ok, same type
110
111 typedef intref volatile intref; // expected-warning {{'volatile' qualifier on reference type 'intref' (aka 'int &') has no effect}}
112 typedef intref _Atomic intref; // expected-warning {{'_Atomic' qualifier on reference type 'intref' (aka 'int &') has no effect}}
113
114 void restrict_ref(__restrict intref); // ok
115 void restrict_ref(int &__restrict); // ok
116 }
117
118 namespace var_template {
119 #if __cplusplus >= 201402L
120 int i;
121 template <typename> int &ref = i; // ok
122 template <> int &ref<float>; // expected-error {{declaration of reference variable 'ref<float>' requires an initializer}}
123 #endif
124 } // namespace var_template
125
const_param(const T)126 template<typename T> int const_param(const T) {}
127 int const_ref_param = const_param<int&>(const_ref_param); // no-warning
128
129
130 class string {
131 char *Data;
132 unsigned Length;
133 public:
134 string();
135 ~string();
136 };
137
138 string getInput();
139
test9()140 void test9() {
141 string &s = getInput(); // expected-error{{lvalue reference}}
142 }
143
test10()144 void test10() {
145 __attribute((vector_size(16))) typedef int vec4;
146 typedef __attribute__(( ext_vector_type(4) )) int ext_vec4;
147
148 vec4 v;
149 int &a = v[0]; // expected-error{{non-const reference cannot bind to vector element}}
150 const int &b = v[0];
151
152 ext_vec4 ev;
153 int &c = ev.x; // expected-error{{non-const reference cannot bind to vector element}}
154 const int &d = ev.x;
155 }
156
157 namespace PR7149 {
158 template<typename T> struct X0
159 {
160 T& first;
X0PR7149::X0161 X0(T& p1) : first(p1) { }
162 };
163
164
f()165 void f()
166 {
167 int p1[1];
168 X0< const int[1]> c(p1);
169 }
170 }
171
172 namespace PR8608 {
f(unsigned char & c)173 bool& f(unsigned char& c) { return (bool&)c; }
174 }
175
176 // The following crashed trying to recursively evaluate the LValue.
177 const int &do_not_crash = do_not_crash; // expected-warning{{reference 'do_not_crash' is not yet bound to a value when used within its own initialization}}
178
179 namespace ExplicitRefInit {
180 // This is invalid: we can't copy-initialize an 'A' temporary using an
181 // explicit constructor.
182 struct A { explicit A(int); };
183 const A &a(0); // expected-error {{reference to type 'const A' could not bind to an rvalue of type 'int'}}
184 }
185
186 namespace RefCollapseTypePrinting {
add_lref()187 template<typename T> void add_lref() {
188 using X = int(T); // expected-note 4{{previous}}
189 using X = const volatile T&;
190 // expected-error@-1 {{'int &' vs 'int (int &)'}}
191 // expected-error@-2 {{'int &' vs 'int (int &&)'}}
192 // expected-error@-3 {{'const int &' vs 'int (const int &)'}}
193 // expected-error@-4 {{'const int &' vs 'int (const int &&)'}}
194 }
195 template void add_lref<int&>(); // expected-note {{instantiation of}}
196 template void add_lref<int&&>(); // expected-note {{instantiation of}}
197 template void add_lref<const int&>(); // expected-note {{instantiation of}}
198 template void add_lref<const int&&>(); // expected-note {{instantiation of}}
199
add_rref()200 template<typename T> void add_rref() {
201 using X = int(T); // expected-note 4{{previous}}
202 using X = const volatile T&&;
203 // expected-error@-1 {{'int &' vs 'int (int &)'}}
204 // expected-error@-2 {{'int &&' vs 'int (int &&)'}}
205 // expected-error@-3 {{'const int &' vs 'int (const int &)'}}
206 // expected-error@-4 {{'const int &&' vs 'int (const int &&)'}}
207 }
208 template void add_rref<int&>(); // expected-note {{instantiation of}}
209 template void add_rref<int&&>(); // expected-note {{instantiation of}}
210 template void add_rref<const int&>(); // expected-note {{instantiation of}}
211 template void add_rref<const int&&>(); // expected-note {{instantiation of}}
212 }
213
214 namespace PR45521 {
215 struct a { template<class b> a(const b * const&); };
216 int *d;
217 const a &r = d;
218 }
219