1 // RUN: %clang_cc1 %s -std=c++1y -triple=x86_64-pc-linux -emit-llvm -o - | FileCheck --check-prefix=ELF --check-prefix=ALL %s 2 // RUN: %clang_cc1 %s -std=c++1y -triple=x86_64-apple-darwin -emit-llvm -o - | FileCheck --check-prefix=MACHO --check-prefix=ALL %s 3 // RUN: %clang_cc1 %s -std=c++1y -triple=x86_64-pc-linux -emit-llvm -fdeclspec -DSELECTANY -o - | FileCheck --check-prefix=ELF-SELECTANY %s 4 5 #ifdef SELECTANY 6 struct S { 7 S(); 8 ~S(); 9 }; 10 11 int f(); 12 13 // ELF-SELECTANY: @llvm.global_ctors = appending global [1 x { i32, ptr, ptr }] [{ i32, ptr, ptr } { i32 65535, ptr @__cxx_global_var_init, ptr @selectany }] 14 // ELF-SELECTANY: @llvm.used = appending global [1 x ptr] [ptr @selectany] 15 int __declspec(selectany) selectany = f(); 16 17 #else 18 19 // ALL: ; ModuleID 20 21 extern "C" int foo(); 22 23 template<typename T> struct A { static int a; }; 24 template<typename T> int A<T>::a = foo(); 25 26 // ALLK-NOT: @_ZN1AIcE1aE 27 template<> int A<char>::a; 28 29 // ALL: @_ZN1AIbE1aE ={{.*}} global i32 10 30 template<> int A<bool>::a = 10; 31 32 // ALL: @llvm.global_ctors = appending global [16 x { i32, ptr, ptr }] 33 34 // ELF: [{ i32, ptr, ptr } { i32 65535, ptr @[[unordered:[^,]*]], ptr @_ZN1AIsE1aE }, 35 // MACHO: [{ i32, ptr, ptr } { i32 65535, ptr @[[unordered:[^,]*]], ptr null }, 36 37 // ELF: { i32, ptr, ptr } { i32 65535, ptr @[[unordered7:[^,]*]], ptr @_Z1xIsE }, 38 // MACHO: { i32, ptr, ptr } { i32 65535, ptr @[[unordered7:[^,]*]], ptr null }, 39 40 // ELF: { i32, ptr, ptr } { i32 65535, ptr @[[unordered10:[^,]*]], ptr @_ZN2ns1aIiE1iE }, 41 // MACHO: { i32, ptr, ptr } { i32 65535, ptr @[[unordered10:[^,]*]], ptr null }, 42 43 // ELF: { i32, ptr, ptr } { i32 65535, ptr @[[unordered11:[^,]*]], ptr @_ZN2ns1b1iIiEE }, 44 // MACHO: { i32, ptr, ptr } { i32 65535, ptr @[[unordered11:[^,]*]], ptr null }, 45 46 // ELF: { i32, ptr, ptr } { i32 65535, ptr @[[unordered14:[^,]*]], ptr @_ZN1AIvE1aE }, 47 // MACHO: { i32, ptr, ptr } { i32 65535, ptr @[[unordered14:[^,]*]], ptr null }, 48 49 // ELF: { i32, ptr, ptr } { i32 65535, ptr @[[unordered15:[^,]*]], ptr @_Z1xIcE }, 50 // MACHO: { i32, ptr, ptr } { i32 65535, ptr @[[unordered15:[^,]*]], ptr null }, 51 52 // ALL: { i32, ptr, ptr } { i32 65535, ptr @[[unordered16:[^,]*]], ptr null }, 53 54 // ELF: { i32, ptr, ptr } { i32 65535, ptr @[[unordered19:[^,]*]], ptr @_ZN3FibILi2EE1aE }, 55 // MACHO: { i32, ptr, ptr } { i32 65535, ptr @[[unordered19:[^,]*]], ptr null }, 56 57 // ELF: { i32, ptr, ptr } { i32 65535, ptr @[[unordered18:[^,]*]], ptr @_ZN3FibILi3EE1aE }, 58 // MACHO: { i32, ptr, ptr } { i32 65535, ptr @[[unordered18:[^,]*]], ptr null }, 59 60 // ELF: { i32, ptr, ptr } { i32 65535, ptr @[[unordered20:[^,]*]], ptr @_ZN3FibILi4EE1aE }, 61 // MACHO: { i32, ptr, ptr } { i32 65535, ptr @[[unordered20:[^,]*]], ptr null }, 62 63 // ELF: { i32, ptr, ptr } { i32 65535, ptr @[[unordered17:[^,]*]], ptr @_ZN3FibILi5EE1aE }, 64 // MACHO: { i32, ptr, ptr } { i32 65535, ptr @[[unordered17:[^,]*]], ptr null }, 65 66 // ELF: { i32, ptr, ptr } { i32 65535, ptr @[[unordered23:[^,]*]], ptr @_ZN4Fib2ILi2EE1aE }, 67 // MACHO: { i32, ptr, ptr } { i32 65535, ptr @[[unordered23:[^,]*]], ptr null }, 68 69 // ELF: { i32, ptr, ptr } { i32 65535, ptr @[[unordered24:[^,]*]], ptr @_ZN4Fib2ILi3EE1aE }, 70 // MACHO: { i32, ptr, ptr } { i32 65535, ptr @[[unordered24:[^,]*]], ptr null }, 71 72 // ELF: { i32, ptr, ptr } { i32 65535, ptr @[[unordered22:[^,]*]], ptr @_ZN4Fib2ILi4EE1aE }, 73 // MACHO: { i32, ptr, ptr } { i32 65535, ptr @[[unordered22:[^,]*]], ptr null }, 74 75 // ELF: { i32, ptr, ptr } { i32 65535, ptr @[[unordered21:[^,]*]], ptr @_ZN4Fib2ILi5EE1aE }, 76 // MACHO: { i32, ptr, ptr } { i32 65535, ptr @[[unordered21:[^,]*]], ptr null }, 77 78 // ALL: { i32, ptr, ptr } { i32 65535, ptr @_GLOBAL__sub_I_static_member_variable_explicit_specialization.cpp, ptr null }] 79 80 /// llvm.used ensures SHT_INIT_ARRAY in a section group cannot be GCed. 81 // ELF: @llvm.used = appending global [14 x ptr] [ptr @_ZN1AIsE1aE, ptr @_Z1xIsE, ptr @_ZN2ns1aIiE1iE, ptr @_ZN2ns1b1iIiEE, ptr @_ZN1AIvE1aE, ptr @_Z1xIcE, ptr @_ZN3FibILi5EE1aE, ptr @_ZN3FibILi3EE1aE, ptr @_ZN3FibILi2EE1aE, ptr @_ZN3FibILi4EE1aE, ptr @_ZN4Fib2ILi5EE1aE, ptr @_ZN4Fib2ILi4EE1aE, ptr @_ZN4Fib2ILi2EE1aE, ptr @_ZN4Fib2ILi3EE1aE] 82 83 template int A<short>::a; // Unordered 84 int b = foo(); 85 int c = foo(); 86 int d = A<void>::a; // Unordered 87 88 // An explicit specialization is ordered, and goes in __GLOBAL_sub_I_static_member_variable_explicit_specialization.cpp. 89 template<> struct A<int> { static int a; }; 90 int A<int>::a = foo(); 91 92 template<typename T> struct S { static T x; static T y; }; 93 template<> int S<int>::x = foo(); 94 template<> int S<int>::y = S<int>::x; 95 96 template<typename T> T x = foo(); 97 template short x<short>; // Unordered 98 template<> int x<int> = foo(); 99 int e = x<char>; // Unordered 100 101 namespace ns { 102 template <typename T> struct a { 103 static int i; 104 }; 105 template<typename T> int a<T>::i = foo(); 106 template struct a<int>; 107 108 struct b { 109 template <typename T> static T i; 110 }; 111 template<typename T> T b::i = foo(); 112 template int b::i<int>; 113 } 114 115 namespace { 116 template<typename T> struct Internal { static int a; }; 117 template<typename T> int Internal<T>::a = foo(); 118 } 119 int *use_internal_a = &Internal<int>::a; 120 121 template<int n> struct Fib { static int a; }; 122 template<> int Fib<0>::a = 0; 123 template<> int Fib<1>::a = 1; 124 template<int n> int Fib<n>::a = Fib<n-2>::a + Fib<n-1>::a; 125 int f = Fib<5>::a; 126 127 template<int n> struct Fib2 { static int a; }; 128 template<> int Fib2<0>::a = 0; 129 template<> int Fib2<1>::a = 1; 130 template<int n> int Fib2<n>::a = Fib2<n-1>::a + Fib2<n-2>::a; 131 int f2 = Fib2<5>::a; 132 133 #endif 134 135 // ALL: define internal void @[[unordered]]( 136 // ALL: call i32 @foo() 137 // ALL: store {{.*}} @_ZN1AIsE1aE 138 // ALL: ret 139 140 // ALL: define internal void @[[unordered7]]( 141 // ALL: call i32 @foo() 142 // ALL: store {{.*}} @_Z1xIsE 143 // ALL: ret 144 145 // ALL: define internal void @[[unordered10]]( 146 // ALL: call i32 @foo() 147 // ALL: store {{.*}} @_ZN2ns1aIiE1iE 148 // ALL: ret 149 150 // ALL: define internal void @[[unordered11]]( 151 // ALL: call i32 @foo() 152 // ALL: store {{.*}} @_ZN2ns1b1iIiEE 153 // ALL: ret 154 155 // ALL: define internal void @[[unordered15]]( 156 // ALL: call i32 @foo() 157 // ALL: store {{.*}} @_Z1xIcE 158 // ALL: ret 159 160 // ALL: define internal void @[[unordered16]]( 161 // ALL: call i32 @foo() 162 // ALL: store {{.*}} @_ZN12_GLOBAL__N_18InternalIiE1aE 163 // ALL: ret 164 165 // ALL: define internal void @[[unordered17]]( 166 // ALL: store {{.*}} @_ZN3FibILi5EE1aE 167 // ALL: ret 168 169 // ALL: define internal void @[[unordered18]]( 170 // ALL: store {{.*}} @_ZN3FibILi3EE1aE 171 // ALL: ret 172 173 // ALL: define internal void @[[unordered19]]( 174 // ALL: store {{.*}} @_ZN3FibILi2EE1aE 175 // ALL: ret 176 177 // ALL: define internal void @[[unordered20]]( 178 // ALL: store {{.*}} @_ZN3FibILi4EE1aE 179 // ALL: ret 180 181 // ALL: define internal void @[[unordered21]]( 182 // ALL: store {{.*}} @_ZN4Fib2ILi5EE1aE 183 // ALL: ret 184 185 // ALL: define internal void @[[unordered22]]( 186 // ALL: store {{.*}} @_ZN4Fib2ILi4EE1aE 187 // ALL: ret 188 189 // ALL: define internal void @[[unordered23]]( 190 // ALL: store {{.*}} @_ZN4Fib2ILi2EE1aE 191 // ALL: ret 192 193 // ALL: define internal void @[[unordered24]]( 194 // ALL: store {{.*}} @_ZN4Fib2ILi3EE1aE 195 // ALL: ret 196 197 // ALL: define internal void @_GLOBAL__sub_I_static_member_variable_explicit_specialization.cpp() 198 // We call unique stubs for every ordered dynamic initializer in the TU. 199 // ALL: call 200 // ALL: call 201 // ALL: call 202 // ALL: call 203 // ALL: call 204 // ALL: call 205 // ALL: call 206 // ALL: call 207 // ALL: call 208 // ALL: call 209 // ALL-NOT: call 210 // ALL: ret 211