[docs] Add LICENSE.txt to the root of the mono-repo
[llvm-project.git] / clang / test / CodeGenCoroutines / coro-symmetric-transfer-03.cpp
blob33384b2f4839eda184db0ac0bb63c3c0d6a1618a
1 // This tests that the symmetric transfer at the final suspend point could happen successfully.
2 // RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -std=c++20 -O2 -emit-llvm %s -o - | FileCheck %s
4 #include "Inputs/coroutine.h"
6 struct Task {
7 struct promise_type {
8 struct FinalAwaiter {
9 bool await_ready() const noexcept { return false; }
10 template <typename PromiseType>
11 std::coroutine_handle<> await_suspend(std::coroutine_handle<PromiseType> h) noexcept {
12 return h.promise().continuation;
14 void await_resume() noexcept {}
16 Task get_return_object() noexcept {
17 return std::coroutine_handle<promise_type>::from_promise(*this);
19 std::suspend_always initial_suspend() noexcept { return {}; }
20 FinalAwaiter final_suspend() noexcept { return {}; }
21 void unhandled_exception() noexcept {}
22 void return_value(int x) noexcept {
23 _value = x;
25 std::coroutine_handle<> continuation;
26 int _value;
29 Task(std::coroutine_handle<promise_type> handle) : handle(handle) {}
30 ~Task() {
31 if (handle)
32 handle.destroy();
35 struct Awaiter {
36 std::coroutine_handle<promise_type> handle;
37 Awaiter(std::coroutine_handle<promise_type> handle) : handle(handle) {}
38 bool await_ready() const noexcept { return false; }
39 std::coroutine_handle<void> await_suspend(std::coroutine_handle<void> continuation) noexcept {
40 handle.promise().continuation = continuation;
41 return handle;
43 int await_resume() noexcept {
44 int ret = handle.promise()._value;
45 handle.destroy();
46 return ret;
50 auto operator co_await() {
51 auto handle_ = handle;
52 handle = nullptr;
53 return Awaiter(handle_);
56 private:
57 std::coroutine_handle<promise_type> handle;
60 Task task0() {
61 co_return 43;
64 // CHECK-LABEL: define{{.*}} void @_Z5task0v.resume
65 // This checks we are still in the scope of the current function.
66 // CHECK-NOT: {{^}}}
67 // CHECK: musttail call fastcc void
68 // CHECK-NEXT: ret void