From cddcf8734ed06ada9384a461bc21d58b44f6eba1 Mon Sep 17 00:00:00 2001 From: David Chisnall Date: Tue, 20 Mar 2012 16:56:14 +0000 Subject: Rework exception matching. - Remove typeinfo since thing break if the compiler decides to use the system one and merge its contents into typeinfo.h - Make each type_info object have a vtable with the same layout as the public vtable in libstdc++'s . This fixes code (e.g. libobjc2's Objective-C++ exception handling) which rely on being able to add new types. - Add some extra tests I suspect exceptions catching pointer-to-member types will not work correctly, but I've never seen anyone do this and don't have any tests for it. --- test/test_exception.cc | 36 +++++++++++++++++++++++++++++++++--- test/test_typeinfo.cc | 3 +++ 2 files changed, 36 insertions(+), 3 deletions(-) (limited to 'test') diff --git a/test/test_exception.cc b/test/test_exception.cc index 6e16ca9..0685329 100644 --- a/test/test_exception.cc +++ b/test/test_exception.cc @@ -9,7 +9,7 @@ void log(void* ignored) { - printf("Cleanup called on %s\n", *(char**)ignored); + //printf("Cleanup called on %s\n", *(char**)ignored); } #define CLEANUP\ __attribute__((cleanup(log))) __attribute__((unused))\ @@ -81,6 +81,35 @@ int outer(int i) throw(float, int, foo, non_pod) return 1; } +static void test_const(void) +{ + int a = 1; + try + { + throw a; + } + catch (const int b) + { + TEST(a == b, "Caught int as const int"); + } + catch(...) + { + TEST(0, "Failed to catch int as const int"); + } + try + { + throw &a; + } + catch (const int *b) + { + TEST(&a == b, "Caught int* as const int*"); + } + catch(...) + { + TEST(0, "Failed to catch int* as const int*"); + } +} + static void test_catch(int s) { cl c; @@ -92,7 +121,7 @@ static void test_catch(int s) } catch(int i) { - fprintf(stderr, "Caught int %d!\n", i); + fprintf(stderr, "Caught int %d in test %d\n", i, s); TEST((s == 0 && i == 1) || (s == 2 && i == 0), "Caught int"); return; } @@ -158,7 +187,7 @@ static int violations = 0; static void throw_zero() { violations++; -fprintf(stderr, "Throwing 0\n"); + fprintf(stderr, "Throwing 0\n"); throw 0; } @@ -207,6 +236,7 @@ void test_exceptions(void) { TEST(0, "Bad cast was not caught correctly"); } + test_const(); //printf("Test: %s\n", diff --git a/test/test_typeinfo.cc b/test/test_typeinfo.cc index 6d2fbdd..9fc8ef7 100644 --- a/test/test_typeinfo.cc +++ b/test/test_typeinfo.cc @@ -79,6 +79,7 @@ void test_type_info(void) Root *b2 = &root; Root *v1 = &virt1; Virt1 *d1 = ⋄ + Root *up = ⋄ b->test = 12; f->test = 12; b2->test = 12; @@ -103,6 +104,8 @@ void test_type_info(void) d2->test = 12; TEST(12 == dynamic_cast(d2)->test, "Casting Diamond2 to Diamond2"); TEST(12 == dynamic_cast(d2)->test, "Casting Diamond2 to Virt2a"); + TEST(&diamond == dynamic_cast(up), "Downcasting root-pointer to diamond"); + TEST(0 == dynamic_cast(&root), "Downcasting root to diamond"); TEST(0 == dynamic_cast(b2), "Casting Root to Sub1 (0 expected)"); } -- cgit v1.2.3