1 // RUN: %libomp-cxx20-compile-and-run | FileCheck %s --match-full-lines 2 3 #ifndef HEADER 4 #define HEADER 5 6 #include <cstdlib> 7 #include <cstdarg> 8 #include <cstdio> 9 #include <vector> 10 11 struct Reporter { 12 const char *name; 13 14 Reporter(const char *name) : name(name) { print("ctor"); } 15 16 Reporter() : name("<anon>") { print("ctor"); } 17 18 Reporter(const Reporter &that) : name(that.name) { print("copy ctor"); } 19 20 Reporter(Reporter &&that) : name(that.name) { print("move ctor"); } 21 22 ~Reporter() { print("dtor"); } 23 24 const Reporter &operator=(const Reporter &that) { 25 print("copy assign"); 26 this->name = that.name; 27 return *this; 28 } 29 30 const Reporter &operator=(Reporter &&that) { 31 print("move assign"); 32 this->name = that.name; 33 return *this; 34 } 35 36 struct Iterator { 37 const Reporter *owner; 38 int pos; 39 40 Iterator(const Reporter *owner, int pos) : owner(owner), pos(pos) {} 41 42 Iterator(const Iterator &that) : owner(that.owner), pos(that.pos) { 43 owner->print("iterator copy ctor"); 44 } 45 46 Iterator(Iterator &&that) : owner(that.owner), pos(that.pos) { 47 owner->print("iterator move ctor"); 48 } 49 50 ~Iterator() { owner->print("iterator dtor"); } 51 52 const Iterator &operator=(const Iterator &that) { 53 owner->print("iterator copy assign"); 54 this->owner = that.owner; 55 this->pos = that.pos; 56 return *this; 57 } 58 59 const Iterator &operator=(Iterator &&that) { 60 owner->print("iterator move assign"); 61 this->owner = that.owner; 62 this->pos = that.pos; 63 return *this; 64 } 65 66 bool operator==(const Iterator &that) const { 67 owner->print("iterator %d == %d", 2 - this->pos, 2 - that.pos); 68 return this->pos == that.pos; 69 } 70 71 bool operator!=(const Iterator &that) const { 72 owner->print("iterator %d != %d", 2 - this->pos, 2 - that.pos); 73 return this->pos != that.pos; 74 } 75 76 Iterator &operator++() { 77 owner->print("iterator prefix ++"); 78 pos -= 1; 79 return *this; 80 } 81 82 Iterator operator++(int) { 83 owner->print("iterator postfix ++"); 84 auto result = *this; 85 pos -= 1; 86 return result; 87 } 88 89 int operator*() const { 90 int result = 2 - pos; 91 owner->print("iterator deref: %i", result); 92 return result; 93 } 94 95 size_t operator-(const Iterator &that) const { 96 int result = (2 - this->pos) - (2 - that.pos); 97 owner->print("iterator distance: %d", result); 98 return result; 99 } 100 101 Iterator operator+(int steps) const { 102 owner->print("iterator advance: %i += %i", 2 - this->pos, steps); 103 return Iterator(owner, pos - steps); 104 } 105 }; 106 107 Iterator begin() const { 108 print("begin()"); 109 return Iterator(this, 2); 110 } 111 112 Iterator end() const { 113 print("end()"); 114 return Iterator(this, -1); 115 } 116 117 void print(const char *msg, ...) const { 118 va_list args; 119 va_start(args, msg); 120 printf("[%s] ", name); 121 vprintf(msg, args); 122 printf("\n"); 123 va_end(args); 124 } 125 }; 126 127 int main() { 128 printf("do\n"); 129 Reporter range("range"); 130 #pragma omp reverse 131 for (auto it = range.begin(); it != range.end(); ++it) 132 printf("v=%d\n", *it); 133 printf("done\n"); 134 return EXIT_SUCCESS; 135 } 136 137 #endif /* HEADER */ 138 139 // CHECK: do 140 // CHECK-NEXT: [range] ctor 141 // CHECK-NEXT: [range] begin() 142 // CHECK-NEXT: [range] begin() 143 // CHECK-NEXT: [range] end() 144 // CHECK-NEXT: [range] iterator distance: 3 145 // CHECK-NEXT: [range] iterator advance: 0 += 2 146 // CHECK-NEXT: [range] iterator move assign 147 // CHECK-NEXT: [range] iterator deref: 2 148 // CHECK-NEXT: v=2 149 // CHECK-NEXT: [range] iterator dtor 150 // CHECK-NEXT: [range] iterator advance: 0 += 1 151 // CHECK-NEXT: [range] iterator move assign 152 // CHECK-NEXT: [range] iterator deref: 1 153 // CHECK-NEXT: v=1 154 // CHECK-NEXT: [range] iterator dtor 155 // CHECK-NEXT: [range] iterator advance: 0 += 0 156 // CHECK-NEXT: [range] iterator move assign 157 // CHECK-NEXT: [range] iterator deref: 0 158 // CHECK-NEXT: v=0 159 // CHECK-NEXT: [range] iterator dtor 160 // CHECK-NEXT: [range] iterator dtor 161 // CHECK-NEXT: [range] iterator dtor 162 // CHECK-NEXT: done 163 // CHECK-NEXT: [range] iterator dtor 164 // CHECK-NEXT: [range] dtor 165