1 // RUN: %clang_cc1 -std=c++2a -fblocks %s -triple x86_64-unknown-unknown -emit-llvm -o %t.ll
2 
3 // This needs to be performed before #line directives which alter filename
4 // RUN: %clang_cc1 -fmacro-prefix-map=%p=/UNLIKELY/PATH -emit-llvm -o - %s | FileCheck %s --check-prefix=CHECK-PREFIX-MAP
5 //
6 // CHECK-PREFIX-MAP: /UNLIKELY/PATH{{/|\\\\}}builtin-source-location.cpp
testRemap()7 void testRemap() {
8   const char *file = __builtin_FILE();
9 }
10 
11 #line 8 "builtin-source-location.cpp"
12 
13 struct source_location {
14 private:
15   unsigned int __m_line = 0;
16   unsigned int __m_col = 0;
17   const char *__m_file = nullptr;
18   const char *__m_func = nullptr;
19 
20 public:
setsource_location21   constexpr void set(unsigned l, unsigned c, const char *f, const char *func) {
22     __m_line = l;
23     __m_col = c;
24     __m_file = f;
25     __m_func = func;
26   }
currentsource_location27   static constexpr source_location current(
28       unsigned int __line = __builtin_LINE(),
29       unsigned int __col = __builtin_COLUMN(),
30       const char *__file = __builtin_FILE(),
31       const char *__func = __builtin_FUNCTION()) noexcept {
32     source_location __loc;
33     __loc.set(__line, __col, __file, __func);
34     return __loc;
35   }
bad_currentsource_location36   static source_location bad_current(
37       unsigned int __line = __builtin_LINE(),
38       unsigned int __col = __builtin_COLUMN(),
39       const char *__file = __builtin_FILE(),
40       const char *__func = __builtin_FUNCTION()) noexcept {
41     source_location __loc;
42     __loc.set(__line, __col, __file, __func);
43     return __loc;
44   }
45   constexpr source_location() = default;
46   constexpr source_location(source_location const &) = default;
linesource_location47   constexpr unsigned int line() const noexcept { return __m_line; }
columnsource_location48   constexpr unsigned int column() const noexcept { return __m_col; }
filesource_location49   constexpr const char *file() const noexcept { return __m_file; }
functionsource_location50   constexpr const char *function() const noexcept { return __m_func; }
51 };
52 
53 using SL = source_location;
54 
55 extern "C" int sink(...);
56 
57 
58 // RUN: FileCheck --input-file %t.ll %s --check-prefix=CHECK-GLOBAL-ONE
59 //
60 // CHECK-GLOBAL-ONE-DAG: @[[FILE:.*]] = {{.*}}c"test_const_init.cpp\00"
61 // CHECK-GLOBAL-ONE-DAG: @[[FUNC:.*]] = private unnamed_addr constant [1 x i8] zeroinitializer, align 1
62 //
63 // CHECK-GLOBAL-ONE: @const_init_global ={{.*}} global %struct.source_location { i32 1000, i32 {{[0-9]+}}, {{[^@]*}}@[[FILE]], {{[^@]*}}@[[FUNC]]
64 #line 1000 "test_const_init.cpp"
65 SL const_init_global = SL::current();
66 
67 // RUN: FileCheck --input-file %t.ll %s --check-prefix=CHECK-GLOBAL-TWO
68 //
69 // CHECK-GLOBAL-TWO-DAG: @runtime_init_global ={{.*}} global %struct.source_location zeroinitializer, align 8
70 //
71 // CHECK-GLOBAL-TWO-DAG: @[[FILE:.*]] = {{.*}}c"test_runtime_init.cpp\00"
72 // CHECK-GLOBAL-TWO-DAG: @[[FUNC:.*]] = private unnamed_addr constant [1 x i8] zeroinitializer, align 1
73 //
74 // CHECK-GLOBAL-TWO: define internal void @__cxx_global_var_init()
75 // CHECK-GLOBAL-TWO-NOT: ret
76 // CHECK-GLOBAL-TWO: call void @_ZN15source_location11bad_currentEjjPKcS1_(%struct.source_location* sret(%struct.source_location) align 8 @runtime_init_global,
77 // CHECK-GLOBAL-TWO-SAME: i32 1100, i32 {{[0-9]+}}, {{[^@]*}}@[[FILE]], {{[^@]*}}@[[FUNC]],
78 #line 1100 "test_runtime_init.cpp"
79 SL runtime_init_global = SL::bad_current();
80 
81 #line 2000 "test_function.cpp"
test_function()82 extern "C" void test_function() {
83 // RUN: FileCheck --input-file %t.ll %s --check-prefix=CHECK-LOCAL-ONE
84 //
85 // CHECK-LOCAL-ONE-DAG: @[[FILE:.*]] = {{.*}}c"test_current.cpp\00"
86 // CHECK-LOCAL-ONE-DAG: @[[FUNC:.*]] = {{.*}}c"test_function\00"
87 //
88 // CHECK-LOCAL-ONE:  call void @_ZN15source_location7currentEjjPKcS1_(%struct.source_location* sret(%struct.source_location) align 8 %local,
89 // CHECK-LOCAL-ONE-SAME: i32 2100, i32 {{[0-9]+}},
90 // CHECK-LOCAL-ONE-SAME: {{[^@]*}}@[[FILE]], {{[^@]*}}@[[FUNC]],
91 #line 2100 "test_current.cpp"
92   SL local = SL::current();
93 }
94 
95 #line 3000 "TestInitClass.cpp"
96 struct TestInit {
97   SL info = SL::current();
98   SL arg_info;
99 
100 #line 3100 "TestInitCtor.cpp"
TestInitTestInit101   TestInit(SL arg_info = SL::current()) : arg_info(arg_info) {}
102 };
103 
104 // RUN: FileCheck --input-file %t.ll %s --check-prefix=CHECK-CTOR-GLOBAL
105 //
106 // CHECK-CTOR-GLOBAL-DAG: @GlobalInitVal ={{.*}} global %struct.TestInit zeroinitializer, align 8
107 // CHECK-CTOR-GLOBAL-DAG: @[[FILE:.*]] = {{.*}}c"GlobalInitVal.cpp\00"
108 // CHECK-CTOR-GLOBAL-DAG: @[[FUNC:.*]] = private unnamed_addr constant [1 x i8] zeroinitializer, align 1
109 //
110 // CHECK-CTOR-GLOBAL: define internal void @__cxx_global_var_init.{{[0-9]+}}()
111 // CHECK-CTOR-GLOBAL-NOT: ret
112 //
113 // CHECK-CTOR-GLOBAL: call void @_ZN15source_location7currentEjjPKcS1_(%struct.source_location* sret(%struct.source_location) align 8 %[[TMP_ONE:[^,]*]],
114 // CHECK-CTOR-GLOBAL-SAME: i32 3400, i32 {{[0-9]+}}, {{[^@]*}}@[[FILE]], {{[^@]*}}@[[FUNC]],
115 // CHECK-CTOR-GLOBAL-NEXT: call void @_ZN8TestInitC1E15source_location(%struct.TestInit* {{[^,]*}} @GlobalInitVal, %struct.source_location* {{.*}}%[[TMP_ONE]])
116 #line 3400 "GlobalInitVal.cpp"
117 TestInit GlobalInitVal;
118 
test_init_function()119 extern "C" void test_init_function() {
120 // RUN: FileCheck --input-file %t.ll %s --check-prefix=CHECK-CTOR-LOCAL
121 //
122 // CHECK-CTOR-LOCAL-DAG: @[[FILE:.*]] = {{.*}}c"LocalInitVal.cpp\00"
123 // CHECK-CTOR-LOCAL-DAG: @[[FUNC:.*]] = {{.*}}c"test_init_function\00"
124 //
125 // CHECK-CTOR-LOCAL: define{{.*}} void @test_init_function()
126 // CHECK-CTOR-LOCAL-NOT: ret
127 //
128 // CHECK-CTOR-LOCAL: call void @_ZN15source_location7currentEjjPKcS1_(%struct.source_location* sret(%struct.source_location) align 8 %[[TMP:[^,]*]],
129 // CHECK-CTOR-LOCAL-SAME: i32 3500, i32 {{[0-9]+}}, {{[^@]*}}@[[FILE]], {{[^@]*}}@[[FUNC]],
130 // CHECK-CTOR-LOCAL-NEXT: call void @_ZN8TestInitC1E15source_location(%struct.TestInit* {{[^,]*}} %init_local, %struct.source_location* {{.*}}%[[TMP]])
131 #line 3500 "LocalInitVal.cpp"
132   TestInit init_local;
133   sink(init_local);
134 }
135 
136 #line 4000 "ConstexprClass.cpp"
137 struct TestInitConstexpr {
138   SL info = SL::current();
139   SL arg_info;
140 #line 4200 "ConstexprCtor.cpp"
TestInitConstexprTestInitConstexpr141   constexpr TestInitConstexpr(SL arg_info = SL::current()) : arg_info(arg_info) {}
142 };
143 
144 // RUN: FileCheck --input-file %t.ll %s --check-prefix=CHECK-CONSTEXPR-T2
145 //
146 // CHECK-CONSTEXPR-T2-DAG: @[[FILE_INIT:.*]] = {{.*}}c"ConstexprCtor.cpp\00"
147 // CHECK-CONSTEXPR-T2-DAG: @[[FUNC_INIT:.*]] = {{.*}}c"TestInitConstexpr\00"
148 // CHECK-CONSTEXPR-T2-DAG: @[[FILE_ARG:.*]] = {{.*}}c"ConstexprGlobal.cpp\00"
149 // CHECK-CONSTEXPR-T2-DAG: @[[EMPTY:.*]] = private unnamed_addr constant [1 x i8] zeroinitializer, align 1
150 //
151 // CHECK-CONSTEXPR-T2: @ConstexprGlobal ={{.*}} global %struct.TestInitConstexpr {
152 // CHECK-CONSTEXPR-T2-SAME: %struct.source_location { i32 4200, i32 {{[0-9]+}}, {{[^@]*}}@[[FILE_INIT]], {{[^@]*}}@[[FUNC_INIT]],
153 // CHECK-CONSTEXPR-T2-SAME: {{[^%]*}}%struct.source_location { i32 4400, i32 {{[0-9]+}},  {{[^@]*}}@[[FILE_ARG]], {{[^@]*}}@[[EMPTY]]
154 #line 4400 "ConstexprGlobal.cpp"
155 TestInitConstexpr ConstexprGlobal;
156 
test_init_function_constexpr()157 extern "C" void test_init_function_constexpr() {
158 // RUN: FileCheck --input-file %t.ll %s --check-prefix=CHECK-CONSTEXPR-LOCAL
159 //
160 // CHECK-CONSTEXPR-LOCAL-DAG: @[[FUNC:.*]] = {{.*}}c"test_init_function_constexpr\00"
161 // CHECK-CONSTEXPR-LOCAL-DAG: @[[FILE:.*]] = {{.*}}c"ConstexprLocal.cpp\00"
162 //
163 // CHECK-CONSTEXPR-LOCAL: define{{.*}} void @test_init_function_constexpr()
164 // CHECK-CONSTEXPR-LOCAL: call void @_ZN15source_location7currentEjjPKcS1_(%struct.source_location* sret(%struct.source_location) align 8 %[[TMP:[^,]*]],
165 // CHECK-CONSTEXPR-LOCAL-SAME: i32 4600, i32 {{[0-9]+}}, {{[^@]*}}@[[FILE]], {{[^@]*}}@[[FUNC]]
166 // CHECK-CONSTEXPR-LOCAL: call void @_ZN17TestInitConstexprC1E15source_location(%struct.TestInitConstexpr* {{[^,]*}} %local_val, {{.*}}%[[TMP]])
167 #line 4600 "ConstexprLocal.cpp"
168   TestInitConstexpr local_val;
169 }
170 
171 #line 5000 "TestInitAgg.cpp"
172 struct TestInitAgg {
173 #line 5100 "i1.cpp"
174   SL i1;
175 #line 5200 "i2.cpp"
176   SL i2 = SL::current();
177 #line 5300 "TestInitAggEnd.cpp"
178 };
179 
180 // RUN: FileCheck --input-file %t.ll %s --check-prefix=CHECK-AGG-DEFAULT
181 //
182 // CHECK-AGG-DEFAULT-DAG: @[[FILE:.*]] = {{.*}}c"TestInitAgg.cpp\00"
183 // CHECK-AGG-DEFAULT-DAG: @[[FUNC:.*]] = {{.*}}c"TestInitAgg\00"
184 //
185 // CHECK-AGG-DEFAULT: @GlobalAggDefault ={{.*}} global %struct.TestInitAgg {
186 // CHECK-AGG-DEFAULT-SAME: %struct.source_location zeroinitializer,
187 // CHECK-AGG-DEFAULT-SAME: %struct.source_location { i32 5000, i32 {{[0-9]+}}, {{[^@]*}}@[[FILE]], {{[^@]*}}@[[FUNC]]
188 #line 5400 "GlobalAggDefault.cpp"
189 TestInitAgg GlobalAggDefault;
190 
191 #line 5500 "test_agg_init_test.cpp"
test_agg_init()192 extern "C" void test_agg_init() {
193 // RUN: FileCheck --input-file %t.ll %s --check-prefix=CHECK-AGG-BRACE
194 //
195 // CHECK-AGG-BRACE-DAG: @[[FILE:.*]] = {{.*}}c"BraceInitEnd.cpp\00"
196 // CHECK-AGG-BRACE-DAG: @[[FUNC:.*]] = {{.*}}c"test_agg_init\00"
197 //
198 // CHECK-AGG-BRACE: define{{.*}} void @test_agg_init()
199 // CHECK-AGG-BRACE: %[[I2:.*]] = getelementptr inbounds %struct.TestInitAgg, %struct.TestInitAgg* %local_brace_init, i32 0, i32 1
200 // CHECK-AGG-BRACE-NEXT: call void @_ZN15source_location7currentEjjPKcS1_(%struct.source_location* sret(%struct.source_location) align 8 %[[I2]],
201 // CHECK-AGG-BRACE-SAME: i32 5700, i32 {{[0-9]+}}, {{[^@]*}}@[[FILE]], {{[^@]*}}@[[FUNC]]
202 #line 5600 "BraceInitStart.cpp"
203   TestInitAgg local_brace_init{
204 #line 5700 "BraceInitEnd.cpp"
205   };
206 
207 // RUN: FileCheck --input-file %t.ll %s --check-prefix=CHECK-AGG-EQUAL
208 //
209 // CHECK-AGG-EQUAL-DAG: @[[FILE:.*]] = {{.*}}c"EqualInitEnd.cpp\00"
210 // CHECK-AGG-EQUAL-DAG: @[[FUNC:.*]] = {{.*}}c"test_agg_init\00"
211 //
212 // CHECK-AGG-EQUAL: define{{.*}} void @test_agg_init()
213 // CHECK-AGG-EQUAL: %[[I2:.*]] = getelementptr inbounds %struct.TestInitAgg, %struct.TestInitAgg* %local_equal_init, i32 0, i32 1
214 // CHECK-AGG-EQUAL-NEXT: call void @_ZN15source_location7currentEjjPKcS1_(%struct.source_location* sret(%struct.source_location) align 8 %[[I2]],
215 // CHECK-AGG-EQUAL-SAME: i32 5900, i32 {{[0-9]+}}, {{[^@]*}}@[[FILE]], {{[^@]*}}@[[FUNC]]
216 #line 5800 "EqualInitStart.cpp"
217   TestInitAgg local_equal_init =
218       {
219 #line 5900 "EqualInitEnd.cpp"
220       };
221 
222 // RUN: FileCheck --input-file %t.ll %s --check-prefix=CHECK-AGG-LIST
223 //
224 // CHECK-AGG-LIST-DAG: @[[FILE_DEFAULT:.*]] = {{.*}}c"InitListEnd.cpp\00"
225 // CHECK-AGG-LIST-DAG: @[[FILE_ELEM:.*]] = {{.*}}c"ListElem.cpp\00"
226 // CHECK-AGG-LIST-DAG: @[[FUNC:.*]] = {{.*}}c"test_agg_init\00"
227 //
228 // CHECK-AGG-LIST: define{{.*}} void @test_agg_init()
229 //
230 // CHECK-AGG-LIST: %[[I1:.*]] =  getelementptr inbounds %struct.TestInitAgg, %struct.TestInitAgg* %local_list_init, i32 0, i32 0
231 // CHECK-AGG-LIST-NEXT: call void @_ZN15source_location7currentEjjPKcS1_(%struct.source_location* sret(%struct.source_location) align 8 %[[I1]],
232 // CHECK-AGG-LIST-SAME: i32 6100, i32 {{[0-9]+}}, {{[^@]*}}@[[FILE_ELEM]], {{[^@]*}}@[[FUNC]]
233 //
234 // CHECK-AGG-LIST: %[[I2:.*]] = getelementptr inbounds %struct.TestInitAgg, %struct.TestInitAgg* %local_list_init, i32 0, i32 1
235 // CHECK-AGG-LIST-NEXT: call void @_ZN15source_location7currentEjjPKcS1_(%struct.source_location* sret(%struct.source_location) align 8 %[[I2]],
236 // CHECK-AGG-LIST-SAME: i32 6200, i32 {{[0-9]+}}, {{[^@]*}}@[[FILE_DEFAULT]], {{[^@]*}}@[[FUNC]]
237 #line 6000 "InitListStart.cpp"
238   TestInitAgg local_list_init =
239       {
240 #line 6100 "ListElem.cpp"
241           {SL::current()}
242 #line 6200 "InitListEnd.cpp"
243       };
244 }
245 
246 #line 7000 "TestTemplate.cpp"
247 template <class Tp, int>
248 struct TestTemplate {
249   Tp info = Tp::current();
250   Tp arg_info;
251 #line 7100 "TestTemplateCtor.cpp"
TestTemplateTestTemplate252   constexpr TestTemplate(Tp arg_info = Tp::current()) : arg_info(arg_info) {}
253 };
254 
255 #line 7200 "test_template.cpp"
256 template <class T, int V>
test_template()257 void test_template() {
258 
259 // RUN: FileCheck --input-file %t.ll %s --check-prefix=CHECK-TEMPL -DINT_ID=0
260 // RUN: FileCheck --input-file %t.ll %s --check-prefix=CHECK-TEMPL -DINT_ID=1
261 //
262 // CHECK-TEMPL-DAG: @[[FILE:.*]] = {{.*}}c"local_templ.cpp\00"
263 // CHECK-TEMPL-DAG: @[[FUNC:.*]] = {{.*}}c"test_template\00"
264 //
265 // CHECK-TEMPL: define weak_odr void @_Z13test_templateI15source_locationLi[[INT_ID]]EEvv()
266 // CHECK-TEMPL-NEXT: entry:
267 // CHECK-TEMPL-NOT: ret
268 //
269 // CHECK-TEMPL:  call void @_ZN15source_location7currentEjjPKcS1_(%struct.source_location* sret(%struct.source_location) align 8 %[[TMP:[^,]*]],
270 // CHECK-TEMPL-SAME: i32 7300, i32 {{[0-9]+}}, {{[^@]*}}@[[FILE]], {{[^@]*}}@[[FUNC]]
271 #line 7300 "local_templ.cpp"
272   TestTemplate<T, V> local_templ;
273 }
274 #line 7400 "EndTestTemplate.cpp"
275 template void test_template<SL, 0>();
276 template void test_template<SL, 1>();
277