From 23e699b9a4c84d2e9c3ad10901fe92f84d3a8d09 Mon Sep 17 00:00:00 2001 From: Peter Dimov Date: Thu, 3 Feb 2022 20:28:59 +0200 Subject: [PATCH] Use std::source_location::current() in BOOST_CURRENT_LOCATION --- include/boost/assert/source_location.hpp | 4 +++ test/source_location_test.cpp | 7 +++- test/source_location_test3.cpp | 41 ++++++++++++++++++++++-- 3 files changed, 49 insertions(+), 3 deletions(-) diff --git a/include/boost/assert/source_location.hpp b/include/boost/assert/source_location.hpp index 9b46e05..c556f93 100644 --- a/include/boost/assert/source_location.hpp +++ b/include/boost/assert/source_location.hpp @@ -118,6 +118,10 @@ template std::basic_ostream & operator<<( std::basic_ost # define BOOST_CURRENT_LOCATION ::boost::source_location() +#elif defined(__cpp_lib_source_location) && __cpp_lib_source_location >= 201907L + +# define BOOST_CURRENT_LOCATION ::boost::source_location(std::source_location::current()) + #elif defined(__clang_analyzer__) // Cast to char const* to placate clang-tidy diff --git a/test/source_location_test.cpp b/test/source_location_test.cpp index fc13802..1eaabf8 100644 --- a/test/source_location_test.cpp +++ b/test/source_location_test.cpp @@ -20,9 +20,14 @@ int main() boost::source_location loc = BOOST_CURRENT_LOCATION; BOOST_TEST_CSTR_EQ( loc.file_name(), __FILE__ ); - BOOST_TEST_CSTR_EQ( loc.function_name(), BOOST_CURRENT_FUNCTION ); BOOST_TEST_EQ( loc.line(), 20 ); + +#if !( defined(__cpp_lib_source_location) && __cpp_lib_source_location >= 201907L ) + + BOOST_TEST_CSTR_EQ( loc.function_name(), BOOST_CURRENT_FUNCTION ); BOOST_TEST_EQ( loc.column(), 0 ); + +#endif } #if defined(__cpp_lib_source_location) && __cpp_lib_source_location >= 201907L diff --git a/test/source_location_test3.cpp b/test/source_location_test3.cpp index 3bccd81..ae73dd7 100644 --- a/test/source_location_test3.cpp +++ b/test/source_location_test3.cpp @@ -23,17 +23,54 @@ int main() } { - boost::source_location loc = BOOST_CURRENT_LOCATION; + boost::source_location loc( __FILE__, __LINE__, BOOST_CURRENT_FUNCTION ); BOOST_TEST_EQ( loc.to_string(), std::string( __FILE__ ) + ":26 in function '" + BOOST_CURRENT_FUNCTION + "'" ); } + { + boost::source_location loc( __FILE__, __LINE__, BOOST_CURRENT_FUNCTION ); + + std::ostringstream os; + os << loc; + + BOOST_TEST_EQ( os.str(), std::string( __FILE__ ) + ":31 in function '" + BOOST_CURRENT_FUNCTION + "'" ); + } + + { + boost::source_location loc = BOOST_CURRENT_LOCATION; + + std::string prefix = std::string( __FILE__ ) + ":40"; + +#if !( defined(__cpp_lib_source_location) && __cpp_lib_source_location >= 201907L ) + + BOOST_TEST_EQ( loc.to_string(), prefix + " in function '" + BOOST_CURRENT_FUNCTION + "'" ); + +#else + + // column and function vary when coming from std::source_location::current() + BOOST_TEST_EQ( loc.to_string().substr( 0, prefix.size() ), prefix ); + +#endif + } + { boost::source_location loc = BOOST_CURRENT_LOCATION; std::ostringstream os; os << loc; - BOOST_TEST_EQ( os.str(), std::string( __FILE__ ) + ":31 in function '" + BOOST_CURRENT_FUNCTION + "'" ); + std::string prefix = std::string( __FILE__ ) + ":57"; + +#if !( defined(__cpp_lib_source_location) && __cpp_lib_source_location >= 201907L ) + + BOOST_TEST_EQ( os.str(), prefix + " in function '" + BOOST_CURRENT_FUNCTION + "'" ); + +#else + + // column and function vary when coming from std::source_location::current() + BOOST_TEST_EQ( os.str().substr( 0, prefix.size() ), prefix ); + +#endif } return boost::report_errors();