references.cpp revision 20093b4bf698f292c664676987541d5103b65b15
1// RUN: clang-cc -verify -emit-llvm -o - %s | FileCheck %s
2void t1() {
3  extern int& a;
4  int b = a;
5}
6
7void t2(int& a) {
8  int b = a;
9}
10
11int g;
12int& gr = g;
13int& grr = gr;
14void t3() {
15  int b = gr;
16}
17
18// Test reference binding.
19
20struct C { int a; };
21void f(const bool&);
22void f(const int&);
23void f(const _Complex int&);
24void f(const C&);
25
26C aggregate_return();
27
28bool& bool_reference_return();
29int& int_reference_return();
30_Complex int& complex_int_reference_return();
31C& aggregate_reference_return();
32
33void test_bool() {
34  bool a = true;
35  f(a);
36
37  f(true);
38
39  bool_reference_return() = true;
40  a = bool_reference_return();
41}
42
43void test_scalar() {
44  int a = 10;
45  f(a);
46
47  struct { int bitfield : 3; } s = { 3 };
48  f(s.bitfield);
49
50  f(10);
51
52  __attribute((vector_size(16))) typedef int vec4;
53  f((vec4){1,2,3,4}[0]);
54
55  int_reference_return() = 10;
56  a = int_reference_return();
57}
58
59void test_complex() {
60  _Complex int a = 10i;
61  f(a);
62
63  f(10i);
64
65  complex_int_reference_return() = 10i;
66  a = complex_int_reference_return();
67}
68
69void test_aggregate() {
70  C c;
71  f(c);
72
73  f(aggregate_return());
74  aggregate_reference_return().a = 10;
75
76  c = aggregate_reference_return();
77}
78
79int& reference_return() {
80  return g;
81}
82
83int reference_decl() {
84  int& a = g;
85  const int& b = 1;
86  return a+b;
87}
88
89struct A {
90  int& b();
91};
92
93void f(A* a) {
94  int b = a->b();
95}
96
97// PR5122
98void *foo = 0;
99void * const & kFoo = foo;
100
101struct D : C { D(); ~D(); };
102
103void h() {
104  // CHECK: call void @_ZN1DD1Ev
105  const C& c = D();
106}
107
108namespace T {
109  struct A {
110    A();
111    ~A();
112  };
113
114  struct B {
115    B();
116    ~B();
117    A f();
118  };
119
120  void f() {
121    // CHECK: call void @_ZN1T1BC1Ev
122    // CHECK: call void @_ZN1T1B1fEv
123    // CHECK: call void @_ZN1T1BD1Ev
124    const A& a = B().f();
125    // CHECK: call void @_ZN1T1fEv
126    f();
127    // CHECK: call void @_ZN1T1AD1Ev
128  }
129}
130
131// PR5227.
132namespace PR5227 {
133void f(int &a) {
134  (a = 10) = 20;
135}
136}
137
138// PR5590
139struct s0;
140struct s1 { struct s0 &s0; };
141void f0(s1 a) { s1 b = a; }
142