Merge pull request #205 from gennaroprota/fix/use_do_while_false_for_boost_test_throws_and_boost_test_no_throw

Use the do {} while (false) idiom for BOOST_TEST_THROWS() and BOOST_TEST_NO_THROW()
This commit is contained in:
Peter Dimov
2025-10-18 16:06:01 +03:00
committed by GitHub

View File

@@ -551,39 +551,56 @@ inline void lwt_init()
#define BOOST_TEST_ALL_EQ(begin1, end1, begin2, end2) ( ::boost::detail::test_all_eq_impl(BOOST_LIGHTWEIGHT_TEST_OSTREAM, __FILE__, __LINE__, BOOST_CURRENT_FUNCTION, begin1, end1, begin2, end2) ) #define BOOST_TEST_ALL_EQ(begin1, end1, begin2, end2) ( ::boost::detail::test_all_eq_impl(BOOST_LIGHTWEIGHT_TEST_OSTREAM, __FILE__, __LINE__, BOOST_CURRENT_FUNCTION, begin1, end1, begin2, end2) )
#define BOOST_TEST_ALL_WITH(begin1, end1, begin2, end2, predicate) ( ::boost::detail::test_all_with_impl(BOOST_LIGHTWEIGHT_TEST_OSTREAM, __FILE__, __LINE__, BOOST_CURRENT_FUNCTION, begin1, end1, begin2, end2, predicate) ) #define BOOST_TEST_ALL_WITH(begin1, end1, begin2, end2, predicate) ( ::boost::detail::test_all_with_impl(BOOST_LIGHTWEIGHT_TEST_OSTREAM, __FILE__, __LINE__, BOOST_CURRENT_FUNCTION, begin1, end1, begin2, end2, predicate) )
#ifndef BOOST_NO_EXCEPTIONS #if !defined(BOOST_MSVC) || (BOOST_MSVC >= 1900)
#define BOOST_TEST_THROWS( EXPR, EXCEP ) \ // The usual idiom for multiline macros. But disabled for MSVC versions
try { \ // prior to 2015, which would emit a "conditional expression is constant"
EXPR; \ // warning that we could not silence with a _Pragma, despite this being
::boost::detail::throw_failed_impl \ // the same thing described here:
(#EXPR, #EXCEP, __FILE__, __LINE__, BOOST_CURRENT_FUNCTION); \ //
} \ // <https://learn.microsoft.com/en-us/cpp/preprocessor/pragma-directives-and-the-pragma-keyword?view=msvc-140>.
catch(EXCEP const&) { \ //
::boost::detail::test_results(); \ #define BOOST_LWT_DETAIL_DO_WHILE_FALSE( x ) do { x } while (false)
} \
catch(...) { \
::boost::detail::throw_failed_impl \
(#EXPR, #EXCEP, __FILE__, __LINE__, BOOST_CURRENT_FUNCTION); \
} \
//
#else #else
#define BOOST_TEST_THROWS( EXPR, EXCEP ) #define BOOST_LWT_DETAIL_DO_WHILE_FALSE( x ) x
#endif #endif
#ifndef BOOST_NO_EXCEPTIONS #ifndef BOOST_NO_EXCEPTIONS
# define BOOST_TEST_NO_THROW(EXPR) \ #define BOOST_TEST_THROWS( EXPR, EXCEP ) \
try { \ BOOST_LWT_DETAIL_DO_WHILE_FALSE( \
EXPR; \ try { \
} catch (const std::exception& e) { \ EXPR; \
::boost::detail::no_throw_failed_impl \ ::boost::detail::throw_failed_impl \
(#EXPR, e.what(), __FILE__, __LINE__, BOOST_CURRENT_FUNCTION); \ (#EXPR, #EXCEP, __FILE__, __LINE__, BOOST_CURRENT_FUNCTION); \
} catch (...) { \ } \
::boost::detail::no_throw_failed_impl \ catch(EXCEP const&) { \
(#EXPR, __FILE__, __LINE__, BOOST_CURRENT_FUNCTION); \ ::boost::detail::test_results(); \
} } \
catch(...) { \
::boost::detail::throw_failed_impl \
(#EXPR, #EXCEP, __FILE__, __LINE__, BOOST_CURRENT_FUNCTION); \
} \
)
//
#else
#define BOOST_TEST_THROWS( EXPR, EXCEP ) BOOST_LWT_DETAIL_DO_WHILE_FALSE((void)0;)
#endif
#ifndef BOOST_NO_EXCEPTIONS
# define BOOST_TEST_NO_THROW(EXPR) \
BOOST_LWT_DETAIL_DO_WHILE_FALSE( \
try { \
EXPR; \
} catch (const std::exception& e) { \
::boost::detail::no_throw_failed_impl \
(#EXPR, e.what(), __FILE__, __LINE__, BOOST_CURRENT_FUNCTION); \
} catch (...) { \
::boost::detail::no_throw_failed_impl \
(#EXPR, __FILE__, __LINE__, BOOST_CURRENT_FUNCTION); \
} \
)
// //
#else #else
# define BOOST_TEST_NO_THROW(EXPR) { EXPR; } # define BOOST_TEST_NO_THROW(EXPR) BOOST_LWT_DETAIL_DO_WHILE_FALSE(EXPR;)
#endif #endif
#endif // #ifndef BOOST_CORE_LIGHTWEIGHT_TEST_HPP #endif // #ifndef BOOST_CORE_LIGHTWEIGHT_TEST_HPP