Skip to content

Commit

Permalink
Update source_location to use __builtin_FUNCSIG (#3206)
Browse files Browse the repository at this point in the history
Co-authored-by: Igor Zhukov <[email protected]>
Co-authored-by: nicole mazzuca <[email protected]>
Co-authored-by: Stephan T. Lavavej <[email protected]>
  • Loading branch information
4 people committed Dec 15, 2022
1 parent faaf094 commit 8f5de32
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 11 deletions.
7 changes: 6 additions & 1 deletion stl/inc/source_location
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,12 @@ _STD_BEGIN
_EXPORT_STD struct source_location {
_NODISCARD static consteval source_location current(const uint_least32_t _Line_ = __builtin_LINE(),
const uint_least32_t _Column_ = __builtin_COLUMN(), const char* const _File_ = __builtin_FILE(),
const char* const _Function_ = __builtin_FUNCTION()) noexcept {
#if defined(__clang__) || defined(__EDG__) // TRANSITION, DevCom-10199227 and LLVM-58951
const char* const _Function_ = __builtin_FUNCTION()
#else // ^^^ workaround / no workaround vvv
const char* const _Function_ = __builtin_FUNCSIG()
#endif // TRANSITION, DevCom-10199227 and LLVM-58951
) noexcept {
source_location _Result{};
_Result._Line = _Line_;
_Result._Column = _Column_;
Expand Down
4 changes: 4 additions & 0 deletions tests/std/include/test_header_units_and_modules.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -652,7 +652,11 @@ constexpr bool impl_test_source_location() {
const auto sl = source_location::current();
assert(sl.line() == __LINE__ - 1);
assert(sl.column() == 1);
#if defined(__clang__) || defined(__EDG__) // TRANSITION, DevCom-10199227 and LLVM-58951
assert(sl.function_name() == "impl_test_source_location"sv);
#else // ^^^ workaround / no workaround vvv
assert(sl.function_name() == "bool __cdecl impl_test_source_location(void)"sv);
#endif // TRANSITION, DevCom-10199227 and LLVM-58951
assert(string_view{sl.file_name()}.ends_with("test_header_units_and_modules.hpp"sv));
return true;
}
Expand Down
4 changes: 4 additions & 0 deletions tests/std/tests/P1208R6_source_location/header.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ constexpr void header_test() {
const auto x = source_location::current();
assert(x.line() == __LINE__ - 1);
assert(x.column() == 37);
#if defined(__clang__) || defined(__EDG__) // TRANSITION, DevCom-10199227 and LLVM-58951
assert(x.function_name() == "header_test"sv);
#else // ^^^ workaround / no workaround vvv
assert(x.function_name() == "void __cdecl header_test(void)"sv);
#endif // TRANSITION, DevCom-10199227 and LLVM-58951
assert(string_view{x.file_name()}.ends_with("header.h"sv));
}
73 changes: 63 additions & 10 deletions tests/std/tests/P1208R6_source_location/test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,15 +50,23 @@ constexpr void local_test() {
const auto x = source_location::current();
assert(x.line() == __LINE__ - 1);
assert(x.column() == 37);
#if defined(__clang__) || defined(__EDG__) // TRANSITION, DevCom-10199227 and LLVM-58951
assert(x.function_name() == "local_test"sv);
#else // ^^^ workaround / no workaround vvv
assert(x.function_name() == "void __cdecl local_test(void)"sv);
#endif // TRANSITION, DevCom-10199227 and LLVM-58951
assert(string_view{x.file_name()}.ends_with(test_cpp));
}

constexpr void argument_test(
const unsigned int line, const unsigned int column, const source_location x = source_location::current()) {
assert(x.line() == line);
assert(x.column() == column);
#if defined(__clang__) || defined(__EDG__) // TRANSITION, DevCom-10199227 and LLVM-58951
assert(x.function_name() == "test"sv);
#else // ^^^ workaround / no workaround vvv
assert(x.function_name() == "bool __cdecl test(void)"sv);
#endif // TRANSITION, DevCom-10199227 and LLVM-58951
assert(string_view{x.file_name()}.ends_with(test_cpp));
}

Expand All @@ -67,9 +75,13 @@ constexpr void sloc_constructor_test() {
assert(x.loc.line() == __LINE__ - 1);
assert(x.loc.column() == 13);
if (is_constant_evaluated()) {
assert(x.loc.function_name() == "main"sv); // TRANSITION, VSO-1285783
assert(x.loc.function_name() == "int __cdecl main(void)"sv); // TRANSITION, VSO-1285783
} else {
#if defined(__clang__) || defined(__EDG__) // TRANSITION, DevCom-10199227 and LLVM-58951
assert(x.loc.function_name() == "sloc_constructor_test"sv);
#else // ^^^ workaround / no workaround vvv
assert(x.loc.function_name() == "void __cdecl sloc_constructor_test(void)"sv);
#endif // TRANSITION, DevCom-10199227 and LLVM-58951
}
assert(string_view{x.loc.file_name()}.ends_with(test_cpp));
}
Expand All @@ -78,7 +90,13 @@ constexpr void different_constructor_test() {
const s x{1};
assert(x.loc.line() == s_int_line);
assert(x.loc.column() == 5);
#if defined(__clang__) || defined(__EDG__) // TRANSITION, DevCom-10199227 and LLVM-58951
assert(x.loc.function_name() == "s"sv);
#elif defined(_M_IX86) // ^^^ workaround / no workaround vvv
assert(x.loc.function_name() == "__thiscall s::s(int)"sv);
#else // ^^^ _M_IX86 / !_M_IX86 vvv
assert(x.loc.function_name() == "__cdecl s::s(int)"sv);
#endif // TRANSITION, DevCom-10199227 and LLVM-58951
assert(string_view{x.loc.file_name()}.ends_with(test_cpp));
}

Expand All @@ -87,28 +105,55 @@ constexpr void sub_member_test() {
assert(s.x.loc.line() == __LINE__ - 1);
assert(s.x.loc.column() == 14);
if (is_constant_evaluated()) {
assert(s.x.loc.function_name() == "main"sv); // TRANSITION, VSO-1285783
assert(s.x.loc.function_name() == "int __cdecl main(void)"sv); // TRANSITION, VSO-1285783
} else {
#if defined(__clang__) || defined(__EDG__) // TRANSITION, DevCom-10199227 and LLVM-58951
assert(s.x.loc.function_name() == "sub_member_test"sv);
#else // ^^^ workaround / no workaround vvv
assert(s.x.loc.function_name() == "void __cdecl sub_member_test(void)"sv);
#endif // TRANSITION, DevCom-10199227 and LLVM-58951
}
assert(string_view{s.x.loc.file_name()}.ends_with(test_cpp));

const s2 s_i{1};
assert(s_i.x.loc.line() == s2_int_line);
assert(s_i.x.loc.column() == 5);
#if defined(__clang__) || defined(__EDG__) // TRANSITION, DevCom-10199227 and LLVM-58951
assert(s_i.x.loc.function_name() == "s2"sv);
#elif defined(_M_IX86) // ^^^ workaround / no workaround vvv
assert(s_i.x.loc.function_name() == "__thiscall s2::s2(int)"sv);
#else // ^^^ _M_IX86 / !_M_IX86 vvv
assert(s_i.x.loc.function_name() == "__cdecl s2::s2(int)"sv);
#endif // TRANSITION, DevCom-10199227 and LLVM-58951
assert(string_view{s_i.x.loc.file_name()}.ends_with(test_cpp));
}

constexpr void lambda_test() {
const auto l = [loc = source_location::current()] { return loc; };
const auto x = l();
assert(x.line() == __LINE__ - 2);
const auto l1 = [loc = source_location::current()] { return loc; };
const auto l2 = [] { return source_location::current(); };
const auto x1 = l1();
const auto x2 = l2();
assert(x1.line() == __LINE__ - 4);
assert(x2.line() == __LINE__ - 4);
#ifndef _M_CEE // TRANSITION, VSO-1665663
assert(x.column() == 51);
#endif // _M_CEE
assert(x.function_name() == "lambda_test"sv);
assert(string_view{x.file_name()}.ends_with(test_cpp));
assert(x1.column() == 52);
#endif // !_M_CEE
assert(x2.column() == 50);
#if defined(__clang__) || defined(__EDG__) // TRANSITION, DevCom-10199227 and LLVM-58951
assert(x1.function_name() == "lambda_test"sv);
assert(x2.function_name() == "operator()"sv);
#elif defined(_M_IX86) // ^^^ workaround / no workaround vvv
assert(x1.function_name() == "void __cdecl lambda_test(void)"sv);
assert(
string_view{x2.function_name()}.starts_with("struct std::source_location __thiscall lambda_test::<lambda_"sv));
assert(string_view{x2.function_name()}.ends_with("::operator ()(void) const"sv));
#else // ^^^ _M_IX86 / !_M_IX86 vvv
assert(x1.function_name() == "void __cdecl lambda_test(void)"sv);
assert(string_view{x2.function_name()}.starts_with("struct std::source_location __cdecl lambda_test::<lambda_"sv));
assert(string_view{x2.function_name()}.ends_with("::operator ()(void) const"sv));
#endif // TRANSITION, DevCom-10199227 and LLVM-58951
assert(string_view{x1.file_name()}.ends_with(test_cpp));
assert(string_view{x2.file_name()}.ends_with(test_cpp));
}

template <class T>
Expand All @@ -120,13 +165,21 @@ constexpr void function_template_test() {
const auto x1 = function_template<void>();
assert(x1.line() == __LINE__ - 5);
assert(x1.column() == 29);
#if defined(__clang__) || defined(__EDG__) // TRANSITION, DevCom-10199227 and LLVM-58951
assert(x1.function_name() == "function_template"sv);
#else // ^^^ workaround / no workaround vvv
assert(x1.function_name() == "struct std::source_location __cdecl function_template<void>(void)"sv);
#endif // TRANSITION, DevCom-10199227 and LLVM-58951
assert(string_view{x1.file_name()}.ends_with(test_cpp));

const auto x2 = function_template<int>();
assert(x1.line() == x2.line());
assert(x1.column() == x2.column());
assert(string_view{x1.function_name()} == string_view{x2.function_name()});
#if defined(__clang__) || defined(__EDG__) // TRANSITION, DevCom-10199227 and LLVM-58951
assert(x2.function_name() == "function_template"sv);
#else // ^^^ workaround / no workaround vvv
assert(x2.function_name() == "struct std::source_location __cdecl function_template<int>(void)"sv);
#endif // TRANSITION, DevCom-10199227 and LLVM-58951
assert(string_view{x1.file_name()} == string_view{x2.file_name()});
}

Expand Down

0 comments on commit 8f5de32

Please sign in to comment.