3
3
4
4
#include < tempest/int.hpp>
5
5
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
7
61
8
62
namespace tempest
9
63
{
@@ -16,10 +70,8 @@ namespace tempest
16
70
const char* file = __builtin_FILE(),
17
71
const char* func = __builtin_FUNCSIG()) noexcept ;
18
72
#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 ;
23
75
#else
24
76
#error "Unsupported compiler."
25
77
#endif
@@ -44,6 +96,7 @@ namespace tempest
44
96
impl _impl;
45
97
};
46
98
99
+ #if defined(_MSC_VER)
47
100
inline consteval source_location source_location::current (const uint32_t line, const uint32_t column,
48
101
const char * file, const char * func) noexcept
49
102
{
@@ -54,6 +107,18 @@ namespace tempest
54
107
loc._impl ._column = column;
55
108
return loc;
56
109
}
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
57
122
58
123
inline constexpr const char * source_location::file_name () const noexcept
59
124
{
0 commit comments