1 #ifndef BOOST_ASSERT_SOURCE_LOCATION_HPP_INCLUDED
2 #define BOOST_ASSERT_SOURCE_LOCATION_HPP_INCLUDED
4 // http://www.boost.org/libs/assert
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
10 #include <boost/current_function.hpp>
11 #include <boost/config.hpp>
12 #include <boost/config/workaround.hpp>
13 #include <boost/cstdint.hpp>
19 #if defined(__cpp_lib_source_location) && __cpp_lib_source_location >= 201907L
20 # include <source_location>
26 struct source_location
31 char const * function_;
32 boost::uint_least32_t line_;
33 boost::uint_least32_t column_;
37 BOOST_CONSTEXPR source_location() BOOST_NOEXCEPT: file_( "" ), function_( "" ), line_( 0 ), column_( 0 )
41 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 )
45 #if defined(__cpp_lib_source_location) && __cpp_lib_source_location >= 201907L
47 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() )
53 BOOST_CONSTEXPR char const * file_name() const BOOST_NOEXCEPT
58 BOOST_CONSTEXPR char const * function_name() const BOOST_NOEXCEPT
63 BOOST_CONSTEXPR boost::uint_least32_t line() const BOOST_NOEXCEPT
68 BOOST_CONSTEXPR boost::uint_least32_t column() const BOOST_NOEXCEPT
73 #if defined(BOOST_MSVC)
74 # pragma warning( push )
75 # pragma warning( disable: 4996 )
78 std::string to_string() const
80 unsigned long ln = line();
84 return "(unknown source location)";
87 std::string r = file_name();
91 std::sprintf( buffer, ":%lu", ln );
94 unsigned long co = column();
98 std::sprintf( buffer, ":%lu", co );
102 char const* fn = function_name();
106 r += " in function '";
114 #if defined(BOOST_MSVC)
115 # pragma warning( pop )
120 template<class E, class T> std::basic_ostream<E, T> & operator<<( std::basic_ostream<E, T> & os, source_location const & loc )
122 os << loc.to_string();
128 #if defined(BOOST_DISABLE_CURRENT_LOCATION)
130 # define BOOST_CURRENT_LOCATION ::boost::source_location()
132 #elif defined(BOOST_MSVC) && BOOST_MSVC >= 1926
134 // std::source_location::current() is available in -std:c++20, but fails with consteval errors before 19.31, and doesn't produce
135 // the correct result under 19.31, so prefer the built-ins
136 # define BOOST_CURRENT_LOCATION ::boost::source_location(__builtin_FILE(), __builtin_LINE(), __builtin_FUNCTION(), __builtin_COLUMN())
138 #elif defined(__cpp_lib_source_location) && __cpp_lib_source_location >= 201907L
140 # define BOOST_CURRENT_LOCATION ::boost::source_location(::std::source_location::current())
142 #elif defined(BOOST_CLANG) && BOOST_CLANG_VERSION >= 90000
144 # define BOOST_CURRENT_LOCATION ::boost::source_location(__builtin_FILE(), __builtin_LINE(), __builtin_FUNCTION(), __builtin_COLUMN())
146 #elif defined(BOOST_GCC) && BOOST_GCC >= 70000
148 // The built-ins are available in 4.8+, but are not constant expressions until 7
149 # define BOOST_CURRENT_LOCATION ::boost::source_location(__builtin_FILE(), __builtin_LINE(), __builtin_FUNCTION())
151 #elif defined(BOOST_GCC) && BOOST_GCC >= 50000
153 // __PRETTY_FUNCTION__ is allowed outside functions under GCC, but 4.x suffers from codegen bugs
154 # define BOOST_CURRENT_LOCATION ::boost::source_location(__FILE__, __LINE__, __PRETTY_FUNCTION__)
158 // __func__ macros aren't allowed outside functions, but BOOST_CURRENT_LOCATION is
159 # define BOOST_CURRENT_LOCATION ::boost::source_location(__FILE__, __LINE__, "")
163 #endif // #ifndef BOOST_ASSERT_SOURCE_LOCATION_HPP_INCLUDED