211 lines · cpp
1// RUN: %clang_cc1 %s -std=c++1y -triple=x86_64-pc-linux -emit-llvm -o - | FileCheck --check-prefix=ELF --check-prefix=ALL %s2// RUN: %clang_cc1 %s -std=c++1y -triple=x86_64-apple-darwin -emit-llvm -o - | FileCheck --check-prefix=MACHO --check-prefix=ALL %s3// RUN: %clang_cc1 %s -std=c++1y -triple=x86_64-pc-linux -emit-llvm -fdeclspec -DSELECTANY -o - | FileCheck --check-prefix=ELF-SELECTANY %s4 5#ifdef SELECTANY6struct S {7 S();8 ~S();9};10 11int 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]15int __declspec(selectany) selectany = f();16 17#else18 19// ALL: ; ModuleID20 21extern "C" int foo();22 23template<typename T> struct A { static int a; };24template<typename T> int A<T>::a = foo();25 26// ALLK-NOT: @_ZN1AIcE1aE27template<> int A<char>::a;28 29// ALL: @_ZN1AIbE1aE ={{.*}} global i32 1030template<> 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 83template int A<short>::a; // Unordered84int b = foo();85int c = foo();86int d = A<void>::a; // Unordered87 88// An explicit specialization is ordered, and goes in __GLOBAL_sub_I_static_member_variable_explicit_specialization.cpp.89template<> struct A<int> { static int a; };90int A<int>::a = foo();91 92template<typename T> struct S { static T x; static T y; };93template<> int S<int>::x = foo();94template<> int S<int>::y = S<int>::x;95 96template<typename T> T x = foo();97template short x<short>; // Unordered98template<> int x<int> = foo();99int e = x<char>; // Unordered100 101namespace ns {102template <typename T> struct a {103 static int i;104};105template<typename T> int a<T>::i = foo();106template struct a<int>;107 108struct b {109 template <typename T> static T i;110};111template<typename T> T b::i = foo();112template int b::i<int>;113}114 115namespace {116template<typename T> struct Internal { static int a; };117template<typename T> int Internal<T>::a = foo();118}119int *use_internal_a = &Internal<int>::a;120 121template<int n> struct Fib { static int a; };122template<> int Fib<0>::a = 0;123template<> int Fib<1>::a = 1;124template<int n> int Fib<n>::a = Fib<n-2>::a + Fib<n-1>::a;125int f = Fib<5>::a;126 127template<int n> struct Fib2 { static int a; };128template<> int Fib2<0>::a = 0;129template<> int Fib2<1>::a = 1;130template<int n> int Fib2<n>::a = Fib2<n-1>::a + Fib2<n-2>::a;131int f2 = Fib2<5>::a;132 133#endif134 135// ALL: define internal void @[[unordered]](136// ALL: call i32 @foo()137// ALL: store {{.*}} @_ZN1AIsE1aE138// ALL: ret139 140// ALL: define internal void @[[unordered7]](141// ALL: call i32 @foo()142// ALL: store {{.*}} @_Z1xIsE143// ALL: ret144 145// ALL: define internal void @[[unordered10]](146// ALL: call i32 @foo()147// ALL: store {{.*}} @_ZN2ns1aIiE1iE148// ALL: ret149 150// ALL: define internal void @[[unordered11]](151// ALL: call i32 @foo()152// ALL: store {{.*}} @_ZN2ns1b1iIiEE153// ALL: ret154 155// ALL: define internal void @[[unordered15]](156// ALL: call i32 @foo()157// ALL: store {{.*}} @_Z1xIcE158// ALL: ret159 160// ALL: define internal void @[[unordered16]](161// ALL: call i32 @foo()162// ALL: store {{.*}} @_ZN12_GLOBAL__N_18InternalIiE1aE163// ALL: ret164 165// ALL: define internal void @[[unordered17]](166// ALL: store {{.*}} @_ZN3FibILi5EE1aE167// ALL: ret168 169// ALL: define internal void @[[unordered18]](170// ALL: store {{.*}} @_ZN3FibILi3EE1aE171// ALL: ret172 173// ALL: define internal void @[[unordered19]](174// ALL: store {{.*}} @_ZN3FibILi2EE1aE175// ALL: ret176 177// ALL: define internal void @[[unordered20]](178// ALL: store {{.*}} @_ZN3FibILi4EE1aE179// ALL: ret180 181// ALL: define internal void @[[unordered21]](182// ALL: store {{.*}} @_ZN4Fib2ILi5EE1aE183// ALL: ret184 185// ALL: define internal void @[[unordered22]](186// ALL: store {{.*}} @_ZN4Fib2ILi4EE1aE187// ALL: ret188 189// ALL: define internal void @[[unordered23]](190// ALL: store {{.*}} @_ZN4Fib2ILi2EE1aE191// ALL: ret192 193// ALL: define internal void @[[unordered24]](194// ALL: store {{.*}} @_ZN4Fib2ILi3EE1aE195// ALL: ret196 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: call200// ALL: call201// ALL: call202// ALL: call203// ALL: call204// ALL: call205// ALL: call206// ALL: call207// ALL: call208// ALL: call209// ALL-NOT: call210// ALL: ret211