blob: b1768b1a3a4d80be7880992f131b4f4e5f65e405 [file] [log] [blame]
Daniel Dunbara5728872009-12-15 20:14:24 +00001// RUN: %clang_cc1 -fsyntax-only -verify %s
Bill Wendling08ad47c2007-07-17 03:52:31 +00002int g(int);
3
4void f() {
5 int i;
6 int &r = i;
7 r = 1;
8 int *p = &r;
9 int &rr = r;
Douglas Gregor3eb1c542008-12-17 16:19:15 +000010 int (&rg)(int) = g;
Bill Wendling08ad47c2007-07-17 03:52:31 +000011 rg(i);
12 int a[3];
Douglas Gregor3eb1c542008-12-17 16:19:15 +000013 int (&ra)[3] = a;
Bill Wendling08ad47c2007-07-17 03:52:31 +000014 ra[1] = i;
15 int *Q;
16 int *& P = Q;
17 P[1] = 1;
18}
Chris Lattnere39245b2007-09-04 16:49:09 +000019
Chris Lattner943140e2007-10-16 02:55:40 +000020typedef int t[1];
21void test2() {
22 t a;
23 t& b = a;
24
25
26 int c[3];
Douglas Gregor3eb1c542008-12-17 16:19:15 +000027 int (&rc)[3] = c;
Douglas Gregor27c8dc02008-10-29 00:13:59 +000028}
29
30// C++ [dcl.init.ref]p5b1
31struct A { };
32struct B : A { } b;
33
34void 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
43B fB();
44
45// C++ [dcl.init.ref]p5b2
46void test4() {
Douglas Gregor20093b42009-12-09 23:02:17 +000047 double& rd2 = 2.0; // expected-error{{non-const lvalue reference to type 'double' cannot bind to a temporary of type 'double'}}
Douglas Gregor27c8dc02008-10-29 00:13:59 +000048 int i = 2;
Douglas Gregor20093b42009-12-09 23:02:17 +000049 double& rd3 = i; // expected-error{{non-const lvalue reference to type 'double' cannot bind to a value of unrelated type 'int'}}
Douglas Gregor27c8dc02008-10-29 00:13:59 +000050
51 const A& rca = fB();
52}
53
54void test5() {
Douglas Gregor20093b42009-12-09 23:02:17 +000055 // const double& rcd2 = 2; // rcd2 refers to temporary with value 2.0
Douglas Gregor27c8dc02008-10-29 00:13:59 +000056 const volatile int cvi = 1;
Chris Lattner58f9e132010-09-05 00:04:01 +000057 const int& r = cvi; // expected-error{{binding of reference to type 'const int' to a value of type 'const volatile int' drops qualifiers}}
Douglas Gregor27c8dc02008-10-29 00:13:59 +000058}
59
60// C++ [dcl.init.ref]p3
61int& test6(int& x) {
62 int& yo; // expected-error{{declaration of reference variable 'yo' requires an initializer}}
63
Douglas Gregor27c8dc02008-10-29 00:13:59 +000064 return x;
65}
66int& not_initialized_error; // expected-error{{declaration of reference variable 'not_initialized_error' requires an initializer}}
67extern int& not_initialized_okay;
68
Douglas Gregor325e5932010-04-15 00:00:53 +000069class 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 Gregor27c8dc02008-10-29 00:13:59 +000071};
72
Stephen Hines0e2c34f2015-03-23 12:09:02 -070073struct C : B, A { }; // expected-warning {{direct base 'A' is inaccessible due to ambiguity:\n struct C -> struct B -> struct A\nstruct C -> struct A}}
Douglas Gregor27c8dc02008-10-29 00:13:59 +000074
75void test7(C& c) {
John McCall7c2342d2010-03-10 11:27:22 +000076 A& a1 = c; // expected-error {{ambiguous conversion from derived class 'C' to base class 'A':}}
Chris Lattner943140e2007-10-16 02:55:40 +000077}
Douglas Gregorf1f9b4e2008-11-03 15:51:28 +000078
79// C++ [dcl.ref]p1, C++ [dcl.ref]p4
80void 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 Lattner08631c52008-11-23 21:45:46 +000083 int& &) // expected-error{{type name declared as a reference to a reference}}
Douglas Gregorf1f9b4e2008-11-03 15:51:28 +000084{
85 typedef int& intref;
86 typedef intref& intrefref; // C++ DR 106: reference collapsing
87
Stephen Hines651f13c2014-04-23 16:59:28 -070088 typedef intref const intref_c; // expected-warning {{'const' qualifier on reference type 'intref' (aka 'int &') has no effect}}
89 typedef intref_c intref; // ok, same type
90
91 typedef intref volatile intref; // expected-warning {{'volatile' qualifier on reference type 'intref' (aka 'int &') has no effect}}
92 typedef intref _Atomic intref; // expected-warning {{'_Atomic' qualifier on reference type 'intref' (aka 'int &') has no effect}}
93
94 void restrict_ref(__restrict intref); // ok
95 void restrict_ref(int &__restrict); // ok
Douglas Gregorf1f9b4e2008-11-03 15:51:28 +000096}
Douglas Gregor96be6912009-09-23 22:51:26 +000097
Stephen Hines651f13c2014-04-23 16:59:28 -070098template<typename T> int const_param(const T) {}
99int const_ref_param = const_param<int&>(const_ref_param); // no-warning
100
Douglas Gregor96be6912009-09-23 22:51:26 +0000101
102class string {
103 char *Data;
104 unsigned Length;
105public:
106 string();
107 ~string();
108};
109
110string getInput();
111
112void test9() {
113 string &s = getInput(); // expected-error{{lvalue reference}}
114}
Anders Carlsson09380262010-01-31 17:18:49 +0000115
116void test10() {
117 __attribute((vector_size(16))) typedef int vec4;
118 typedef __attribute__(( ext_vector_type(4) )) int ext_vec4;
119
120 vec4 v;
121 int &a = v[0]; // expected-error{{non-const reference cannot bind to vector element}}
122 const int &b = v[0];
123
124 ext_vec4 ev;
125 int &c = ev.x; // expected-error{{non-const reference cannot bind to vector element}}
126 const int &d = ev.x;
127}
Douglas Gregor9dadd942010-05-17 18:45:21 +0000128
129namespace PR7149 {
130 template<typename T> struct X0
131 {
132 T& first;
133 X0(T& p1) : first(p1) { }
134 };
135
136
137 void f()
138 {
139 int p1[1];
140 X0< const int[1]> c(p1);
141 }
142}
John McCall5082d342010-11-16 00:12:50 +0000143
144namespace PR8608 {
145 bool& f(unsigned char& c) { return (bool&)c; }
146}
Chandler Carruth01248392011-08-22 17:24:56 +0000147
148// The following crashed trying to recursively evaluate the LValue.
Hans Wennborg5965b7c2012-08-20 08:52:22 +0000149const 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}}
Richard Smith4e47ecb2013-06-13 00:57:57 +0000150
151namespace ExplicitRefInit {
152 // This is invalid: we can't copy-initialize an 'A' temporary using an
153 // explicit constructor.
154 struct A { explicit A(int); };
155 const A &a(0); // expected-error {{reference to type 'const ExplicitRefInit::A' could not bind to an rvalue of type 'int'}}
156}