blob: 4ad339db985be7c23ec7c73dc934ab3f16dc436a [file] [log] [blame]
John McCall3030eb82010-11-06 09:44:32 +00001// RUN: %clang_cc1 -triple x86_64-apple-darwin10 -emit-llvm -o - %s | FileCheck %s
Anders Carlsson3bb92692010-01-26 17:43:42 +00002
John McCall3030eb82010-11-06 09:44:32 +00003// CHECK: @_ZN5test11A1aE = constant i32 10, align 4
4// CHECK: @_ZN5test212_GLOBAL__N_11AIiE1xE = internal global i32 0, align 4
John McCall99ace162011-04-12 01:46:54 +00005// CHECK: @_ZN5test31AIiE1xE = weak_odr global i32 0, align 4
6// CHECK: @_ZGVN5test31AIiE1xE = weak_odr global i64 0
Anders Carlsson3bb92692010-01-26 17:43:42 +00007
Richard Smithb9c64d82012-02-16 20:41:22 +00008// 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
Anders Carlsson3bb92692010-01-26 17:43:42 +000014// PR5564.
John McCall3030eb82010-11-06 09:44:32 +000015namespace test1 {
16 struct A {
17 static const int a = 10;
18 };
Anders Carlsson3bb92692010-01-26 17:43:42 +000019
John McCall3030eb82010-11-06 09:44:32 +000020 const int A::a;
Anders Carlsson3bb92692010-01-26 17:43:42 +000021
John McCall3030eb82010-11-06 09:44:32 +000022 struct S {
23 static int i;
24 };
Anders Carlssona0d4b632009-09-02 21:01:21 +000025
John McCall3030eb82010-11-06 09:44:32 +000026 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: 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: 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
Anders Carlssona0d4b632009-09-02 21:01:21 +000072}
Richard Smithc49bd112011-10-28 17:51:58 +000073
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: define i32 @_ZN5test41fEPNS_1AE
83 // CHECK: ret i32 76
84 return a->n;
85 }
86}
Richard Smithb9c64d82012-02-16 20:41:22 +000087
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}