blob: 69d59b25285539ff75d947ce49015ded0cd42672 [file] [log] [blame]
Stephen Hines176edba2014-12-01 14:53:08 -08001// RUN: %clang_cc1 -triple x86_64-pc-linux -emit-llvm -o - %s | FileCheck %s
2// RUN: %clang_cc1 -triple x86_64-apple-darwin -emit-llvm -o - %s | \
3// RUN: FileCheck --check-prefix=MACHO %s
Anders Carlsson3bb92692010-01-26 17:43:42 +00004
John McCall3030eb82010-11-06 09:44:32 +00005// CHECK: @_ZN5test11A1aE = constant i32 10, align 4
6// CHECK: @_ZN5test212_GLOBAL__N_11AIiE1xE = internal global i32 0, align 4
Stephen Hines0e2c34f2015-03-23 12:09:02 -07007// CHECK: @_ZN5test31AIiE1xE = weak_odr global i32 0, comdat, align 4
8// CHECK: @_ZGVN5test31AIiE1xE = weak_odr global i64 0, comdat($_ZN5test31AIiE1xE)
Stephen Hines176edba2014-12-01 14:53:08 -08009// MACHO: @_ZGVN5test31AIiE1xE = weak_odr global i64 0
10// MACHO-NOT: comdat
Anders Carlsson3bb92692010-01-26 17:43:42 +000011
Richard Smithb9c64d82012-02-16 20:41:22 +000012// CHECK: _ZN5test51U2k0E = global i32 0
13// CHECK: _ZN5test51U2k1E = global i32 0
14// CHECK: _ZN5test51U2k2E = constant i32 76
15// CHECK-NOT: test51U2k3E
16// CHECK-NOT: test51U2k4E
17
Anders Carlsson3bb92692010-01-26 17:43:42 +000018// PR5564.
John McCall3030eb82010-11-06 09:44:32 +000019namespace test1 {
20 struct A {
21 static const int a = 10;
22 };
Anders Carlsson3bb92692010-01-26 17:43:42 +000023
John McCall3030eb82010-11-06 09:44:32 +000024 const int A::a;
Anders Carlsson3bb92692010-01-26 17:43:42 +000025
John McCall3030eb82010-11-06 09:44:32 +000026 struct S {
27 static int i;
28 };
Anders Carlssona0d4b632009-09-02 21:01:21 +000029
John McCall3030eb82010-11-06 09:44:32 +000030 void f() {
31 int a = S::i;
32 }
33}
34
35// Test that we don't use guards for initializing template static data
36// members with internal linkage.
37namespace test2 {
38 int foo();
39
40 namespace {
41 template <class T> struct A {
42 static int x;
43 };
44
45 template <class T> int A<T>::x = foo();
46 template struct A<int>;
47 }
48
Stephen Lin93ab6bf2013-08-15 06:47:53 +000049 // CHECK-LABEL: define internal void @__cxx_global_var_init()
John McCall3030eb82010-11-06 09:44:32 +000050 // CHECK: [[TMP:%.*]] = call i32 @_ZN5test23fooEv()
51 // CHECK-NEXT: store i32 [[TMP]], i32* @_ZN5test212_GLOBAL__N_11AIiE1xE, align 4
52 // CHECK-NEXT: ret void
53}
54
55// Test that we don't use threadsafe statics when initializing
56// template static data members.
57namespace test3 {
58 int foo();
59
60 template <class T> struct A {
61 static int x;
62 };
63
64 template <class T> int A<T>::x = foo();
65 template struct A<int>;
66
Stephen Hines0e2c34f2015-03-23 12:09:02 -070067 // CHECK-LABEL: define internal void @__cxx_global_var_init1() {{.*}} comdat($_ZN5test31AIiE1xE)
Stephen Hines176edba2014-12-01 14:53:08 -080068 // MACHO-LABEL: define internal void @__cxx_global_var_init1()
69 // MACHO-NOT: comdat
Pirama Arumuga Nainar3ea9e332015-04-08 08:57:32 -070070 // CHECK: [[GUARDBYTE:%.*]] = load i8, i8* bitcast (i64* @_ZGVN5test31AIiE1xE to i8*)
John McCall3030eb82010-11-06 09:44:32 +000071 // CHECK-NEXT: [[UNINITIALIZED:%.*]] = icmp eq i8 [[GUARDBYTE]], 0
72 // CHECK-NEXT: br i1 [[UNINITIALIZED]]
73 // CHECK: [[TMP:%.*]] = call i32 @_ZN5test33fooEv()
74 // CHECK-NEXT: store i32 [[TMP]], i32* @_ZN5test31AIiE1xE, align 4
75 // CHECK-NEXT: store i64 1, i64* @_ZGVN5test31AIiE1xE
76 // CHECK-NEXT: br label
77 // CHECK: ret void
Anders Carlssona0d4b632009-09-02 21:01:21 +000078}
Richard Smithc49bd112011-10-28 17:51:58 +000079
80// Test that we can fold member lookup expressions which resolve to static data
81// members.
82namespace test4 {
83 struct A {
84 static const int n = 76;
85 };
86
87 int f(A *a) {
Stephen Lin93ab6bf2013-08-15 06:47:53 +000088 // CHECK-LABEL: define i32 @_ZN5test41fEPNS_1AE
Richard Smithc49bd112011-10-28 17:51:58 +000089 // CHECK: ret i32 76
90 return a->n;
91 }
92}
Richard Smithb9c64d82012-02-16 20:41:22 +000093
94// Test that static data members in unions behave properly.
95namespace test5 {
96 union U {
97 static int k0;
98 static const int k1;
99 static const int k2 = 76;
100 static const int k3;
101 static const int k4 = 81;
102 };
103 int U::k0;
104 const int U::k1 = (k0 = 9, 42);
105 const int U::k2;
106
107 // CHECK: store i32 9, i32* @_ZN5test51U2k0E
108 // CHECK: store i32 {{.*}}, i32* @_ZN5test51U2k1E
109 // CHECK-NOT: store {{.*}} i32* @_ZN5test51U2k2E
110}