Run DCE after a LoopFlatten test to reduce spurious output [nfc]
[llvm-project.git] / clang / test / SemaCXX / references.cpp
blob7ef3f43ff55ae63b6baeaf60c826a0bcf48443d9
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);
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;
22 typedef int t[1];
23 void test2() {
24 t a;
25 t& b = a;
28 int c[3];
29 int (&rc)[3] = c;
32 // C++ [dcl.init.ref]p5b1
33 struct A { };
34 struct B : A { } b;
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
41 A& ra = b; // ra refers to A subobject in b
42 const A& rca = b; // rca refers to A subobject in b
45 B fB();
47 // C++ [dcl.init.ref]p5b2
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'}}
53 const A& rca = fB();
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}}
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}}
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}}
67 const int&& r4{a}; // expected-error{{rvalue reference to type 'const int' cannot bind to lvalue of type 'const int'}}
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
80 // C++ [dcl.init.ref]p3
81 int& test6(int& x) {
82 int& yo; // expected-error{{declaration of reference variable 'yo' requires an initializer}}
84 return x;
86 int& not_initialized_error; // expected-error{{declaration of reference variable 'not_initialized_error' requires an initializer}}
87 extern int& not_initialized_okay;
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}}
93 struct C : B, A { }; // expected-warning {{direct base 'A' is inaccessible due to ambiguity:\n struct C -> B -> A\nstruct C -> A}}
95 void test7(C& c) {
96 A& a1 = c; // expected-error {{ambiguous conversion from derived class 'C' to base class 'A':}}
99 // C++ [dcl.ref]p1, C++ [dcl.ref]p4
100 void test8(int& const,// expected-error{{'const' qualifier may not be applied to a reference}}
102 void&, // expected-error{{cannot form a reference to 'void'}}
103 int& &) // expected-error{{type name declared as a reference to a reference}}
105 typedef int& intref;
106 typedef intref& intrefref; // C++ DR 106: reference collapsing
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
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}}
114 void restrict_ref(__restrict intref); // ok
115 void restrict_ref(int &__restrict); // ok
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
126 template<typename T> int const_param(const T) {}
127 int const_ref_param = const_param<int&>(const_ref_param); // no-warning
130 class string {
131 char *Data;
132 unsigned Length;
133 public:
134 string();
135 ~string();
138 string getInput();
140 void test9() {
141 string &s = getInput(); // expected-error{{lvalue reference}}
144 void test10() {
145 __attribute((vector_size(16))) typedef int vec4;
146 typedef __attribute__(( ext_vector_type(4) )) int ext_vec4;
148 vec4 v;
149 int &a = v[0]; // expected-error{{non-const reference cannot bind to vector element}}
150 const int &b = v[0];
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;
157 namespace PR7149 {
158 template<typename T> struct X0
160 T& first;
161 X0(T& p1) : first(p1) { }
165 void f()
167 int p1[1];
168 X0< const int[1]> c(p1);
172 namespace PR8608 {
173 bool& f(unsigned char& c) { return (bool&)c; }
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}}
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'}}
186 namespace 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 &&)'}}
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}}
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 &&)'}}
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}}
214 namespace PR45521 {
215 struct a { template<class b> a(const b * const&); };
216 int *d;
217 const a &r = d;