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