Debian Codegraph @

src/mpt/base/source_location.hpp

raw
1 /* SPDX-License-Identifier: BSL-1.0 OR BSD-3-Clause */
2
3 #ifndef MPT_BASE_SOURCE_LOCATION_HPP
4 #define MPT_BASE_SOURCE_LOCATION_HPP
5
6
7
8 #include "mpt/base/detect.hpp"
9 #include "mpt/base/integer.hpp"
10 #include "mpt/base/namespace.hpp"
11
12 #if MPT_CXX_AT_LEAST(20) && !MPT_MSVC_BEFORE(2022, 0) && !MPT_COMPILER_CLANG
13 #include <source_location>
14 #endif // C++20
15
16
17
18 namespace mpt {
19 inline namespace MPT_INLINE_NS {
20
21
22 #if MPT_CXX_AT_LEAST(20) && !MPT_MSVC_BEFORE(2022, 0) && !MPT_COMPILER_CLANG
23
24 using std::source_location;
25
26 #define MPT_SOURCE_LOCATION_CURRENT() std::source_location::current()
27
28 #else // !C++20
29
30 #if MPT_COMPILER_MSVC && MPT_MSVC_AT_LEAST(2019, 6)
31
32 #define MPT_SOURCE_LOCATION_FILE __builtin_FILE()
33 #define MPT_SOURCE_LOCATION_FUNCTION __builtin_FUNCTION()
34 #define MPT_SOURCE_LOCATION_LINE __builtin_LINE()
35 #define MPT_SOURCE_LOCATION_COLUMN __builtin_COLUMN()
36
37 #elif MPT_COMPILER_GCC
38
39 #define MPT_SOURCE_LOCATION_FILE __builtin_FILE()
40 #define MPT_SOURCE_LOCATION_FUNCTION __builtin_FUNCTION()
41 #define MPT_SOURCE_LOCATION_LINE __builtin_LINE()
42 #define MPT_SOURCE_LOCATION_COLUMN 0
43
44 #elif MPT_COMPILER_CLANG && ((!MPT_OS_MACOSX_OR_IOS && MPT_CLANG_AT_LEAST(9, 0, 0)) || (MPT_OS_MACOSX_OR_IOS && MPT_CLANG_AT_LEAST(12, 0, 0)))
45
46 // We do not know which Apple Clang version introduced __builtin_FILE().
47 // It fails with 10.x (see <https://github.com/fink/fink-distributions/issues/1202>),
48 // and IRC dicussion decided on 12.x as a somewhat safe choice.
49
50 #define MPT_SOURCE_LOCATION_FILE __builtin_FILE()
51 #define MPT_SOURCE_LOCATION_FUNCTION __builtin_FUNCTION()
52 #define MPT_SOURCE_LOCATION_LINE __builtin_LINE()
53 #define MPT_SOURCE_LOCATION_COLUMN __builtin_COLUMN()
54
55 #else
56
57 #define MPT_SOURCE_LOCATION_FILE __FILE__
58 #define MPT_SOURCE_LOCATION_FUNCTION ""
59 #define MPT_SOURCE_LOCATION_LINE __LINE__
60 #define MPT_SOURCE_LOCATION_COLUMN 0
61
62 #endif
63
64 // compatible with C++20 std::source_location
65 struct source_location {
66 private:
67 const char * m_file_name;
68 const char * m_function_name;
69 uint32 m_line;
70 uint32 m_column;
71
72 public:
73 constexpr source_location() noexcept
74 : m_file_name("")
75 , m_function_name("")
76 , m_line(0)
77 , m_column(0) {
78 }
79 constexpr source_location(const char * file, const char * function, uint32 line, uint32 column) noexcept
80 : m_file_name(file)
81 , m_function_name(function)
82 , m_line(line)
83 , m_column(column) {
84 }
85 source_location(const source_location &) = default;
86 source_location(source_location &&) = default;
87 static constexpr source_location current(const char * file = MPT_SOURCE_LOCATION_FILE, const char * function = MPT_SOURCE_LOCATION_FUNCTION, uint32 line = MPT_SOURCE_LOCATION_LINE, uint32 column = MPT_SOURCE_LOCATION_COLUMN) noexcept {
88 return source_location(file, function, line, column);
89 }
90 constexpr uint32 line() const noexcept {
91 return m_line;
92 }
93 constexpr uint32 column() const noexcept {
94 return m_column;
95 }
96 constexpr const char * file_name() const noexcept {
97 return m_file_name;
98 }
99 constexpr const char * function_name() const noexcept {
100 return m_function_name;
101 }
102 };
103
104
105 #if (MPT_COMPILER_MSVC && MPT_MSVC_AT_LEAST(2019, 6)) || MPT_COMPILER_GCC || (MPT_COMPILER_CLANG && MPT_CLANG_AT_LEAST(9, 0, 0))
106 #define MPT_SOURCE_LOCATION_CURRENT() mpt::source_location::current()
107 #else
108 #define MPT_SOURCE_LOCATION_CURRENT() mpt::source_location::current(__FILE__, __func__, __LINE__, 0)
109 #endif
110
111 #endif // C++20
112
113
114 } // namespace MPT_INLINE_NS
115 } // namespace mpt
116
117
118
119 #endif // MPT_BASE_SOURCE_LOCATION_HPP