xref: /llvm-project/clang/test/CodeGenCXX/cxx1y-variable-template.cpp (revision 3e25ae605edd88720de3a4407fdd4ea85f758dce)
1 // RUN: %clang_cc1 -std=c++1y -triple x86_64-linux-gnu -emit-llvm -o - %s | FileCheck %s
2 
3 // Check that we keep the 'extern' when we instantiate the definition of this
4 // variable template specialization.
5 template<typename T> extern const int extern_redecl;
6 template<typename T> const int extern_redecl = 5;
7 template const int extern_redecl<int>;
8 
9 // CHECK: @_Z13extern_redeclIiE = weak_odr constant
10 
11 template<typename T> struct Outer {
12   template<typename U> struct Inner {
13     template<typename V> static int arr[];
14   };
15 };
16 Outer<char[100]> outer_int;
17 int init_arr();
18 template<typename T> template<typename U> template<typename V> int Outer<T>::Inner<U>::arr[sizeof(T) + sizeof(U) + sizeof(V)] = { init_arr() };
19 int *p = Outer<char[100]>::Inner<char[20]>::arr<char[3]>;
20 
21 namespace PR35456 {
22 // CHECK: @_ZN7PR354561nILi0EEE = linkonce_odr global i32 0
23 template<int> int n;
24 int *p = &n<0>;
25 }
26 
27 // CHECK: @_ZN5OuterIA100_cE5InnerIA20_cE3arrIA3_cEE = linkonce_odr global [123 x i32] zeroinitializer
28 // CHECK: @_ZGVN5OuterIA100_cE5InnerIA20_cE3arrIA3_cEE = linkonce_odr global
29 
30 // CHECK: @_ZTHN7PR4211112_GLOBAL__N_11nILi0EEE = internal alias {{.*}} @[[PR42111_CTOR:.*]]
31 
32 // CHECK: call {{.*}}@_Z8init_arrv
33 
34 // Ensure that we use guarded initialization for an instantiated thread_local
35 // variable with internal linkage.
36 namespace PR42111 {
37   int f();
38   namespace { template <int = 0> thread_local int n = f(); }
39   // CHECK: define {{.*}}@[[PR42111_CTOR]](
40   // CHECK: load {{.*}} @_ZGVN7PR4211112_GLOBAL__N_11nILi0EEE
41   // CHECK: icmp eq i8 {{.*}}, 0
42   // CHECK: br i1
43   // CHECK: store i8 1, ptr @_ZGVN7PR4211112_GLOBAL__N_11nILi0EEE
44   // CHECK: call noundef i32 @_ZN7PR421111fEv(
45   // CHECK: [[N_ADDR:%.+]] = call align 4 ptr @llvm.threadlocal.address.p0(ptr align 4 @_ZN7PR4211112_GLOBAL__N_11nILi0EEE)
46   // CHECK: store i32 {{.*}}, ptr [[N_ADDR]]
g()47   int g() { return n<> + n<>; }
48 }
49