1 | // Copyright 2022 Peter Dimov |
2 | // Distributed under the Boost Software License, Version 1.0. |
3 | // https://www.boost.org/LICENSE_1_0.txt |
4 | |
5 | #include <boost/throw_exception.hpp> |
6 | #include <boost/core/lightweight_test.hpp> |
7 | |
8 | #if defined(_MSC_VER) |
9 | # pragma warning(disable: 4702) // unreachable code |
10 | #endif |
11 | |
12 | class my_exception: public std::exception |
13 | { |
14 | }; |
15 | |
16 | int main() |
17 | { |
18 | try |
19 | { |
20 | boost::throw_with_location( e: my_exception() ); |
21 | |
22 | BOOST_ERROR( "boost::throw_with_location failed to throw" ); |
23 | } |
24 | catch( std::exception const & x ) |
25 | { |
26 | boost::source_location loc = boost::get_throw_location( e: x ); |
27 | |
28 | // When not supplied explicitly, the source location is best effort. |
29 | // It should be the location of the throw_with_location call on |
30 | // recent compilers, but that's not guaranteed for every compiler. |
31 | // So we can't be more specific in testing it. |
32 | |
33 | BOOST_TEST_CSTR_NE( loc.file_name(), "" ); |
34 | BOOST_TEST_NE( loc.line(), 0u ); |
35 | } |
36 | catch( ... ) |
37 | { |
38 | BOOST_ERROR( "boost::throw_with_location failed to throw std::exception" ); |
39 | } |
40 | |
41 | boost::source_location location = BOOST_CURRENT_LOCATION; |
42 | |
43 | try |
44 | { |
45 | boost::throw_with_location( e: my_exception(), loc: location ); |
46 | |
47 | BOOST_ERROR( "boost::throw_with_location failed to throw" ); |
48 | } |
49 | catch( std::exception const & x ) |
50 | { |
51 | boost::source_location loc = boost::get_throw_location( e: x ); |
52 | |
53 | BOOST_TEST_CSTR_EQ( loc.file_name(), location.file_name() ); |
54 | BOOST_TEST_CSTR_EQ( loc.function_name(), location.function_name() ); |
55 | BOOST_TEST_EQ( loc.line(), location.line() ); |
56 | BOOST_TEST_EQ( loc.column(), location.column() ); |
57 | } |
58 | catch( ... ) |
59 | { |
60 | BOOST_ERROR( "boost::throw_with_location failed to throw std::exception" ); |
61 | } |
62 | |
63 | return boost::report_errors(); |
64 | } |
65 | |