source_location.hpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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/current_function.hpp>
  9. #include <boost/config.hpp>
  10. #include <boost/config/workaround.hpp>
  11. #include <boost/cstdint.hpp>
  12. #include <iosfwd>
  13. #include <string>
  14. #include <cstdio>
  15. #include <cstring>
  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. std::string to_string() const
  61. {
  62. unsigned long ln = line();
  63. if( ln == 0 )
  64. {
  65. return "(unknown source location)";
  66. }
  67. std::string r = file_name();
  68. char buffer[ 16 ];
  69. std::sprintf( buffer, ":%lu", ln );
  70. r += buffer;
  71. unsigned long co = column();
  72. if( co )
  73. {
  74. std::sprintf( buffer, ":%lu", co );
  75. r += buffer;
  76. }
  77. char const* fn = function_name();
  78. if( *fn != 0 )
  79. {
  80. r += " in function '";
  81. r += fn;
  82. r += '\'';
  83. }
  84. return r;
  85. }
  86. #if defined(BOOST_MSVC)
  87. # pragma warning( pop )
  88. #endif
  89. };
  90. template<class E, class T> std::basic_ostream<E, T> & operator<<( std::basic_ostream<E, T> & os, source_location const & loc )
  91. {
  92. os << loc.to_string();
  93. return os;
  94. }
  95. } // namespace boost
  96. #if defined(BOOST_DISABLE_CURRENT_LOCATION)
  97. # define BOOST_CURRENT_LOCATION ::boost::source_location()
  98. #elif defined(BOOST_MSVC) && BOOST_MSVC >= 1926
  99. // std::source_location::current() is available in -std:c++20, but fails with consteval errors before 19.31, and doesn't produce
  100. // the correct result under 19.31, so prefer the built-ins
  101. # define BOOST_CURRENT_LOCATION ::boost::source_location(__builtin_FILE(), __builtin_LINE(), __builtin_FUNCTION(), __builtin_COLUMN())
  102. #elif defined(__cpp_lib_source_location) && __cpp_lib_source_location >= 201907L
  103. # define BOOST_CURRENT_LOCATION ::boost::source_location(::std::source_location::current())
  104. #elif defined(BOOST_CLANG) && BOOST_CLANG_VERSION >= 90000
  105. # define BOOST_CURRENT_LOCATION ::boost::source_location(__builtin_FILE(), __builtin_LINE(), __builtin_FUNCTION(), __builtin_COLUMN())
  106. #elif defined(BOOST_GCC) && BOOST_GCC >= 70000
  107. // The built-ins are available in 4.8+, but are not constant expressions until 7
  108. # define BOOST_CURRENT_LOCATION ::boost::source_location(__builtin_FILE(), __builtin_LINE(), __builtin_FUNCTION())
  109. #elif defined(BOOST_GCC) && BOOST_GCC >= 50000
  110. // __PRETTY_FUNCTION__ is allowed outside functions under GCC, but 4.x suffers from codegen bugs
  111. # define BOOST_CURRENT_LOCATION ::boost::source_location(__FILE__, __LINE__, __PRETTY_FUNCTION__)
  112. #else
  113. // __func__ macros aren't allowed outside functions, but BOOST_CURRENT_LOCATION is
  114. # define BOOST_CURRENT_LOCATION ::boost::source_location(__FILE__, __LINE__, "")
  115. #endif
  116. #endif // #ifndef BOOST_ASSERT_SOURCE_LOCATION_HPP_INCLUDED