1#ifndef BOOST_ASSERT_SOURCE_LOCATION_HPP_INCLUDED
2#define BOOST_ASSERT_SOURCE_LOCATION_HPP_INCLUDED
3
4// http://www.boost.org/libs/assert
5//
6// Copyright 2019, 2021 Peter Dimov
7// Distributed under the Boost Software License, Version 1.0.
8// http://www.boost.org/LICENSE_1_0.txt
9
10#include <boost/config.hpp>
11#include <boost/cstdint.hpp>
12#include <iosfwd>
13#include <string>
14#include <cstdio>
15#include <cstring>
16
17#if defined(__cpp_lib_source_location) && __cpp_lib_source_location >= 201907L
18# include <source_location>
19#endif
20
21namespace boost
22{
23
24struct source_location
25{
26private:
27
28 char const * file_;
29 char const * function_;
30 boost::uint_least32_t line_;
31 boost::uint_least32_t column_;
32
33public:
34
35 BOOST_CONSTEXPR source_location() BOOST_NOEXCEPT: file_( "" ), function_( "" ), line_( 0 ), column_( 0 )
36 {
37 }
38
39 BOOST_CONSTEXPR source_location( char const * file, boost::uint_least32_t ln, char const * function, boost::uint_least32_t col = 0 ) BOOST_NOEXCEPT: file_( file ), function_( function ), line_( ln ), column_( col )
40 {
41 }
42
43#if defined(__cpp_lib_source_location) && __cpp_lib_source_location >= 201907L
44
45 BOOST_CONSTEXPR source_location( std::source_location const& loc ) BOOST_NOEXCEPT: file_( loc.file_name() ), function_( loc.function_name() ), line_( loc.line() ), column_( loc.column() )
46 {
47 }
48
49#endif
50
51 BOOST_CONSTEXPR char const * file_name() const BOOST_NOEXCEPT
52 {
53 return file_;
54 }
55
56 BOOST_CONSTEXPR char const * function_name() const BOOST_NOEXCEPT
57 {
58 return function_;
59 }
60
61 BOOST_CONSTEXPR boost::uint_least32_t line() const BOOST_NOEXCEPT
62 {
63 return line_;
64 }
65
66 BOOST_CONSTEXPR boost::uint_least32_t column() const BOOST_NOEXCEPT
67 {
68 return column_;
69 }
70
71#if defined(BOOST_MSVC)
72# pragma warning( push )
73# pragma warning( disable: 4996 )
74#endif
75
76#if ( defined(_MSC_VER) && _MSC_VER < 1900 ) || ( defined(__MINGW32__) && !defined(__MINGW64_VERSION_MAJOR) )
77# define BOOST_ASSERT_SNPRINTF(buffer, format, arg) std::sprintf(buffer, format, arg)
78#else
79# define BOOST_ASSERT_SNPRINTF(buffer, format, arg) std::snprintf(buffer, sizeof(buffer)/sizeof(buffer[0]), format, arg)
80#endif
81
82 std::string to_string() const
83 {
84 unsigned long ln = line();
85
86 if( ln == 0 )
87 {
88 return "(unknown source location)";
89 }
90
91 std::string r = file_name();
92
93 char buffer[ 16 ];
94
95 BOOST_ASSERT_SNPRINTF( buffer, ":%lu", ln );
96 r += buffer;
97
98 unsigned long co = column();
99
100 if( co )
101 {
102 BOOST_ASSERT_SNPRINTF( buffer, ":%lu", co );
103 r += buffer;
104 }
105
106 char const* fn = function_name();
107
108 if( *fn != 0 )
109 {
110 r += " in function '";
111 r += fn;
112 r += '\'';
113 }
114
115 return r;
116 }
117
118#undef BOOST_ASSERT_SNPRINTF
119
120#if defined(BOOST_MSVC)
121# pragma warning( pop )
122#endif
123
124 inline friend bool operator==( source_location const& s1, source_location const& s2 ) BOOST_NOEXCEPT
125 {
126 return std::strcmp( s1: s1.file_, s2: s2.file_ ) == 0 && std::strcmp( s1: s1.function_, s2: s2.function_ ) == 0 && s1.line_ == s2.line_ && s1.column_ == s2.column_;
127 }
128
129 inline friend bool operator!=( source_location const& s1, source_location const& s2 ) BOOST_NOEXCEPT
130 {
131 return !( s1 == s2 );
132 }
133};
134
135template<class E, class T> std::basic_ostream<E, T> & operator<<( std::basic_ostream<E, T> & os, source_location const & loc )
136{
137 os << loc.to_string();
138 return os;
139}
140
141} // namespace boost
142
143#if defined(BOOST_DISABLE_CURRENT_LOCATION)
144
145# define BOOST_CURRENT_LOCATION ::boost::source_location()
146
147#elif defined(BOOST_MSVC) && BOOST_MSVC >= 1935
148
149# define BOOST_CURRENT_LOCATION ::boost::source_location(__builtin_FILE(), __builtin_LINE(), __builtin_FUNCSIG(), __builtin_COLUMN())
150
151#elif defined(BOOST_MSVC) && BOOST_MSVC >= 1926
152
153// std::source_location::current() is available in -std:c++20, but fails with consteval errors before 19.31, and doesn't produce
154// the correct result under 19.31, so prefer the built-ins
155# define BOOST_CURRENT_LOCATION ::boost::source_location(__builtin_FILE(), __builtin_LINE(), __builtin_FUNCTION(), __builtin_COLUMN())
156
157#elif defined(BOOST_MSVC)
158
159// __LINE__ is not a constant expression under /ZI (edit and continue) for 1925 and before
160
161# define BOOST_CURRENT_LOCATION_IMPL_1(x) BOOST_CURRENT_LOCATION_IMPL_2(x)
162# define BOOST_CURRENT_LOCATION_IMPL_2(x) (x##0 / 10)
163
164# define BOOST_CURRENT_LOCATION ::boost::source_location(__FILE__, BOOST_CURRENT_LOCATION_IMPL_1(__LINE__), "")
165
166#elif defined(__cpp_lib_source_location) && __cpp_lib_source_location >= 201907L && !defined(__NVCC__)
167
168// Under nvcc, __builtin_source_location is not constexpr
169// https://github.com/boostorg/assert/issues/32
170
171# define BOOST_CURRENT_LOCATION ::boost::source_location(::std::source_location::current())
172
173#elif defined(BOOST_CLANG) && BOOST_CLANG_VERSION >= 90000
174
175# define BOOST_CURRENT_LOCATION ::boost::source_location(__builtin_FILE(), __builtin_LINE(), __builtin_FUNCTION(), __builtin_COLUMN())
176
177#elif defined(BOOST_GCC) && BOOST_GCC >= 70000
178
179// The built-ins are available in 4.8+, but are not constant expressions until 7
180# define BOOST_CURRENT_LOCATION ::boost::source_location(__builtin_FILE(), __builtin_LINE(), __builtin_FUNCTION())
181
182#elif defined(BOOST_GCC) && BOOST_GCC >= 50000
183
184// __PRETTY_FUNCTION__ is allowed outside functions under GCC, but 4.x suffers from codegen bugs
185# define BOOST_CURRENT_LOCATION ::boost::source_location(__FILE__, __LINE__, __PRETTY_FUNCTION__)
186
187#else
188
189// __func__ macros aren't allowed outside functions, but BOOST_CURRENT_LOCATION is
190# define BOOST_CURRENT_LOCATION ::boost::source_location(__FILE__, __LINE__, "")
191
192#endif
193
194#endif // #ifndef BOOST_ASSERT_SOURCE_LOCATION_HPP_INCLUDED
195

source code of boost/libs/assert/include/boost/assert/source_location.hpp