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