Debian Codegraph @

gcc/testsuite/g++.dg/coroutines/torture/local-var-05-awaitable.C

raw
1 // { dg-do run }
2 // { dg-skip-if "requires hosted libstdc++ for vector" { ! hostedlib } }
3
4 // Test the case where the awaitables are local vars, and therefore already
5 // have a frame representation - and should not be copied to a second frame
6 // entry (since elision of that copy would break the assumptions made in the
7 // management of the lifetime of the awaitable).
8
9 #include "../coro.h"
10 #include <vector>
11
12 // boiler-plate for tests of codegen
13 #include "../coro1-ret-int-yield-int.h"
14
15 /* Make a non-trivial awaitable. */
16 struct Awaitable
17 {
18 int v;
19 std::vector<int> x;
20 Awaitable () : v(0), x(1,0) {PRINTF ("Awaitable()\n");}
21 Awaitable (int _v) : v(_v), x(1,_v) {PRINTF ("Awaitable(%d)\n",_v);}
22
23 bool await_ready () { return false; }
24 void await_suspend(coro::coroutine_handle<>) {}
25 int await_resume() { return v + x[0];}
26
27 ~Awaitable () {PRINTF ("~Awaitable(%d)\n",v);}
28 };
29
30 coro1
31 my_coro (int start) noexcept
32 {
33 PRINT ("my_coro");
34 Awaitable aw0 = Awaitable (start);
35 Awaitable aw1 = Awaitable (4);
36 Awaitable aw2 = Awaitable (10);
37
38 int sum;
39 /* We are started with a suspend_always init suspend expr. */
40 sum = co_await aw0;
41 PRINT ("my_coro 1");
42 sum += co_await aw1;
43 PRINT ("my_coro 2");
44 sum += co_await aw2;
45 PRINT ("my_coro 3");
46
47 co_return sum;
48 }
49
50 int main ()
51 {
52 PRINT ("main: create my_coro");
53 struct coro1 x = my_coro (7);
54 PRINT ("main: ramp done, resuming init suspend");
55 if (x.handle.done())
56 abort();
57 x.handle.resume();
58
59 // now do the three co_awaits.
60 while(!x.handle.done())
61 x.handle.resume();
62 PRINT ("main: after resuming 3 co_awaits");
63
64 /* Now we should have the co_returned value. */
65 int y = x.handle.promise().get_value();
66 if (y != 42)
67 {
68 PRINTF ("main: wrong result (%d).", y);
69 abort ();
70 }
71
72 PRINT ("main: returning");
73 return 0;
74 }