Debian Codegraph @

gcc/testsuite/g++.dg/coroutines/pr100772-b.C

raw
1 // { dg-skip-if "requires hosted libstdc++ for cstdio" { ! hostedlib } }
2 #if !__has_include(<coroutine>) \
3 && __has_include(<experimental/coroutine>) // for __clang__
4 #include <experimental/coroutine>
5 namespace std {
6 using namespace std::experimental;
7 }
8 #else
9 #include <coroutine>
10 #endif
11 #include <cstdio>
12 #include <typeinfo>
13 #include <cxxabi.h> // needed for abi::__cxa_demangle
14 #include <memory>
15
16 std::shared_ptr<char> cppDemangle(const char *abiName)
17 {
18 int status;
19 char *ret = abi::__cxa_demangle(abiName, 0, 0, &status);
20
21 /* NOTE: must free() the returned char when done with it! */
22 std::shared_ptr<char> retval;
23 retval.reset( (char *)ret, [](char *mem) { if (mem) free((void*)mem); } );
24 return retval;
25 }
26
27 template <typename T>
28 struct Id{};
29 struct Task
30 {
31 struct promise_type
32 {
33 void return_void() const noexcept {}
34
35 static void is_int (std::string x) {
36 if (x != "Id<int>")
37 abort() ;
38 }
39 template <typename ... Args>
40 void* operator new(std::size_t len, Args ...args) noexcept
41 {
42 (is_int (cppDemangle(typeid(Id<Args>).name()).get()), ...);
43 (std::puts (cppDemangle(typeid(Id<Args>).name()).get()), ...);
44 return nullptr;
45 }
46
47 static Task get_return_object_on_allocation_failure() noexcept
48 {
49 return {};
50 }
51
52 Task get_return_object() noexcept
53 {
54 return Task{ *this };
55 }
56
57 std::suspend_always initial_suspend() noexcept
58 {
59 return {};
60 }
61
62 std::suspend_always final_suspend() noexcept
63 {
64 return {};
65 }
66
67 void unhandled_exception() noexcept {}
68 };
69
70 using promise_handle = std::coroutine_handle<promise_type>;
71
72 Task() = default;
73 Task(promise_type & promise) noexcept
74 : m_handle{ promise_handle::from_promise(promise) }
75 {}
76
77 ~Task()
78 {
79 if (m_handle.address()) { m_handle.destroy(); }
80 }
81
82 promise_handle m_handle{};
83 };
84
85
86 Task Foo(auto && ... args) noexcept
87 {
88 co_return;
89 }
90
91 int main()
92 {
93 int v;
94 Foo(v, 2134);
95 }