| 1 |
#include <coroutine> |
| 2 |
#include <exception> |
| 3 |
#include <cstdio> |
| 4 |
#include <cstdlib> |
| 5 |
|
| 6 |
#ifndef OUTPUT |
| 7 |
# define PRINT(X) |
| 8 |
# define PRINTF(...) |
| 9 |
#else |
| 10 |
//#include <iostream> |
| 11 |
# define PRINT(X) puts(X) |
| 12 |
//# define PRINT(X) std::cout << (X) << std::endl |
| 13 |
# define PRINTF(...) printf(__VA_ARGS__) |
| 14 |
#endif |
| 15 |
|
| 16 |
int frame_live = 0; |
| 17 |
int promise_live = 0; |
| 18 |
int Y_live = 0; |
| 19 |
int task_live = 0; |
| 20 |
int unhandled_exception_called = 0; |
| 21 |
|
| 22 |
struct X {}; |
| 23 |
|
| 24 |
struct Y |
| 25 |
{ |
| 26 |
Y () { |
| 27 |
PRINT("Y ()"); |
| 28 |
Y_live++; |
| 29 |
} |
| 30 |
Y (const Y&) { |
| 31 |
PRINT("Y (const Y&)"); |
| 32 |
Y_live++; |
| 33 |
} |
| 34 |
~Y () { |
| 35 |
PRINT("~Y ()"); |
| 36 |
--Y_live; |
| 37 |
} |
| 38 |
}; |
| 39 |
|
| 40 |
struct task { |
| 41 |
struct promise_type; |
| 42 |
using coro_handle = std::coroutine_handle<promise_type>; |
| 43 |
struct Z { |
| 44 |
std::exception_ptr eptr; |
| 45 |
coro_handle h; |
| 46 |
}; |
| 47 |
struct promise_type { |
| 48 |
void* operator new(size_t sz) { |
| 49 |
PRINT("operator new()"); |
| 50 |
frame_live++; |
| 51 |
return ::operator new(sz); |
| 52 |
} |
| 53 |
|
| 54 |
static void operator delete(void* p, size_t sz) { |
| 55 |
PRINT("operator delete"); |
| 56 |
--frame_live; |
| 57 |
::operator delete(p, sz); |
| 58 |
} |
| 59 |
|
| 60 |
promise_type() { |
| 61 |
PRINT("promise_type()"); |
| 62 |
#ifdef PROMISE_CTOR_THROWS |
| 63 |
throw X{}; |
| 64 |
#endif |
| 65 |
promise_live = true; |
| 66 |
} |
| 67 |
|
| 68 |
~promise_type() { |
| 69 |
PRINT("~promise_type()"); |
| 70 |
--promise_live; |
| 71 |
} |
| 72 |
|
| 73 |
struct awaiter { |
| 74 |
bool await_ready() { |
| 75 |
PRINT("initial await_ready()"); |
| 76 |
#ifdef INITIAL_AWAIT_READY_THROWS |
| 77 |
throw X{}; |
| 78 |
#endif |
| 79 |
// If we are testing the suspend infra, then suspend, otherwise continue |
| 80 |
// so that we can test later paths (effectively, suspend_never). |
| 81 |
#ifdef INITIAL_AWAIT_SUSPEND_THROWS |
| 82 |
return false; |
| 83 |
#else |
| 84 |
return true; |
| 85 |
#endif |
| 86 |
} |
| 87 |
void await_suspend(std::coroutine_handle<>) { |
| 88 |
#ifdef INITIAL_AWAIT_SUSPEND_THROWS |
| 89 |
throw X{}; |
| 90 |
#endif |
| 91 |
} |
| 92 |
void await_resume() { |
| 93 |
PRINT("initial await_resume()"); |
| 94 |
#ifdef INITIAL_AWAIT_RESUME_THROWS |
| 95 |
// This should be caught by unhandled_exception () |
| 96 |
throw X{}; |
| 97 |
#endif |
| 98 |
} |
| 99 |
}; |
| 100 |
|
| 101 |
awaiter initial_suspend() { |
| 102 |
PRINT("initial_suspend()"); |
| 103 |
#ifdef INITIAL_SUSPEND_THROWS |
| 104 |
throw X{}; |
| 105 |
#endif |
| 106 |
return {}; |
| 107 |
} |
| 108 |
|
| 109 |
task get_return_object() { |
| 110 |
PRINT("get_return_object()"); |
| 111 |
#ifdef GET_RETURN_OBJECT_THROWS |
| 112 |
throw X{}; |
| 113 |
#endif |
| 114 |
return task{}; |
| 115 |
} |
| 116 |
|
| 117 |
std::suspend_never final_suspend() noexcept { |
| 118 |
PRINT("final_suspend()"); |
| 119 |
return {}; |
| 120 |
} |
| 121 |
void return_void() { |
| 122 |
PRINT("return_void()"); |
| 123 |
} |
| 124 |
void unhandled_exception() { |
| 125 |
PRINT("unhandled_exception()"); |
| 126 |
unhandled_exception_called++; |
| 127 |
#ifdef UNHANDLED_EXCEPTION_THROWS |
| 128 |
Z z{std::current_exception(),coro_handle::from_promise(*this)}; |
| 129 |
throw z; |
| 130 |
//throw coro_handle::from_promise(*this); |
| 131 |
#endif |
| 132 |
} |
| 133 |
}; |
| 134 |
|
| 135 |
task() noexcept { |
| 136 |
PRINT("task()"); |
| 137 |
task_live++; |
| 138 |
} |
| 139 |
task(task&& t) noexcept { |
| 140 |
PRINT("task(task&&)"); |
| 141 |
task_live++; |
| 142 |
} |
| 143 |
~task() noexcept { |
| 144 |
PRINT("~task()"); |
| 145 |
task_live--; |
| 146 |
} |
| 147 |
|
| 148 |
}; |
| 149 |
|
| 150 |
task f(Y Val __attribute__((__unused__))) { |
| 151 |
PRINT("f()"); |
| 152 |
#ifdef BODY_THROWS |
| 153 |
throw X{}; |
| 154 |
#endif |
| 155 |
co_return; |
| 156 |
} |
| 157 |
|
| 158 |
int main() { |
| 159 |
bool failed = false; |
| 160 |
Y Val; |
| 161 |
try { |
| 162 |
PRINT("try-block-s"); |
| 163 |
task tsk = f(Val); |
| 164 |
PRINT("try-block-e"); |
| 165 |
} catch (X) { |
| 166 |
PRINT("caught X"); |
| 167 |
} catch (task::Z& z) { |
| 168 |
PRINTF("caught Z : %d\n",(bool)z.eptr); |
| 169 |
if (z.h.done()) |
| 170 |
{ |
| 171 |
PRINT("done, cleaning up"); |
| 172 |
z.h.destroy (); |
| 173 |
} |
| 174 |
else |
| 175 |
PRINT("not done?"); |
| 176 |
} catch (std::coroutine_handle<task::promise_type>& h) { |
| 177 |
PRINT("caught handle"); |
| 178 |
if (h.done()) |
| 179 |
{ |
| 180 |
PRINT("done, cleaning up"); |
| 181 |
h.destroy (); |
| 182 |
} |
| 183 |
else |
| 184 |
PRINT("not done?"); |
| 185 |
} |
| 186 |
|
| 187 |
if (task_live) |
| 188 |
failed = true, __builtin_printf("task still live\n") ; |
| 189 |
if (promise_live) |
| 190 |
failed = true, __builtin_printf("promise still live\n"); |
| 191 |
if (frame_live) |
| 192 |
failed = true, __builtin_printf("frame still live : %d\n", frame_live); |
| 193 |
if (Y_live != 1) |
| 194 |
failed = true, __builtin_printf("Y live count : %d\n", Y_live); |
| 195 |
#ifdef SHOULD_CALL_UNHANDLED_EXCEPTION |
| 196 |
if (unhandled_exception_called != 1) |
| 197 |
failed = true, __builtin_printf("unhandled exception : %d\n", unhandled_exception_called); |
| 198 |
#else |
| 199 |
if (unhandled_exception_called != 0) |
| 200 |
failed = true, __builtin_printf("unhandled exception : %d\n", unhandled_exception_called); |
| 201 |
#endif |
| 202 |
|
| 203 |
__builtin_printf("checking result : %s \n",(failed?"bad":"good")); |
| 204 |
|
| 205 |
fflush(stdout); |
| 206 |
|
| 207 |
if (failed) |
| 208 |
__builtin_abort(); |
| 209 |
} |