1 // RUN: not %clang_cc1 -verify -triple x86_64-apple-darwin -emit-llvm -o - %s | FileCheck %s
2 
3 // CHECK: @a = global i32 10
4 int a = 10;
5 // CHECK: @ar = constant i32* @a
6 int &ar = a;
7 
8 void f();
9 // CHECK: @fr = constant void ()* @_Z1fv
10 void (&fr)() = f;
11 
12 struct S { int& a; };
13 // CHECK: @s = global %struct.S { i32* @a }
14 S s = { a };
15 
16 // PR5581
17 namespace PR5581 {
18 class C {
19 public:
20   enum { e0, e1 };
21   unsigned f;
22 };
23 
24 // CHECK: @_ZN6PR55812g0E = global %"class.PR5581::C" { i32 1 }
25 C g0 = { C::e1 };
26 }
27 
28 namespace test2 {
29   struct A {
30     static const double d = 1.0;
31     static const float f = d / 2;
32     static int g();
33   } a;
34 
35   // CHECK: @_ZN5test22t0E = global double {{1\.0+e\+0+}}, align 8
36   // CHECK: @_ZN5test22t1E = global [2 x double] [double {{1\.0+e\+0+}}, double {{5\.0+e-0*}}1], align 16
37   // CHECK: @_ZN5test22t2E = global double* @_ZN5test21A1d
38   // CHECK: @_ZN5test22t3E = global {{.*}} @_ZN5test21A1g
39   double t0 = A::d;
40   double t1[] = { A::d, A::f };
41   const double *t2 = &a.d;
42   int (*t3)() = &a.g;
43 }
44 
45 // We don't expect to fold this in the frontend, but make sure it doesn't crash.
46 // CHECK: @PR9558 = global float 0.000000e+0
47 float PR9558 = reinterpret_cast<const float&>("asd");
48 
49 // An initialized const automatic variable cannot be promoted to a constant
50 // global if it has a mutable member.
51 struct MutableMember {
52   mutable int n;
53 };
54 int writeToMutable() {
55   // CHECK-NOT: {{.*}}MM{{.*}} = {{.*}}constant
56   const MutableMember MM = { 0 };
57   return ++MM.n;
58 }
59 
60 // Make sure we don't try to fold this in the frontend; the backend can't
61 // handle it.
62 // CHECK: @PR11705 = global i128 0
63 __int128_t PR11705 = (__int128_t)&PR11705;
64 
65 // Make sure we don't try to fold this either.
66 // CHECK: @_ZZ23UnfoldableAddrLabelDiffvE1x = internal global i128 0
67 void UnfoldableAddrLabelDiff() { static __int128_t x = (long)&&a-(long)&&b; a:b:return;}
68 
69 // But make sure we do fold this.
70 // CHECK: @_ZZ21FoldableAddrLabelDiffvE1x = internal global i64 sub (i64 ptrtoint (i8* blockaddress(@_Z21FoldableAddrLabelDiffv
71 void FoldableAddrLabelDiff() { static long x = (long)&&a-(long)&&b; a:b:return;}
72 
73 // CHECK: @i = constant i32* bitcast (float* @PR9558 to i32*)
74 int &i = reinterpret_cast<int&>(PR9558);
75 
76 int arr[2];
77 // CHECK: @pastEnd = constant i32* bitcast (i8* getelementptr (i8* bitcast ([2 x i32]* @arr to i8*), i64 8) to i32*)
78 int &pastEnd = arr[2];
79