| 1 |
// { dg-do run } |
| 2 |
// { dg-skip-if "requires hosted libstdc++ for cstdlib abort" { ! hostedlib } } |
| 3 |
|
| 4 |
// Test exceptions. |
| 5 |
|
| 6 |
#include "../coro.h" |
| 7 |
#include <exception> |
| 8 |
|
| 9 |
int gX = 0; |
| 10 |
|
| 11 |
struct coro1 { |
| 12 |
struct promise_type; |
| 13 |
using handle_type = coro::coroutine_handle<coro1::promise_type>; |
| 14 |
handle_type handle; |
| 15 |
coro1 () : handle(0) {} |
| 16 |
coro1 (handle_type _handle) |
| 17 |
: handle(_handle) { |
| 18 |
PRINT("Created coro1 object from handle"); |
| 19 |
} |
| 20 |
coro1 (const coro1 &) = delete; // no copying |
| 21 |
coro1 (coro1 &&s) : handle(s.handle) { |
| 22 |
s.handle = nullptr; |
| 23 |
PRINT("coro1 mv ctor "); |
| 24 |
} |
| 25 |
coro1 &operator = (coro1 &&s) { |
| 26 |
handle = s.handle; |
| 27 |
s.handle = nullptr; |
| 28 |
PRINT("coro1 op= "); |
| 29 |
return *this; |
| 30 |
} |
| 31 |
~coro1() { |
| 32 |
PRINT("Destroyed coro1"); |
| 33 |
if ( handle ) |
| 34 |
handle.destroy(); |
| 35 |
} |
| 36 |
|
| 37 |
struct suspend_never_prt { |
| 38 |
bool await_ready() const noexcept { return true; } |
| 39 |
void await_suspend(handle_type) const noexcept { PRINT ("susp-never-susp"); } |
| 40 |
void await_resume() const noexcept { PRINT ("susp-never-resume");} |
| 41 |
}; |
| 42 |
|
| 43 |
/* NOTE: this has a DTOR to test that pathway. */ |
| 44 |
struct suspend_always_prt { |
| 45 |
bool await_ready() const noexcept { return false; } |
| 46 |
void await_suspend(handle_type) const noexcept { PRINT ("susp-always-susp"); } |
| 47 |
void await_resume() const noexcept { PRINT ("susp-always-resume"); } |
| 48 |
~suspend_always_prt() { PRINT ("susp-always-DTOR"); } |
| 49 |
}; |
| 50 |
|
| 51 |
struct promise_type { |
| 52 |
int value; |
| 53 |
promise_type() { PRINT ("Created Promise"); } |
| 54 |
~promise_type() { PRINT ("Destroyed Promise"); } |
| 55 |
|
| 56 |
auto get_return_object () { |
| 57 |
PRINT ("get_return_object: handle from promise"); |
| 58 |
return handle_type::from_promise (*this); |
| 59 |
} |
| 60 |
auto initial_suspend () { |
| 61 |
PRINT ("get initial_suspend (always)"); |
| 62 |
return suspend_always_prt{}; |
| 63 |
} |
| 64 |
auto final_suspend () noexcept { |
| 65 |
PRINT ("get final_suspend (always)"); |
| 66 |
return suspend_always_prt{}; |
| 67 |
} |
| 68 |
void return_value (int v) { |
| 69 |
PRINTF ("return_value () %d\n",v); |
| 70 |
value = v; |
| 71 |
} |
| 72 |
auto yield_value (int v) { |
| 73 |
PRINTF ("yield_value () %d and suspend always\n",v); |
| 74 |
value = v; |
| 75 |
return suspend_always_prt{}; |
| 76 |
} |
| 77 |
/* Some non-matching overloads. */ |
| 78 |
auto yield_value (suspend_always_prt s, int x) { |
| 79 |
return s; |
| 80 |
} |
| 81 |
auto yield_value (void) { |
| 82 |
return 42;//suspend_always_prt{}; |
| 83 |
} |
| 84 |
int get_value (void) { return value; } |
| 85 |
|
| 86 |
void unhandled_exception() { |
| 87 |
PRINT ("unhandled_exception: caught one!"); |
| 88 |
gX = -1; |
| 89 |
// returning from here should end up in final_suspend. |
| 90 |
} |
| 91 |
}; |
| 92 |
}; |
| 93 |
|
| 94 |
// So we want to check that the internal behaviour of try/catch is |
| 95 |
// working OK - and that if we have an unhandled exception it is caught |
| 96 |
// by the wrapper that we add to the rewritten func. |
| 97 |
|
| 98 |
struct coro1 throw_and_catch () noexcept |
| 99 |
{ |
| 100 |
int caught = 0; |
| 101 |
|
| 102 |
try { |
| 103 |
PRINT ("f: about to yield 42"); |
| 104 |
co_yield 42; |
| 105 |
|
| 106 |
throw (20); |
| 107 |
|
| 108 |
PRINT ("f: about to yield 6174"); |
| 109 |
co_return 6174; |
| 110 |
|
| 111 |
} catch (int x) { |
| 112 |
PRINTF ("f: caught %d\n", x); |
| 113 |
caught = x; |
| 114 |
} |
| 115 |
|
| 116 |
PRINTF ("f: about to yield what we caught %d\n", caught); |
| 117 |
co_yield caught; |
| 118 |
|
| 119 |
throw ("bah"); |
| 120 |
|
| 121 |
PRINT ("f: about to return 22"); |
| 122 |
co_return 22; |
| 123 |
} |
| 124 |
|
| 125 |
int main () |
| 126 |
{ |
| 127 |
PRINT ("main: create coro1"); |
| 128 |
struct coro1 x = throw_and_catch (); |
| 129 |
if (x.handle.done()) |
| 130 |
abort(); |
| 131 |
x.handle.resume(); |
| 132 |
PRINT ("main: got coro, resuming.."); |
| 133 |
int y = x.handle.promise().get_value(); |
| 134 |
if ( y != 42 ) |
| 135 |
abort (); |
| 136 |
PRINT ("main: apparently got the expected 42"); |
| 137 |
if (x.handle.done()) |
| 138 |
abort(); |
| 139 |
PRINT ("main: resuming..."); |
| 140 |
x.handle.resume(); |
| 141 |
|
| 142 |
y = x.handle.promise().get_value(); |
| 143 |
if ( y != 20 ) |
| 144 |
abort (); |
| 145 |
PRINT ("main: apparently got 20, which we expected"); |
| 146 |
if (x.handle.done()) |
| 147 |
abort(); |
| 148 |
|
| 149 |
PRINT ("main: resuming..."); |
| 150 |
x.handle.resume(); |
| 151 |
// This should cause the throw of "bah" which is unhandled. |
| 152 |
// We should catch the unhandled exception and then fall through |
| 153 |
// to the final suspend point... thus be "done". |
| 154 |
if (!x.handle.done()) |
| 155 |
{ |
| 156 |
PRINT ("main: apparently not done..."); |
| 157 |
abort (); |
| 158 |
} |
| 159 |
// When we caught the unhandled exception we flagged it instead of |
| 160 |
// std::terminate-ing. |
| 161 |
if (gX != -1) |
| 162 |
{ |
| 163 |
PRINT ("main: apparently failed to catch"); |
| 164 |
abort (); |
| 165 |
} |
| 166 |
PRINT ("main: returning"); |
| 167 |
return 0; |
| 168 |
} |