Skip to content

Commit 0c46d6c

Browse files
committed
Remove STL from source_location.hpp
1 parent 2f38e89 commit 0c46d6c

File tree

1 file changed

+70
-5
lines changed

1 file changed

+70
-5
lines changed

projects/core/include/tempest/source_location.hpp

+70-5
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,61 @@
33

44
#include <tempest/int.hpp>
55

6-
#include <source_location>
6+
// Check for GLIBCXX or LIBCPP
7+
#if ((defined(__GLIBCXX__) && !defined(_GLIBCXX_SRCLOC)) || \
8+
(defined(_LIBCPP_VERSION) && !defined(_LIBCPP_SOURCE_LOCATION))) && \
9+
(defined(__GNUC__) || defined(__clang__))
10+
11+
// Known UB. If libstdc++ or libc++ changes the layout of std::source_location::__impl, this will break
12+
// TODO: Investigate a robust way to test for this. Maybe C++26 static reflection will provide a solution.
13+
namespace std
14+
{
15+
class source_location
16+
{
17+
struct __impl
18+
{
19+
const char* _M_file_name = nullptr;
20+
const char* _M_function_name = nullptr;
21+
unsigned _M_line = 0;
22+
unsigned _M_column = 0;
23+
};
24+
25+
public:
26+
const __impl* _impl;
27+
28+
constexpr source_location() noexcept = default;
29+
30+
constexpr const char* file_name() const noexcept
31+
{
32+
return _impl->_M_file_name;
33+
}
34+
35+
constexpr const char* function_name() const noexcept
36+
{
37+
return _impl->_M_function_name;
38+
}
39+
40+
constexpr size_t line() const noexcept
41+
{
42+
return _impl->_M_line;
43+
}
44+
45+
constexpr size_t column() const noexcept
46+
{
47+
return _impl->_M_column;
48+
}
49+
50+
static consteval source_location current(
51+
decltype(__builtin_source_location()) ptr = __builtin_source_location()) noexcept
52+
{
53+
source_location loc;
54+
loc._impl = static_cast<const __impl*>(ptr);
55+
return loc;
56+
}
57+
};
58+
} // namespace std
59+
60+
#endif
761

862
namespace tempest
963
{
@@ -16,10 +70,8 @@ namespace tempest
1670
const char* file = __builtin_FILE(),
1771
const char* func = __builtin_FUNCSIG()) noexcept;
1872
#elif defined(__GNUC__)
19-
static consteval source_location current(const uint32_t line = __builtin_LINE(),
20-
const uint32_t column = __builtin_COLUMN(),
21-
const char* file = __builtin_FILE(),
22-
const char* func = __builtin_source_location()->_M_function_name) noexcept;
73+
static consteval source_location current(
74+
decltype(__builtin_source_location()) ptr = __builtin_source_location()) noexcept;
2375
#else
2476
#error "Unsupported compiler."
2577
#endif
@@ -44,6 +96,7 @@ namespace tempest
4496
impl _impl;
4597
};
4698

99+
#if defined(_MSC_VER)
47100
inline consteval source_location source_location::current(const uint32_t line, const uint32_t column,
48101
const char* file, const char* func) noexcept
49102
{
@@ -54,6 +107,18 @@ namespace tempest
54107
loc._impl._column = column;
55108
return loc;
56109
}
110+
#else
111+
inline consteval source_location source_location::current(decltype(__builtin_source_location()) ptr) noexcept
112+
{
113+
auto loc = std::source_location::current(ptr);
114+
source_location result;
115+
result._impl._file = loc.file_name();
116+
result._impl._function = loc.function_name();
117+
result._impl._line = loc.line();
118+
result._impl._column = loc.column();
119+
return result;
120+
}
121+
#endif
57122

58123
inline constexpr const char* source_location::file_name() const noexcept
59124
{

0 commit comments

Comments
 (0)