Shamrock 2025.10.0
Astrophysical Code
Loading...
Searching...
No Matches
source_location.hpp
Go to the documentation of this file.
1// -------------------------------------------------------//
2//
3// SHAMROCK code for hydrodynamics
4// Copyright (c) 2021-2026 Timothée David--Cléris <tim.shamrock@proton.me>
5// SPDX-License-Identifier: CeCILL Free Software License Agreement v2.1
6// Shamrock is licensed under the CeCILL 2.1 License, see LICENSE for more information
7//
8// -------------------------------------------------------//
9
10#pragma once
11
74namespace std {
75
76#if defined __has_builtin
77 #if __has_builtin(__builtin_source_location)
78 // if the builtin __builtin_source_location is available we can use the source_location
79 // definition from libc++, otherwise we rebuild it using __builtin_FILE,
80 // __builtin_FUNCTION,
81 // __builtin_LINE and __builtin_COLUMN
82 #define _INT_SOURCE_LOC_DEF
83 #endif
84#endif
85
86#if defined(DOXYGEN)
88 static constexpr source_location current() noexcept;
89 constexpr source_location() noexcept = default;
90 constexpr unsigned line() const noexcept;
91 constexpr unsigned column() const noexcept;
92 constexpr const char *file_name() const noexcept;
93 constexpr const char *function_name() const noexcept;
94 }
95#else
96
97 struct source_location {
98
99 #ifndef _INT_SOURCE_LOC_DEF
100 const char *_M_file_name = "";
101 const char *_M_function_name = "";
102 unsigned _M_line = 0;
103 unsigned _M_column = 0;
104
105 public:
106 static constexpr source_location current(
107
108 #if defined __has_builtin
109 #if __has_builtin(__builtin_FILE)
110 const char *fileName = __builtin_FILE(),
111 #else
112 const char *fileName = "",
113 #endif
114 #if __has_builtin(__builtin_FUNCTION)
115 const char *functionName = __builtin_FUNCTION(),
116 #else
117 const char *functionName = "",
118 #endif
119 #if __has_builtin(__builtin_LINE)
120 const unsigned lineNumber = __builtin_LINE(),
121 #else
122 const unsigned lineNumber = 0,
123 #endif
124 #if __has_builtin(__builtin_COLUMN)
125 const unsigned columnOffset = __builtin_COLUMN()
126 #else
127 const unsigned columnOffset = 0
128 #endif
129 #else
130
131 const char *fileName = "",
132 const char *functionName = "",
133 const unsigned lineNumber = 0,
134 const unsigned columnOffset = 0
135 #endif
136
137 ) noexcept {
138 source_location __sl;
139
140 __sl._M_file_name = fileName;
141 __sl._M_function_name = functionName;
142 __sl._M_line = lineNumber;
143 __sl._M_column = columnOffset;
144 return __sl;
145 }
146
147 constexpr source_location() noexcept = default;
148
149 constexpr unsigned line() const noexcept { return _M_line; }
150 constexpr unsigned column() const noexcept { return _M_column; }
151 constexpr const char *file_name() const noexcept { return _M_file_name; }
152 constexpr const char *function_name() const noexcept { return _M_function_name; }
153
154 #else
155 struct __impl {
156 const char *_M_file_name;
157 const char *_M_function_name;
158 unsigned _M_line;
159 unsigned _M_column;
160 };
161
162 const __impl *__ptr_ = nullptr;
163 // GCC returns the type 'const void*' from the builtin, while clang returns
164 // `const __impl*`. Per C++ [expr.const], casts from void* are not permitted
165 // in constant evaluation, so we don't want to use `void*` as the argument
166 // type unless the builtin returned that, anyhow, and the invalid cast is
167 // unavoidable.
168
169 using __bsl_ty = decltype(__builtin_source_location());
170
171 public:
172 // The defaulted __ptr argument is necessary so that the builtin is evaluated
173 // in the context of the caller. An explicit value should never be provided.
174 static constexpr source_location current(
175 __bsl_ty __ptr = __builtin_source_location()) noexcept {
176 source_location __sl;
177 __sl.__ptr_ = static_cast<const __impl *>(__ptr);
178 return __sl;
179 }
180 constexpr source_location() noexcept = default;
181
182 constexpr unsigned line() const noexcept { return __ptr_ != nullptr ? __ptr_->_M_line : 0; }
183 constexpr unsigned column() const noexcept {
184 return __ptr_ != nullptr ? __ptr_->_M_column : 0;
185 }
186 constexpr const char *file_name() const noexcept {
187 return __ptr_ != nullptr ? __ptr_->_M_file_name : "";
188 }
189 constexpr const char *function_name() const noexcept {
190 return __ptr_ != nullptr ? __ptr_->_M_function_name : "";
191 }
192
193 #undef _INT_SOURCE_LOC_DEF
194 #endif
195 };
196
197#endif
198
199} // namespace std
STL namespace.
Utility class to emulates std::source_location class introduced in c++20 This class provides informat...
static constexpr source_location current() noexcept
Returns the source location where this function is called.
constexpr const char * file_name() const noexcept
Returns the file name of the source location.
constexpr unsigned column() const noexcept
Returns the column offset of the source location.
constexpr unsigned line() const noexcept
Returns the line number of the source location.
constexpr const char * function_name() const noexcept
Returns the function name of the source location.