Debian Codegraph @

clang/test/CodeGenCXX/cxx1z-inline-variables.cpp

raw
1 // RUN: %clang_cc1 -std=c++1z %s -emit-llvm -o - -triple x86_64-linux-gnu | FileCheck %s
2
3 template <typename T> struct InlineAuto {
4 template <typename G> inline static auto var = 5;
5 };
6 int inlineauto = InlineAuto<int>::var<int>;
7 // CHECK: @_ZN10InlineAutoIiE3varIiEE = {{.*}}i32 5{{.*}}comdat
8
9 template <typename> struct PartialInlineAuto {
10 template <typename, typename> inline static auto var = 6;
11 template <typename T> inline static auto var<int, T> = 7;
12 };
13
14 int partialinlineauto = PartialInlineAuto<int>::var<int, int>;
15 // CHECK: @_ZN17PartialInlineAutoIiE3varIiiEE = {{.*}}i32 7{{.*}}comdat
16
17 struct Q {
18 // CHECK: @_ZN1Q1kE = linkonce_odr constant i32 5, comdat
19 static constexpr int k = 5;
20 };
21 const int &r = Q::k;
22
23 int f();
24
25 // const does not imply internal linkage.
26 // CHECK: @external_inline = linkonce_odr constant i32 5, comdat
27 inline const int external_inline = 5;
28 const int &use1 = external_inline;
29
30 // static still does, though.
31 // CHECK: @_ZL15internal_inline = internal constant i32 5
32 static inline const int internal_inline = 5;
33 const int &use2 = internal_inline;
34
35 int a = f();
36 // CHECK: @b = linkonce_odr global i32 0, comdat
37 // CHECK: @_ZGV1b = linkonce_odr global i64 0, comdat($b)
38 inline int b = f();
39 int c = f();
40
41 // For compatibility with C++11 and C++14, an out-of-line declaration of a
42 // static constexpr local variable promotes the variable to weak_odr.
43 struct compat {
44 static constexpr int a = 1;
45 static constexpr int b = 2;
46 static constexpr int c = 3;
47 static inline constexpr int d = 4;
48 static const int e = 5;
49 static const int f = 6;
50 static const int g = 7;
51 };
52 const int &compat_use_before_redecl = compat::b;
53 const int compat::a;
54 const int compat::b;
55 const int compat::c;
56 const int compat::d;
57 const int compat::e;
58 constexpr int compat::f;
59 constexpr inline int compat::g;
60 const int &compat_use_after_redecl1 = compat::c;
61 const int &compat_use_after_redecl2 = compat::d;
62 const int &compat_use_after_redecl3 = compat::g;
63 // CHECK-DAG: @_ZN6compat1bE = weak_odr constant i32 2
64 // CHECK-DAG: @_ZN6compat1aE = weak_odr constant i32 1
65 // CHECK-DAG: @_ZN6compat1cE = weak_odr constant i32 3
66 // CHECK-DAG: @_ZN6compat1dE = linkonce_odr constant i32 4
67 // CHECK-DAG: @_ZN6compat1eE ={{.*}} constant i32 5
68 // CHECK-DAG: @_ZN6compat1fE = weak_odr constant i32 6
69 // CHECK-DAG: @_ZN6compat1gE = linkonce_odr constant i32 7
70
71 template<typename T> struct X {
72 static int a;
73 static inline int b;
74 static int c;
75 static const int d;
76 static int e;
77 };
78 // CHECK: @_ZN1XIiE1aE = linkonce_odr global i32 10
79 // CHECK: @_ZN1XIiE1bE ={{.*}} global i32 20
80 // CHECK-NOT: @_ZN1XIiE1cE
81 // CHECK: @_ZN1XIiE1dE = linkonce_odr constant i32 40
82 // CHECK: @_ZN1XIiE1eE = linkonce_odr global i32 50
83 template<> inline int X<int>::a = 10;
84 int &use3 = X<int>::a;
85 template<> int X<int>::b = 20;
86 template<> inline int X<int>::c = 30;
87 template<typename T> constexpr int X<T>::d = 40;
88 template<typename T> inline int X<T>::e = 50;
89 const int *use_x_int_d = &X<int>::d;
90 const int *use_x_int_e = &X<int>::e;
91
92 template<typename T> struct Y;
93 template<> struct Y<int> {
94 static constexpr int a = 123;
95 static constexpr int b = 456;
96 static constexpr int c = 789;
97 };
98 // CHECK: @_ZN1YIiE1aE = weak_odr constant i32 123
99 constexpr int Y<int>::a;
100 // CHECK: @_ZN1YIiE1bE = linkonce_odr constant i32 456
101 const int &yib = Y<int>::b;
102 // CHECK-NOT: @_ZN1YIiE1cE
103
104 // CHECK-LABEL: define {{.*}}global_var_init
105 // CHECK: call noundef i32 @_Z1fv
106
107 // CHECK-LABEL: define {{.*}}global_var_init{{.*}} comdat($b)
108 // CHECK: load atomic {{.*}} acquire, align
109 // CHECK: br
110 // CHECK: __cxa_guard_acquire(ptr @_ZGV1b)
111 // CHECK: br
112 // CHECK: call noundef i32 @_Z1fv
113 // CHECK: __cxa_guard_release(ptr @_ZGV1b)
114
115 // CHECK-LABEL: define {{.*}}global_var_init
116 // CHECK: call noundef i32 @_Z1fv
117
118 template<typename T> inline int d = f();
119 int e = d<int>;
120
121 // CHECK-LABEL: define {{.*}}global_var_init{{.*}}comdat
122 // CHECK: _ZGV1dIiE
123 // CHECK-NOT: __cxa_guard_acquire(ptr @_ZGV1b)
124 // CHECK: call noundef i32 @_Z1fv
125 // CHECK-NOT: __cxa_guard_release(ptr @_ZGV1b)
126
127 namespace PR35599 {
128 struct Marker1 {};
129 struct Marker2 {};
130
131 template <typename>
132 struct Foo {
133 struct Bar { Bar(); };
134 inline static Bar bar;
135 };
136
137 void run() {
138 // All we want here are ODR uses. Anything that requires that the type is
139 // complete is uninteresting.
140 #pragma clang diagnostic push
141 #pragma clang diagnostic ignored "-Wunused-value"
142 Foo<Marker1>::bar;
143 #pragma clang diagnostic pop
144 static_cast<void>(Foo<Marker2>::bar);
145 }
146
147 // CHECK-LABEL: define {{.*}}global_var_init{{.*}}comdat
148 // CHECK: call void @_ZN7PR355993FooINS_7Marker1EE3BarC1Ev
149 // CHECK-LABEL: define {{.*}}global_var_init{{.*}}comdat
150 // CHECK: call void @_ZN7PR355993FooINS_7Marker2EE3BarC1Ev
151 }