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