source_location.hpp 5.7 KB

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