1 // RUN: %clang_cc1 -fsyntax-only -fopenmp=libiomp5 -x c++ -std=c++11 -fexceptions -fcxx-exceptions -verify %s
2
3 class S {
4 int a;
S()5 S() : a(0) {}
6
7 public:
S(int v)8 S(int v) : a(v) {}
S(const S & s)9 S(const S &s) : a(s.a) {}
10 };
11
12 static int sii;
13 #pragma omp threadprivate(sii) // expected-note {{defined as threadprivate or thread local}}
14 static int globalii;
15
test_iteration_spaces()16 int test_iteration_spaces() {
17 const int N = 100;
18 float a[N], b[N], c[N];
19 int ii, jj, kk;
20 float fii;
21 double dii;
22 #pragma omp parallel
23 #pragma omp for
24 for (int i = 0; i < 10; i += 1) {
25 c[i] = a[i] + b[i];
26 }
27 #pragma omp parallel
28 #pragma omp for
29 for (char i = 0; i < 10; i++) {
30 c[i] = a[i] + b[i];
31 }
32 #pragma omp parallel
33 #pragma omp for
34 for (char i = 0; i < 10; i += '\1') {
35 c[i] = a[i] + b[i];
36 }
37 #pragma omp parallel
38 #pragma omp for
39 for (long long i = 0; i < 10; i++) {
40 c[i] = a[i] + b[i];
41 }
42 #pragma omp parallel
43 // expected-error@+2 {{expression must have integral or unscoped enumeration type, not 'double'}}
44 #pragma omp for
45 for (long long i = 0; i < 10; i += 1.5) {
46 c[i] = a[i] + b[i];
47 }
48 #pragma omp parallel
49 #pragma omp for
50 for (long long i = 0; i < 'z'; i += 1u) {
51 c[i] = a[i] + b[i];
52 }
53 #pragma omp parallel
54 // expected-error@+2 {{variable must be of integer or random access iterator type}}
55 #pragma omp for
56 for (float fi = 0; fi < 10.0; fi++) {
57 c[(int)fi] = a[(int)fi] + b[(int)fi];
58 }
59 #pragma omp parallel
60 // expected-error@+2 {{variable must be of integer or random access iterator type}}
61 #pragma omp for
62 for (double fi = 0; fi < 10.0; fi++) {
63 c[(int)fi] = a[(int)fi] + b[(int)fi];
64 }
65 #pragma omp parallel
66 // expected-error@+2 {{variable must be of integer or random access iterator type}}
67 #pragma omp for
68 for (int &ref = ii; ref < 10; ref++) {
69 }
70 #pragma omp parallel
71 // expected-error@+2 {{initialization clause of OpenMP for loop must be of the form 'var = init' or 'T var = init'}}
72 #pragma omp for
73 for (int i; i < 10; i++)
74 c[i] = a[i];
75
76 #pragma omp parallel
77 // expected-error@+2 {{initialization clause of OpenMP for loop must be of the form 'var = init' or 'T var = init'}}
78 #pragma omp for
79 for (int i = 0, j = 0; i < 10; ++i)
80 c[i] = a[i];
81
82 #pragma omp parallel
83 // expected-error@+2 {{initialization clause of OpenMP for loop must be of the form 'var = init' or 'T var = init'}}
84 #pragma omp for
85 for (; ii < 10; ++ii)
86 c[ii] = a[ii];
87
88 #pragma omp parallel
89 // expected-warning@+3 {{expression result unused}}
90 // expected-error@+2 {{initialization clause of OpenMP for loop must be of the form 'var = init' or 'T var = init'}}
91 #pragma omp for
92 for (ii + 1; ii < 10; ++ii)
93 c[ii] = a[ii];
94
95 #pragma omp parallel
96 // expected-error@+2 {{initialization clause of OpenMP for loop must be of the form 'var = init' or 'T var = init'}}
97 #pragma omp for
98 for (c[ii] = 0; ii < 10; ++ii)
99 c[ii] = a[ii];
100
101 #pragma omp parallel
102 // Ok to skip parenthesises.
103 #pragma omp for
104 for (((ii)) = 0; ii < 10; ++ii)
105 c[ii] = a[ii];
106
107 #pragma omp parallel
108 // expected-error@+2 {{condition of OpenMP for loop must be a relational comparison ('<', '<=', '>', or '>=') of loop variable 'i'}}
109 #pragma omp for
110 for (int i = 0; i; i++)
111 c[i] = a[i];
112
113 #pragma omp parallel
114 // expected-error@+3 {{condition of OpenMP for loop must be a relational comparison ('<', '<=', '>', or '>=') of loop variable 'i'}}
115 // expected-error@+2 {{increment clause of OpenMP for loop must perform simple addition or subtraction on loop variable 'i'}}
116 #pragma omp for
117 for (int i = 0; jj < kk; ii++)
118 c[i] = a[i];
119
120 #pragma omp parallel
121 // expected-error@+2 {{condition of OpenMP for loop must be a relational comparison ('<', '<=', '>', or '>=') of loop variable 'i'}}
122 #pragma omp for
123 for (int i = 0; !!i; i++)
124 c[i] = a[i];
125
126 #pragma omp parallel
127 // expected-error@+2 {{condition of OpenMP for loop must be a relational comparison ('<', '<=', '>', or '>=') of loop variable 'i'}}
128 #pragma omp for
129 for (int i = 0; i != 1; i++)
130 c[i] = a[i];
131
132 #pragma omp parallel
133 // expected-error@+2 {{condition of OpenMP for loop must be a relational comparison ('<', '<=', '>', or '>=') of loop variable 'i'}}
134 #pragma omp for
135 for (int i = 0;; i++)
136 c[i] = a[i];
137
138 #pragma omp parallel
139 // Ok.
140 #pragma omp for
141 for (int i = 11; i > 10; i--)
142 c[i] = a[i];
143
144 #pragma omp parallel
145 // Ok.
146 #pragma omp for
147 for (int i = 0; i < 10; ++i)
148 c[i] = a[i];
149
150 #pragma omp parallel
151 // Ok.
152 #pragma omp for
153 for (ii = 0; ii < 10; ++ii)
154 c[ii] = a[ii];
155
156 #pragma omp parallel
157 // expected-error@+2 {{increment clause of OpenMP for loop must perform simple addition or subtraction on loop variable 'ii'}}
158 #pragma omp for
159 for (ii = 0; ii < 10; ++jj)
160 c[ii] = a[jj];
161
162 #pragma omp parallel
163 // expected-error@+2 {{increment clause of OpenMP for loop must perform simple addition or subtraction on loop variable 'ii'}}
164 #pragma omp for
165 for (ii = 0; ii < 10; ++++ii)
166 c[ii] = a[ii];
167
168 #pragma omp parallel
169 // Ok but undefined behavior (in general, cannot check that incr
170 // is really loop-invariant).
171 #pragma omp for
172 for (ii = 0; ii < 10; ii = ii + ii)
173 c[ii] = a[ii];
174
175 #pragma omp parallel
176 // expected-error@+2 {{expression must have integral or unscoped enumeration type, not 'float'}}
177 #pragma omp for
178 for (ii = 0; ii < 10; ii = ii + 1.0f)
179 c[ii] = a[ii];
180
181 #pragma omp parallel
182 // Ok - step was converted to integer type.
183 #pragma omp for
184 for (ii = 0; ii < 10; ii = ii + (int)1.1f)
185 c[ii] = a[ii];
186
187 #pragma omp parallel
188 // expected-error@+2 {{increment clause of OpenMP for loop must perform simple addition or subtraction on loop variable 'ii'}}
189 #pragma omp for
190 for (ii = 0; ii < 10; jj = ii + 2)
191 c[ii] = a[ii];
192
193 #pragma omp parallel
194 // expected-warning@+3 {{relational comparison result unused}}
195 // expected-error@+2 {{increment clause of OpenMP for loop must perform simple addition or subtraction on loop variable 'ii'}}
196 #pragma omp for
197 for (ii = 0; ii<10; jj> kk + 2)
198 c[ii] = a[ii];
199
200 #pragma omp parallel
201 // expected-error@+2 {{increment clause of OpenMP for loop must perform simple addition or subtraction on loop variable 'ii'}}
202 #pragma omp for
203 for (ii = 0; ii < 10;)
204 c[ii] = a[ii];
205
206 #pragma omp parallel
207 // expected-warning@+3 {{expression result unused}}
208 // expected-error@+2 {{increment clause of OpenMP for loop must perform simple addition or subtraction on loop variable 'ii'}}
209 #pragma omp for
210 for (ii = 0; ii < 10; !ii)
211 c[ii] = a[ii];
212
213 #pragma omp parallel
214 // expected-error@+2 {{increment clause of OpenMP for loop must perform simple addition or subtraction on loop variable 'ii'}}
215 #pragma omp for
216 for (ii = 0; ii < 10; ii ? ++ii : ++jj)
217 c[ii] = a[ii];
218
219 #pragma omp parallel
220 // expected-error@+2 {{increment clause of OpenMP for loop must perform simple addition or subtraction on loop variable 'ii'}}
221 #pragma omp for
222 for (ii = 0; ii < 10; ii = ii < 10)
223 c[ii] = a[ii];
224
225 #pragma omp parallel
226 // expected-note@+3 {{loop step is expected to be positive due to this condition}}
227 // expected-error@+2 {{increment expression must cause 'ii' to increase on each iteration of OpenMP for loop}}
228 #pragma omp for
229 for (ii = 0; ii < 10; ii = ii + 0)
230 c[ii] = a[ii];
231
232 #pragma omp parallel
233 // expected-note@+3 {{loop step is expected to be positive due to this condition}}
234 // expected-error@+2 {{increment expression must cause 'ii' to increase on each iteration of OpenMP for loop}}
235 #pragma omp for
236 for (ii = 0; ii < 10; ii = ii + (int)(0.8 - 0.45))
237 c[ii] = a[ii];
238
239 #pragma omp parallel
240 // expected-note@+3 {{loop step is expected to be positive due to this condition}}
241 // expected-error@+2 {{increment expression must cause 'ii' to increase on each iteration of OpenMP for loop}}
242 #pragma omp for
243 for (ii = 0; (ii) < 10; ii -= 25)
244 c[ii] = a[ii];
245
246 #pragma omp parallel
247 // expected-note@+3 {{loop step is expected to be positive due to this condition}}
248 // expected-error@+2 {{increment expression must cause 'ii' to increase on each iteration of OpenMP for loop}}
249 #pragma omp for
250 for (ii = 0; (ii < 10); ii -= 0)
251 c[ii] = a[ii];
252
253 #pragma omp parallel
254 // expected-note@+3 {{loop step is expected to be negative due to this condition}}
255 // expected-error@+2 {{increment expression must cause 'ii' to decrease on each iteration of OpenMP for loop}}
256 #pragma omp for
257 for (ii = 0; ii > 10; (ii += 0))
258 c[ii] = a[ii];
259
260 #pragma omp parallel
261 // expected-note@+3 {{loop step is expected to be positive due to this condition}}
262 // expected-error@+2 {{increment expression must cause 'ii' to increase on each iteration of OpenMP for loop}}
263 #pragma omp for
264 for (ii = 0; ii < 10; (ii) = (1 - 1) + (ii))
265 c[ii] = a[ii];
266
267 #pragma omp parallel
268 // expected-note@+3 {{loop step is expected to be negative due to this condition}}
269 // expected-error@+2 {{increment expression must cause 'ii' to decrease on each iteration of OpenMP for loop}}
270 #pragma omp for
271 for ((ii = 0); ii > 10; (ii -= 0))
272 c[ii] = a[ii];
273
274 #pragma omp parallel
275 // expected-note@+3 {{loop step is expected to be positive due to this condition}}
276 // expected-error@+2 {{increment expression must cause 'ii' to increase on each iteration of OpenMP for loop}}
277 #pragma omp for
278 for (ii = 0; (ii < 10); (ii -= 0))
279 c[ii] = a[ii];
280
281 #pragma omp parallel
282 // expected-note@+2 {{defined as firstprivate}}
283 // expected-error@+2 {{loop iteration variable in the associated loop of 'omp for' directive may not be firstprivate, predetermined as private}}
284 #pragma omp for firstprivate(ii)
285 for (ii = 0; ii < 10; ii++)
286 c[ii] = a[ii];
287
288 #pragma omp parallel
289 // expected-error@+3 {{unexpected OpenMP clause 'linear' in directive '#pragma omp for'}}
290 // expected-note@+2 {{defined as linear}}
291 // expected-error@+2 {{loop iteration variable in the associated loop of 'omp for' directive may not be linear, predetermined as private}}
292 #pragma omp for linear(ii)
293 for (ii = 0; ii < 10; ii++)
294 c[ii] = a[ii];
295
296 #pragma omp parallel
297 #pragma omp for private(ii)
298 for (ii = 0; ii < 10; ii++)
299 c[ii] = a[ii];
300
301 #pragma omp parallel
302 #pragma omp for lastprivate(ii)
303 for (ii = 0; ii < 10; ii++)
304 c[ii] = a[ii];
305
306 #pragma omp parallel
307 {
308 // expected-error@+2 {{loop iteration variable in the associated loop of 'omp for' directive may not be threadprivate or thread local, predetermined as private}}
309 #pragma omp for
310 for (sii = 0; sii < 10; sii += 1)
311 c[sii] = a[sii];
312 }
313
314 #pragma omp parallel
315 {
316 // expected-error@+2 {{loop iteration variable in the associated loop of 'omp for' directive may not be a variable with global storage without being explicitly marked as private}}
317 #pragma omp for
318 for (globalii = 0; globalii < 10; globalii += 1)
319 c[globalii] = a[globalii];
320 }
321
322 #pragma omp parallel
323 {
324 // expected-error@+3 {{loop iteration variable in the associated loop of 'omp for' directive may not be a variable with global storage without being explicitly marked as private}}
325 #pragma omp for collapse(2)
326 for (ii = 0; ii < 10; ii += 1)
327 for (globalii = 0; globalii < 10; globalii += 1)
328 c[globalii] += a[globalii] + ii;
329 }
330
331 #pragma omp parallel
332 // expected-error@+2 {{statement after '#pragma omp for' must be a for loop}}
333 #pragma omp for
334 for (auto &item : a) {
335 item = item + 1;
336 }
337
338 #pragma omp parallel
339 // expected-note@+3 {{loop step is expected to be positive due to this condition}}
340 // expected-error@+2 {{increment expression must cause 'i' to increase on each iteration of OpenMP for loop}}
341 #pragma omp for
342 for (unsigned i = 9; i < 10; i--) {
343 c[i] = a[i] + b[i];
344 }
345
346 int(*lb)[4] = nullptr;
347 #pragma omp parallel
348 #pragma omp for
349 for (int(*p)[4] = lb; p < lb + 8; ++p) {
350 }
351
352 #pragma omp parallel
353 // expected-warning@+2 {{initialization clause of OpenMP for loop is not in canonical form ('var = init' or 'T var = init')}}
354 #pragma omp for
355 for (int a{0}; a < 10; ++a) {
356 }
357
358 return 0;
359 }
360
361 // Iterators allowed in openmp for-loops.
362 namespace std {
363 struct random_access_iterator_tag {};
364 template <class Iter>
365 struct iterator_traits {
366 typedef typename Iter::difference_type difference_type;
367 typedef typename Iter::iterator_category iterator_category;
368 };
369 template <class Iter>
370 typename iterator_traits<Iter>::difference_type
distance(Iter first,Iter last)371 distance(Iter first, Iter last) { return first - last; }
372 }
373 class Iter0 {
374 public:
Iter0()375 Iter0() {}
Iter0(const Iter0 &)376 Iter0(const Iter0 &) {}
operator ++()377 Iter0 operator++() { return *this; }
operator --()378 Iter0 operator--() { return *this; }
operator <(Iter0 a)379 bool operator<(Iter0 a) { return true; }
380 };
381 // expected-note@+2 {{candidate function not viable: no known conversion from 'GoodIter' to 'Iter0' for 1st argument}}
382 // expected-note@+1 2 {{candidate function not viable: no known conversion from 'Iter1' to 'Iter0' for 1st argument}}
operator -(Iter0 a,Iter0 b)383 int operator-(Iter0 a, Iter0 b) { return 0; }
384 class Iter1 {
385 public:
Iter1(float f=0.0f,double d=0.0)386 Iter1(float f = 0.0f, double d = 0.0) {}
Iter1(const Iter1 &)387 Iter1(const Iter1 &) {}
operator ++()388 Iter1 operator++() { return *this; }
operator --()389 Iter1 operator--() { return *this; }
operator <(Iter1 a)390 bool operator<(Iter1 a) { return true; }
operator >=(Iter1 a)391 bool operator>=(Iter1 a) { return false; }
392 };
393 class GoodIter {
394 public:
GoodIter()395 GoodIter() {}
GoodIter(const GoodIter &)396 GoodIter(const GoodIter &) {}
GoodIter(int fst,int snd)397 GoodIter(int fst, int snd) {}
operator =(const GoodIter & that)398 GoodIter &operator=(const GoodIter &that) { return *this; }
operator =(const Iter0 & that)399 GoodIter &operator=(const Iter0 &that) { return *this; }
operator +=(int x)400 GoodIter &operator+=(int x) { return *this; }
operator -=(int x)401 GoodIter &operator-=(int x) { return *this; }
GoodIter(void *)402 explicit GoodIter(void *) {}
operator ++()403 GoodIter operator++() { return *this; }
operator --()404 GoodIter operator--() { return *this; }
operator !()405 bool operator!() { return true; }
operator <(GoodIter a)406 bool operator<(GoodIter a) { return true; }
operator <=(GoodIter a)407 bool operator<=(GoodIter a) { return true; }
operator >=(GoodIter a)408 bool operator>=(GoodIter a) { return false; }
409 typedef int difference_type;
410 typedef std::random_access_iterator_tag iterator_category;
411 };
412 // expected-note@+2 {{candidate function not viable: no known conversion from 'const Iter0' to 'GoodIter' for 2nd argument}}
413 // expected-note@+1 2 {{candidate function not viable: no known conversion from 'Iter1' to 'GoodIter' for 1st argument}}
operator -(GoodIter a,GoodIter b)414 int operator-(GoodIter a, GoodIter b) { return 0; }
415 // expected-note@+1 3 {{candidate function not viable: requires single argument 'a', but 2 arguments were provided}}
operator -(GoodIter a)416 GoodIter operator-(GoodIter a) { return a; }
417 // expected-note@+2 {{candidate function not viable: no known conversion from 'const Iter0' to 'int' for 2nd argument}}
418 // expected-note@+1 2 {{candidate function not viable: no known conversion from 'Iter1' to 'GoodIter' for 1st argument}}
operator -(GoodIter a,int v)419 GoodIter operator-(GoodIter a, int v) { return GoodIter(); }
420 // expected-note@+1 2 {{candidate function not viable: no known conversion from 'Iter0' to 'GoodIter' for 1st argument}}
operator +(GoodIter a,int v)421 GoodIter operator+(GoodIter a, int v) { return GoodIter(); }
422 // expected-note@+2 {{candidate function not viable: no known conversion from 'GoodIter' to 'int' for 1st argument}}
423 // expected-note@+1 2 {{candidate function not viable: no known conversion from 'Iter1' to 'int' for 1st argument}}
operator -(int v,GoodIter a)424 GoodIter operator-(int v, GoodIter a) { return GoodIter(); }
425 // expected-note@+1 2 {{candidate function not viable: no known conversion from 'Iter0' to 'int' for 1st argument}}
operator +(int v,GoodIter a)426 GoodIter operator+(int v, GoodIter a) { return GoodIter(); }
427
test_with_random_access_iterator()428 int test_with_random_access_iterator() {
429 GoodIter begin, end;
430 Iter0 begin0, end0;
431 #pragma omp parallel
432 #pragma omp for
433 for (GoodIter I = begin; I < end; ++I)
434 ++I;
435 #pragma omp parallel
436 // expected-error@+2 {{variable must be of integer or random access iterator type}}
437 #pragma omp for
438 for (GoodIter &I = begin; I < end; ++I)
439 ++I;
440 #pragma omp parallel
441 #pragma omp for
442 for (GoodIter I = begin; I >= end; --I)
443 ++I;
444 #pragma omp parallel
445 // expected-warning@+2 {{initialization clause of OpenMP for loop is not in canonical form ('var = init' or 'T var = init')}}
446 #pragma omp for
447 for (GoodIter I(begin); I < end; ++I)
448 ++I;
449 #pragma omp parallel
450 // expected-warning@+2 {{initialization clause of OpenMP for loop is not in canonical form ('var = init' or 'T var = init')}}
451 #pragma omp for
452 for (GoodIter I(nullptr); I < end; ++I)
453 ++I;
454 #pragma omp parallel
455 // expected-warning@+2 {{initialization clause of OpenMP for loop is not in canonical form ('var = init' or 'T var = init')}}
456 #pragma omp for
457 for (GoodIter I(0); I < end; ++I)
458 ++I;
459 #pragma omp parallel
460 // expected-warning@+2 {{initialization clause of OpenMP for loop is not in canonical form ('var = init' or 'T var = init')}}
461 #pragma omp for
462 for (GoodIter I(1, 2); I < end; ++I)
463 ++I;
464 #pragma omp parallel
465 #pragma omp for
466 for (begin = GoodIter(0); begin < end; ++begin)
467 ++begin;
468 // expected-error@+4 {{invalid operands to binary expression ('GoodIter' and 'const Iter0')}}
469 // expected-error@+3 {{could not calculate number of iterations calling 'operator-' with upper and lower loop bounds}}
470 #pragma omp parallel
471 #pragma omp for
472 for (begin = begin0; begin < end; ++begin)
473 ++begin;
474 #pragma omp parallel
475 // expected-error@+2 {{initialization clause of OpenMP for loop must be of the form 'var = init' or 'T var = init'}}
476 #pragma omp for
477 for (++begin; begin < end; ++begin)
478 ++begin;
479 #pragma omp parallel
480 #pragma omp for
481 for (begin = end; begin < end; ++begin)
482 ++begin;
483 #pragma omp parallel
484 // expected-error@+2 {{condition of OpenMP for loop must be a relational comparison ('<', '<=', '>', or '>=') of loop variable 'I'}}
485 #pragma omp for
486 for (GoodIter I = begin; I - I; ++I)
487 ++I;
488 #pragma omp parallel
489 // expected-error@+2 {{condition of OpenMP for loop must be a relational comparison ('<', '<=', '>', or '>=') of loop variable 'I'}}
490 #pragma omp for
491 for (GoodIter I = begin; begin < end; ++I)
492 ++I;
493 #pragma omp parallel
494 // expected-error@+2 {{condition of OpenMP for loop must be a relational comparison ('<', '<=', '>', or '>=') of loop variable 'I'}}
495 #pragma omp for
496 for (GoodIter I = begin; !I; ++I)
497 ++I;
498 #pragma omp parallel
499 // expected-note@+3 {{loop step is expected to be negative due to this condition}}
500 // expected-error@+2 {{increment expression must cause 'I' to decrease on each iteration of OpenMP for loop}}
501 #pragma omp for
502 for (GoodIter I = begin; I >= end; I = I + 1)
503 ++I;
504 #pragma omp parallel
505 #pragma omp for
506 for (GoodIter I = begin; I >= end; I = I - 1)
507 ++I;
508 #pragma omp parallel
509 // expected-error@+2 {{increment clause of OpenMP for loop must perform simple addition or subtraction on loop variable 'I'}}
510 #pragma omp for
511 for (GoodIter I = begin; I >= end; I = -I)
512 ++I;
513 #pragma omp parallel
514 // expected-note@+3 {{loop step is expected to be negative due to this condition}}
515 // expected-error@+2 {{increment expression must cause 'I' to decrease on each iteration of OpenMP for loop}}
516 #pragma omp for
517 for (GoodIter I = begin; I >= end; I = 2 + I)
518 ++I;
519 #pragma omp parallel
520 // expected-error@+2 {{increment clause of OpenMP for loop must perform simple addition or subtraction on loop variable 'I'}}
521 #pragma omp for
522 for (GoodIter I = begin; I >= end; I = 2 - I)
523 ++I;
524 // In the following example, we cannot update the loop variable using '+='
525 // expected-error@+3 {{invalid operands to binary expression ('Iter0' and 'int')}}
526 #pragma omp parallel
527 #pragma omp for
528 for (Iter0 I = begin0; I < end0; ++I)
529 ++I;
530 #pragma omp parallel
531 // Initializer is constructor without params.
532 // expected-error@+3 {{invalid operands to binary expression ('Iter0' and 'int')}}
533 // expected-warning@+2 {{initialization clause of OpenMP for loop is not in canonical form ('var = init' or 'T var = init')}}
534 #pragma omp for
535 for (Iter0 I; I < end0; ++I)
536 ++I;
537 Iter1 begin1, end1;
538 // expected-error@+4 {{invalid operands to binary expression ('Iter1' and 'Iter1')}}
539 // expected-error@+3 {{could not calculate number of iterations calling 'operator-' with upper and lower loop bounds}}
540 #pragma omp parallel
541 #pragma omp for
542 for (Iter1 I = begin1; I < end1; ++I)
543 ++I;
544 #pragma omp parallel
545 // expected-note@+3 {{loop step is expected to be negative due to this condition}}
546 // expected-error@+2 {{increment expression must cause 'I' to decrease on each iteration of OpenMP for loop}}
547 #pragma omp for
548 for (Iter1 I = begin1; I >= end1; ++I)
549 ++I;
550 #pragma omp parallel
551 // expected-error@+5 {{invalid operands to binary expression ('Iter1' and 'Iter1')}}
552 // expected-error@+4 {{could not calculate number of iterations calling 'operator-' with upper and lower loop bounds}}
553 // Initializer is constructor with all default params.
554 // expected-warning@+2 {{initialization clause of OpenMP for loop is not in canonical form ('var = init' or 'T var = init')}}
555 #pragma omp for
556 for (Iter1 I; I < end1; ++I) {
557 }
558 return 0;
559 }
560
561 template <typename IT, int ST>
562 class TC {
563 public:
dotest_lt(IT begin,IT end)564 int dotest_lt(IT begin, IT end) {
565 #pragma omp parallel
566 // expected-note@+3 {{loop step is expected to be positive due to this condition}}
567 // expected-error@+2 {{increment expression must cause 'I' to increase on each iteration of OpenMP for loop}}
568 #pragma omp for
569 for (IT I = begin; I < end; I = I + ST) {
570 ++I;
571 }
572 #pragma omp parallel
573 // expected-note@+3 {{loop step is expected to be positive due to this condition}}
574 // expected-error@+2 {{increment expression must cause 'I' to increase on each iteration of OpenMP for loop}}
575 #pragma omp for
576 for (IT I = begin; I <= end; I += ST) {
577 ++I;
578 }
579 #pragma omp parallel
580 #pragma omp for
581 for (IT I = begin; I < end; ++I) {
582 ++I;
583 }
584 }
585
step()586 static IT step() {
587 return IT(ST);
588 }
589 };
590 template <typename IT, int ST = 0>
dotest_gt(IT begin,IT end)591 int dotest_gt(IT begin, IT end) {
592 #pragma omp parallel
593 // expected-note@+3 2 {{loop step is expected to be negative due to this condition}}
594 // expected-error@+2 2 {{increment expression must cause 'I' to decrease on each iteration of OpenMP for loop}}
595 #pragma omp for
596 for (IT I = begin; I >= end; I = I + ST) {
597 ++I;
598 }
599 #pragma omp parallel
600 // expected-note@+3 2 {{loop step is expected to be negative due to this condition}}
601 // expected-error@+2 2 {{increment expression must cause 'I' to decrease on each iteration of OpenMP for loop}}
602 #pragma omp for
603 for (IT I = begin; I >= end; I += ST) {
604 ++I;
605 }
606
607 #pragma omp parallel
608 // expected-note@+3 {{loop step is expected to be negative due to this condition}}
609 // expected-error@+2 {{increment expression must cause 'I' to decrease on each iteration of OpenMP for loop}}
610 #pragma omp for
611 for (IT I = begin; I >= end; ++I) {
612 ++I;
613 }
614
615 #pragma omp parallel
616 #pragma omp for
617 for (IT I = begin; I < end; I += TC<int, ST>::step()) {
618 ++I;
619 }
620 }
621
test_with_template()622 void test_with_template() {
623 GoodIter begin, end;
624 TC<GoodIter, 100> t1;
625 TC<GoodIter, -100> t2;
626 t1.dotest_lt(begin, end);
627 t2.dotest_lt(begin, end); // expected-note {{in instantiation of member function 'TC<GoodIter, -100>::dotest_lt' requested here}}
628 dotest_gt(begin, end); // expected-note {{in instantiation of function template specialization 'dotest_gt<GoodIter, 0>' requested here}}
629 dotest_gt<unsigned, -10>(0, 100); // expected-note {{in instantiation of function template specialization 'dotest_gt<unsigned int, -10>' requested here}}
630 }
631
test_loop_break()632 void test_loop_break() {
633 const int N = 100;
634 float a[N], b[N], c[N];
635 #pragma omp parallel
636 #pragma omp for
637 for (int i = 0; i < 10; i++) {
638 c[i] = a[i] + b[i];
639 for (int j = 0; j < 10; ++j) {
640 if (a[i] > b[j])
641 break; // OK in nested loop
642 }
643 switch (i) {
644 case 1:
645 b[i]++;
646 break;
647 default:
648 break;
649 }
650 if (c[i] > 10)
651 break; // expected-error {{'break' statement cannot be used in OpenMP for loop}}
652
653 if (c[i] > 11)
654 break; // expected-error {{'break' statement cannot be used in OpenMP for loop}}
655 }
656
657 #pragma omp parallel
658 #pragma omp for
659 for (int i = 0; i < 10; i++) {
660 for (int j = 0; j < 10; j++) {
661 c[i] = a[i] + b[i];
662 if (c[i] > 10) {
663 if (c[i] < 20) {
664 break; // OK
665 }
666 }
667 }
668 }
669 }
670
test_loop_eh()671 void test_loop_eh() {
672 const int N = 100;
673 float a[N], b[N], c[N];
674 #pragma omp parallel
675 #pragma omp for
676 for (int i = 0; i < 10; i++) {
677 c[i] = a[i] + b[i];
678 try {
679 for (int j = 0; j < 10; ++j) {
680 if (a[i] > b[j])
681 throw a[i];
682 }
683 throw a[i];
684 } catch (float f) {
685 if (f > 0.1)
686 throw a[i];
687 return; // expected-error {{cannot return from OpenMP region}}
688 }
689 switch (i) {
690 case 1:
691 b[i]++;
692 break;
693 default:
694 break;
695 }
696 for (int j = 0; j < 10; j++) {
697 if (c[i] > 10)
698 throw c[i];
699 }
700 }
701 if (c[9] > 10)
702 throw c[9]; // OK
703
704 #pragma omp parallel
705 #pragma omp for
706 for (int i = 0; i < 10; ++i) {
707 struct S {
708 void g() { throw 0; }
709 };
710 }
711 }
712
test_loop_firstprivate_lastprivate()713 void test_loop_firstprivate_lastprivate() {
714 S s(4);
715 #pragma omp parallel
716 #pragma omp for lastprivate(s) firstprivate(s)
717 for (int i = 0; i < 16; ++i)
718 ;
719 }
720
test_ordered()721 void test_ordered() {
722 #pragma omp parallel
723 #pragma omp for ordered ordered // expected-error {{directive '#pragma omp for' cannot contain more than one 'ordered' clause}}
724 for (int i = 0; i < 16; ++i)
725 ;
726 }
727
test_nowait()728 void test_nowait() {
729 #pragma omp parallel
730 #pragma omp for nowait nowait // expected-error {{directive '#pragma omp for' cannot contain more than one 'nowait' clause}}
731 for (int i = 0; i < 16; ++i)
732 ;
733 }
734