Debian Codegraph @

gcc/testsuite/g++.dg/coroutines/range-for2.C

raw
1 // PR c++/118574
2 // { dg-do run }
3 // { dg-additional-options "-std=c++23 -O2" }
4
5 #include <coroutine>
6
7 [[gnu::noipa]] void
8 baz (int *)
9 {
10 }
11
12 struct D {
13 D () : d (new int (42)) {}
14 ~D () { if (*d != 42) __builtin_abort (); *d = 0; baz (d); delete d; }
15 int *d;
16 };
17
18 struct E {
19 E (const D &x) : e (x) {}
20 void test () const { if (*e.d != 42) __builtin_abort (); }
21 ~E () { test (); }
22 const D &e;
23 };
24
25 struct A {
26 const char **a = nullptr;
27 int n = 0;
28 const E *e1 = nullptr;
29 const E *e2 = nullptr;
30 void test () const { if (e1) e1->test (); if (e2) e2->test (); }
31 void push_back (const char *x) { test (); if (!a) a = new const char *[2]; a[n++] = x; }
32 const char **begin () const { test (); return a; }
33 const char **end () const { test (); return a + n; }
34 ~A () { test (); delete[] a; }
35 };
36
37 struct B {
38 long ns;
39 bool await_ready () const noexcept { return false; }
40 void await_suspend (std::coroutine_handle<> h) const noexcept {
41 volatile int v = 0;
42 while (v < ns)
43 v = v + 1;
44 h.resume ();
45 }
46 void await_resume () const noexcept {}
47 };
48
49 struct C {
50 struct promise_type {
51 const char *value;
52 std::suspend_never initial_suspend () { return {}; }
53 std::suspend_always final_suspend () noexcept { return {}; }
54 void return_value (const char *v) { value = v; }
55 void unhandled_exception () { __builtin_abort (); }
56 C get_return_object () { return C{this}; }
57 };
58 promise_type *p;
59 explicit C (promise_type *p) : p(p) {}
60 const char *get () { return p->value; }
61 };
62
63 A
64 foo (const E &e1, const E &e2)
65 {
66 A a;
67 a.e1 = &e1;
68 a.e2 = &e2;
69 a.push_back ("foo");
70 a.push_back ("bar");
71 return a;
72 }
73
74 C
75 bar ()
76 {
77 A ret;
78 for (const auto &item : foo (E{D {}}, E{D {}}))
79 {
80 co_await B{200000};
81 ret.push_back (item);
82 }
83 co_return "foobar";
84 }
85
86 int
87 main ()
88 {
89 auto task = bar ();
90 if (__builtin_strcmp (task.get (), "foobar"))
91 __builtin_abort ();
92 }