source_location.hpp 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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 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/cstdint.hpp>
  11. #include <iosfwd>
  12. namespace boost
  13. {
  14. struct source_location
  15. {
  16. private:
  17. char const * file_;
  18. char const * function_;
  19. boost::uint_least32_t line_;
  20. boost::uint_least32_t column_;
  21. public:
  22. BOOST_CONSTEXPR source_location() BOOST_NOEXCEPT: file_( "(unknown)" ), function_( "(unknown)" ), line_( 0 ), column_( 0 )
  23. {
  24. }
  25. 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 )
  26. {
  27. }
  28. BOOST_CONSTEXPR char const * file_name() const BOOST_NOEXCEPT
  29. {
  30. return file_;
  31. }
  32. BOOST_CONSTEXPR char const * function_name() const BOOST_NOEXCEPT
  33. {
  34. return function_;
  35. }
  36. BOOST_CONSTEXPR boost::uint_least32_t line() const BOOST_NOEXCEPT
  37. {
  38. return line_;
  39. }
  40. BOOST_CONSTEXPR boost::uint_least32_t column() const BOOST_NOEXCEPT
  41. {
  42. return column_;
  43. }
  44. };
  45. template<class E, class T> std::basic_ostream<E, T> & operator<<( std::basic_ostream<E, T> & os, source_location const & loc )
  46. {
  47. os.width( 0 );
  48. if( loc.line() == 0 )
  49. {
  50. os << "(unknown source location)";
  51. }
  52. else
  53. {
  54. os << loc.file_name() << ':' << loc.line();
  55. if( loc.column() )
  56. {
  57. os << ':' << loc.column();
  58. }
  59. os << ": in function '" << loc.function_name() << '\'';
  60. }
  61. return os;
  62. }
  63. } // namespace boost
  64. #if defined( BOOST_DISABLE_CURRENT_LOCATION )
  65. # define BOOST_CURRENT_LOCATION ::boost::source_location()
  66. #elif defined(__clang_analyzer__)
  67. // Cast to char const* to placate clang-tidy
  68. // https://bugs.llvm.org/show_bug.cgi?id=28480
  69. # define BOOST_CURRENT_LOCATION ::boost::source_location(__FILE__, __LINE__, static_cast<char const*>(BOOST_CURRENT_FUNCTION))
  70. #else
  71. # define BOOST_CURRENT_LOCATION ::boost::source_location(__FILE__, __LINE__, BOOST_CURRENT_FUNCTION)
  72. #endif
  73. #endif // #ifndef BOOST_ASSERT_SOURCE_LOCATION_HPP_INCLUDED