John McCall | 3030eb8 | 2010-11-06 09:44:32 +0000 | [diff] [blame] | 1 | // RUN: %clang_cc1 -triple x86_64-apple-darwin10 -emit-llvm -o - %s | FileCheck %s |
Anders Carlsson | 3bb9269 | 2010-01-26 17:43:42 +0000 | [diff] [blame] | 2 | |
John McCall | 3030eb8 | 2010-11-06 09:44:32 +0000 | [diff] [blame] | 3 | // CHECK: @_ZN5test11A1aE = constant i32 10, align 4 |
| 4 | // CHECK: @_ZN5test212_GLOBAL__N_11AIiE1xE = internal global i32 0, align 4 |
John McCall | 99ace16 | 2011-04-12 01:46:54 +0000 | [diff] [blame] | 5 | // CHECK: @_ZN5test31AIiE1xE = weak_odr global i32 0, align 4 |
| 6 | // CHECK: @_ZGVN5test31AIiE1xE = weak_odr global i64 0 |
Anders Carlsson | 3bb9269 | 2010-01-26 17:43:42 +0000 | [diff] [blame] | 7 | |
Richard Smith | b9c64d8 | 2012-02-16 20:41:22 +0000 | [diff] [blame] | 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 | |
Anders Carlsson | 3bb9269 | 2010-01-26 17:43:42 +0000 | [diff] [blame] | 14 | // PR5564. |
John McCall | 3030eb8 | 2010-11-06 09:44:32 +0000 | [diff] [blame] | 15 | namespace test1 { |
| 16 | struct A { |
| 17 | static const int a = 10; |
| 18 | }; |
Anders Carlsson | 3bb9269 | 2010-01-26 17:43:42 +0000 | [diff] [blame] | 19 | |
John McCall | 3030eb8 | 2010-11-06 09:44:32 +0000 | [diff] [blame] | 20 | const int A::a; |
Anders Carlsson | 3bb9269 | 2010-01-26 17:43:42 +0000 | [diff] [blame] | 21 | |
John McCall | 3030eb8 | 2010-11-06 09:44:32 +0000 | [diff] [blame] | 22 | struct S { |
| 23 | static int i; |
| 24 | }; |
Anders Carlsson | a0d4b63 | 2009-09-02 21:01:21 +0000 | [diff] [blame] | 25 | |
John McCall | 3030eb8 | 2010-11-06 09:44:32 +0000 | [diff] [blame] | 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. |
| 33 | namespace 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. |
| 53 | namespace 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 Carlsson | a0d4b63 | 2009-09-02 21:01:21 +0000 | [diff] [blame] | 72 | } |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 73 | |
| 74 | // Test that we can fold member lookup expressions which resolve to static data |
| 75 | // members. |
| 76 | namespace 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 Smith | b9c64d8 | 2012-02-16 20:41:22 +0000 | [diff] [blame] | 87 | |
| 88 | // Test that static data members in unions behave properly. |
| 89 | namespace 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 | } |