Debian Codegraph @

libgomp/testsuite/libgomp.c++/range-for-1.C

raw
1 // P2718R0 - Wording for P2644R1 Fix for Range-based for Loop
2 // { dg-do run }
3 // { dg-additional-options "-std=c++17" }
4 // { dg-require-effective-target tls_runtime }
5
6 #ifndef RANGE_FOR_EXT_TEMPS
7 #define RANGE_FOR_EXT_TEMPS (__cpp_range_based_for >= 202211L)
8 #endif
9
10 extern "C" void abort ();
11 void check (bool);
12
13 struct S
14 {
15 S () { ++s; }
16 S (const S &) { ++s; }
17 ~S () { check (true); --s; }
18 [[omp::decl (threadprivate)]] static int s;
19 };
20 int S::s;
21 S sv;
22 struct T
23 {
24 T (const S &, const S &) { ++t; }
25 T (const T &) { ++t; }
26 ~T () { check (false); --t; }
27 [[omp::decl (threadprivate)]] static int t;
28 };
29 int T::t;
30 T tv (sv, sv);
31 int a[4];
32 int c;
33
34 void
35 check (bool is_s)
36 {
37 if (c)
38 {
39 if (is_s)
40 {
41 if (T::t != (c == 1))
42 abort ();
43 }
44 else
45 {
46 if (S::s != (c == 1 ? 0 : 2))
47 abort ();
48 }
49 }
50 }
51
52 template <typename T>
53 int *
54 begin (const T &)
55 {
56 return &a[0];
57 }
58
59 template <typename T>
60 int *
61 end (const T &)
62 {
63 return &a[4];
64 }
65
66 const S &
67 foo (const S &)
68 {
69 return sv;
70 }
71
72 const T &
73 foo (const T &)
74 {
75 return tv;
76 }
77
78 void
79 bar ()
80 {
81 #pragma omp parallel num_threads (4)
82 {
83 if (S::s != 0)
84 abort ();
85 #pragma omp for
86 for (auto x : S ())
87 {
88 if (S::s != 1)
89 abort ();
90 }
91 if (S::s != 0)
92 abort ();
93 #pragma omp for
94 for (auto x : foo (S ()))
95 {
96 if (S::s != RANGE_FOR_EXT_TEMPS)
97 abort ();
98 }
99 if (S::s != 0)
100 abort ();
101 if (T::t != 0)
102 abort ();
103 }
104 c = 1 + RANGE_FOR_EXT_TEMPS;
105 #pragma omp parallel num_threads (4)
106 {
107 #pragma omp for
108 for (auto x : T (S (), S ()))
109 {
110 if (S::s != 2 * RANGE_FOR_EXT_TEMPS || T::t != 1)
111 abort ();
112 }
113 if (S::s != 0 || T::t != 0)
114 abort ();
115 }
116 c = 2;
117 #pragma omp parallel num_threads (4)
118 {
119 #pragma omp for
120 for (auto x : foo (T (S (), S ())))
121 {
122 if (S::s != 2 * RANGE_FOR_EXT_TEMPS
123 || T::t != RANGE_FOR_EXT_TEMPS)
124 abort ();
125 }
126 if (S::s != 0 || T::t != 0)
127 abort ();
128 }
129 c = 0;
130 }
131
132 template <int N>
133 void
134 baz ()
135 {
136 #pragma omp parallel num_threads (4)
137 {
138 if (S::s != 0)
139 abort ();
140 #pragma omp for
141 for (auto x : S ())
142 {
143 if (S::s != 1)
144 abort ();
145 }
146 if (S::s != 0)
147 abort ();
148 #pragma omp for
149 for (auto x : foo (S ()))
150 {
151 if (S::s != RANGE_FOR_EXT_TEMPS)
152 abort ();
153 }
154 if (S::s != 0)
155 abort ();
156 if (T::t != 0)
157 abort ();
158 }
159 c = 1 + RANGE_FOR_EXT_TEMPS;
160 #pragma omp parallel num_threads (4)
161 {
162 #pragma omp for
163 for (auto x : T (S (), S ()))
164 {
165 if (S::s != 2 * RANGE_FOR_EXT_TEMPS || T::t != 1)
166 abort ();
167 }
168 if (S::s != 0 || T::t != 0)
169 abort ();
170 }
171 c = 2;
172 #pragma omp parallel num_threads (4)
173 {
174 #pragma omp for
175 for (auto x : foo (T (S (), S ())))
176 {
177 if (S::s != 2 * RANGE_FOR_EXT_TEMPS
178 || T::t != RANGE_FOR_EXT_TEMPS)
179 abort ();
180 }
181 if (S::s != 0 || T::t != 0)
182 abort ();
183 }
184 c = 0;
185 }
186
187 template <typename S, typename T>
188 void
189 qux ()
190 {
191 #pragma omp parallel num_threads (4)
192 {
193 if (S::s != 0)
194 abort ();
195 #pragma omp for
196 for (auto x : S ())
197 {
198 if (S::s != 1)
199 abort ();
200 }
201 if (S::s != 0)
202 abort ();
203 #pragma omp for
204 for (auto x : foo (S ()))
205 {
206 if (S::s != RANGE_FOR_EXT_TEMPS)
207 abort ();
208 }
209 if (S::s != 0)
210 abort ();
211 if (T::t != 0)
212 abort ();
213 }
214 c = 1 + RANGE_FOR_EXT_TEMPS;
215 #pragma omp parallel num_threads (4)
216 {
217 #pragma omp for
218 for (auto x : T (S (), S ()))
219 {
220 if (S::s != 2 * RANGE_FOR_EXT_TEMPS || T::t != 1)
221 abort ();
222 }
223 if (S::s != 0 || T::t != 0)
224 abort ();
225 }
226 c = 2;
227 #pragma omp parallel num_threads (4)
228 {
229 #pragma omp for
230 for (auto x : foo (T (S (), S ())))
231 {
232 if (S::s != 2 * RANGE_FOR_EXT_TEMPS
233 || T::t != RANGE_FOR_EXT_TEMPS)
234 abort ();
235 }
236 if (S::s != 0 || T::t != 0)
237 abort ();
238 }
239 c = 0;
240 }
241
242 int
243 main ()
244 {
245 S::s--;
246 T::t--;
247 bar ();
248 baz <0> ();
249 qux <S, T> ();
250 }