1// Copyright 2018 Peter Dimov
2//
3// Distributed under the Boost Software License, Version 1.0.
4//
5// See accompanying file LICENSE_1_0.txt or copy at
6// http://www.boost.org/LICENSE_1_0.txt
7
8#include <boost/throw_exception.hpp>
9#include <boost/exception/get_error_info.hpp>
10#include <boost/detail/lightweight_test.hpp>
11#include <cstring>
12
13class my_exception: public std::exception
14{
15};
16
17class my_exception2: public std::exception, public boost::exception
18{
19};
20
21class my_exception3: public std::exception, public virtual boost::exception
22{
23};
24
25char const* translate_function( char const * fn, char const * cfn )
26{
27 // translate "" and "main" to BOOST_CURRENT_FUNCTION
28 return fn[0] == 0 || std::strcmp( s1: fn, s2: "main" ) == 0? cfn: fn;
29}
30
31static char const* adjust_filename( char const* file )
32{
33#if defined(__INTEL_LLVM_COMPILER) && __INTEL_LLVM_COMPILER >= 20210300
34
35 char const* fn = std::strrchr( file, '/' );
36 return fn? fn + 1: file;
37
38#else
39
40 return file;
41
42#endif
43}
44
45int main()
46{
47 try
48 {
49 BOOST_THROW_EXCEPTION( my_exception() );
50 }
51 catch( boost::exception const & x )
52 {
53 {
54 char const * const * file = boost::get_error_info<boost::throw_file>( some_exception: x );
55
56 BOOST_TEST( file != 0 );
57 BOOST_TEST_CSTR_EQ( *file, adjust_filename(__FILE__) );
58 }
59
60 {
61 int const * line = boost::get_error_info<boost::throw_line>( some_exception: x );
62
63 BOOST_TEST( line != 0 );
64 BOOST_TEST_EQ( *line, 49 );
65 }
66
67 {
68 char const * const * function = boost::get_error_info<boost::throw_function>( some_exception: x );
69
70 BOOST_TEST( function != 0 );
71 BOOST_TEST_CSTR_EQ( translate_function( *function, BOOST_CURRENT_FUNCTION ), BOOST_CURRENT_FUNCTION );
72 }
73 }
74
75 try
76 {
77 BOOST_THROW_EXCEPTION( my_exception2() );
78 }
79 catch( boost::exception const & x )
80 {
81 {
82 char const * const * file = boost::get_error_info<boost::throw_file>( some_exception: x );
83
84 BOOST_TEST( file != 0 );
85 BOOST_TEST_CSTR_EQ( *file, adjust_filename(__FILE__) );
86 }
87
88 {
89 int const * line = boost::get_error_info<boost::throw_line>( some_exception: x );
90
91 BOOST_TEST( line != 0 );
92 BOOST_TEST_EQ( *line, 77 );
93 }
94
95 {
96 char const * const * function = boost::get_error_info<boost::throw_function>( some_exception: x );
97
98 BOOST_TEST( function != 0 );
99 BOOST_TEST_CSTR_EQ( translate_function( *function, BOOST_CURRENT_FUNCTION ), BOOST_CURRENT_FUNCTION );
100 }
101 }
102
103 try
104 {
105 BOOST_THROW_EXCEPTION( my_exception3() );
106 }
107 catch( boost::exception const & x )
108 {
109 {
110 char const * const * file = boost::get_error_info<boost::throw_file>( some_exception: x );
111
112 BOOST_TEST( file != 0 );
113 BOOST_TEST_CSTR_EQ( *file, adjust_filename(__FILE__) );
114 }
115
116 {
117 int const * line = boost::get_error_info<boost::throw_line>( some_exception: x );
118
119 BOOST_TEST( line != 0 );
120 BOOST_TEST_EQ( *line, 105 );
121 }
122
123 {
124 char const * const * function = boost::get_error_info<boost::throw_function>( some_exception: x );
125
126 BOOST_TEST( function != 0 );
127 BOOST_TEST_CSTR_EQ( translate_function( *function, BOOST_CURRENT_FUNCTION ), BOOST_CURRENT_FUNCTION );
128 }
129 }
130
131 return boost::report_errors();
132}
133

source code of boost/libs/throw_exception/test/throw_exception_test4.cpp