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