source_location.hpp 5.9 KB

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