219 lines · cpp
1// RUN: %clang_cc1 -fsyntax-only -verify %s2// RUN: %clang_cc1 -std=c++11 -fsyntax-only -verify %s3// RUN: %clang_cc1 -std=c++14 -fsyntax-only -verify %s4int g(int);5 6void 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 22typedef int t[1];23void 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]p5b133struct A { };34struct B : A { } b;35 36void test3() {37 double d = 2.0;38 double& rd = d; // rd refers to d39 const double& rcd = d; // rcd refers to d40 41 A& ra = b; // ra refers to A subobject in b42 const A& rca = b; // rca refers to A subobject in b43}44 45B fB();46 47// C++ [dcl.init.ref]p5b248void 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 56void test5() {57 // const double& rcd2 = 2; // rcd2 refers to temporary with value 2.058 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 >= 201103L62 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#endif78}79 80// C++ [dcl.init.ref]p381int& test6(int& x) {82 int& yo; // expected-error{{declaration of reference variable 'yo' requires an initializer}}83 84 return x;85}86int& not_initialized_error; // expected-error{{declaration of reference variable 'not_initialized_error' requires an initializer}}87extern int& not_initialized_okay;88 89class 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 93struct C : B, A { }; // expected-warning {{direct base 'A' is inaccessible due to ambiguity:\n struct C -> B -> A\nstruct C -> A}}94 95void 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]p4100void 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 collapsing107 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 type110 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); // ok115 void restrict_ref(int &__restrict); // ok116}117 118namespace var_template {119#if __cplusplus >= 201402L120int i;121template <typename> int &ref = i; // ok122template <> int &ref<float>; // expected-error {{declaration of reference variable 'ref<float>' requires an initializer}}123#endif124} // namespace var_template125 126template<typename T> int const_param(const T) {}127int const_ref_param = const_param<int&>(const_ref_param); // no-warning128 129 130class string {131 char *Data;132 unsigned Length;133public:134 string(); 135 ~string();136};137 138string getInput();139 140void test9() {141 string &s = getInput(); // expected-error{{lvalue reference}}142}143 144void 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 157namespace PR7149 {158 template<typename T> struct X0159 {160 T& first;161 X0(T& p1) : first(p1) { }162 };163 164 165 void f()166 {167 int p1[1];168 X0< const int[1]> c(p1);169 }170}171 172namespace PR8608 {173 bool& f(unsigned char& c) { return (bool&)c; }174}175 176// The following crashed trying to recursively evaluate the LValue.177const 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 179namespace ExplicitRefInit {180 // This is invalid: we can't copy-initialize an 'A' temporary using an181 // 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 186namespace RefCollapseTypePrinting {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 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 214namespace PR45521 {215 struct a { template<class b> a(const b * const&); };216 int *d;217 const a &r = d;218}219