1 // Test the behavior of http://wg21.link/P0664, a proposal to catch any
2 // exceptions thrown after the initial suspend point of a coroutine by
3 // executing the handler specified by the promise type's 'unhandled_exception'
4 // member function.
5 //
6 // RUN: %clang_cc1 -std=c++14 -fcoroutines-ts \
7 // RUN:   -triple=x86_64-unknown-linux-gnu -emit-llvm -o - %s \
8 // RUN:   -fexceptions -fcxx-exceptions -disable-llvm-passes \
9 // RUN:   | FileCheck %s
10 
11 #include "Inputs/coroutine.h"
12 
13 namespace coro = std::experimental::coroutines_v1;
14 
15 struct throwing_awaitable {
await_readythrowing_awaitable16   bool await_ready() { return true; }
await_suspendthrowing_awaitable17   void await_suspend(coro::coroutine_handle<>) {}
await_resumethrowing_awaitable18   void await_resume() { throw 42; }
19 };
20 
21 struct throwing_task {
22   struct promise_type {
get_return_objectthrowing_task::promise_type23     auto get_return_object() { return throwing_task{}; }
initial_suspendthrowing_task::promise_type24     auto initial_suspend() { return throwing_awaitable{}; }
final_suspendthrowing_task::promise_type25     auto final_suspend() noexcept { return coro::suspend_never{}; }
return_voidthrowing_task::promise_type26     void return_void() {}
unhandled_exceptionthrowing_task::promise_type27     void unhandled_exception() {}
28   };
29 };
30 
31 // CHECK-LABEL: define{{.*}} void @_Z1fv()
f()32 throwing_task f() {
33   // A variable RESUMETHREW is used to keep track of whether the body
34   // of 'await_resume' threw an exception. Exceptions thrown in
35   // 'await_resume' are unwound to RESUMELPAD.
36   // CHECK: init.ready:
37   // CHECK-NEXT: store i1 true, i1* %[[RESUMETHREW:.+]], align 1
38   // CHECK-NEXT: invoke void @_ZN18throwing_awaitable12await_resumeEv
39   // CHECK-NEXT: to label %[[RESUMECONT:.+]] unwind label %[[RESUMELPAD:.+]]
40 
41   // If 'await_resume' does not throw an exception, 'false' is stored in
42   // variable RESUMETHREW.
43   // CHECK: [[RESUMECONT]]:
44   // CHECK-NEXT: store i1 false, i1* %[[RESUMETHREW]]
45   // CHECK-NEXT: br label %[[RESUMETRYCONT:.+]]
46 
47   // 'unhandled_exception' is called for the exception thrown in
48   // 'await_resume'. The variable RESUMETHREW is never set to false,
49   // and a jump is made to RESUMETRYCONT.
50   // CHECK: [[RESUMELPAD]]:
51   // CHECK: br label %[[RESUMECATCH:.+]]
52   // CHECK: [[RESUMECATCH]]:
53   // CHECK: invoke void @_ZN13throwing_task12promise_type19unhandled_exceptionEv
54   // CHECK-NEXT: to label %[[RESUMEENDCATCH:.+]] unwind label
55   // CHECK: [[RESUMEENDCATCH]]:
56   // CHECK-NEXT: invoke void @__cxa_end_catch()
57   // CHECK-NEXT: to label %[[RESUMEENDCATCHCONT:.+]] unwind label
58   // CHECK: [[RESUMEENDCATCHCONT]]:
59   // CHECK-NEXT: br label %[[RESUMETRYCONT]]
60   // CHECK: [[RESUMETRYCONT]]:
61   // CHECK-NEXT: br label %[[CLEANUP:.+]]
62   // CHECK: [[CLEANUP]]:
63   // CHECK: switch i32 %{{.+}}, label %{{.+}} [
64   // CHECK-NEXT: i32 0, label %[[CLEANUPCONT:.+]]
65   // CHECK-NEXT: ]
66 
67   // The variable RESUMETHREW is loaded and if true, then 'await_resume'
68   // threw an exception and the coroutine body is skipped, and the final
69   // suspend is executed immediately. Otherwise, the coroutine body is
70   // executed, and then the final suspend.
71   // CHECK: [[CLEANUPCONT]]:
72   // CHECK-NEXT: %[[RESUMETHREWLOAD:.+]] = load i1, i1* %[[RESUMETHREW]]
73   // CHECK-NEXT: br i1 %[[RESUMETHREWLOAD]], label %[[RESUMEDCONT:.+]], label %[[RESUMEDBODY:.+]]
74 
75   // CHECK: [[RESUMEDBODY]]:
76   // CHECK: invoke void @_ZN13throwing_task12promise_type11return_voidEv
77   // CHECK-NEXT: to label %[[REDUMEDBODYCONT:.+]] unwind label
78   // CHECK: [[REDUMEDBODYCONT]]:
79   // CHECK-NEXT: br label %[[COROFINAL:.+]]
80 
81   // CHECK: [[RESUMEDCONT]]:
82   // CHECK-NEXT: br label %[[COROFINAL]]
83 
84   // CHECK: [[COROFINAL]]:
85   // CHECK: call void @_ZN13throwing_task12promise_type13final_suspendEv
86   co_return;
87 }
88 
89 struct noexcept_awaitable {
await_readynoexcept_awaitable90   bool await_ready() { return true; }
await_suspendnoexcept_awaitable91   void await_suspend(coro::coroutine_handle<>) {}
await_resumenoexcept_awaitable92   void await_resume() noexcept {}
93 };
94 
95 struct noexcept_task {
96   struct promise_type {
get_return_objectnoexcept_task::promise_type97     auto get_return_object() { return noexcept_task{}; }
initial_suspendnoexcept_task::promise_type98     auto initial_suspend() { return noexcept_awaitable{}; }
final_suspendnoexcept_task::promise_type99     auto final_suspend() noexcept { return coro::suspend_never{}; }
return_voidnoexcept_task::promise_type100     void return_void() {}
unhandled_exceptionnoexcept_task::promise_type101     void unhandled_exception() {}
102   };
103 };
104 
105 // CHECK-LABEL: define{{.*}} void @_Z1gv()
g()106 noexcept_task g() {
107   // If the await_resume function is marked as noexcept, none of the additional
108   // conditions that are present in f() above are added to the IR.
109   // This means that no i1 are stored before or after calling await_resume:
110   // CHECK: init.ready:
111   // CHECK-NEXT: call void @_ZN18noexcept_awaitable12await_resumeEv
112   // CHECK-NOT: store i1 false, i1*
113   co_return;
114 }
115