Compare commits

...

11 Commits

11 changed files with 313 additions and 51 deletions

View File

@ -41,11 +41,11 @@ BOOST_NORETURN void throw_exception( std::exception const & e, boost::source_loc
#endif
// boost::wrapexcept<E>
namespace detail
{
// wrapexcept_add_base
typedef char (&wrapexcept_s1)[ 1 ];
typedef char (&wrapexcept_s2)[ 2 ];
@ -64,12 +64,37 @@ template<class E, class B> struct wrapexcept_add_base<E, B, 2>
typedef B type;
};
// wrapexcept_location
struct wrapexcept_location
{
wrapexcept_location() {}
explicit wrapexcept_location( boost::source_location const& loc ): location_( loc ) {}
boost::source_location location_;
};
} // namespace detail
template<class E> struct BOOST_SYMBOL_VISIBLE wrapexcept:
// wrapexcept<E>
#if !defined(BOOST_NO_CXX11_HDR_EXCEPTION)
template<class E, int cxxstd = 11> struct BOOST_SYMBOL_VISIBLE wrapexcept;
#else
template<class E, int cxxstd = 98> struct BOOST_SYMBOL_VISIBLE wrapexcept;
#endif
// C++98 version for backward compatibility
template<class E> struct BOOST_SYMBOL_VISIBLE wrapexcept<E, 98>:
public detail::wrapexcept_add_base<E, boost::exception_detail::clone_base>::type,
public E,
public detail::wrapexcept_add_base<E, boost::exception>::type
public detail::wrapexcept_add_base<E, boost::exception>::type,
public detail::wrapexcept_location
{
private:
@ -97,7 +122,7 @@ public:
copy_from( &e );
}
explicit wrapexcept( E const & e, boost::source_location const & loc ): E( e )
explicit wrapexcept( E const & e, boost::source_location const & loc ): E( e ), detail::wrapexcept_location( loc )
{
copy_from( &e );
@ -131,6 +156,49 @@ public:
}
};
// C++11 version
template<class E> struct BOOST_SYMBOL_VISIBLE wrapexcept<E, 11>:
public E,
public detail::wrapexcept_location
{
private:
void copy_from( void const* )
{
}
void copy_from( boost::exception const* p )
{
static_cast<boost::exception&>( *this ) = *p;
}
template<class T> void set_info_impl( void*, T const& )
{
}
template<class T> void set_info_impl( boost::exception * p, T const& t )
{
exception_detail::set_info( *p, t );
}
public:
explicit wrapexcept( E const & e ): E( e )
{
copy_from( &e );
}
explicit wrapexcept( E const & e, boost::source_location const & loc ): E( e ), detail::wrapexcept_location( loc )
{
copy_from( &e );
set_info_impl( this, throw_file( loc.file_name() ) );
set_info_impl( this, throw_line( loc.line() ) );
set_info_impl( this, throw_function( loc.function_name() ) );
}
};
// All boost exceptions are required to derive from std::exception,
// to ensure compatibility with BOOST_NO_EXCEPTIONS.
@ -154,24 +222,54 @@ template<class E> BOOST_NORETURN void throw_exception( E const & e, boost::sourc
throw e;
}
#else // defined( BOOST_EXCEPTION_DISABLE )
#elif !defined(BOOST_NO_CXX11_HDR_EXCEPTION)
template<class E> BOOST_NORETURN void throw_exception( E const & e )
{
throw_exception_assert_compatibility( e );
throw wrapexcept<E>( e );
throw e;
}
template<class E> BOOST_NORETURN void throw_exception( E const & e, boost::source_location const & loc )
{
throw_exception_assert_compatibility( e );
throw wrapexcept<E>( e, loc );
throw wrapexcept<E, 11>( e, loc );
}
#else // C++98
template<class E> BOOST_NORETURN void throw_exception( E const & e )
{
throw_exception_assert_compatibility( e );
throw wrapexcept<E, 98>( e );
}
template<class E> BOOST_NORETURN void throw_exception( E const & e, boost::source_location const & loc )
{
throw_exception_assert_compatibility( e );
throw wrapexcept<E, 98>( e, loc );
}
#endif // defined( BOOST_EXCEPTION_DISABLE )
#endif // !defined( BOOST_NO_EXCEPTIONS )
// get_throw_location
template<class E> boost::source_location get_throw_location( E const& e )
{
#if defined(BOOST_NO_RTTI)
return boost::source_location();
#else
detail::wrapexcept_location const* p = dynamic_cast< detail::wrapexcept_location const* >( &e );
return p? p->location_: boost::source_location();
#endif
}
} // namespace boost
// BOOST_THROW_EXCEPTION

View File

@ -27,13 +27,16 @@ run throw_exception_test2.cpp ;
run throw_exception_test3.cpp ;
run throw_exception_test4.cpp ;
run throw_exception_test5.cpp ;
run throw_exception_test6.cpp ;
lib lib1_throw : lib1_throw.cpp : <define>LIB1_SOURCE=1 <link>shared:<define>LIB1_DYN_LINK=1 : : <link>shared:<define>LIB1_DYN_LINK=1 ;
lib lib2_throw : lib2_throw.cpp : <define>LIB2_SOURCE=1 <link>shared:<define>LIB2_DYN_LINK=1 : : <link>shared:<define>LIB2_DYN_LINK=1 ;
lib lib3_throw : lib3_throw.cpp : <define>LIB3_SOURCE=1 <link>shared:<define>LIB3_DYN_LINK=1 : : <link>shared:<define>LIB3_DYN_LINK=1 ;
lib lib4_throw : lib4_throw.cpp : <define>LIB4_SOURCE=1 <link>shared:<define>LIB4_DYN_LINK=1 : : <link>shared:<define>LIB4_DYN_LINK=1 ;
lib lib5_throw : lib5_throw.cpp : <define>LIB5_SOURCE=1 <link>shared:<define>LIB5_DYN_LINK=1 : : <link>shared:<define>LIB5_DYN_LINK=1 ;
run throw_from_library_test.cpp lib1_throw lib2_throw lib3_throw : : : <link>static : throw_from_library_static ;
run throw_from_library_test.cpp lib1_throw lib2_throw lib3_throw : : : <link>shared : throw_from_library_shared ;
run throw_from_library_test.cpp lib1_throw lib2_throw lib3_throw lib4_throw lib5_throw : : : <link>static : throw_from_library_static ;
run throw_from_library_test.cpp lib1_throw lib2_throw lib3_throw lib4_throw lib5_throw : : : <link>shared : throw_from_library_shared ;
run throw_exception_nx_test.cpp : : : <exception-handling>off ;
run throw_exception_nx_test2.cpp : : : <exception-handling>off ;

14
test/lib4_throw.cpp Normal file
View File

@ -0,0 +1,14 @@
// Copyright 2018 Peter Dimov
//
// Distributed under the Boost Software License, Version 1.0.
//
// See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt
#include "lib4_throw.hpp"
#include <boost/throw_exception.hpp>
void lib4::f()
{
boost::throw_exception( lib4::exception() );
}

36
test/lib4_throw.hpp Normal file
View File

@ -0,0 +1,36 @@
#ifndef LIB4_THROW_HPP_INCLUDED
#define LIB4_THROW_HPP_INCLUDED
// Copyright 2018 Peter Dimov
//
// Distributed under the Boost Software License, Version 1.0.
//
// See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt
#include <boost/exception/exception.hpp>
#include <boost/config.hpp>
#include <exception>
#if defined(LIB4_DYN_LINK)
# if defined(LIB4_SOURCE)
# define LIB4_DECL BOOST_SYMBOL_EXPORT
# else
# define LIB4_DECL BOOST_SYMBOL_IMPORT
# endif
#else
# define LIB4_DECL
#endif
namespace lib4
{
struct BOOST_SYMBOL_VISIBLE exception: public std::exception, public boost::exception
{
};
LIB4_DECL void f();
} // namespace lib4
#endif // #ifndef LIB4_THROW_HPP_INCLUDED

14
test/lib5_throw.cpp Normal file
View File

@ -0,0 +1,14 @@
// Copyright 2018 Peter Dimov
//
// Distributed under the Boost Software License, Version 1.0.
//
// See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt
#include "lib5_throw.hpp"
#include <boost/throw_exception.hpp>
void lib5::f()
{
BOOST_THROW_EXCEPTION( lib5::exception() );
}

36
test/lib5_throw.hpp Normal file
View File

@ -0,0 +1,36 @@
#ifndef LIB5_THROW_HPP_INCLUDED
#define LIB5_THROW_HPP_INCLUDED
// Copyright 2018 Peter Dimov
//
// Distributed under the Boost Software License, Version 1.0.
//
// See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt
#include <boost/exception/exception.hpp>
#include <boost/config.hpp>
#include <exception>
#if defined(LIB5_DYN_LINK)
# if defined(LIB5_SOURCE)
# define LIB5_DECL BOOST_SYMBOL_EXPORT
# else
# define LIB5_DECL BOOST_SYMBOL_IMPORT
# endif
#else
# define LIB5_DECL
#endif
namespace lib5
{
struct BOOST_SYMBOL_VISIBLE exception: public std::exception, public boost::exception
{
};
LIB5_DECL void f();
} // namespace lib5
#endif // #ifndef LIB5_THROW_HPP_INCLUDED

View File

@ -26,12 +26,20 @@ class my_exception3: public std::exception, public virtual boost::exception
int main()
{
BOOST_TEST_THROWS( boost::throw_exception( my_exception() ), boost::exception );
BOOST_TEST_THROWS( boost::throw_exception( my_exception() ), my_exception );
BOOST_TEST_THROWS( boost::throw_exception( my_exception2() ), my_exception2 );
BOOST_TEST_THROWS( boost::throw_exception( my_exception2() ), boost::exception );
BOOST_TEST_THROWS( boost::throw_exception( my_exception3() ), my_exception3 );
BOOST_TEST_THROWS( boost::throw_exception( my_exception3() ), boost::exception );
BOOST_TEST_THROWS( BOOST_THROW_EXCEPTION( my_exception() ), boost::exception );
BOOST_TEST_THROWS( BOOST_THROW_EXCEPTION( my_exception() ), my_exception );
BOOST_TEST_THROWS( BOOST_THROW_EXCEPTION( my_exception2() ), my_exception2 );
BOOST_TEST_THROWS( BOOST_THROW_EXCEPTION( my_exception2() ), boost::exception );
BOOST_TEST_THROWS( BOOST_THROW_EXCEPTION( my_exception3() ), my_exception3 );
BOOST_TEST_THROWS( BOOST_THROW_EXCEPTION( my_exception3() ), boost::exception );
return boost::report_errors();

View File

@ -44,7 +44,6 @@ int main()
boost::exception_ptr p = boost::current_exception();
BOOST_TEST_THROWS( boost::rethrow_exception( p ), my_exception );
BOOST_TEST_THROWS( boost::rethrow_exception( p ), boost::exception );
}
try
@ -80,7 +79,6 @@ int main()
boost::exception_ptr p = boost::current_exception();
BOOST_TEST_THROWS( boost::rethrow_exception( p ), my_exception );
BOOST_TEST_THROWS( boost::rethrow_exception( p ), boost::exception );
}
try

View File

@ -9,10 +9,6 @@
#include <boost/exception/get_error_info.hpp>
#include <boost/detail/lightweight_test.hpp>
class my_exception: public std::exception
{
};
class my_exception2: public std::exception, public boost::exception
{
};
@ -25,7 +21,7 @@ int main()
{
try
{
BOOST_THROW_EXCEPTION( my_exception() );
BOOST_THROW_EXCEPTION( my_exception2() );
}
catch( boost::exception const & x )
{
@ -40,7 +36,7 @@ int main()
int const * line = boost::get_error_info<boost::throw_line>( x );
BOOST_TEST( line != 0 );
BOOST_TEST_EQ( *line, 28 );
BOOST_TEST_EQ( *line, 24 );
}
{
@ -50,10 +46,14 @@ int main()
BOOST_TEST_CSTR_EQ( *function, BOOST_CURRENT_FUNCTION );
}
}
catch( ... )
{
BOOST_ERROR( "BOOST_THROW_EXCEPTION( my_exception2() ) didn't throw boost::exception" );
}
try
{
BOOST_THROW_EXCEPTION( my_exception2() );
BOOST_THROW_EXCEPTION( my_exception3() );
}
catch( boost::exception const & x )
{
@ -78,33 +78,9 @@ int main()
BOOST_TEST_CSTR_EQ( *function, BOOST_CURRENT_FUNCTION );
}
}
try
catch( ... )
{
BOOST_THROW_EXCEPTION( my_exception3() );
}
catch( boost::exception const & x )
{
{
char const * const * file = boost::get_error_info<boost::throw_file>( x );
BOOST_TEST( file != 0 );
BOOST_TEST_CSTR_EQ( *file, __FILE__ );
}
{
int const * line = boost::get_error_info<boost::throw_line>( x );
BOOST_TEST( line != 0 );
BOOST_TEST_EQ( *line, 84 );
}
{
char const * const * function = boost::get_error_info<boost::throw_function>( x );
BOOST_TEST( function != 0 );
BOOST_TEST_CSTR_EQ( *function, BOOST_CURRENT_FUNCTION );
}
BOOST_ERROR( "BOOST_THROW_EXCEPTION( my_exception3() ) didn't throw boost::exception" );
}
return boost::report_errors();

View File

@ -0,0 +1,53 @@
// Copyright 2018, 2022 Peter Dimov
// Distributed under the Boost Software License, Version 1.0.
// https://www.boost.org/LICENSE_1_0.txt
#include <boost/throw_exception.hpp>
#include <boost/core/lightweight_test.hpp>
class my_exception: public std::exception
{
};
class my_exception2: public std::exception, public virtual boost::exception
{
};
int main()
{
try
{
BOOST_THROW_EXCEPTION( my_exception() );
}
catch( std::exception const & x )
{
boost::source_location loc = boost::get_throw_location( x );
BOOST_TEST_CSTR_EQ( loc.file_name(), __FILE__ );
BOOST_TEST_EQ( loc.line(), 20 );
BOOST_TEST_CSTR_EQ( loc.function_name(), BOOST_CURRENT_FUNCTION );
}
catch( ... )
{
BOOST_ERROR( "'BOOST_THROW_EXCEPTION( my_exception() )' didn't throw 'std::exception'" );
}
try
{
BOOST_THROW_EXCEPTION( my_exception2() );
}
catch( boost::exception const & x )
{
boost::source_location loc = boost::get_throw_location( x );
BOOST_TEST_CSTR_EQ( loc.file_name(), __FILE__ );
BOOST_TEST_EQ( loc.line(), 37 );
BOOST_TEST_CSTR_EQ( loc.function_name(), BOOST_CURRENT_FUNCTION );
}
catch( ... )
{
BOOST_ERROR( "'BOOST_THROW_EXCEPTION( my_exception2() )' didn't throw 'boost::exception'" );
}
return boost::report_errors();
}

View File

@ -20,6 +20,8 @@
#include "lib1_throw.hpp"
#include "lib2_throw.hpp"
#include "lib3_throw.hpp"
#include "lib4_throw.hpp"
#include "lib5_throw.hpp"
#include <boost/exception/exception.hpp>
#include <boost/exception_ptr.hpp>
#include <boost/exception/get_error_info.hpp>
@ -30,12 +32,14 @@ void test_catch_by_type()
BOOST_TEST_THROWS( lib1::f(), lib1::exception );
BOOST_TEST_THROWS( lib2::f(), lib2::exception );
BOOST_TEST_THROWS( lib3::f(), lib3::exception );
BOOST_TEST_THROWS( lib4::f(), lib4::exception );
BOOST_TEST_THROWS( lib5::f(), lib5::exception );
}
void test_catch_by_exception()
{
BOOST_TEST_THROWS( lib2::f(), boost::exception );
BOOST_TEST_THROWS( lib3::f(), boost::exception );
BOOST_TEST_THROWS( lib4::f(), boost::exception );
BOOST_TEST_THROWS( lib5::f(), boost::exception );
}
void test_exception_ptr()
@ -49,7 +53,6 @@ void test_exception_ptr()
boost::exception_ptr p = boost::current_exception();
BOOST_TEST_THROWS( boost::rethrow_exception( p ), lib2::exception );
BOOST_TEST_THROWS( boost::rethrow_exception( p ), boost::exception );
}
try
@ -61,6 +64,29 @@ void test_exception_ptr()
boost::exception_ptr p = boost::current_exception();
BOOST_TEST_THROWS( boost::rethrow_exception( p ), lib3::exception );
}
try
{
lib4::f();
}
catch( ... )
{
boost::exception_ptr p = boost::current_exception();
BOOST_TEST_THROWS( boost::rethrow_exception( p ), lib4::exception );
BOOST_TEST_THROWS( boost::rethrow_exception( p ), boost::exception );
}
try
{
lib5::f();
}
catch( ... )
{
boost::exception_ptr p = boost::current_exception();
BOOST_TEST_THROWS( boost::rethrow_exception( p ), lib5::exception );
BOOST_TEST_THROWS( boost::rethrow_exception( p ), boost::exception );
}
}
@ -69,7 +95,7 @@ void test_throw_line()
{
try
{
lib3::f();
lib5::f();
}
catch( boost::exception const & x )
{