1 // RUN: %clang_cc1 -fsyntax-only -fopenmp -x c++ -std=c++11 -fexceptions -fcxx-exceptions -verify %s
9 S(const S &s) : a(s.a) {}
13 #pragma omp threadprivate(sii)
16 register int reg0 __asm__("0");
18 int test_iteration_spaces() {
20 float a[N], b[N], c[N];
24 register int reg; // expected-warning {{'register' storage class specifier is deprecated}}
27 for (int i = 0; i < 10; i += 1) {
32 for (char i = 0; i < 10; i++) {
37 for (char i = 0; i < 10; i += '\1') {
42 for (long long i = 0; i < 10; i++) {
46 // expected-error@+2 {{expression must have integral or unscoped enumeration type, not 'double'}}
48 for (long long i = 0; i < 10; i += 1.5) {
53 for (long long i = 0; i < 'z'; i += 1u) {
57 // expected-error@+2 {{variable must be of integer or random access iterator type}}
59 for (float fi = 0; fi < 10.0; fi++) {
60 c[(int)fi] = a[(int)fi] + b[(int)fi];
63 // expected-error@+2 {{variable must be of integer or random access iterator type}}
65 for (double fi = 0; fi < 10.0; fi++) {
66 c[(int)fi] = a[(int)fi] + b[(int)fi];
69 // expected-error@+2 {{variable must be of integer or random access iterator type}}
71 for (int &ref = ii; ref < 10; ref++) {
74 // expected-error@+2 {{initialization clause of OpenMP for loop must be of the form 'var = init' or 'T var = init'}}
76 for (int i; i < 10; i++)
80 // expected-error@+2 {{initialization clause of OpenMP for loop must be of the form 'var = init' or 'T var = init'}}
82 for (int i = 0, j = 0; i < 10; ++i)
86 // expected-error@+2 {{initialization clause of OpenMP for loop must be of the form 'var = init' or 'T var = init'}}
92 // expected-warning@+3 {{expression result unused}}
93 // expected-error@+2 {{initialization clause of OpenMP for loop must be of the form 'var = init' or 'T var = init'}}
95 for (ii + 1; ii < 10; ++ii)
99 // expected-error@+2 {{initialization clause of OpenMP for loop must be of the form 'var = init' or 'T var = init'}}
101 for (c[ii] = 0; ii < 10; ++ii)
105 // Ok to skip parenthesises.
107 for (((ii)) = 0; ii < 10; ++ii)
111 // expected-error@+2 {{condition of OpenMP for loop must be a relational comparison ('<', '<=', '>', or '>=') of loop variable 'i'}}
113 for (int i = 0; i; i++)
117 // expected-error@+3 {{condition of OpenMP for loop must be a relational comparison ('<', '<=', '>', or '>=') of loop variable 'i'}}
118 // expected-error@+2 {{increment clause of OpenMP for loop must perform simple addition or subtraction on loop variable 'i'}}
120 for (int i = 0; jj < kk; ii++)
124 // expected-error@+2 {{condition of OpenMP for loop must be a relational comparison ('<', '<=', '>', or '>=') of loop variable 'i'}}
126 for (int i = 0; !!i; i++)
130 // expected-error@+2 {{condition of OpenMP for loop must be a relational comparison ('<', '<=', '>', or '>=') of loop variable 'i'}}
132 for (int i = 0; i != 1; i++)
136 // expected-error@+2 {{condition of OpenMP for loop must be a relational comparison ('<', '<=', '>', or '>=') of loop variable 'i'}}
138 for (int i = 0;; i++)
144 for (int i = 11; i > 10; i--)
150 for (int i = 0; i < 10; ++i)
156 for (ii = 0; ii < 10; ++ii)
160 // expected-error@+2 {{increment clause of OpenMP for loop must perform simple addition or subtraction on loop variable 'ii'}}
162 for (ii = 0; ii < 10; ++jj)
166 // expected-error@+2 {{increment clause of OpenMP for loop must perform simple addition or subtraction on loop variable 'ii'}}
168 for (ii = 0; ii < 10; ++++ii)
172 // Ok but undefined behavior (in general, cannot check that incr
173 // is really loop-invariant).
175 for (ii = 0; ii < 10; ii = ii + ii)
179 // expected-error@+2 {{expression must have integral or unscoped enumeration type, not 'float'}}
181 for (ii = 0; ii < 10; ii = ii + 1.0f)
185 // Ok - step was converted to integer type.
187 for (ii = 0; ii < 10; ii = ii + (int)1.1f)
191 // expected-error@+2 {{increment clause of OpenMP for loop must perform simple addition or subtraction on loop variable 'ii'}}
193 for (ii = 0; ii < 10; jj = ii + 2)
197 // expected-warning@+3 {{relational comparison result unused}}
198 // expected-error@+2 {{increment clause of OpenMP for loop must perform simple addition or subtraction on loop variable 'ii'}}
200 for (ii = 0; ii<10; jj> kk + 2)
204 // expected-error@+2 {{increment clause of OpenMP for loop must perform simple addition or subtraction on loop variable 'ii'}}
206 for (ii = 0; ii < 10;)
210 // expected-warning@+3 {{expression result unused}}
211 // expected-error@+2 {{increment clause of OpenMP for loop must perform simple addition or subtraction on loop variable 'ii'}}
213 for (ii = 0; ii < 10; !ii)
217 // expected-error@+2 {{increment clause of OpenMP for loop must perform simple addition or subtraction on loop variable 'ii'}}
219 for (ii = 0; ii < 10; ii ? ++ii : ++jj)
223 // expected-error@+2 {{increment clause of OpenMP for loop must perform simple addition or subtraction on loop variable 'ii'}}
225 for (ii = 0; ii < 10; ii = ii < 10)
229 // expected-note@+3 {{loop step is expected to be positive due to this condition}}
230 // expected-error@+2 {{increment expression must cause 'ii' to increase on each iteration of OpenMP for loop}}
232 for (ii = 0; ii < 10; ii = ii + 0)
236 // expected-note@+3 {{loop step is expected to be positive due to this condition}}
237 // expected-error@+2 {{increment expression must cause 'ii' to increase on each iteration of OpenMP for loop}}
239 for (ii = 0; ii < 10; ii = ii + (int)(0.8 - 0.45))
243 // expected-note@+3 {{loop step is expected to be positive due to this condition}}
244 // expected-error@+2 {{increment expression must cause 'ii' to increase on each iteration of OpenMP for loop}}
246 for (ii = 0; (ii) < 10; ii -= 25)
250 // expected-note@+3 {{loop step is expected to be positive due to this condition}}
251 // expected-error@+2 {{increment expression must cause 'ii' to increase on each iteration of OpenMP for loop}}
253 for (ii = 0; (ii < 10); ii -= 0)
257 // expected-note@+3 {{loop step is expected to be negative due to this condition}}
258 // expected-error@+2 {{increment expression must cause 'ii' to decrease on each iteration of OpenMP for loop}}
260 for (ii = 0; ii > 10; (ii += 0))
264 // expected-note@+3 {{loop step is expected to be positive due to this condition}}
265 // expected-error@+2 {{increment expression must cause 'ii' to increase on each iteration of OpenMP for loop}}
267 for (ii = 0; ii < 10; (ii) = (1 - 1) + (ii))
271 // expected-note@+3 {{loop step is expected to be negative due to this condition}}
272 // expected-error@+2 {{increment expression must cause 'ii' to decrease on each iteration of OpenMP for loop}}
274 for ((ii = 0); ii > 10; (ii -= 0))
278 // expected-note@+3 {{loop step is expected to be positive due to this condition}}
279 // expected-error@+2 {{increment expression must cause 'ii' to increase on each iteration of OpenMP for loop}}
281 for (ii = 0; (ii < 10); (ii -= 0))
285 // expected-note@+2 {{defined as firstprivate}}
286 // expected-error@+2 {{loop iteration variable in the associated loop of 'omp for' directive may not be firstprivate, predetermined as private}}
287 #pragma omp for firstprivate(ii)
288 for (ii = 0; ii < 10; ii++)
292 // expected-error@+3 {{unexpected OpenMP clause 'linear' in directive '#pragma omp for'}}
293 // expected-note@+2 {{defined as linear}}
294 // expected-error@+2 {{loop iteration variable in the associated loop of 'omp for' directive may not be linear, predetermined as private}}
295 #pragma omp for linear(ii)
296 for (ii = 0; ii < 10; ii++)
300 #pragma omp for private(ii)
301 for (ii = 0; ii < 10; ii++)
305 #pragma omp for lastprivate(ii)
306 for (ii = 0; ii < 10; ii++)
312 for (sii = 0; sii < 10; sii += 1)
319 for (reg0 = 0; reg0 < 10; reg0 += 1)
326 for (reg = 0; reg < 10; reg += 1)
333 for (globalii = 0; globalii < 10; globalii += 1)
334 c[globalii] = a[globalii];
339 #pragma omp for collapse(2)
340 for (ii = 0; ii < 10; ii += 1)
341 for (globalii = 0; globalii < 10; globalii += 1)
342 c[globalii] += a[globalii] + ii;
346 // expected-error@+2 {{statement after '#pragma omp for' must be a for loop}}
348 for (auto &item : a) {
353 // expected-note@+3 {{loop step is expected to be positive due to this condition}}
354 // expected-error@+2 {{increment expression must cause 'i' to increase on each iteration of OpenMP for loop}}
356 for (unsigned i = 9; i < 10; i--) {
360 int(*lb)[4] = nullptr;
363 for (int(*p)[4] = lb; p < lb + 8; ++p) {
367 // expected-warning@+2 {{initialization clause of OpenMP for loop is not in canonical form ('var = init' or 'T var = init')}}
369 for (int a{0}; a < 10; ++a) {
375 // Iterators allowed in openmp for-loops.
377 struct random_access_iterator_tag {};
378 template <class Iter>
379 struct iterator_traits {
380 typedef typename Iter::difference_type difference_type;
381 typedef typename Iter::iterator_category iterator_category;
383 template <class Iter>
384 typename iterator_traits<Iter>::difference_type
385 distance(Iter first, Iter last) { return first - last; }
390 Iter0(const Iter0 &) {}
391 Iter0 operator++() { return *this; }
392 Iter0 operator--() { return *this; }
393 bool operator<(Iter0 a) { return true; }
395 // expected-note@+2 {{candidate function not viable: no known conversion from 'GoodIter' to 'Iter0' for 1st argument}}
396 // expected-note@+1 2 {{candidate function not viable: no known conversion from 'Iter1' to 'Iter0' for 1st argument}}
397 int operator-(Iter0 a, Iter0 b) { return 0; }
400 Iter1(float f = 0.0f, double d = 0.0) {}
401 Iter1(const Iter1 &) {}
402 Iter1 operator++() { return *this; }
403 Iter1 operator--() { return *this; }
404 bool operator<(Iter1 a) { return true; }
405 bool operator>=(Iter1 a) { return false; }
410 GoodIter(const GoodIter &) {}
411 GoodIter(int fst, int snd) {}
412 GoodIter &operator=(const GoodIter &that) { return *this; }
413 GoodIter &operator=(const Iter0 &that) { return *this; }
414 GoodIter &operator+=(int x) { return *this; }
415 GoodIter &operator-=(int x) { return *this; }
416 explicit GoodIter(void *) {}
417 GoodIter operator++() { return *this; }
418 GoodIter operator--() { return *this; }
419 bool operator!() { return true; }
420 bool operator<(GoodIter a) { return true; }
421 bool operator<=(GoodIter a) { return true; }
422 bool operator>=(GoodIter a) { return false; }
423 typedef int difference_type;
424 typedef std::random_access_iterator_tag iterator_category;
426 // expected-note@+2 {{candidate function not viable: no known conversion from 'const Iter0' to 'GoodIter' for 2nd argument}}
427 // expected-note@+1 2 {{candidate function not viable: no known conversion from 'Iter1' to 'GoodIter' for 1st argument}}
428 int operator-(GoodIter a, GoodIter b) { return 0; }
429 // expected-note@+1 3 {{candidate function not viable: requires single argument 'a', but 2 arguments were provided}}
430 GoodIter operator-(GoodIter a) { return a; }
431 // expected-note@+2 {{candidate function not viable: no known conversion from 'const Iter0' to 'int' for 2nd argument}}
432 // expected-note@+1 2 {{candidate function not viable: no known conversion from 'Iter1' to 'GoodIter' for 1st argument}}
433 GoodIter operator-(GoodIter a, int v) { return GoodIter(); }
434 // expected-note@+1 2 {{candidate function not viable: no known conversion from 'Iter0' to 'GoodIter' for 1st argument}}
435 GoodIter operator+(GoodIter a, int v) { return GoodIter(); }
436 // expected-note@+2 {{candidate function not viable: no known conversion from 'GoodIter' to 'int' for 1st argument}}
437 // expected-note@+1 2 {{candidate function not viable: no known conversion from 'Iter1' to 'int' for 1st argument}}
438 GoodIter operator-(int v, GoodIter a) { return GoodIter(); }
439 // expected-note@+1 2 {{candidate function not viable: no known conversion from 'Iter0' to 'int' for 1st argument}}
440 GoodIter operator+(int v, GoodIter a) { return GoodIter(); }
442 int test_with_random_access_iterator() {
447 for (GoodIter I = begin; I < end; ++I)
450 // expected-error@+2 {{variable must be of integer or random access iterator type}}
452 for (GoodIter &I = begin; I < end; ++I)
456 for (GoodIter I = begin; I >= end; --I)
459 // expected-warning@+2 {{initialization clause of OpenMP for loop is not in canonical form ('var = init' or 'T var = init')}}
461 for (GoodIter I(begin); I < end; ++I)
464 // expected-warning@+2 {{initialization clause of OpenMP for loop is not in canonical form ('var = init' or 'T var = init')}}
466 for (GoodIter I(nullptr); I < end; ++I)
469 // expected-warning@+2 {{initialization clause of OpenMP for loop is not in canonical form ('var = init' or 'T var = init')}}
471 for (GoodIter I(0); I < end; ++I)
474 // expected-warning@+2 {{initialization clause of OpenMP for loop is not in canonical form ('var = init' or 'T var = init')}}
476 for (GoodIter I(1, 2); I < end; ++I)
480 for (begin = GoodIter(0); begin < end; ++begin)
482 // expected-error@+4 {{invalid operands to binary expression ('GoodIter' and 'const Iter0')}}
483 // expected-error@+3 {{could not calculate number of iterations calling 'operator-' with upper and lower loop bounds}}
486 for (begin = begin0; begin < end; ++begin)
489 // expected-error@+2 {{initialization clause of OpenMP for loop must be of the form 'var = init' or 'T var = init'}}
491 for (++begin; begin < end; ++begin)
495 for (begin = end; begin < end; ++begin)
498 // expected-error@+2 {{condition of OpenMP for loop must be a relational comparison ('<', '<=', '>', or '>=') of loop variable 'I'}}
500 for (GoodIter I = begin; I - I; ++I)
503 // expected-error@+2 {{condition of OpenMP for loop must be a relational comparison ('<', '<=', '>', or '>=') of loop variable 'I'}}
505 for (GoodIter I = begin; begin < end; ++I)
508 // expected-error@+2 {{condition of OpenMP for loop must be a relational comparison ('<', '<=', '>', or '>=') of loop variable 'I'}}
510 for (GoodIter I = begin; !I; ++I)
513 // expected-note@+3 {{loop step is expected to be negative due to this condition}}
514 // expected-error@+2 {{increment expression must cause 'I' to decrease on each iteration of OpenMP for loop}}
516 for (GoodIter I = begin; I >= end; I = I + 1)
520 for (GoodIter I = begin; I >= end; I = I - 1)
523 // expected-error@+2 {{increment clause of OpenMP for loop must perform simple addition or subtraction on loop variable 'I'}}
525 for (GoodIter I = begin; I >= end; I = -I)
528 // expected-note@+3 {{loop step is expected to be negative due to this condition}}
529 // expected-error@+2 {{increment expression must cause 'I' to decrease on each iteration of OpenMP for loop}}
531 for (GoodIter I = begin; I >= end; I = 2 + I)
534 // expected-error@+2 {{increment clause of OpenMP for loop must perform simple addition or subtraction on loop variable 'I'}}
536 for (GoodIter I = begin; I >= end; I = 2 - I)
538 // In the following example, we cannot update the loop variable using '+='
539 // expected-error@+3 {{invalid operands to binary expression ('Iter0' and 'int')}}
542 for (Iter0 I = begin0; I < end0; ++I)
545 // Initializer is constructor without params.
546 // expected-error@+3 {{invalid operands to binary expression ('Iter0' and 'int')}}
547 // expected-warning@+2 {{initialization clause of OpenMP for loop is not in canonical form ('var = init' or 'T var = init')}}
549 for (Iter0 I; I < end0; ++I)
552 // expected-error@+4 {{invalid operands to binary expression ('Iter1' and 'Iter1')}}
553 // expected-error@+3 {{could not calculate number of iterations calling 'operator-' with upper and lower loop bounds}}
556 for (Iter1 I = begin1; I < end1; ++I)
559 // expected-note@+3 {{loop step is expected to be negative due to this condition}}
560 // expected-error@+2 {{increment expression must cause 'I' to decrease on each iteration of OpenMP for loop}}
562 for (Iter1 I = begin1; I >= end1; ++I)
565 // expected-error@+5 {{invalid operands to binary expression ('Iter1' and 'Iter1')}}
566 // expected-error@+4 {{could not calculate number of iterations calling 'operator-' with upper and lower loop bounds}}
567 // Initializer is constructor with all default params.
568 // expected-warning@+2 {{initialization clause of OpenMP for loop is not in canonical form ('var = init' or 'T var = init')}}
570 for (Iter1 I; I < end1; ++I) {
575 template <typename IT, int ST>
578 int dotest_lt(IT begin, IT end) {
580 // expected-note@+3 {{loop step is expected to be positive due to this condition}}
581 // expected-error@+2 {{increment expression must cause 'I' to increase on each iteration of OpenMP for loop}}
583 for (IT I = begin; I < end; I = I + ST) {
587 // expected-note@+3 {{loop step is expected to be positive due to this condition}}
588 // expected-error@+2 {{increment expression must cause 'I' to increase on each iteration of OpenMP for loop}}
590 for (IT I = begin; I <= end; I += ST) {
595 for (IT I = begin; I < end; ++I) {
604 template <typename IT, int ST = 0>
605 int dotest_gt(IT begin, IT end) {
607 // expected-note@+3 2 {{loop step is expected to be negative due to this condition}}
608 // expected-error@+2 2 {{increment expression must cause 'I' to decrease on each iteration of OpenMP for loop}}
610 for (IT I = begin; I >= end; I = I + ST) {
614 // expected-note@+3 2 {{loop step is expected to be negative due to this condition}}
615 // expected-error@+2 2 {{increment expression must cause 'I' to decrease on each iteration of OpenMP for loop}}
617 for (IT I = begin; I >= end; I += ST) {
622 // expected-note@+3 {{loop step is expected to be negative due to this condition}}
623 // expected-error@+2 {{increment expression must cause 'I' to decrease on each iteration of OpenMP for loop}}
625 for (IT I = begin; I >= end; ++I) {
631 for (IT I = begin; I < end; I += TC<int, ST>::step()) {
636 void test_with_template() {
638 TC<GoodIter, 100> t1;
639 TC<GoodIter, -100> t2;
640 t1.dotest_lt(begin, end);
641 t2.dotest_lt(begin, end); // expected-note {{in instantiation of member function 'TC<GoodIter, -100>::dotest_lt' requested here}}
642 dotest_gt(begin, end); // expected-note {{in instantiation of function template specialization 'dotest_gt<GoodIter, 0>' requested here}}
643 dotest_gt<unsigned, -10>(0, 100); // expected-note {{in instantiation of function template specialization 'dotest_gt<unsigned int, -10>' requested here}}
646 void test_loop_break() {
648 float a[N], b[N], c[N];
651 for (int i = 0; i < 10; i++) {
653 for (int j = 0; j < 10; ++j) {
655 break; // OK in nested loop
665 break; // expected-error {{'break' statement cannot be used in OpenMP for loop}}
668 break; // expected-error {{'break' statement cannot be used in OpenMP for loop}}
673 for (int i = 0; i < 10; i++) {
674 for (int j = 0; j < 10; j++) {
685 void test_loop_eh() {
687 float a[N], b[N], c[N];
690 for (int i = 0; i < 10; i++) {
693 for (int j = 0; j < 10; ++j) {
701 return; // expected-error {{cannot return from OpenMP region}}
710 for (int j = 0; j < 10; j++) {
720 for (int i = 0; i < 10; ++i) {
722 void g() { throw 0; }
727 void test_loop_firstprivate_lastprivate() {
730 #pragma omp for lastprivate(s) firstprivate(s)
731 for (int i = 0; i < 16; ++i)
735 void test_ordered() {
737 #pragma omp for ordered ordered // expected-error {{directive '#pragma omp for' cannot contain more than one 'ordered' clause}}
738 for (int i = 0; i < 16; ++i)
744 #pragma omp for nowait nowait // expected-error {{directive '#pragma omp for' cannot contain more than one 'nowait' clause}}
745 for (int i = 0; i < 16; ++i)