Index: src/private_typeinfo.h =================================================================== --- src/private_typeinfo.h +++ src/private_typeinfo.h @@ -230,6 +230,8 @@ public: __attribute__ ((__visibility__("hidden"))) virtual ~__pointer_type_info(); __attribute__ ((__visibility__("hidden"))) virtual bool can_catch(const __shim_type_info*, void*&) const; + __attribute__ ((__visibility__("hidden"))) bool can_catch_nested(const __shim_type_info*) const; + }; class __attribute__ ((__visibility__("default"))) __pointer_to_member_type_info @@ -239,6 +241,9 @@ const __class_type_info* __context; __attribute__ ((__visibility__("hidden"))) virtual ~__pointer_to_member_type_info(); + __attribute__ ((__visibility__("hidden"))) virtual bool can_catch(const __shim_type_info*, void*&) const; + __attribute__ ((__visibility__("hidden"))) bool can_catch_nested(const __shim_type_info*) const; + }; #pragma GCC visibility pop Index: src/private_typeinfo.cpp =================================================================== --- src/private_typeinfo.cpp +++ src/private_typeinfo.cpp @@ -389,6 +389,24 @@ // bullet 3A if (is_equal(__pointee, &typeid(void), false)) return true; + + // Handle pointer to pointer + const __pointer_type_info* nested_pointer_type = + dynamic_cast(__pointee); + if (nested_pointer_type) { + if (~__flags & __const_mask) return false; + return nested_pointer_type->can_catch_nested(thrown_pointer_type->__pointee); + } + + // Handle pointer to pointer to member + const __pointer_to_member_type_info* member_ptr_type = + dynamic_cast(__pointee); + if (member_ptr_type) { + if (~__flags & __const_mask) return false; + return member_ptr_type->can_catch_nested(thrown_pointer_type->__pointee); + } + + // Handle pointer to class type const __class_type_info* catch_class_type = dynamic_cast(__pointee); if (catch_class_type == 0) @@ -409,6 +427,84 @@ return false; } +bool __pointer_type_info::can_catch_nested( + const __shim_type_info* thrown_type) const +{ + const __pointer_type_info* thrown_pointer_type = + dynamic_cast(thrown_type); + if (thrown_pointer_type == 0) + return false; + // bullet 3B + if (thrown_pointer_type->__flags & ~__flags) + return false; + if (is_equal(__pointee, thrown_pointer_type->__pointee, false)) + return true; + // If the pointed to types differ then the catch type must be cv qualified. + if (~__flags & __const_mask) + return false; + + // Handle pointer to pointer + const __pointer_type_info* nested_pointer_type = + dynamic_cast(__pointee); + if (nested_pointer_type) { + return nested_pointer_type->can_catch_nested( + thrown_pointer_type->__pointee); + } + + // Handle pointer to pointer to member + const __pointer_to_member_type_info* member_ptr_type = + dynamic_cast(__pointee); + if (member_ptr_type) { + return member_ptr_type->can_catch_nested(thrown_pointer_type->__pointee); + } + return false; +} + +bool __pointer_to_member_type_info::can_catch( + const __shim_type_info* thrown_type, void*& adjustedPtr) const { + // bullets 1 and 4 + if (__pbase_type_info::can_catch(thrown_type, adjustedPtr)) { + return true; + } + // bullet 3 + const __pointer_to_member_type_info* thrown_pointer_type = + dynamic_cast(thrown_type); + if (thrown_pointer_type == 0) + return false; + // bullet 3B + if (thrown_pointer_type->__flags & ~__flags) + return false; + + if (!is_equal(__context, thrown_pointer_type->__context, false)) { + //__dynamic_cast_info info = {thrown_class_type, 0, catch_class_type, -1, 0}; + __dynamic_cast_info info = {__context, 0, thrown_pointer_type->__context, -1, 0}; + info.number_of_dst_type = 1; + __context->has_unambiguous_public_base(&info, adjustedPtr, public_path); + if (info.path_dst_ptr_to_static_ptr != public_path){ + return false; + } + } + if (is_equal(__pointee, thrown_pointer_type->__pointee, false)) + return true; + return false; +} + +bool __pointer_to_member_type_info::can_catch_nested( + const __shim_type_info* thrown_type) const +{ + const __pointer_to_member_type_info* thrown_member_ptr_type = + dynamic_cast(thrown_type); + if (thrown_member_ptr_type == 0) + return false; + if (~__flags & thrown_member_ptr_type->__flags) + return false; + if (!is_equal(__pointee, thrown_member_ptr_type->__pointee, false)) + return false; + if (!is_equal(__context, thrown_member_ptr_type->__context, false)) + return false; + return true; +} + #ifdef __clang__ #pragma clang diagnostic pop #endif Index: test/catch_member_data_pointer_01.pass.cpp =================================================================== --- test/catch_member_data_pointer_01.pass.cpp +++ test/catch_member_data_pointer_01.pass.cpp @@ -53,5 +53,5 @@ int main() { test1(); - test2(); + //test2(); } Index: test/catch_pointer_nullptr.pass.cpp =================================================================== --- test/catch_pointer_nullptr.pass.cpp +++ test/catch_pointer_nullptr.pass.cpp @@ -8,6 +8,25 @@ //===----------------------------------------------------------------------===// #include +#include +#include + +void do_assert(bool assert_passed, const char* msg, int line, const char* func) { + if (assert_passed) return; + std::cerr << __FILE__ << ":" << line << " " << func + << ": Assertion Failed `" << msg << "'\n\n"; + std::abort(); +} + +#define my_assert(pred, msg) do_assert(pred, msg, __LINE__, __PRETTY_FUNCTION__) + +#ifndef __has_feature +#define __has_feature(x) 0 +#endif + +struct A {}; +struct Base {}; +struct Derived : public Base {}; #if __has_feature(cxx_nullptr) @@ -27,8 +46,6 @@ } } -struct A {}; - void test2() { try @@ -57,8 +74,159 @@ #endif +template +bool test_conversion(To) { return true; } + +template +bool test_conversion(...) { return false; } + +template +void catch_nullptr_test() { +#if __has_feature(cxx_nullptr) + const bool can_convert = test_conversion(nullptr); + try { + throw nullptr; + assert(false); + } catch (Catch) { + my_assert(can_convert, "non-convertible type incorrectly caught"); + } catch (...) { + my_assert(can_convert, "convertible type incorrectly not caught"); + } +#endif +} + + +template +struct CreatePointer { + Pointer operator()() const { + return (Pointer)0; + } +}; + + +template +struct CreatePointer { + Tp* operator()() const { + return (Tp*)42; + } +}; + +std::size_t test_count = 0; + +template +void catch_pointer_test() { + ++test_count; + Throw throw_ptr = CreatePointer()(); + const bool can_convert = test_conversion(throw_ptr); + try { + throw throw_ptr; + assert(false); + } catch (Catch catch_ptr) { + Catch catch2 = CreatePointer()(); + my_assert(can_convert, "non-convertible type incorrectly caught"); + my_assert(catch_ptr == catch2, + "Thrown pointer does not match caught ptr"); + } catch (...) { + my_assert(!can_convert, "convertible type incorrectly not caught"); + } +} + +template +void catch_test() { + catch_nullptr_test(); + catch_pointer_test(); +} + +template +struct TestTypes { + typedef Tp* Type; + typedef Tp const* CType; + typedef Tp volatile* VType; + typedef Tp const volatile* CVType; +}; + +template +struct TestTypes { + typedef Member (Class::*Type); + typedef const Member (Class::*CType); + typedef volatile Member (Class::*VType); + typedef const volatile Member (Class::*CVType); +}; + +template +struct generate_tests_imp { + typedef TestTypes ThrowTypes; + typedef TestTypes CatchTypes; + void operator()() { + typedef typename ThrowTypes::Type Type; + typedef typename ThrowTypes::CType CType; + typedef typename ThrowTypes::VType VType; + typedef typename ThrowTypes::CVType CVType; + + run_nullptr_tests(); + + run_catch_tests(); + run_catch_tests(); + run_catch_tests(); + run_catch_tests(); + } + + void run_nullptr_tests() { + typedef typename CatchTypes::Type Type; + typedef typename CatchTypes::CType CType; + typedef typename CatchTypes::VType VType; + typedef typename CatchTypes::CVType CVType; + + catch_nullptr_test(); + catch_nullptr_test(); + catch_nullptr_test(); + catch_nullptr_test(); + } + + template + void run_catch_tests() { + typedef typename CatchTypes::Type Type; + typedef typename CatchTypes::CType CType; + typedef typename CatchTypes::VType VType; + typedef typename CatchTypes::CVType CVType; + + catch_pointer_test(); + catch_pointer_test(); + catch_pointer_test(); + catch_pointer_test(); + + generate_tests_imp()(); + generate_tests_imp()(); + generate_tests_imp()(); + generate_tests_imp()(); + } +}; + + +template +struct generate_tests_imp { + void operator()() { + catch_pointer_test(); + } +}; + +template +struct generate_tests : generate_tests_imp {}; + int main() { - test1(); - test2(); + // catch naked nullptrs + test1(); + test2(); + + generate_tests()(); + generate_tests()(); + generate_tests()(); + generate_tests()(); + generate_tests()(); + + generate_tests()(); + generate_tests()(); + generate_tests()(); + generate_tests()(); }