1f4a2713aSLionel Sambuc // RUN: %clang_cc1 -fsyntax-only -verify %s 2f4a2713aSLionel Sambuc int g(int); 3f4a2713aSLionel Sambuc f()4f4a2713aSLionel Sambucvoid f() { 5f4a2713aSLionel Sambuc int i; 6f4a2713aSLionel Sambuc int &r = i; 7f4a2713aSLionel Sambuc r = 1; 8f4a2713aSLionel Sambuc int *p = &r; 9f4a2713aSLionel Sambuc int &rr = r; 10f4a2713aSLionel Sambuc int (&rg)(int) = g; 11f4a2713aSLionel Sambuc rg(i); 12f4a2713aSLionel Sambuc int a[3]; 13f4a2713aSLionel Sambuc int (&ra)[3] = a; 14f4a2713aSLionel Sambuc ra[1] = i; 15f4a2713aSLionel Sambuc int *Q; 16f4a2713aSLionel Sambuc int *& P = Q; 17f4a2713aSLionel Sambuc P[1] = 1; 18f4a2713aSLionel Sambuc } 19f4a2713aSLionel Sambuc 20f4a2713aSLionel Sambuc typedef int t[1]; test2()21f4a2713aSLionel Sambucvoid test2() { 22f4a2713aSLionel Sambuc t a; 23f4a2713aSLionel Sambuc t& b = a; 24f4a2713aSLionel Sambuc 25f4a2713aSLionel Sambuc 26f4a2713aSLionel Sambuc int c[3]; 27f4a2713aSLionel Sambuc int (&rc)[3] = c; 28f4a2713aSLionel Sambuc } 29f4a2713aSLionel Sambuc 30f4a2713aSLionel Sambuc // C++ [dcl.init.ref]p5b1 31f4a2713aSLionel Sambuc struct A { }; 32f4a2713aSLionel Sambuc struct B : A { } b; 33f4a2713aSLionel Sambuc test3()34f4a2713aSLionel Sambucvoid test3() { 35f4a2713aSLionel Sambuc double d = 2.0; 36f4a2713aSLionel Sambuc double& rd = d; // rd refers to d 37f4a2713aSLionel Sambuc const double& rcd = d; // rcd refers to d 38f4a2713aSLionel Sambuc 39f4a2713aSLionel Sambuc A& ra = b; // ra refers to A subobject in b 40f4a2713aSLionel Sambuc const A& rca = b; // rca refers to A subobject in b 41f4a2713aSLionel Sambuc } 42f4a2713aSLionel Sambuc 43f4a2713aSLionel Sambuc B fB(); 44f4a2713aSLionel Sambuc 45f4a2713aSLionel Sambuc // C++ [dcl.init.ref]p5b2 test4()46f4a2713aSLionel Sambucvoid test4() { 47f4a2713aSLionel Sambuc double& rd2 = 2.0; // expected-error{{non-const lvalue reference to type 'double' cannot bind to a temporary of type 'double'}} 48f4a2713aSLionel Sambuc int i = 2; 49f4a2713aSLionel Sambuc double& rd3 = i; // expected-error{{non-const lvalue reference to type 'double' cannot bind to a value of unrelated type 'int'}} 50f4a2713aSLionel Sambuc 51f4a2713aSLionel Sambuc const A& rca = fB(); 52f4a2713aSLionel Sambuc } 53f4a2713aSLionel Sambuc test5()54f4a2713aSLionel Sambucvoid test5() { 55f4a2713aSLionel Sambuc // const double& rcd2 = 2; // rcd2 refers to temporary with value 2.0 56f4a2713aSLionel Sambuc const volatile int cvi = 1; 57f4a2713aSLionel Sambuc const int& r = cvi; // expected-error{{binding of reference to type 'const int' to a value of type 'const volatile int' drops qualifiers}} 58f4a2713aSLionel Sambuc } 59f4a2713aSLionel Sambuc 60f4a2713aSLionel Sambuc // C++ [dcl.init.ref]p3 test6(int & x)61f4a2713aSLionel Sambucint& test6(int& x) { 62f4a2713aSLionel Sambuc int& yo; // expected-error{{declaration of reference variable 'yo' requires an initializer}} 63f4a2713aSLionel Sambuc 64f4a2713aSLionel Sambuc return x; 65f4a2713aSLionel Sambuc } 66f4a2713aSLionel Sambuc int& not_initialized_error; // expected-error{{declaration of reference variable 'not_initialized_error' requires an initializer}} 67f4a2713aSLionel Sambuc extern int& not_initialized_okay; 68f4a2713aSLionel Sambuc 69f4a2713aSLionel Sambuc class Test6 { // expected-warning{{class 'Test6' does not declare any constructor to initialize its non-modifiable members}} 70f4a2713aSLionel Sambuc int& okay; // expected-note{{reference member 'okay' will never be initialized}} 71f4a2713aSLionel Sambuc }; 72f4a2713aSLionel Sambuc 73f4a2713aSLionel Sambuc struct C : B, A { }; 74f4a2713aSLionel Sambuc test7(C & c)75f4a2713aSLionel Sambucvoid test7(C& c) { 76f4a2713aSLionel Sambuc A& a1 = c; // expected-error {{ambiguous conversion from derived class 'C' to base class 'A':}} 77f4a2713aSLionel Sambuc } 78f4a2713aSLionel Sambuc 79f4a2713aSLionel Sambuc // C++ [dcl.ref]p1, C++ [dcl.ref]p4 test8(int & const,void &,int & &)80f4a2713aSLionel Sambucvoid test8(int& const,// expected-error{{'const' qualifier may not be applied to a reference}} 81f4a2713aSLionel Sambuc 82f4a2713aSLionel Sambuc void&, // expected-error{{cannot form a reference to 'void'}} 83f4a2713aSLionel Sambuc int& &) // expected-error{{type name declared as a reference to a reference}} 84f4a2713aSLionel Sambuc { 85f4a2713aSLionel Sambuc typedef int& intref; 86f4a2713aSLionel Sambuc typedef intref& intrefref; // C++ DR 106: reference collapsing 87f4a2713aSLionel Sambuc 88*0a6a1f1dSLionel Sambuc typedef intref const intref_c; // expected-warning {{'const' qualifier on reference type 'intref' (aka 'int &') has no effect}} 89*0a6a1f1dSLionel Sambuc typedef intref_c intref; // ok, same type 90*0a6a1f1dSLionel Sambuc 91*0a6a1f1dSLionel Sambuc typedef intref volatile intref; // expected-warning {{'volatile' qualifier on reference type 'intref' (aka 'int &') has no effect}} 92*0a6a1f1dSLionel Sambuc typedef intref _Atomic intref; // expected-warning {{'_Atomic' qualifier on reference type 'intref' (aka 'int &') has no effect}} 93*0a6a1f1dSLionel Sambuc 94*0a6a1f1dSLionel Sambuc void restrict_ref(__restrict intref); // ok 95*0a6a1f1dSLionel Sambuc void restrict_ref(int &__restrict); // ok 96f4a2713aSLionel Sambuc } 97f4a2713aSLionel Sambuc const_param(const T)98*0a6a1f1dSLionel Sambuctemplate<typename T> int const_param(const T) {} 99*0a6a1f1dSLionel Sambuc int const_ref_param = const_param<int&>(const_ref_param); // no-warning 100*0a6a1f1dSLionel Sambuc 101f4a2713aSLionel Sambuc 102f4a2713aSLionel Sambuc class string { 103f4a2713aSLionel Sambuc char *Data; 104f4a2713aSLionel Sambuc unsigned Length; 105f4a2713aSLionel Sambuc public: 106f4a2713aSLionel Sambuc string(); 107f4a2713aSLionel Sambuc ~string(); 108f4a2713aSLionel Sambuc }; 109f4a2713aSLionel Sambuc 110f4a2713aSLionel Sambuc string getInput(); 111f4a2713aSLionel Sambuc test9()112f4a2713aSLionel Sambucvoid test9() { 113f4a2713aSLionel Sambuc string &s = getInput(); // expected-error{{lvalue reference}} 114f4a2713aSLionel Sambuc } 115f4a2713aSLionel Sambuc test10()116f4a2713aSLionel Sambucvoid test10() { 117f4a2713aSLionel Sambuc __attribute((vector_size(16))) typedef int vec4; 118f4a2713aSLionel Sambuc typedef __attribute__(( ext_vector_type(4) )) int ext_vec4; 119f4a2713aSLionel Sambuc 120f4a2713aSLionel Sambuc vec4 v; 121f4a2713aSLionel Sambuc int &a = v[0]; // expected-error{{non-const reference cannot bind to vector element}} 122f4a2713aSLionel Sambuc const int &b = v[0]; 123f4a2713aSLionel Sambuc 124f4a2713aSLionel Sambuc ext_vec4 ev; 125f4a2713aSLionel Sambuc int &c = ev.x; // expected-error{{non-const reference cannot bind to vector element}} 126f4a2713aSLionel Sambuc const int &d = ev.x; 127f4a2713aSLionel Sambuc } 128f4a2713aSLionel Sambuc 129f4a2713aSLionel Sambuc namespace PR7149 { 130f4a2713aSLionel Sambuc template<typename T> struct X0 131f4a2713aSLionel Sambuc { 132f4a2713aSLionel Sambuc T& first; X0PR7149::X0133f4a2713aSLionel Sambuc X0(T& p1) : first(p1) { } 134f4a2713aSLionel Sambuc }; 135f4a2713aSLionel Sambuc 136f4a2713aSLionel Sambuc f()137f4a2713aSLionel Sambuc void f() 138f4a2713aSLionel Sambuc { 139f4a2713aSLionel Sambuc int p1[1]; 140f4a2713aSLionel Sambuc X0< const int[1]> c(p1); 141f4a2713aSLionel Sambuc } 142f4a2713aSLionel Sambuc } 143f4a2713aSLionel Sambuc 144f4a2713aSLionel Sambuc namespace PR8608 { f(unsigned char & c)145f4a2713aSLionel Sambuc bool& f(unsigned char& c) { return (bool&)c; } 146f4a2713aSLionel Sambuc } 147f4a2713aSLionel Sambuc 148f4a2713aSLionel Sambuc // The following crashed trying to recursively evaluate the LValue. 149f4a2713aSLionel Sambuc 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}} 150f4a2713aSLionel Sambuc 151f4a2713aSLionel Sambuc namespace ExplicitRefInit { 152f4a2713aSLionel Sambuc // This is invalid: we can't copy-initialize an 'A' temporary using an 153f4a2713aSLionel Sambuc // explicit constructor. 154f4a2713aSLionel Sambuc struct A { explicit A(int); }; 155f4a2713aSLionel Sambuc const A &a(0); // expected-error {{reference to type 'const ExplicitRefInit::A' could not bind to an rvalue of type 'int'}} 156f4a2713aSLionel Sambuc } 157