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
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
14 #define assert(...) ((__VA_ARGS__) ? ((void)0) : throw 42)
15 #define CURRENT_FROM_MACRO() SL::current()
16 #define FORWARD(...) __VA_ARGS__
22 #define SOURCE_LOC_EVAL_KIND consteval
24 #define SOURCE_LOC_EVAL_KIND constexpr
28 class source_location
{
32 static SOURCE_LOC_EVAL_KIND source_location
33 current(const __impl
*__p
= __builtin_source_location()) noexcept
{
34 source_location __loc
;
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
: ""; }
46 // Note: The type name "std::source_location::__impl", and its constituent
47 // field-names are required by __builtin_source_location().
49 const char *_M_file_name
;
50 const char *_M_function_name
;
54 const __impl
*__m_impl
= nullptr;
57 using public_impl_alias
= __impl
;
61 using SL
= std::source_location
;
63 #include "Inputs/source-location-file.h"
64 namespace SLF
= source_location_file
;
66 constexpr bool is_equal(const char *LHS
, const char *RHS
) {
67 while (*LHS
!= 0 && *RHS
!= 0) {
73 return *LHS
== 0 && *RHS
== 0;
77 constexpr T
identity(T t
) {
81 template <class T
, class U
>
87 template <class T
, class U
>
88 constexpr bool is_same
= false;
90 constexpr bool is_same
<T
, T
> = true;
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 *>);
99 static_assert(is_same
<decltype(__builtin_FUNCSIG()), const char *>);
101 static_assert(is_same
<decltype(__builtin_source_location()), const std::source_location::public_impl_alias
*>);
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()));
110 static_assert(noexcept(__builtin_FUNCSIG()));
112 static_assert(noexcept(__builtin_source_location()));
114 //===----------------------------------------------------------------------===//
116 //===----------------------------------------------------------------------===//
118 namespace test_line
{
119 static_assert(SL::current().line() == __LINE__
);
120 static_assert(SL::current().line() == CURRENT_FROM_MACRO().line());
122 static constexpr SL GlobalS
= SL::current();
124 static_assert(GlobalS
.line() == __LINE__
- 2);
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;
134 SL
// Call expression starts here
141 static_assert(S2
.line() == ExpectLine
, "");
149 == __LINE__
- 1, "");
154 == __LINE__
- 2, "");
158 == __LINE__
- 2, "");
163 static_assert(test_line_fn());
165 static_assert(__builtin_LINE() == __LINE__
, "");
167 constexpr int baz() { return 101; }
169 constexpr int test_line_fn_simple(int z
= baz(), int x
= __builtin_LINE()) {
173 static_assert(test_line_fn_simple() == __LINE__
, "");
174 static_assert(test_line_fn_simple() == __LINE__
, "");
178 constexpr int operator()(int x
= __builtin_LINE()) const { return x
; }
180 constexpr CallExpr
get_call() { return CallExpr
{}; }
181 static_assert(get_call()() == __LINE__
, "");
184 constexpr bool test_line_fn_template(T Expect
, int L
= __builtin_LINE()) {
187 static_assert(test_line_fn_template(__LINE__
));
190 constexpr bool check(int expect
) const {
191 return info
.line() == expect
;
193 SL info
= SL::current();
194 InMemInit() = default;
195 constexpr InMemInit(int) {}
197 static_assert(InMemInit
{}.check(__LINE__
- 3), "");
198 static_assert(InMemInit
{42}.check(__LINE__
- 3), "");
200 template <class T
, class U
= SL
>
201 struct InMemInitTemplate
{
202 constexpr bool check(int expect
) const {
203 return info
.line() == expect
;
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())
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__
), "");
222 int y
= __builtin_LINE();
223 constexpr bool check(int expect
) const {
227 constexpr AggInit AI
{42};
228 static_assert(AI
.check(__LINE__
- 1), "");
230 template <class T
, class U
= SL
>
231 struct AggInitTemplate
{
232 constexpr bool check(int expect
) const {
233 return expect
== info
.line();
236 U info
= U::current();
239 template <class T
, class U
= SL
>
240 constexpr U
test_fn_template(T
, U u
= U::current()) {
243 void fn_template_tests() {
244 static_assert(test_fn_template(42).line() == __LINE__
, "");
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());
254 void method_template_tests() {
255 static_assert(TestMethodTemplate
{}.get(42).line() == __LINE__
, "");
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());
263 const int x4
= __builtin_LINE();
264 int x5
= __builtin_LINE();
266 const int InStaticInit::x3
= __builtin_LINE();
267 static_assert(InStaticInit::x1
== InStaticInit::LINE
+ 1, "");
268 static_assert(InStaticInit::x2
== InStaticInit::LINE
+ 2, "");
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
);
275 template void check_fn_template_param(int);
276 template void check_fn_template_param
<long, 42, 42>(long);
281 int x
= __builtin_LINE();
282 int y
= __builtin_LINE();
283 int z
= __builtin_LINE();
286 struct AggDer
: AggBase
{
289 static_assert(AggDer
{}.x
== 400, "");
293 int x
= __builtin_LINE();
297 ClassBase() = default;
298 constexpr ClassBase(int yy
, int zz
= __builtin_LINE())
301 struct ClassDer
: ClassBase
{
303 ClassDer() = default;
304 constexpr ClassDer(int yy
) : ClassBase(yy
) {}
305 constexpr ClassDer(int yy
, int zz
) : ClassBase(yy
, zz
) {}
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, "");
313 struct ClassAggDer
: AggBase
{
315 ClassAggDer() = default;
316 constexpr ClassAggDer(int, int x
= __builtin_LINE()) : AggBase
{x
} {}
318 static_assert(ClassAggDer
{}.x
== 100, "");
320 } // namespace test_line
322 //===----------------------------------------------------------------------===//
324 //===----------------------------------------------------------------------===//
326 namespace test_file
{
327 constexpr const char *test_file_simple(const char *__f
= __builtin_FILE()) {
330 void test_function() {
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__
), "");
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()), "");
339 static_assert(test_file_simple() != nullptr);
340 static_assert(!is_equal(test_file_simple(), "source_location.cpp"));
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__
), "");
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__
), "");
364 } // namespace test_file
366 //===----------------------------------------------------------------------===//
367 // __builtin_FILE_NAME()
368 //===----------------------------------------------------------------------===//
370 namespace test_file_name
{
371 constexpr const char *test_file_name_simple(
372 const char *__f
= __builtin_FILE_NAME()) {
375 void test_function() {
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),
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
), "");
387 static_assert(test_file_name_simple() != nullptr);
388 static_assert(is_equal(test_file_name_simple(), "source_location.cpp"));
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__
), "");
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__
), "");
412 } // namespace test_file_name
414 //===----------------------------------------------------------------------===//
415 // __builtin_FUNCTION()
416 //===----------------------------------------------------------------------===//
418 namespace test_func
{
420 constexpr const char *test_func_simple(const char *__f
= __builtin_FUNCTION()) {
423 constexpr const char *get_function() {
426 constexpr bool test_function() {
427 return is_equal(__func__
, test_func_simple()) &&
428 !is_equal(get_function(), test_func_simple());
430 static_assert(test_function());
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()};
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__), "");
443 template void func_template_tests
<int>();
445 template <class = int, class T
= SL
>
447 T info
= T::current();
449 TestCtor() = default;
450 template <class U
= SL
>
451 constexpr TestCtor(int, U u
= U::current()) : ctor_info(u
) {}
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__
));
463 constexpr SL global_sl
= SL::current();
464 static_assert(is_equal(global_sl
.function(), ""));
471 static_assert(is_equal(__FUNCTION__
, "test_func::TestBI<int>::TestBI"));
473 static_assert(is_equal(__FUNCTION__
, "TestBI"));
475 static_assert(is_equal(__func__
, "TestBI"));
484 static_assert(is_equal(__FUNCTION__
, "test_func::TestClass<class test_func::C>::TestClass"));
486 static_assert(is_equal(__FUNCTION__
, "TestClass"));
488 static_assert(is_equal(__func__
, "TestClass"));
497 static_assert(is_equal(__FUNCTION__
, "test_func::TestStruct<struct test_func::S>::TestStruct"));
499 static_assert(is_equal(__FUNCTION__
, "TestStruct"));
501 static_assert(is_equal(__func__
, "TestStruct"));
510 static_assert(is_equal(__FUNCTION__
, "test_func::TestEnum<enum test_func::E>::TestEnum"));
512 static_assert(is_equal(__FUNCTION__
, "TestEnum"));
514 static_assert(is_equal(__func__
, "TestEnum"));
523 TestClass
<test_func::C
> t2
;
524 TestStruct
<test_func::S
> t3
;
525 TestEnum
<test_func::E
> t4
;
527 } // namespace test_func
530 //===----------------------------------------------------------------------===//
531 // __builtin_FUNCSIG()
532 //===----------------------------------------------------------------------===//
535 namespace test_funcsig
{
537 constexpr const char *test_funcsig_simple(const char *f
= __builtin_FUNCSIG()) {
540 constexpr const char *get_funcsig() {
543 constexpr bool test_funcsig() {
544 return is_equal(__FUNCSIG__
, test_funcsig_simple()) &&
545 !is_equal(get_funcsig(), test_funcsig_simple());
547 static_assert(test_funcsig());
550 constexpr Pair
<const char*, const char*> test_funcsig_template(T
, const char* f
= __builtin_FUNCSIG()) {
551 return {f
, __builtin_FUNCSIG()};
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__
), "");
559 template void func_template_tests
<int>();
561 template <class = int, class T
= const char*>
563 T funcsig
= __builtin_FUNCSIG();
565 TestCtor() = default;
566 template <class F
= const char*>
567 constexpr TestCtor(int, F f
= __builtin_FUNCSIG()) : ctor_funcsig(f
) {}
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__
));
575 constexpr const char* global_funcsig
= __builtin_FUNCSIG();
576 static_assert(is_equal(global_funcsig
, ""));
578 } // namespace test_funcsig
581 //===----------------------------------------------------------------------===//
582 // __builtin_COLUMN()
583 //===----------------------------------------------------------------------===//
585 namespace test_column
{
588 constexpr bool test_column_fn() {
589 constexpr SL S
= SL::current();
590 static_assert(S
.line() == (__LINE__
- 1), "");
591 constexpr int Indent
= 4;
593 // The start of the call expression to `current()` begins at the token `SL`
594 constexpr int ExpectCol
= Indent
+ 3;
597 SL
// Call expression starts here
604 static_assert(S2
.column() == ExpectCol
, "");
607 constexpr int ExpectCol
= 2;
609 __builtin_COLUMN
// Expect call expression to start here
611 static_assert(C
== ExpectCol
);
616 static_assert(test_column_fn());
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"));
621 int x
= __builtin_COLUMN();
622 TestClass() = default; /* indented to 3 spaces for testing */
623 constexpr TestClass(int, int o
= __builtin_COLUMN()) : x(o
) {}
625 struct TestAggClass
{
626 int x
= __builtin_COLUMN();
628 constexpr bool test_class() {
630 auto check
= [](int V
, const char* S
, int indent
= 4) {
631 assert(V
== (__builtin_strlen(S
) + indent
));
635 check(t
.x
, " T", 0); // Start of default constructor decl.
640 check(t1
.x
, "TestClass t"); // Start of variable being constructed.
644 check(t
.x
, "TestAggClass t { }");
647 TestAggClass t
= { };
648 check(t
.x
, "TestAggClass t = { }");
652 static_assert(test_class());
654 } // namespace test_column
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;
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
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
; }
681 int n
= __builtin_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();
693 #line 4300 "test_passed.cpp"
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(), ""));
705 constexpr bool test_in_func() {
706 #line 4400 "test_func_passed.cpp"
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()"));
719 static_assert(test_in_func());
721 } // end namespace test_out_of_line_init
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
; }
730 unsigned l
= get_line();
731 const char *f
= get_file();
732 const char *func
= get_func();
734 #line 5200 "in_init.cpp"
735 constexpr InInit() {}
740 static_assert(II
.l
== 5200, "");
741 static_assert(is_equal(II
.f
, "in_init.cpp"));
742 static_assert(is_equal(II
.func
, "InInit"));
746 unsigned l
= get_line();
747 const char *f
= get_file();
748 const char *func
= get_func();
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
, ""));
756 } // namespace test_global_scope
758 namespace TestFuncInInit
{
759 #line 6000 "InitClass.cpp"
762 #line 6100 "InitCtor.cpp"
763 constexpr Init(SL info
= SL::current()) : info(info
) {}
765 #line 6200 "InitGlobal.cpp"
767 static_assert(I
.info
.line() == 6200);
768 static_assert(is_equal(I
.info
.file(), "InitGlobal.cpp"));
770 } // namespace TestFuncInInit
772 namespace TestConstexprContext
{
773 #line 7000 "TestConstexprContext.cpp"
774 constexpr const char* foo() { return __builtin_FILE(); }
776 constexpr const char* bar(const char* x
= foo()) { return x
; }
777 constexpr bool test() {
778 static_assert(is_equal(bar(), "TestConstexprContext.cpp"));
781 static_assert(test());
785 #line 8000 "TestLambda.cpp"
786 constexpr int nested_lambda(int l
= []{
787 return SL::current().line();
791 static_assert(nested_lambda() == __LINE__
- 4);
793 constexpr int lambda_param(int l
= [](int l
= SL::current().line()) {
798 static_assert(lambda_param() == __LINE__
);
803 constexpr int compound_literal_fun(int a
=
804 (int){ SL::current().line() }
806 static_assert(compound_literal_fun() == __LINE__
);
808 struct CompoundLiteral
{
809 int a
= (int){ SL::current().line() };
811 static_assert(CompoundLiteral
{}.a
== __LINE__
);
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
; }()) {
826 #if defined(USE_CONSTEVAL) && !defined(NEW_INTERP)
827 static_assert(test_init_capture() == __LINE__
- 4);
829 static_assert(test_init_capture() == __LINE__
);
832 namespace check_immediate_invocations_in_templates
{
834 template <typename T
= int>
836 T line
= __builtin_LINE();
838 template <typename T
>
842 static_assert(S
<int>{}.i
!= // intentional new line
845 template <typename T
>
846 constexpr int f(int i
= G
<T
>{}.line
) {
850 static_assert(f
<int>() != // intentional new line
858 int i
= SL::current().line();
859 int j
= __builtin_LINE();
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
);
880 constexpr auto* F1
= std::source_location::current().function();
881 static_assert(__builtin_strlen(F1
) == f
<__builtin_strlen(F1
)>());
883 constexpr auto* F2
= __builtin_FUNCTION();
884 static_assert(__builtin_strlen(F2
) == f
<__builtin_strlen(F2
)>());
887 constexpr auto* F3
= __builtin_FUNCSIG();
888 static_assert(__builtin_strlen(F3
) == f
<__builtin_strlen(F3
)>());
900 #define GH80630_LAMBDA \
901 []( char const* fn ) { \
902 static constexpr std::source_location loc = std::source_location::current(); \
904 }( std::source_location::current().function() )
906 auto f( std::source_location
const* loc
= GH80630_LAMBDA
) {
918 struct IntConstuctible
{
919 IntConstuctible(std::source_location
= std::source_location::current());
923 auto construct_at(IntConstuctible
) -> decltype(IntConstuctible()) {
928 construct_at
<IntConstuctible
>({});
936 template <typename T
>
937 constexpr auto operator()(T lhs
, T rhs
,
938 const std::source_location loc
= std::source_location::current())
946 template <class _Fp
, class... _Args
>
947 decltype(_Fp
{}(0, 0))
951 struct type_identity
{ using type
= T
; };
954 struct invoke_result
: type_identity
<decltype(__invoke(Fn
{}))> {};
956 using i
= invoke_result
<add_fn
>::type
;
957 static_assert(__is_same(i
, int));
961 #if __cplusplus >= 202002L
965 buff(buff
&, const char * = __builtin_FUNCTION());
972 auto Call(buff arg
) -> decltype(Fx
{}(arg
));
978 struct InvocableR
: F
<decltype(Call
<Fx
>(declval
<buff
>()))> {
979 static constexpr bool value
= false;
982 template <class Fx
, bool = InvocableR
<Fx
>::value
>
995 template <int loc
= std::source_location::current().line()>
996 constexpr auto f(std::source_location loc2
= std::source_location::current()) { return loc
; }
998 int g
= []() -> decltype(f()) { return 0; }();
1001 #if __cplusplus >= 202002L
1002 return []<decltype(f()) = 0>() -> decltype(f()) { return 0; }();
1004 return []() -> decltype(f()) { return 0; }();
1007 #if __cplusplus >= 202002L
1008 template<typename T
>
1009 int Var
= requires
{ []() -> decltype(f()){}; };