1 // Verify that coroutine promise and allocated memory are freed up on exception. 2 // RUN: %clang_cc1 -std=c++1z -fcoroutines-ts -triple=x86_64-unknown-linux-gnu -emit-llvm -o - %s -fexceptions -fcxx-exceptions -disable-llvm-passes | FileCheck %s 3 4 namespace std::experimental { 5 template <typename... T> struct coroutine_traits; 6 7 template <class Promise = void> struct coroutine_handle { 8 coroutine_handle() = default; 9 static coroutine_handle from_address(void *) { return {}; } 10 }; 11 template <> struct coroutine_handle<void> { 12 static coroutine_handle from_address(void *) { return {}; } 13 coroutine_handle() = default; 14 template <class PromiseType> 15 coroutine_handle(coroutine_handle<PromiseType>) {} 16 }; 17 } 18 19 struct suspend_always { 20 bool await_ready(); 21 void await_suspend(std::experimental::coroutine_handle<>); 22 void await_resume(); 23 }; 24 25 template <> struct std::experimental::coroutine_traits<void> { 26 struct promise_type { 27 void get_return_object(); 28 suspend_always initial_suspend(); 29 suspend_always final_suspend(); 30 void return_void(); 31 promise_type(); 32 ~promise_type(); 33 void unhandled_exception(); 34 }; 35 }; 36 37 struct Cleanup { ~Cleanup(); }; 38 void may_throw(); 39 40 // CHECK: define void @_Z1fv( 41 void f() { 42 // CHECK: call i8* @_Znwm(i64 43 44 // If promise constructor throws, check that we free the memory. 45 46 // CHECK: invoke void @_ZNSt12experimental16coroutine_traitsIJvEE12promise_typeC1Ev( 47 // CHECK-NEXT: to label %{{.+}} unwind label %[[DeallocPad:.+]] 48 49 Cleanup cleanup; 50 may_throw(); 51 52 // if may_throw throws, check that we destroy the promise and free the memory. 53 54 // CHECK: invoke void @_Z9may_throwv( 55 // CHECK-NEXT: to label %{{.+}} unwind label %[[PromDtorPad:.+]] 56 57 // CHECK: [[DeallocPad]]: 58 // CHECK-NEXT: landingpad 59 // CHECK-NEXT: cleanup 60 // CHECK: br label %[[Dealloc:.+]] 61 62 // CHECK: [[PromDtorPad]]: 63 // CHECK-NEXT: landingpad 64 // CHECK-NEXT: cleanup 65 // CHECK: call void @_ZN7CleanupD1Ev(%struct.Cleanup* 66 // CHECK: call void @_ZNSt12experimental16coroutine_traitsIJvEE12promise_typeD1Ev( 67 // CHECK: br label %[[Dealloc]] 68 69 // CHECK: [[Dealloc]]: 70 // CHECK: %[[Mem:.+]] = call i8* @llvm.coro.free( 71 // CHECK: call void @_ZdlPv(i8* %[[Mem]]) 72 73 co_return; 74 } 75