xref: /minix3/external/bsd/llvm/dist/clang/test/CodeGenCXX/static-data-member.cpp (revision f4a2713ac843a11c696ec80c0a5e3e5d80b4d338)
1*f4a2713aSLionel Sambuc // RUN: %clang_cc1 -triple x86_64-apple-darwin10 -emit-llvm -o - %s | FileCheck %s
2*f4a2713aSLionel Sambuc 
3*f4a2713aSLionel Sambuc // CHECK: @_ZN5test11A1aE = constant i32 10, align 4
4*f4a2713aSLionel Sambuc // CHECK: @_ZN5test212_GLOBAL__N_11AIiE1xE = internal global i32 0, align 4
5*f4a2713aSLionel Sambuc // CHECK: @_ZN5test31AIiE1xE = weak_odr global i32 0, align 4
6*f4a2713aSLionel Sambuc // CHECK: @_ZGVN5test31AIiE1xE = weak_odr global i64 0
7*f4a2713aSLionel Sambuc 
8*f4a2713aSLionel Sambuc // CHECK: _ZN5test51U2k0E = global i32 0
9*f4a2713aSLionel Sambuc // CHECK: _ZN5test51U2k1E = global i32 0
10*f4a2713aSLionel Sambuc // CHECK: _ZN5test51U2k2E = constant i32 76
11*f4a2713aSLionel Sambuc // CHECK-NOT: test51U2k3E
12*f4a2713aSLionel Sambuc // CHECK-NOT: test51U2k4E
13*f4a2713aSLionel Sambuc 
14*f4a2713aSLionel Sambuc // PR5564.
15*f4a2713aSLionel Sambuc namespace test1 {
16*f4a2713aSLionel Sambuc   struct A {
17*f4a2713aSLionel Sambuc     static const int a = 10;
18*f4a2713aSLionel Sambuc   };
19*f4a2713aSLionel Sambuc 
20*f4a2713aSLionel Sambuc   const int A::a;
21*f4a2713aSLionel Sambuc 
22*f4a2713aSLionel Sambuc   struct S {
23*f4a2713aSLionel Sambuc     static int i;
24*f4a2713aSLionel Sambuc   };
25*f4a2713aSLionel Sambuc 
26*f4a2713aSLionel Sambuc   void f() {
27*f4a2713aSLionel Sambuc     int a = S::i;
28*f4a2713aSLionel Sambuc   }
29*f4a2713aSLionel Sambuc }
30*f4a2713aSLionel Sambuc 
31*f4a2713aSLionel Sambuc // Test that we don't use guards for initializing template static data
32*f4a2713aSLionel Sambuc // members with internal linkage.
33*f4a2713aSLionel Sambuc namespace test2 {
34*f4a2713aSLionel Sambuc   int foo();
35*f4a2713aSLionel Sambuc 
36*f4a2713aSLionel Sambuc   namespace {
37*f4a2713aSLionel Sambuc     template <class T> struct A {
38*f4a2713aSLionel Sambuc       static int x;
39*f4a2713aSLionel Sambuc     };
40*f4a2713aSLionel Sambuc 
41*f4a2713aSLionel Sambuc     template <class T> int A<T>::x = foo();
42*f4a2713aSLionel Sambuc     template struct A<int>;
43*f4a2713aSLionel Sambuc   }
44*f4a2713aSLionel Sambuc 
45*f4a2713aSLionel Sambuc   // CHECK-LABEL: define internal void @__cxx_global_var_init()
46*f4a2713aSLionel Sambuc   // CHECK:      [[TMP:%.*]] = call i32 @_ZN5test23fooEv()
47*f4a2713aSLionel Sambuc   // CHECK-NEXT: store i32 [[TMP]], i32* @_ZN5test212_GLOBAL__N_11AIiE1xE, align 4
48*f4a2713aSLionel Sambuc   // CHECK-NEXT: ret void
49*f4a2713aSLionel Sambuc }
50*f4a2713aSLionel Sambuc 
51*f4a2713aSLionel Sambuc // Test that we don't use threadsafe statics when initializing
52*f4a2713aSLionel Sambuc // template static data members.
53*f4a2713aSLionel Sambuc namespace test3 {
54*f4a2713aSLionel Sambuc   int foo();
55*f4a2713aSLionel Sambuc 
56*f4a2713aSLionel Sambuc   template <class T> struct A {
57*f4a2713aSLionel Sambuc     static int x;
58*f4a2713aSLionel Sambuc   };
59*f4a2713aSLionel Sambuc 
60*f4a2713aSLionel Sambuc   template <class T> int A<T>::x = foo();
61*f4a2713aSLionel Sambuc   template struct A<int>;
62*f4a2713aSLionel Sambuc 
63*f4a2713aSLionel Sambuc   // CHECK-LABEL: define internal void @__cxx_global_var_init1()
64*f4a2713aSLionel Sambuc   // CHECK:      [[GUARDBYTE:%.*]] = load i8* bitcast (i64* @_ZGVN5test31AIiE1xE to i8*)
65*f4a2713aSLionel Sambuc   // CHECK-NEXT: [[UNINITIALIZED:%.*]] = icmp eq i8 [[GUARDBYTE]], 0
66*f4a2713aSLionel Sambuc   // CHECK-NEXT: br i1 [[UNINITIALIZED]]
67*f4a2713aSLionel Sambuc   // CHECK:      [[TMP:%.*]] = call i32 @_ZN5test33fooEv()
68*f4a2713aSLionel Sambuc   // CHECK-NEXT: store i32 [[TMP]], i32* @_ZN5test31AIiE1xE, align 4
69*f4a2713aSLionel Sambuc   // CHECK-NEXT: store i64 1, i64* @_ZGVN5test31AIiE1xE
70*f4a2713aSLionel Sambuc   // CHECK-NEXT: br label
71*f4a2713aSLionel Sambuc   // CHECK:      ret void
72*f4a2713aSLionel Sambuc }
73*f4a2713aSLionel Sambuc 
74*f4a2713aSLionel Sambuc // Test that we can fold member lookup expressions which resolve to static data
75*f4a2713aSLionel Sambuc // members.
76*f4a2713aSLionel Sambuc namespace test4 {
77*f4a2713aSLionel Sambuc   struct A {
78*f4a2713aSLionel Sambuc     static const int n = 76;
79*f4a2713aSLionel Sambuc   };
80*f4a2713aSLionel Sambuc 
81*f4a2713aSLionel Sambuc   int f(A *a) {
82*f4a2713aSLionel Sambuc     // CHECK-LABEL: define i32 @_ZN5test41fEPNS_1AE
83*f4a2713aSLionel Sambuc     // CHECK: ret i32 76
84*f4a2713aSLionel Sambuc     return a->n;
85*f4a2713aSLionel Sambuc   }
86*f4a2713aSLionel Sambuc }
87*f4a2713aSLionel Sambuc 
88*f4a2713aSLionel Sambuc // Test that static data members in unions behave properly.
89*f4a2713aSLionel Sambuc namespace test5 {
90*f4a2713aSLionel Sambuc   union U {
91*f4a2713aSLionel Sambuc     static int k0;
92*f4a2713aSLionel Sambuc     static const int k1;
93*f4a2713aSLionel Sambuc     static const int k2 = 76;
94*f4a2713aSLionel Sambuc     static const int k3;
95*f4a2713aSLionel Sambuc     static const int k4 = 81;
96*f4a2713aSLionel Sambuc   };
97*f4a2713aSLionel Sambuc   int U::k0;
98*f4a2713aSLionel Sambuc   const int U::k1 = (k0 = 9, 42);
99*f4a2713aSLionel Sambuc   const int U::k2;
100*f4a2713aSLionel Sambuc 
101*f4a2713aSLionel Sambuc   // CHECK: store i32 9, i32* @_ZN5test51U2k0E
102*f4a2713aSLionel Sambuc   // CHECK: store i32 {{.*}}, i32* @_ZN5test51U2k1E
103*f4a2713aSLionel Sambuc   // CHECK-NOT: store {{.*}} i32* @_ZN5test51U2k2E
104*f4a2713aSLionel Sambuc }
105