1 // Verify that coroutine promise and allocated memory are freed up on exception.
2 // RUN: %clang_cc1 -std=c++20 -triple=x86_64-unknown-linux-gnu -emit-llvm -o - %s -fexceptions -fcxx-exceptions -disable-llvm-passes | FileCheck %s
5 template <typename
... T
> struct coroutine_traits
;
7 template <class Promise
= void> struct coroutine_handle
{
8 coroutine_handle() = default;
9 static coroutine_handle
from_address(void *) noexcept
;
11 template <> struct coroutine_handle
<void> {
12 static coroutine_handle
from_address(void *) noexcept
;
13 coroutine_handle() = default;
14 template <class PromiseType
>
15 coroutine_handle(coroutine_handle
<PromiseType
>) noexcept
;
19 struct suspend_always
{
20 bool await_ready() noexcept
;
21 void await_suspend(std::coroutine_handle
<>) noexcept
;
22 void await_resume() noexcept
;
25 template <> struct std::coroutine_traits
<void> {
27 void get_return_object() noexcept
;
28 suspend_always
initial_suspend() noexcept
;
29 suspend_always
final_suspend() noexcept
;
30 void return_void() noexcept
;
33 void unhandled_exception() noexcept
;
37 struct Cleanup
{ ~Cleanup(); };
40 // CHECK-LABEL: define{{.*}} void @_Z1fv(
42 // CHECK: call noalias noundef nonnull ptr @_Znwm(i64
44 // If promise constructor throws, check that we free the memory.
46 // CHECK: invoke void @_ZNSt16coroutine_traitsIJvEE12promise_typeC1Ev(
47 // CHECK-NEXT: to label %{{.+}} unwind label %[[DeallocPad:.+]]
49 // CHECK: [[DeallocPad]]:
50 // CHECK-NEXT: landingpad
51 // CHECK-NEXT: cleanup
52 // CHECK: br label %[[Dealloc:.+]]
57 // if may_throw throws, check that we destroy the promise and free the memory.
59 // CHECK: invoke void @_Z9may_throwv(
60 // CHECK-NEXT: to label %{{.+}} unwind label %[[CatchPad:.+]]
62 // CHECK: [[CatchPad]]:
63 // CHECK-NEXT: landingpad
64 // CHECK-NEXT: catch ptr null
65 // CHECK: call void @_ZN7CleanupD1Ev(
66 // CHECK: br label %[[Catch:.+]]
69 // CHECK: call ptr @__cxa_begin_catch(
70 // CHECK: call void @_ZNSt16coroutine_traitsIJvEE12promise_type19unhandled_exceptionEv(
71 // CHECK: invoke void @__cxa_end_catch()
72 // CHECK-NEXT: to label %[[Cont:.+]] unwind
75 // CHECK-NEXT: br label %[[Cont2:.+]]
77 // CHECK-NEXT: br label %[[Cleanup:.+]]
79 // CHECK: [[Cleanup]]:
80 // CHECK: call void @_ZNSt16coroutine_traitsIJvEE12promise_typeD1Ev(
81 // CHECK: %[[Mem0:.+]] = call ptr @llvm.coro.free(
82 // CHECK: call void @_ZdlPv(ptr noundef %[[Mem0]]
84 // CHECK: [[Dealloc]]:
85 // CHECK: %[[Mem:.+]] = call ptr @llvm.coro.free(
86 // CHECK: call void @_ZdlPv(ptr noundef %[[Mem]])
91 // CHECK-LABEL: define{{.*}} void @_Z1gv(
94 co_await suspend_always
{};
95 // Since this is the endless loop there should be no fallthrough handler (call to 'return_void').
96 // CHECK-NOT: call void @_ZNSt16coroutine_traitsIJvEE12promise_type11return_voidEv