source_location.hpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. #ifndef BHO_ASSERT_SOURCE_LOCATION_HPP_INCLUDED
  2. #define BHO_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 <asio2/bho/config.hpp>
  9. #include <asio2/bho/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 bho
  18. {
  19. struct source_location
  20. {
  21. private:
  22. char const * file_;
  23. char const * function_;
  24. bho::uint_least32_t line_;
  25. bho::uint_least32_t column_;
  26. public:
  27. BHO_CONSTEXPR source_location() BHO_NOEXCEPT: file_( "" ), function_( "" ), line_( 0 ), column_( 0 )
  28. {
  29. }
  30. BHO_CONSTEXPR source_location( char const * file, bho::uint_least32_t ln, char const * function, bho::uint_least32_t col = 0 ) BHO_NOEXCEPT: file_( file ), function_( function ), line_( ln ), column_( col )
  31. {
  32. }
  33. #if defined(__cpp_lib_source_location) && __cpp_lib_source_location >= 201907L
  34. BHO_CONSTEXPR source_location( std::source_location const& loc ) BHO_NOEXCEPT: file_( loc.file_name() ), function_( loc.function_name() ), line_( loc.line() ), column_( loc.column() )
  35. {
  36. }
  37. #endif
  38. BHO_CONSTEXPR char const * file_name() const BHO_NOEXCEPT
  39. {
  40. return file_;
  41. }
  42. BHO_CONSTEXPR char const * function_name() const BHO_NOEXCEPT
  43. {
  44. return function_;
  45. }
  46. BHO_CONSTEXPR bho::uint_least32_t line() const BHO_NOEXCEPT
  47. {
  48. return line_;
  49. }
  50. BHO_CONSTEXPR bho::uint_least32_t column() const BHO_NOEXCEPT
  51. {
  52. return column_;
  53. }
  54. #if defined(BHO_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 BHO_ASSERT_SNPRINTF(buffer, format, arg) std::sprintf(buffer, format, arg)
  60. #else
  61. # define BHO_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. BHO_ASSERT_SNPRINTF( buffer, ":%lu", ln );
  73. r += buffer;
  74. unsigned long co = column();
  75. if( co )
  76. {
  77. BHO_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 BHO_ASSERT_SNPRINTF
  90. #if defined(BHO_MSVC)
  91. # pragma warning( pop )
  92. #endif
  93. inline friend bool operator==( source_location const& s1, source_location const& s2 ) BHO_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 ) BHO_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 bho
  108. #if defined(BHO_DISABLE_CURRENT_LOCATION)
  109. # define BHO_CURRENT_LOCATION ::bho::source_location()
  110. #elif defined(BHO_MSVC) && BHO_MSVC >= 1926
  111. // std::source_location::current() is available in -std:c++20, but fails with consteval errors before 19.31, and doesn't produce
  112. // the correct result under 19.31, so prefer the built-ins
  113. # define BHO_CURRENT_LOCATION ::bho::source_location(__builtin_FILE(), __builtin_LINE(), __builtin_FUNCTION(), __builtin_COLUMN())
  114. #elif defined(BHO_MSVC)
  115. // __LINE__ is not a constant expression under /ZI (edit and continue) for 1925 and before
  116. # define BHO_CURRENT_LOCATION_IMPL_1(x) BHO_CURRENT_LOCATION_IMPL_2(x)
  117. # define BHO_CURRENT_LOCATION_IMPL_2(x) (x##0 / 10)
  118. # define BHO_CURRENT_LOCATION ::bho::source_location(__FILE__, BHO_CURRENT_LOCATION_IMPL_1(__LINE__), "")
  119. #elif defined(__cpp_lib_source_location) && __cpp_lib_source_location >= 201907L && !defined(__NVCC__)
  120. // Under nvcc, __builtin_source_location is not constexpr
  121. // https://github.com/boostorg/assert/issues/32
  122. # define BHO_CURRENT_LOCATION ::bho::source_location(::std::source_location::current())
  123. #elif defined(BHO_CLANG) && BHO_CLANG_VERSION >= 90000
  124. # define BHO_CURRENT_LOCATION ::bho::source_location(__builtin_FILE(), __builtin_LINE(), __builtin_FUNCTION(), __builtin_COLUMN())
  125. #elif defined(BHO_GCC) && BHO_GCC >= 70000
  126. // The built-ins are available in 4.8+, but are not constant expressions until 7
  127. # define BHO_CURRENT_LOCATION ::bho::source_location(__builtin_FILE(), __builtin_LINE(), __builtin_FUNCTION())
  128. #elif defined(BHO_GCC) && BHO_GCC >= 50000
  129. // __PRETTY_FUNCTION__ is allowed outside functions under GCC, but 4.x suffers from codegen bugs
  130. # define BHO_CURRENT_LOCATION ::bho::source_location(__FILE__, __LINE__, __PRETTY_FUNCTION__)
  131. #else
  132. // __func__ macros aren't allowed outside functions, but BHO_CURRENT_LOCATION is
  133. # define BHO_CURRENT_LOCATION ::bho::source_location(__FILE__, __LINE__, "")
  134. #endif
  135. #endif // #ifndef BHO_ASSERT_SOURCE_LOCATION_HPP_INCLUDED