Debian Codegraph @

clang/test/SemaCXX/source_location.cpp

raw
1 // RUN: %clang_cc1 -std=c++1z -fcxx-exceptions -fexceptions -verify %s
2 // RUN: %clang_cc1 -std=c++2a -fcxx-exceptions -DUSE_CONSTEVAL -fexceptions -verify %s
3 // RUN: %clang_cc1 -std=c++2b -fcxx-exceptions -DUSE_CONSTEVAL -DPAREN_INIT -fexceptions -verify %s
4 // RUN: %clang_cc1 -std=c++1z -fcxx-exceptions -fms-extensions -DMS -fexceptions -fms-compatibility -verify %s
5 // RUN: %clang_cc1 -std=c++2a -fcxx-exceptions -fms-extensions -DMS -DUSE_CONSTEVAL -fexceptions -fms-compatibility -verify %s
6 //
7 // RUN: %clang_cc1 -std=c++1z -fcxx-exceptions -fexceptions -fexperimental-new-constant-interpreter -DNEW_INTERP -verify %s
8 // RUN: %clang_cc1 -std=c++2a -fcxx-exceptions -DUSE_CONSTEVAL -fexceptions -fexperimental-new-constant-interpreter -DNEW_INTERP -verify %s
9 // RUN: %clang_cc1 -std=c++2b -fcxx-exceptions -DUSE_CONSTEVAL -DPAREN_INIT -fexceptions -fexperimental-new-constant-interpreter -DNEW_INTERP -verify %s
10 // RUN: %clang_cc1 -std=c++1z -fcxx-exceptions -fms-extensions -DMS -fexceptions -fexperimental-new-constant-interpreter -DNEW_INTERP -fms-compatibility -verify %s
11 // RUN: %clang_cc1 -std=c++2a -fcxx-exceptions -fms-extensions -DMS -DUSE_CONSTEVAL -fexceptions -fexperimental-new-constant-interpreter -DNEW_INTERP -verify -fms-compatibility %s
12 // expected-no-diagnostics
13
14 #define assert(...) ((__VA_ARGS__) ? ((void)0) : throw 42)
15 #define CURRENT_FROM_MACRO() SL::current()
16 #define FORWARD(...) __VA_ARGS__
17
18 template <unsigned>
19 struct Printer;
20
21 #ifdef USE_CONSTEVAL
22 #define SOURCE_LOC_EVAL_KIND consteval
23 #else
24 #define SOURCE_LOC_EVAL_KIND constexpr
25 #endif
26
27 namespace std {
28 class source_location {
29 struct __impl;
30
31 public:
32 static SOURCE_LOC_EVAL_KIND source_location
33 current(const __impl *__p = __builtin_source_location()) noexcept {
34 source_location __loc;
35 __loc.__m_impl = __p;
36 return __loc;
37 }
38 constexpr source_location() = default;
39 constexpr source_location(source_location const &) = default;
40 constexpr unsigned int line() const noexcept { return __m_impl ? __m_impl->_M_line : 0; }
41 constexpr unsigned int column() const noexcept { return __m_impl ? __m_impl->_M_column : 0; }
42 constexpr const char *file() const noexcept { return __m_impl ? __m_impl->_M_file_name : ""; }
43 constexpr const char *function() const noexcept { return __m_impl ? __m_impl->_M_function_name : ""; }
44
45 private:
46 // Note: The type name "std::source_location::__impl", and its constituent
47 // field-names are required by __builtin_source_location().
48 struct __impl {
49 const char *_M_file_name;
50 const char *_M_function_name;
51 unsigned _M_line;
52 unsigned _M_column;
53 };
54 const __impl *__m_impl = nullptr;
55
56 public:
57 using public_impl_alias = __impl;
58 };
59 } // namespace std
60
61 using SL = std::source_location;
62
63 #include "Inputs/source-location-file.h"
64 namespace SLF = source_location_file;
65
66 constexpr bool is_equal(const char *LHS, const char *RHS) {
67 while (*LHS != 0 && *RHS != 0) {
68 if (*LHS != *RHS)
69 return false;
70 ++LHS;
71 ++RHS;
72 }
73 return *LHS == 0 && *RHS == 0;
74 }
75
76 template <class T>
77 constexpr T identity(T t) {
78 return t;
79 }
80
81 template <class T, class U>
82 struct Pair {
83 T first;
84 U second;
85 };
86
87 template <class T, class U>
88 constexpr bool is_same = false;
89 template <class T>
90 constexpr bool is_same<T, T> = true;
91
92 // test types
93 static_assert(is_same<decltype(__builtin_LINE()), unsigned>);
94 static_assert(is_same<decltype(__builtin_COLUMN()), unsigned>);
95 static_assert(is_same<decltype(__builtin_FILE()), const char *>);
96 static_assert(is_same<decltype(__builtin_FILE_NAME()), const char *>);
97 static_assert(is_same<decltype(__builtin_FUNCTION()), const char *>);
98 #ifdef MS
99 static_assert(is_same<decltype(__builtin_FUNCSIG()), const char *>);
100 #endif
101 static_assert(is_same<decltype(__builtin_source_location()), const std::source_location::public_impl_alias *>);
102
103 // test noexcept
104 static_assert(noexcept(__builtin_LINE()));
105 static_assert(noexcept(__builtin_COLUMN()));
106 static_assert(noexcept(__builtin_FILE()));
107 static_assert(noexcept(__builtin_FILE_NAME()));
108 static_assert(noexcept(__builtin_FUNCTION()));
109 #ifdef MS
110 static_assert(noexcept(__builtin_FUNCSIG()));
111 #endif
112 static_assert(noexcept(__builtin_source_location()));
113
114 //===----------------------------------------------------------------------===//
115 // __builtin_LINE()
116 //===----------------------------------------------------------------------===//
117
118 namespace test_line {
119 static_assert(SL::current().line() == __LINE__);
120 static_assert(SL::current().line() == CURRENT_FROM_MACRO().line());
121
122 static constexpr SL GlobalS = SL::current();
123
124 static_assert(GlobalS.line() == __LINE__ - 2);
125
126 // clang-format off
127 constexpr bool test_line_fn() {
128 constexpr SL S = SL::current();
129 static_assert(S.line() == (__LINE__ - 1), "");
130 // The start of the call expression to `current()` begins at the token `SL`
131 constexpr int ExpectLine = __LINE__ + 3;
132 constexpr SL S2
133 =
134 SL // Call expression starts here
135 ::
136 current
137 (
138
139 )
140 ;
141 static_assert(S2.line() == ExpectLine, "");
142
143 static_assert(
144 FORWARD(
145 __builtin_LINE
146 (
147 )
148 )
149 == __LINE__ - 1, "");
150 static_assert(\
151 \
152 __builtin_LINE()\
153 \
154 == __LINE__ - 2, "");
155 static_assert(\
156 _\
157 _builtin_LINE()
158 == __LINE__ - 2, "");
159
160 return true;
161 }
162 // clang-format on
163 static_assert(test_line_fn());
164
165 static_assert(__builtin_LINE() == __LINE__, "");
166
167 constexpr int baz() { return 101; }
168
169 constexpr int test_line_fn_simple(int z = baz(), int x = __builtin_LINE()) {
170 return x;
171 }
172 void bar() {
173 static_assert(test_line_fn_simple() == __LINE__, "");
174 static_assert(test_line_fn_simple() == __LINE__, "");
175 }
176
177 struct CallExpr {
178 constexpr int operator()(int x = __builtin_LINE()) const { return x; }
179 };
180 constexpr CallExpr get_call() { return CallExpr{}; }
181 static_assert(get_call()() == __LINE__, "");
182
183 template <class T>
184 constexpr bool test_line_fn_template(T Expect, int L = __builtin_LINE()) {
185 return Expect == L;
186 }
187 static_assert(test_line_fn_template(__LINE__));
188
189 struct InMemInit {
190 constexpr bool check(int expect) const {
191 return info.line() == expect;
192 }
193 SL info = SL::current();
194 InMemInit() = default;
195 constexpr InMemInit(int) {}
196 };
197 static_assert(InMemInit{}.check(__LINE__ - 3), "");
198 static_assert(InMemInit{42}.check(__LINE__ - 3), "");
199
200 template <class T, class U = SL>
201 struct InMemInitTemplate {
202 constexpr bool check(int expect) const {
203 return info.line() == expect;
204 }
205 U info = U::current();
206 InMemInitTemplate() = default;
207 constexpr InMemInitTemplate(T) {}
208 constexpr InMemInitTemplate(T, T) : info(U::current()) {}
209 template <class V = U> constexpr InMemInitTemplate(T, T, T, V info = U::current())
210 : info(info) {}
211 };
212 void test_mem_init_template() {
213 constexpr int line_offset = 8;
214 static_assert(InMemInitTemplate<int>{}.check(__LINE__ - line_offset), "");
215 static_assert(InMemInitTemplate<unsigned>{42}.check(__LINE__ - line_offset), "");
216 static_assert(InMemInitTemplate<unsigned>{42, 42}.check(__LINE__ - line_offset), "");
217 static_assert(InMemInitTemplate<unsigned>{42, 42, 42}.check(__LINE__), "");
218 }
219
220 struct AggInit {
221 int x;
222 int y = __builtin_LINE();
223 constexpr bool check(int expect) const {
224 return y == expect;
225 }
226 };
227 constexpr AggInit AI{42};
228 static_assert(AI.check(__LINE__ - 1), "");
229
230 template <class T, class U = SL>
231 struct AggInitTemplate {
232 constexpr bool check(int expect) const {
233 return expect == info.line();
234 }
235 T x;
236 U info = U::current();
237 };
238
239 template <class T, class U = SL>
240 constexpr U test_fn_template(T, U u = U::current()) {
241 return u;
242 }
243 void fn_template_tests() {
244 static_assert(test_fn_template(42).line() == __LINE__, "");
245 }
246
247 struct TestMethodTemplate {
248 template <class T, class U = SL, class U2 = SL>
249 constexpr U get(T, U u = U::current(), U2 u2 = identity(U2::current())) const {
250 assert(u.line() == u2.line());
251 return u;
252 }
253 };
254 void method_template_tests() {
255 static_assert(TestMethodTemplate{}.get(42).line() == __LINE__, "");
256 }
257
258 struct InStaticInit {
259 static constexpr int LINE = __LINE__;
260 static constexpr const int x1 = __builtin_LINE();
261 static constexpr const int x2 = identity(__builtin_LINE());
262 static const int x3;
263 const int x4 = __builtin_LINE();
264 int x5 = __builtin_LINE();
265 };
266 const int InStaticInit::x3 = __builtin_LINE();
267 static_assert(InStaticInit::x1 == InStaticInit::LINE + 1, "");
268 static_assert(InStaticInit::x2 == InStaticInit::LINE + 2, "");
269
270 template <class T, int N = __builtin_LINE(), int Expect = -1>
271 constexpr void check_fn_template_param(T) {
272 constexpr int RealExpect = Expect == -1 ? __LINE__ - 2 : Expect;
273 static_assert(N == RealExpect);
274 }
275 template void check_fn_template_param(int);
276 template void check_fn_template_param<long, 42, 42>(long);
277
278 #line 100
279 struct AggBase {
280 #line 200
281 int x = __builtin_LINE();
282 int y = __builtin_LINE();
283 int z = __builtin_LINE();
284 };
285 #line 300
286 struct AggDer : AggBase {
287 };
288 #line 400
289 static_assert(AggDer{}.x == 400, "");
290
291 struct ClassBase {
292 #line 400
293 int x = __builtin_LINE();
294 int y = 0;
295 int z = 0;
296 #line 500
297 ClassBase() = default;
298 constexpr ClassBase(int yy, int zz = __builtin_LINE())
299 : y(yy), z(zz) {}
300 };
301 struct ClassDer : ClassBase {
302 #line 600
303 ClassDer() = default;
304 constexpr ClassDer(int yy) : ClassBase(yy) {}
305 constexpr ClassDer(int yy, int zz) : ClassBase(yy, zz) {}
306 };
307 #line 700
308 static_assert(ClassDer{}.x == 500, "");
309 static_assert(ClassDer{42}.x == 501, "");
310 static_assert(ClassDer{42}.z == 601, "");
311 static_assert(ClassDer{42, 42}.x == 501, "");
312
313 struct ClassAggDer : AggBase {
314 #line 800
315 ClassAggDer() = default;
316 constexpr ClassAggDer(int, int x = __builtin_LINE()) : AggBase{x} {}
317 };
318 static_assert(ClassAggDer{}.x == 100, "");
319
320 } // namespace test_line
321
322 //===----------------------------------------------------------------------===//
323 // __builtin_FILE()
324 //===----------------------------------------------------------------------===//
325
326 namespace test_file {
327 constexpr const char *test_file_simple(const char *__f = __builtin_FILE()) {
328 return __f;
329 }
330 void test_function() {
331 #line 900
332 static_assert(is_equal(test_file_simple(), __FILE__));
333 static_assert(is_equal(SLF::test_function().file(), __FILE__), "");
334 static_assert(is_equal(SLF::test_function_template(42).file(), __FILE__), "");
335
336 static_assert(is_equal(SLF::test_function_indirect().file(), SLF::global_info.file()), "");
337 static_assert(is_equal(SLF::test_function_template_indirect(42).file(), SLF::global_info.file()), "");
338
339 static_assert(test_file_simple() != nullptr);
340 static_assert(!is_equal(test_file_simple(), "source_location.cpp"));
341 }
342
343 void test_class() {
344 #line 315
345 using SLF::TestClass;
346 constexpr TestClass Default;
347 constexpr TestClass InParam{42};
348 constexpr TestClass Template{42, 42};
349 constexpr auto *F = Default.info.file();
350 constexpr auto Char = F[0];
351 static_assert(is_equal(Default.info.file(), SLF::FILE), "");
352 static_assert(is_equal(InParam.info.file(), SLF::FILE), "");
353 static_assert(is_equal(InParam.ctor_info.file(), __FILE__), "");
354 }
355
356 void test_aggr_class() {
357 using Agg = SLF::AggrClass<>;
358 constexpr Agg Default{};
359 constexpr Agg InitOne{42};
360 static_assert(is_equal(Default.init_info.file(), __FILE__), "");
361 static_assert(is_equal(InitOne.init_info.file(), __FILE__), "");
362 }
363
364 } // namespace test_file
365
366 //===----------------------------------------------------------------------===//
367 // __builtin_FILE_NAME()
368 //===----------------------------------------------------------------------===//
369
370 namespace test_file_name {
371 constexpr const char *test_file_name_simple(
372 const char *__f = __builtin_FILE_NAME()) {
373 return __f;
374 }
375 void test_function() {
376 #line 900
377 static_assert(is_equal(test_file_name_simple(), __FILE_NAME__));
378 static_assert(is_equal(SLF::test_function_filename(), __FILE_NAME__), "");
379 static_assert(is_equal(SLF::test_function_filename_template(42),
380 __FILE_NAME__), "");
381
382 static_assert(is_equal(SLF::test_function_filename_indirect(),
383 SLF::global_info_filename), "");
384 static_assert(is_equal(SLF::test_function_filename_template_indirect(42),
385 SLF::global_info_filename), "");
386
387 static_assert(test_file_name_simple() != nullptr);
388 static_assert(is_equal(test_file_name_simple(), "source_location.cpp"));
389 }
390
391 void test_class() {
392 #line 315
393 using SLF::TestClass;
394 constexpr TestClass Default;
395 constexpr TestClass InParam{42};
396 constexpr TestClass Template{42, 42};
397 constexpr auto *F = Default.info_file_name;
398 constexpr auto Char = F[0];
399 static_assert(is_equal(Default.info_file_name, SLF::FILE_NAME), "");
400 static_assert(is_equal(InParam.info_file_name, SLF::FILE_NAME), "");
401 static_assert(is_equal(InParam.ctor_info_file_name, __FILE_NAME__), "");
402 }
403
404 void test_aggr_class() {
405 using Agg = SLF::AggrClass<>;
406 constexpr Agg Default{};
407 constexpr Agg InitOne{42};
408 static_assert(is_equal(Default.init_info_file_name, __FILE_NAME__), "");
409 static_assert(is_equal(InitOne.init_info_file_name, __FILE_NAME__), "");
410 }
411
412 } // namespace test_file_name
413
414 //===----------------------------------------------------------------------===//
415 // __builtin_FUNCTION()
416 //===----------------------------------------------------------------------===//
417
418 namespace test_func {
419
420 constexpr const char *test_func_simple(const char *__f = __builtin_FUNCTION()) {
421 return __f;
422 }
423 constexpr const char *get_function() {
424 return __func__;
425 }
426 constexpr bool test_function() {
427 return is_equal(__func__, test_func_simple()) &&
428 !is_equal(get_function(), test_func_simple());
429 }
430 static_assert(test_function());
431
432 template <class T, class U = SL>
433 constexpr Pair<U, U> test_func_template(T, U u = U::current()) {
434 static_assert(is_equal(__PRETTY_FUNCTION__, U::current().function()));
435 return {u, U::current()};
436 }
437 template <class T>
438 void func_template_tests() {
439 constexpr auto P = test_func_template(42);
440 //static_assert(is_equal(P.first.function(), __func__), "");
441 //static_assert(!is_equal(P.second.function(), __func__), "");
442 }
443 template void func_template_tests<int>();
444
445 template <class = int, class T = SL>
446 struct TestCtor {
447 T info = T::current();
448 T ctor_info;
449 TestCtor() = default;
450 template <class U = SL>
451 constexpr TestCtor(int, U u = U::current()) : ctor_info(u) {}
452 };
453 void ctor_tests() {
454 constexpr TestCtor<> Default;
455 constexpr TestCtor<> Template{42};
456 static const char *XYZZY = Template.info.function();
457 static_assert(is_equal(Default.info.function(), "test_func::TestCtor<>::TestCtor() [T = std::source_location]"));
458 static_assert(is_equal(Default.ctor_info.function(), ""));
459 static_assert(is_equal(Template.info.function(), "test_func::TestCtor<>::TestCtor(int, U) [T = std::source_location, U = std::source_location]"));
460 static_assert(is_equal(Template.ctor_info.function(), __PRETTY_FUNCTION__));
461 }
462
463 constexpr SL global_sl = SL::current();
464 static_assert(is_equal(global_sl.function(), ""));
465
466 template <class T>
467 class TestBI {
468 public:
469 TestBI() {
470 #ifdef MS
471 static_assert(is_equal(__FUNCTION__, "test_func::TestBI<int>::TestBI"));
472 #else
473 static_assert(is_equal(__FUNCTION__, "TestBI"));
474 #endif
475 static_assert(is_equal(__func__, "TestBI"));
476 }
477 };
478
479 template <class T>
480 class TestClass {
481 public:
482 TestClass() {
483 #ifdef MS
484 static_assert(is_equal(__FUNCTION__, "test_func::TestClass<class test_func::C>::TestClass"));
485 #else
486 static_assert(is_equal(__FUNCTION__, "TestClass"));
487 #endif
488 static_assert(is_equal(__func__, "TestClass"));
489 }
490 };
491
492 template <class T>
493 class TestStruct {
494 public:
495 TestStruct() {
496 #ifdef MS
497 static_assert(is_equal(__FUNCTION__, "test_func::TestStruct<struct test_func::S>::TestStruct"));
498 #else
499 static_assert(is_equal(__FUNCTION__, "TestStruct"));
500 #endif
501 static_assert(is_equal(__func__, "TestStruct"));
502 }
503 };
504
505 template <class T>
506 class TestEnum {
507 public:
508 TestEnum() {
509 #ifdef MS
510 static_assert(is_equal(__FUNCTION__, "test_func::TestEnum<enum test_func::E>::TestEnum"));
511 #else
512 static_assert(is_equal(__FUNCTION__, "TestEnum"));
513 #endif
514 static_assert(is_equal(__func__, "TestEnum"));
515 }
516 };
517
518 class C {};
519 struct S {};
520 enum E {};
521
522 TestBI<int> t1;
523 TestClass<test_func::C> t2;
524 TestStruct<test_func::S> t3;
525 TestEnum<test_func::E> t4;
526
527 } // namespace test_func
528
529
530 //===----------------------------------------------------------------------===//
531 // __builtin_FUNCSIG()
532 //===----------------------------------------------------------------------===//
533
534 #ifdef MS
535 namespace test_funcsig {
536
537 constexpr const char *test_funcsig_simple(const char *f = __builtin_FUNCSIG()) {
538 return f;
539 }
540 constexpr const char *get_funcsig() {
541 return __FUNCSIG__;
542 }
543 constexpr bool test_funcsig() {
544 return is_equal(__FUNCSIG__, test_funcsig_simple()) &&
545 !is_equal(get_funcsig(), test_funcsig_simple());
546 }
547 static_assert(test_funcsig());
548
549 template <class T>
550 constexpr Pair<const char*, const char*> test_funcsig_template(T, const char* f = __builtin_FUNCSIG()) {
551 return {f, __builtin_FUNCSIG()};
552 }
553 template <class T>
554 void func_template_tests() {
555 constexpr auto P = test_funcsig_template(42);
556 static_assert(is_equal(P.first, __FUNCSIG__), "");
557 static_assert(!is_equal(P.second, __FUNCSIG__), "");
558 }
559 template void func_template_tests<int>();
560
561 template <class = int, class T = const char*>
562 struct TestCtor {
563 T funcsig = __builtin_FUNCSIG();
564 T ctor_funcsig;
565 TestCtor() = default;
566 template <class F = const char*>
567 constexpr TestCtor(int, F f = __builtin_FUNCSIG()) : ctor_funcsig(f) {}
568 };
569 void ctor_tests() {
570 constexpr TestCtor<> Template{42};
571 static_assert(is_equal(Template.funcsig, "__cdecl test_funcsig::TestCtor<>::TestCtor(int, F) [T = const char *, F = const char *]"));
572 static_assert(is_equal(Template.ctor_funcsig, __FUNCSIG__));
573 }
574
575 constexpr const char* global_funcsig = __builtin_FUNCSIG();
576 static_assert(is_equal(global_funcsig, ""));
577
578 } // namespace test_funcsig
579 #endif
580
581 //===----------------------------------------------------------------------===//
582 // __builtin_COLUMN()
583 //===----------------------------------------------------------------------===//
584
585 namespace test_column {
586
587 // clang-format off
588 constexpr bool test_column_fn() {
589 constexpr SL S = SL::current();
590 static_assert(S.line() == (__LINE__ - 1), "");
591 constexpr int Indent = 4;
592 {
593 // The start of the call expression to `current()` begins at the token `SL`
594 constexpr int ExpectCol = Indent + 3;
595 constexpr SL S2
596 =
597 SL // Call expression starts here
598 ::
599 current
600 (
601
602 )
603 ;
604 static_assert(S2.column() == ExpectCol, "");
605 }
606 {
607 constexpr int ExpectCol = 2;
608 constexpr int C =
609 __builtin_COLUMN // Expect call expression to start here
610 ();
611 static_assert(C == ExpectCol);
612 }
613 return true;
614 }
615 #line 420
616 static_assert(test_column_fn());
617
618 // Test that the column matches the start of the call expression 'SL::current()'
619 static_assert(SL::current().column() == __builtin_strlen("static_assert(S"));
620 struct TestClass {
621 int x = __builtin_COLUMN();
622 TestClass() = default; /* indented to 3 spaces for testing */
623 constexpr TestClass(int, int o = __builtin_COLUMN()) : x(o) {}
624 };
625 struct TestAggClass {
626 int x = __builtin_COLUMN();
627 };
628 constexpr bool test_class() {
629
630 auto check = [](int V, const char* S, int indent = 4) {
631 assert(V == (__builtin_strlen(S) + indent));
632 };
633 {
634 TestClass t{};
635 check(t.x, " T", 0); // Start of default constructor decl.
636 }
637 {
638 TestClass t1
639 {42};
640 check(t1.x, "TestClass t"); // Start of variable being constructed.
641 }
642 {
643 TestAggClass t { };
644 check(t.x, "TestAggClass t { }");
645 }
646 {
647 TestAggClass t = { };
648 check(t.x, "TestAggClass t = { }");
649 }
650 return true;
651 }
652 static_assert(test_class());
653 // clang-format on
654 } // namespace test_column
655
656 // Test [reflection.src_loc.creation]p2
657 // > The value should be affected by #line (C++14 16.4) in the same manner as
658 // > for __LINE__ and __FILE__.
659 namespace test_pragma_line {
660 constexpr int StartLine = 42;
661 #line 42
662 static_assert(__builtin_LINE() == StartLine);
663 static_assert(__builtin_LINE() == StartLine + 1);
664 static_assert(SL::current().line() == StartLine + 2);
665 #line 44 "test_file.c"
666 static_assert(is_equal("test_file.c", __FILE__));
667 static_assert(is_equal("test_file.c", __builtin_FILE()));
668 static_assert(is_equal("test_file.c", __builtin_FILE_NAME()));
669 static_assert(is_equal("test_file.c", SL::current().file()));
670 static_assert(is_equal("test_file.c", SLF::test_function().file()));
671 static_assert(is_equal(SLF::FILE, SLF::test_function_indirect().file()));
672 } // end namespace test_pragma_line
673
674 namespace test_out_of_line_init {
675 #line 4000 "test_out_of_line_init.cpp"
676 constexpr unsigned get_line(unsigned n = __builtin_LINE()) { return n; }
677 constexpr const char *get_file(const char *f = __builtin_FILE()) { return f; }
678 constexpr const char *get_func(const char *f = __builtin_FUNCTION()) { return f; }
679 #line 4100 "A.cpp"
680 struct A {
681 int n = __builtin_LINE();
682 int n2 = get_line();
683 const char *f = __builtin_FILE();
684 const char *f2 = get_file();
685 const char *func = __builtin_FUNCTION();
686 const char *func2 = get_func();
687 SL info = SL::current();
688 };
689 #line 4200 "B.cpp"
690 struct B {
691 A a = {};
692 };
693 #line 4300 "test_passed.cpp"
694 constexpr B b = {};
695 static_assert(b.a.n == 4300, "");
696 static_assert(b.a.n2 == 4300, "");
697 static_assert(b.a.info.line() == 4300, "");
698 static_assert(is_equal(b.a.f, "test_passed.cpp"));
699 static_assert(is_equal(b.a.f2, "test_passed.cpp"));
700 static_assert(is_equal(b.a.info.file(), "test_passed.cpp"));
701 static_assert(is_equal(b.a.func, ""));
702 static_assert(is_equal(b.a.func2, ""));
703 static_assert(is_equal(b.a.info.function(), ""));
704
705 constexpr bool test_in_func() {
706 #line 4400 "test_func_passed.cpp"
707 constexpr B b = {};
708 static_assert(b.a.n == 4400, "");
709 static_assert(b.a.n2 == 4400, "");
710 static_assert(b.a.info.line() == 4400, "");
711 static_assert(is_equal(b.a.f, "test_func_passed.cpp"));
712 static_assert(is_equal(b.a.f2, "test_func_passed.cpp"));
713 static_assert(is_equal(b.a.info.file(), "test_func_passed.cpp"));
714 static_assert(is_equal(b.a.func, "test_in_func"));
715 static_assert(is_equal(b.a.func2, "test_in_func"));
716 static_assert(is_equal(b.a.info.function(), "bool test_out_of_line_init::test_in_func()"));
717 return true;
718 }
719 static_assert(test_in_func());
720
721 } // end namespace test_out_of_line_init
722
723 namespace test_global_scope {
724 #line 5000 "test_global_scope.cpp"
725 constexpr unsigned get_line(unsigned n = __builtin_LINE()) { return n; }
726 constexpr const char *get_file(const char *f = __builtin_FILE()) { return f; }
727 constexpr const char *get_func(const char *f = __builtin_FUNCTION()) { return f; }
728 #line 5100
729 struct InInit {
730 unsigned l = get_line();
731 const char *f = get_file();
732 const char *func = get_func();
733
734 #line 5200 "in_init.cpp"
735 constexpr InInit() {}
736 };
737 #line 5300
738 constexpr InInit II;
739
740 static_assert(II.l == 5200, "");
741 static_assert(is_equal(II.f, "in_init.cpp"));
742 static_assert(is_equal(II.func, "InInit"));
743
744 #line 5400
745 struct AggInit {
746 unsigned l = get_line();
747 const char *f = get_file();
748 const char *func = get_func();
749 };
750 #line 5500 "brace_init.cpp"
751 constexpr AggInit AI = {};
752 static_assert(AI.l == 5500);
753 static_assert(is_equal(AI.f, "brace_init.cpp"));
754 static_assert(is_equal(AI.func, ""));
755
756 } // namespace test_global_scope
757
758 namespace TestFuncInInit {
759 #line 6000 "InitClass.cpp"
760 struct Init {
761 SL info;
762 #line 6100 "InitCtor.cpp"
763 constexpr Init(SL info = SL::current()) : info(info) {}
764 };
765 #line 6200 "InitGlobal.cpp"
766 constexpr Init I;
767 static_assert(I.info.line() == 6200);
768 static_assert(is_equal(I.info.file(), "InitGlobal.cpp"));
769
770 } // namespace TestFuncInInit
771
772 namespace TestConstexprContext {
773 #line 7000 "TestConstexprContext.cpp"
774 constexpr const char* foo() { return __builtin_FILE(); }
775 #line 7100 "Bar.cpp"
776 constexpr const char* bar(const char* x = foo()) { return x; }
777 constexpr bool test() {
778 static_assert(is_equal(bar(), "TestConstexprContext.cpp"));
779 return true;
780 }
781 static_assert(test());
782 }
783
784 namespace Lambda {
785 #line 8000 "TestLambda.cpp"
786 constexpr int nested_lambda(int l = []{
787 return SL::current().line();
788 }()) {
789 return l;
790 }
791 static_assert(nested_lambda() == __LINE__ - 4);
792
793 constexpr int lambda_param(int l = [](int l = SL::current().line()) {
794 return l;
795 }()) {
796 return l;
797 }
798 static_assert(lambda_param() == __LINE__);
799
800
801 }
802
803 constexpr int compound_literal_fun(int a =
804 (int){ SL::current().line() }
805 ) { return a ;}
806 static_assert(compound_literal_fun() == __LINE__);
807
808 struct CompoundLiteral {
809 int a = (int){ SL::current().line() };
810 };
811 static_assert(CompoundLiteral{}.a == __LINE__);
812
813
814 // FIXME
815 // Init captures are subexpressions of the lambda expression
816 // so according to the standard immediate invocations in init captures
817 // should be evaluated at the call site.
818 // However Clang does not yet implement this as it would introduce
819 // a fair bit of complexity.
820 // We intend to implement that functionality once we find real world
821 // use cases that require it.
822 constexpr int test_init_capture(int a =
823 [b = SL::current().line()] { return b; }()) {
824 return a;
825 }
826 #if defined(USE_CONSTEVAL) && !defined(NEW_INTERP)
827 static_assert(test_init_capture() == __LINE__ - 4);
828 #else
829 static_assert(test_init_capture() == __LINE__ );
830 #endif
831
832 namespace check_immediate_invocations_in_templates {
833
834 template <typename T = int>
835 struct G {
836 T line = __builtin_LINE();
837 };
838 template <typename T>
839 struct S {
840 int i = G<T>{}.line;
841 };
842 static_assert(S<int>{}.i != // intentional new line
843 S<int>{}.i);
844
845 template <typename T>
846 constexpr int f(int i = G<T>{}.line) {
847 return i;
848 }
849
850 static_assert(f<int>() != // intentional new line
851 f<int>());
852 }
853
854 #ifdef PAREN_INIT
855 namespace GH63903 {
856 struct S {
857 int _;
858 int i = SL::current().line();
859 int j = __builtin_LINE();
860 };
861 // Ensure parent aggregate initialization is consistent with brace
862 // aggregate initialization.
863 // Note: consteval functions are evaluated where they are used.
864 static_assert(S(0).i == __builtin_LINE());
865 static_assert(S(0).i == S{0}.i);
866 static_assert(S(0).j == S{0}.j);
867 static_assert(S(0).j == S{0}.i);
868 }
869 #endif
870
871 namespace GH78128 {
872
873 template<int N>
874 constexpr int f() {
875 return N;
876 }
877
878 template<typename T>
879 void foo() {
880 constexpr auto* F1 = std::source_location::current().function();
881 static_assert(__builtin_strlen(F1) == f<__builtin_strlen(F1)>());
882
883 constexpr auto* F2 = __builtin_FUNCTION();
884 static_assert(__builtin_strlen(F2) == f<__builtin_strlen(F2)>());
885
886 #ifdef MS
887 constexpr auto* F3 = __builtin_FUNCSIG();
888 static_assert(__builtin_strlen(F3) == f<__builtin_strlen(F3)>());
889 #endif
890 }
891
892 void test() {
893 foo<int>();
894 }
895
896 }
897
898 namespace GH80630 {
899
900 #define GH80630_LAMBDA \
901 []( char const* fn ) { \
902 static constexpr std::source_location loc = std::source_location::current(); \
903 return &loc; \
904 }( std::source_location::current().function() )
905
906 auto f( std::source_location const* loc = GH80630_LAMBDA ) {
907 return loc;
908 }
909
910 auto g() {
911 return f();
912 }
913
914 }
915
916 namespace GH92680 {
917
918 struct IntConstuctible {
919 IntConstuctible(std::source_location = std::source_location::current());
920 };
921
922 template <typename>
923 auto construct_at(IntConstuctible) -> decltype(IntConstuctible()) {
924 return {};
925 }
926
927 void test() {
928 construct_at<IntConstuctible>({});
929 }
930
931 }
932
933 namespace GH106428 {
934
935 struct add_fn {
936 template <typename T>
937 constexpr auto operator()(T lhs, T rhs,
938 const std::source_location loc = std::source_location::current())
939 const -> T
940 {
941 return lhs + rhs;
942 }
943 };
944
945
946 template <class _Fp, class... _Args>
947 decltype(_Fp{}(0, 0))
948 __invoke(_Fp&& __f);
949
950 template<typename T>
951 struct type_identity { using type = T; };
952
953 template<class Fn>
954 struct invoke_result : type_identity<decltype(__invoke(Fn{}))> {};
955
956 using i = invoke_result<add_fn>::type;
957 static_assert(__is_same(i, int));
958
959 }
960
961 #if __cplusplus >= 202002L
962
963 namespace GH81155 {
964 struct buff {
965 buff(buff &, const char * = __builtin_FUNCTION());
966 };
967
968 template <class Ty>
969 Ty declval();
970
971 template <class Fx>
972 auto Call(buff arg) -> decltype(Fx{}(arg));
973
974 template <typename>
975 struct F {};
976
977 template <class Fx>
978 struct InvocableR : F<decltype(Call<Fx>(declval<buff>()))> {
979 static constexpr bool value = false;
980 };
981
982 template <class Fx, bool = InvocableR<Fx>::value>
983 void Help(Fx) {}
984
985 void Test() {
986 Help([](buff) {});
987 }
988
989 }
990
991 #endif