[clang] Implement lifetime analysis for lifetime_capture_by(X) (#115921)
[llvm-project.git] / clang / test / SemaCXX / warn-unsequenced-coro.cpp
blob56d2edcf301552b940d5de2ff0466f17cd1326b8
1 // RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -fsyntax-only -verify -std=c++20 -I%S/Inputs -Wno-unused -Wno-uninitialized -Wunsequenced %s
3 // expected-no-diagnostics
5 #include "std-coroutine.h"
7 typedef __PTRDIFF_TYPE__ ptrdiff_t;
9 using namespace std;
11 template<class T>
12 struct Task {
13 struct promise_type {
14 Task<T> get_return_object() noexcept;
15 suspend_always initial_suspend() noexcept;
16 suspend_always final_suspend() noexcept;
17 void return_value(T);
18 void unhandled_exception();
19 auto yield_value(Task<T>) noexcept { return final_suspend(); }
21 bool await_ready() noexcept { return false; }
22 void await_suspend(coroutine_handle<>) noexcept {}
23 T await_resume();
26 template<>
27 struct Task<void> {
28 struct promise_type {
29 Task<void> get_return_object() noexcept;
30 suspend_always initial_suspend() noexcept;
31 suspend_always final_suspend() noexcept;
32 void return_void() noexcept;
33 void unhandled_exception() noexcept;
34 auto yield_value(Task<void>) noexcept { return final_suspend(); }
36 bool await_ready() noexcept { return false; }
37 void await_suspend(coroutine_handle<>) noexcept {}
38 void await_resume() noexcept {}
41 template <typename T>
42 class generator
44 struct Promise
46 auto get_return_object() { return generator{*this}; }
47 auto initial_suspend() { return suspend_never{}; }
48 auto final_suspend() noexcept { return suspend_always{}; }
49 void unhandled_exception() {}
50 void return_void() {}
52 auto yield_value(T value)
54 value_ = std::move(value);
55 return suspend_always{};
58 T value_;
61 using Handle = coroutine_handle<Promise>;
63 struct sentinel{};
64 struct iterator
66 using iterator_category = input_iterator_tag;
67 using value_type = T;
68 using difference_type = ptrdiff_t;
69 using reference = T &;
70 using const_reference = const T &;
71 using pointer = T *;
73 iterator &operator++()
75 h_.resume();
76 return *this;
78 const_reference &operator*() const { return h_.promise().value_; }
79 bool operator!=(sentinel) { return !h_.done(); }
81 Handle h_;
84 explicit generator(Promise &p) : h_(Handle::from_promise(p)) {}
85 Handle h_;
86 public:
87 using promise_type = Promise;
88 auto begin() { return iterator{h_}; }
89 auto end() { return sentinel{}; }
92 Task<void> c(int i) {
93 co_await (i = 0, std::suspend_always{});
96 generator<int> range(int start, int end)
98 while (start < end)
99 co_yield start++;
102 Task<int> go(int const& val);
103 Task<int> go1(int x) {
104 co_return co_await go(++x);