xref: /llvm-project/clang/test/CoverageMapping/coroutine.cpp (revision 050593fc4f9a7f2b9450ee093c4638b8539315b7)
1 // RUN: %clang_cc1 -mllvm -emptyline-comment-coverage=false -triple x86_64-unknown-linux-gnu -std=c++20 -emit-llvm -fprofile-instrument=clang -fcoverage-mapping -dump-coverage-mapping %s -o - | FileCheck %s
2 
3 namespace std {
4 template <typename... T>
5 struct coroutine_traits;
6 
7 template <class Promise = void>
8 struct coroutine_handle {
9   coroutine_handle() = default;
from_addressstd::coroutine_handle10   static coroutine_handle from_address(void *) noexcept { return {}; }
11 };
12 template <>
13 struct coroutine_handle<void> {
from_addressstd::coroutine_handle14   static coroutine_handle from_address(void *) { return {}; }
15   coroutine_handle() = default;
16   template <class PromiseType>
coroutine_handlestd::coroutine_handle17   coroutine_handle(coroutine_handle<PromiseType>) noexcept {}
18 };
19 } // namespace std
20 
21 struct suspend_always {
22   bool await_ready() noexcept;
23   void await_suspend(std::coroutine_handle<>) noexcept;
24   void await_resume() noexcept;
25 };
26 
27 template <>
28 struct std::coroutine_traits<int, int> {
29   struct promise_type {
30     int get_return_object();
31     suspend_always initial_suspend();
32     suspend_always final_suspend() noexcept;
33     void unhandled_exception() noexcept;
34     void return_value(int);
35     suspend_always yield_value(int);
36   };
37 };
38 
39 // CHECK-LABEL: _Z2f1i:
f1(int x)40 int f1(int x) {       // CHECK-NEXT: File 0, [[@LINE]]:15 -> [[@LINE+8]]:2 = #0
41   if (x > 42) {       // CHECK-NEXT: File 0, [[@LINE]]:7 -> [[@LINE]]:13 = #0
42                       // CHECK-NEXT: Branch,File 0, [[@LINE-1]]:7 -> [[@LINE-1]]:13 = #1, (#0 - #1)
43     ++x;              // CHECK-NEXT: Gap,File 0, [[@LINE-2]]:14 -> [[@LINE-2]]:15 = #1
44   } else {            // CHECK-NEXT: File 0, [[@LINE-3]]:15 -> [[@LINE]]:4 = #1
45     co_return x + 42; // CHECK-NEXT: Gap,File 0, [[@LINE-1]]:4 -> [[@LINE-1]]:10 = (#0 - #1)
46   }                   // CHECK-NEXT: File 0, [[@LINE-2]]:10 -> [[@LINE]]:4 = (#0 - #1)
47   co_return x;        // CHECK-NEXT: Gap,File 0, [[@LINE-1]]:4 -> [[@LINE]]:3 = #1
48 } // CHECK-NEXT: File 0, [[@LINE-1]]:3 -> [[@LINE-1]]:14 = #1
49 
50 // CHECK-LABEL: _Z2f2i:
51 // CHECK-NEXT: File 0, [[@LINE+1]]:15 -> [[@LINE+15]]:2 = #0
f2(int x)52 int f2(int x) {
53 // CHECK-NEXT: File 0, [[@LINE+5]]:13 -> [[@LINE+5]]:18 = #0
54 // CHECK-NEXT: Branch,File 0, [[@LINE+4]]:13 -> [[@LINE+4]]:18 = #1, (#0 - #1)
55 // CHECK-NEXT: Gap,File 0, [[@LINE+3]]:20 -> [[@LINE+3]]:21 = #1
56 // CHECK-NEXT: File 0, [[@LINE+2]]:21 -> [[@LINE+2]]:37 = #1
57 // CHECK-NEXT: File 0, [[@LINE+1]]:40 -> [[@LINE+1]]:56 = (#0 - #1)
58   co_await (x > 0 ? suspend_always{} : suspend_always{});
59 // CHECK-NEXT: File 0, [[@LINE+5]]:12 -> [[@LINE+5]]:17 = #0
60 // CHECK-NEXT: Branch,File 0, [[@LINE+4]]:12 -> [[@LINE+4]]:17 = #2, (#0 - #2)
61 // CHECK-NEXT: Gap,File 0, [[@LINE+3]]:19 -> [[@LINE+3]]:20 = #2
62 // CHECK-NEXT: File 0, [[@LINE+2]]:20 -> [[@LINE+2]]:21 = #2
63 // CHECK-NEXT: File 0, [[@LINE+1]]:24 -> [[@LINE+1]]:25 = (#0 - #2)
64   co_yield x > 0 ? 1 : 2;
65   co_return 0;
66 }
67