1 // RUN: %clang_cc1 -fsyntax-only -fopenmp -x c++ -std=c++11 -fexceptions -fcxx-exceptions -verify %s
3 // RUN: %clang_cc1 -fsyntax-only -fopenmp-simd -x c++ -std=c++11 -fexceptions -fcxx-exceptions -verify %s
11 S(const S &s) : a(s.a) {}
15 // expected-note@+1 {{defined as threadprivate or thread local}}
16 #pragma omp threadprivate(sii)
19 // Currently, we cannot use "0" for global register variables.
20 // register int reg0 __asm__("0");
23 int test_iteration_spaces() {
25 float a[N], b[N], c[N];
29 register int reg; // expected-warning {{'register' storage class specifier is deprecated}}
32 for (int i = 0; i < 10; i += 1) {
37 for (char i = 0; i < 10; i++) {
42 for (char i = 0; i < 10; i += '\1') {
47 for (long long i = 0; i < 10; i++) {
51 // expected-error@+2 {{expression must have integral or unscoped enumeration type, not 'double'}}
53 for (long long i = 0; i < 10; i += 1.5) {
58 for (long long i = 0; i < 'z'; i += 1u) {
62 // expected-error@+2 {{variable must be of integer or random access iterator type}}
64 for (float fi = 0; fi < 10.0; fi++) {
65 c[(int)fi] = a[(int)fi] + b[(int)fi];
68 // expected-error@+2 {{variable must be of integer or random access iterator type}}
70 for (double fi = 0; fi < 10.0; fi++) {
71 c[(int)fi] = a[(int)fi] + b[(int)fi];
74 // expected-error@+2 {{initialization clause of OpenMP for loop is not in canonical form ('var = init' or 'T var = init')}}
76 for (int &ref = ii; ref < 10; ref++) {
79 // expected-error@+2 {{initialization clause of OpenMP for loop is not in canonical form ('var = init' or 'T var = init')}}
81 for (int i; i < 10; i++)
85 // expected-error@+2 {{initialization clause of OpenMP for loop is not in canonical form ('var = init' or 'T var = init')}}
87 for (int i = 0, j = 0; i < 10; ++i)
91 // expected-error@+2 {{initialization clause of OpenMP for loop is not in canonical form ('var = init' or 'T var = init')}}
97 // expected-warning@+3 {{expression result unused}}
98 // expected-error@+2 {{initialization clause of OpenMP for loop is not in canonical form ('var = init' or 'T var = init')}}
100 for (ii + 1; ii < 10; ++ii)
104 // expected-error@+2 {{initialization clause of OpenMP for loop is not in canonical form ('var = init' or 'T var = init')}}
106 for (c[ii] = 0; ii < 10; ++ii)
110 // Ok to skip parenthesises.
112 for (((ii)) = 0; ii < 10; ++ii)
116 // expected-error@+2 {{condition of OpenMP for loop must be a relational comparison ('<', '<=', '>', or '>=') of loop variable 'i'}}
118 for (int i = 0; i; i++)
122 // expected-error@+3 {{condition of OpenMP for loop must be a relational comparison ('<', '<=', '>', or '>=') of loop variable 'i'}}
123 // expected-error@+2 {{increment clause of OpenMP for loop must perform simple addition or subtraction on loop variable 'i'}}
125 for (int i = 0; jj < kk; ii++)
129 // expected-error@+2 {{condition of OpenMP for loop must be a relational comparison ('<', '<=', '>', or '>=') of loop variable 'i'}}
131 for (int i = 0; !!i; i++)
137 for (int i = 0; i != 1; i++)
141 // expected-error@+2 {{condition of OpenMP for loop must be a relational comparison ('<', '<=', '>', or '>=') of loop variable 'i'}}
143 for (int i = 0;; i++)
149 for (int i = 11; i > 10; i--)
155 for (int i = 0; i < 10; ++i)
161 for (ii = 0; ii < 10; ++ii)
165 // expected-error@+2 {{increment clause of OpenMP for loop must perform simple addition or subtraction on loop variable 'ii'}}
167 for (ii = 0; ii < 10; ++jj)
171 // expected-error@+2 {{increment clause of OpenMP for loop must perform simple addition or subtraction on loop variable 'ii'}}
173 for (ii = 0; ii < 10; ++++ii)
177 // Ok but undefined behavior (in general, cannot check that incr
178 // is really loop-invariant).
180 for (ii = 0; ii < 10; ii = ii + ii)
184 // expected-error@+2 {{expression must have integral or unscoped enumeration type, not 'float'}}
186 for (ii = 0; ii < 10; ii = ii + 1.0f)
190 // Ok - step was converted to integer type.
192 for (ii = 0; ii < 10; ii = ii + (int)1.1f)
196 // expected-error@+2 {{increment clause of OpenMP for loop must perform simple addition or subtraction on loop variable 'ii'}}
198 for (ii = 0; ii < 10; jj = ii + 2)
202 // expected-warning@+3 {{relational comparison result unused}}
203 // expected-error@+2 {{increment clause of OpenMP for loop must perform simple addition or subtraction on loop variable 'ii'}}
205 for (ii = 0; ii<10; jj> kk + 2)
209 // expected-error@+2 {{increment clause of OpenMP for loop must perform simple addition or subtraction on loop variable 'ii'}}
211 for (ii = 0; ii < 10;)
215 // expected-warning@+3 {{expression result unused}}
216 // expected-error@+2 {{increment clause of OpenMP for loop must perform simple addition or subtraction on loop variable 'ii'}}
218 for (ii = 0; ii < 10; !ii)
222 // expected-error@+2 {{increment clause of OpenMP for loop must perform simple addition or subtraction on loop variable 'ii'}}
224 for (ii = 0; ii < 10; ii ? ++ii : ++jj)
228 // expected-error@+2 {{increment clause of OpenMP for loop must perform simple addition or subtraction on loop variable 'ii'}}
230 for (ii = 0; ii < 10; ii = ii < 10)
234 // expected-note@+3 {{loop step is expected to be positive due to this condition}}
235 // expected-error@+2 {{increment expression must cause 'ii' to increase on each iteration of OpenMP for loop}}
237 for (ii = 0; ii < 10; ii = ii + 0)
241 // expected-note@+3 {{loop step is expected to be positive due to this condition}}
242 // expected-error@+2 {{increment expression must cause 'ii' to increase on each iteration of OpenMP for loop}}
244 for (ii = 0; ii < 10; ii = ii + (int)(0.8 - 0.45))
248 // expected-note@+3 {{loop step is expected to be positive due to this condition}}
249 // expected-error@+2 {{increment expression must cause 'ii' to increase on each iteration of OpenMP for loop}}
251 for (ii = 0; (ii) < 10; ii -= 25)
255 // expected-note@+3 {{loop step is expected to be positive due to this condition}}
256 // expected-error@+2 {{increment expression must cause 'ii' to increase on each iteration of OpenMP for loop}}
258 for (ii = 0; (ii < 10); ii -= 0)
262 // expected-note@+3 {{loop step is expected to be negative due to this condition}}
263 // expected-error@+2 {{increment expression must cause 'ii' to decrease on each iteration of OpenMP for loop}}
265 for (ii = 0; ii > 10; (ii += 0))
269 // expected-note@+3 {{loop step is expected to be positive due to this condition}}
270 // expected-error@+2 {{increment expression must cause 'ii' to increase on each iteration of OpenMP for loop}}
272 for (ii = 0; ii < 10; (ii) = (1 - 1) + (ii))
276 // expected-note@+3 {{loop step is expected to be negative due to this condition}}
277 // expected-error@+2 {{increment expression must cause 'ii' to decrease on each iteration of OpenMP for loop}}
279 for ((ii = 0); ii > 10; (ii -= 0))
283 // expected-note@+3 {{loop step is expected to be positive due to this condition}}
284 // expected-error@+2 {{increment expression must cause 'ii' to increase on each iteration of OpenMP for loop}}
286 for (ii = 0; (ii < 10); (ii -= 0))
290 // expected-note@+2 {{defined as firstprivate}}
291 // expected-error@+2 {{loop iteration variable in the associated loop of 'omp taskloop' directive may not be firstprivate, predetermined as private}}
292 #pragma omp taskloop firstprivate(ii)
293 for (ii = 0; ii < 10; ii++)
297 // expected-error@+1 {{unexpected OpenMP clause 'linear' in directive '#pragma omp taskloop'}}
298 #pragma omp taskloop linear(ii)
299 for (ii = 0; ii < 10; ii++)
303 #pragma omp taskloop private(ii)
304 for (ii = 0; ii < 10; ii++)
308 #pragma omp taskloop lastprivate(ii)
309 for (ii = 0; ii < 10; ii++)
314 // expected-error@+2 {{loop iteration variable in the associated loop of 'omp taskloop' directive may not be threadprivate or thread local, predetermined as private}}
316 for (sii = 0; sii < 10; sii += 1)
323 for (reg0 = 0; reg0 < 10; reg0 += 1)
330 for (reg = 0; reg < 10; reg += 1)
337 for (globalii = 0; globalii < 10; globalii += 1)
338 c[globalii] = a[globalii];
343 #pragma omp taskloop collapse(2)
344 for (ii = 0; ii < 10; ii += 1)
345 for (globalii = 0; globalii < 10; globalii += 1)
346 c[globalii] += a[globalii] + ii;
350 // expected-error@+2 {{statement after '#pragma omp taskloop' must be a for loop}}
352 for (auto &item : a) {
357 // expected-note@+3 {{loop step is expected to be positive due to this condition}}
358 // expected-error@+2 {{increment expression must cause 'i' to increase on each iteration of OpenMP for loop}}
360 for (unsigned i = 9; i < 10; i--) {
364 int(*lb)[4] = nullptr;
367 for (int(*p)[4] = lb; p < lb + 8; ++p) {
371 // expected-warning@+2 {{initialization clause of OpenMP for loop is not in canonical form ('var = init' or 'T var = init')}}
373 for (int a{0}; a < 10; ++a) {
379 // Iterators allowed in openmp for-loops.
381 struct random_access_iterator_tag {};
382 template <class Iter>
383 struct iterator_traits {
384 typedef typename Iter::difference_type difference_type;
385 typedef typename Iter::iterator_category iterator_category;
387 template <class Iter>
388 typename iterator_traits<Iter>::difference_type
389 distance(Iter first, Iter last) { return first - last; }
394 Iter0(const Iter0 &) {}
395 Iter0 operator++() { return *this; }
396 Iter0 operator--() { return *this; }
397 bool operator<(Iter0 a) { return true; }
399 // expected-note@+2 {{candidate function not viable: no known conversion from 'GoodIter' to 'Iter0' for 1st argument}}
400 // expected-note@+1 2 {{candidate function not viable: no known conversion from 'Iter1' to 'Iter0' for 1st argument}}
401 int operator-(Iter0 a, Iter0 b) { return 0; }
404 Iter1(float f = 0.0f, double d = 0.0) {}
405 Iter1(const Iter1 &) {}
406 Iter1 operator++() { return *this; }
407 Iter1 operator--() { return *this; }
408 bool operator<(Iter1 a) { return true; }
409 bool operator>=(Iter1 a) { return false; }
414 GoodIter(const GoodIter &) {}
415 GoodIter(int fst, int snd) {}
416 GoodIter &operator=(const GoodIter &that) { return *this; }
417 GoodIter &operator=(const Iter0 &that) { return *this; }
418 GoodIter &operator+=(int x) { return *this; }
419 GoodIter &operator-=(int x) { return *this; }
420 explicit GoodIter(void *) {}
421 GoodIter operator++() { return *this; }
422 GoodIter operator--() { return *this; }
423 bool operator!() { return true; }
424 bool operator<(GoodIter a) { return true; }
425 bool operator<=(GoodIter a) { return true; }
426 bool operator>=(GoodIter a) { return false; }
427 typedef int difference_type;
428 typedef std::random_access_iterator_tag iterator_category;
430 // expected-note@+2 {{candidate function not viable: no known conversion from 'const Iter0' to 'GoodIter' for 2nd argument}}
431 // expected-note@+1 2 {{candidate function not viable: no known conversion from 'Iter1' to 'GoodIter' for 1st argument}}
432 int operator-(GoodIter a, GoodIter b) { return 0; }
433 // expected-note@+1 3 {{candidate function not viable: requires single argument 'a', but 2 arguments were provided}}
434 GoodIter operator-(GoodIter a) { return a; }
435 // expected-note@+2 {{candidate function not viable: no known conversion from 'const Iter0' to 'int' for 2nd argument}}
436 // expected-note@+1 2 {{candidate function not viable: no known conversion from 'Iter1' to 'GoodIter' for 1st argument}}
437 GoodIter operator-(GoodIter a, int v) { return GoodIter(); }
438 // expected-note@+1 2 {{candidate function not viable: no known conversion from 'Iter0' to 'GoodIter' for 1st argument}}
439 GoodIter operator+(GoodIter a, int v) { return GoodIter(); }
440 // expected-note@+2 {{candidate function not viable: no known conversion from 'GoodIter' to 'int' for 1st argument}}
441 // expected-note@+1 2 {{candidate function not viable: no known conversion from 'Iter1' to 'int' for 1st argument}}
442 GoodIter operator-(int v, GoodIter a) { return GoodIter(); }
443 // expected-note@+1 2 {{candidate function not viable: no known conversion from 'Iter0' to 'int' for 1st argument}}
444 GoodIter operator+(int v, GoodIter a) { return GoodIter(); }
446 int test_with_random_access_iterator() {
451 for (GoodIter I = begin; I < end; ++I)
454 // expected-error@+2 {{initialization clause of OpenMP for loop is not in canonical form ('var = init' or 'T var = init')}}
456 for (GoodIter &I = begin; I < end; ++I)
460 for (GoodIter I = begin; I >= end; --I)
463 // expected-warning@+2 {{initialization clause of OpenMP for loop is not in canonical form ('var = init' or 'T var = init')}}
465 for (GoodIter I(begin); I < end; ++I)
468 // expected-warning@+2 {{initialization clause of OpenMP for loop is not in canonical form ('var = init' or 'T var = init')}}
470 for (GoodIter I(nullptr); I < end; ++I)
473 // expected-warning@+2 {{initialization clause of OpenMP for loop is not in canonical form ('var = init' or 'T var = init')}}
475 for (GoodIter I(0); I < end; ++I)
478 // expected-warning@+2 {{initialization clause of OpenMP for loop is not in canonical form ('var = init' or 'T var = init')}}
480 for (GoodIter I(1, 2); I < end; ++I)
484 for (begin = GoodIter(0); begin < end; ++begin)
486 // expected-error@+4 {{invalid operands to binary expression ('GoodIter' and 'const Iter0')}}
487 // expected-error@+3 {{could not calculate number of iterations calling 'operator-' with upper and lower loop bounds}}
490 for (begin = begin0; begin < end; ++begin)
493 // expected-error@+2 {{initialization clause of OpenMP for loop is not in canonical form ('var = init' or 'T var = init')}}
495 for (++begin; begin < end; ++begin)
499 for (begin = end; begin < end; ++begin)
502 // expected-error@+2 {{condition of OpenMP for loop must be a relational comparison ('<', '<=', '>', or '>=') of loop variable 'I'}}
504 for (GoodIter I = begin; I - I; ++I)
507 // expected-error@+2 {{condition of OpenMP for loop must be a relational comparison ('<', '<=', '>', or '>=') of loop variable 'I'}}
509 for (GoodIter I = begin; begin < end; ++I)
512 // expected-error@+2 {{condition of OpenMP for loop must be a relational comparison ('<', '<=', '>', or '>=') of loop variable 'I'}}
514 for (GoodIter I = begin; !I; ++I)
517 // expected-note@+3 {{loop step is expected to be negative due to this condition}}
518 // expected-error@+2 {{increment expression must cause 'I' to decrease on each iteration of OpenMP for loop}}
520 for (GoodIter I = begin; I >= end; I = I + 1)
524 for (GoodIter I = begin; I >= end; I = I - 1)
527 // expected-error@+2 {{increment clause of OpenMP for loop must perform simple addition or subtraction on loop variable 'I'}}
529 for (GoodIter I = begin; I >= end; I = -I)
532 // expected-note@+3 {{loop step is expected to be negative due to this condition}}
533 // expected-error@+2 {{increment expression must cause 'I' to decrease on each iteration of OpenMP for loop}}
535 for (GoodIter I = begin; I >= end; I = 2 + I)
538 // expected-error@+2 {{increment clause of OpenMP for loop must perform simple addition or subtraction on loop variable 'I'}}
540 for (GoodIter I = begin; I >= end; I = 2 - I)
542 // In the following example, we cannot update the loop variable using '+='
543 // expected-error@+3 {{invalid operands to binary expression ('Iter0' and 'int')}}
546 for (Iter0 I = begin0; I < end0; ++I)
549 // Initializer is constructor without params.
550 // expected-error@+3 {{invalid operands to binary expression ('Iter0' and 'int')}}
551 // expected-warning@+2 {{initialization clause of OpenMP for loop is not in canonical form ('var = init' or 'T var = init')}}
553 for (Iter0 I; I < end0; ++I)
556 // expected-error@+4 {{invalid operands to binary expression ('Iter1' and 'Iter1')}}
557 // expected-error@+3 {{could not calculate number of iterations calling 'operator-' with upper and lower loop bounds}}
560 for (Iter1 I = begin1; I < end1; ++I)
563 // expected-note@+3 {{loop step is expected to be negative due to this condition}}
564 // expected-error@+2 {{increment expression must cause 'I' to decrease on each iteration of OpenMP for loop}}
566 for (Iter1 I = begin1; I >= end1; ++I)
569 // expected-error@+5 {{invalid operands to binary expression ('Iter1' and 'float')}}
570 // expected-error@+4 {{could not calculate number of iterations calling 'operator-' with upper and lower loop bounds}}
571 // Initializer is constructor with all default params.
572 // expected-warning@+2 {{initialization clause of OpenMP for loop is not in canonical form ('var = init' or 'T var = init')}}
574 for (Iter1 I; I < end1; ++I) {
579 template <typename IT, int ST>
582 int dotest_lt(IT begin, IT end) {
584 // expected-note@+3 {{loop step is expected to be positive due to this condition}}
585 // expected-error@+2 {{increment expression must cause 'I' to increase on each iteration of OpenMP for loop}}
587 for (IT I = begin; I < end; I = I + ST) {
591 // expected-note@+3 {{loop step is expected to be positive due to this condition}}
592 // expected-error@+2 {{increment expression must cause 'I' to increase on each iteration of OpenMP for loop}}
594 for (IT I = begin; I <= end; I += ST) {
599 for (IT I = begin; I < end; ++I) {
608 template <typename IT, int ST = 0>
609 int dotest_gt(IT begin, IT end) {
611 // expected-note@+3 2 {{loop step is expected to be negative due to this condition}}
612 // expected-error@+2 2 {{increment expression must cause 'I' to decrease on each iteration of OpenMP for loop}}
614 for (IT I = begin; I >= end; I = I + ST) {
618 // expected-note@+3 2 {{loop step is expected to be negative due to this condition}}
619 // expected-error@+2 2 {{increment expression must cause 'I' to decrease on each iteration of OpenMP for loop}}
621 for (IT I = begin; I >= end; I += ST) {
626 // expected-note@+3 {{loop step is expected to be negative due to this condition}}
627 // expected-error@+2 {{increment expression must cause 'I' to decrease on each iteration of OpenMP for loop}}
629 for (IT I = begin; I >= end; ++I) {
635 for (IT I = begin; I < end; I += TC<int, ST>::step()) {
640 void test_with_template() {
642 TC<GoodIter, 100> t1;
643 TC<GoodIter, -100> t2;
644 t1.dotest_lt(begin, end);
645 t2.dotest_lt(begin, end); // expected-note {{in instantiation of member function 'TC<GoodIter, -100>::dotest_lt' requested here}}
646 dotest_gt(begin, end); // expected-note {{in instantiation of function template specialization 'dotest_gt<GoodIter, 0>' requested here}}
647 dotest_gt<unsigned, 10>(0, 100); // expected-note {{in instantiation of function template specialization 'dotest_gt<unsigned int, 10>' requested here}}
650 void test_loop_break() {
652 float a[N], b[N], c[N];
655 for (int i = 0; i < 10; i++) {
657 for (int j = 0; j < 10; ++j) {
659 break; // OK in nested loop
669 break; // expected-error {{'break' statement cannot be used in OpenMP for loop}}
672 break; // expected-error {{'break' statement cannot be used in OpenMP for loop}}
677 for (int i = 0; i < 10; i++) {
678 for (int j = 0; j < 10; j++) {
689 void test_loop_eh() {
691 float a[N], b[N], c[N];
694 for (int i = 0; i < 10; i++) {
697 for (int j = 0; j < 10; ++j) {
705 return; // expected-error {{cannot return from OpenMP region}}
714 for (int j = 0; j < 10; j++) {
724 for (int i = 0; i < 10; ++i) {
726 void g() { throw 0; }
731 void test_loop_firstprivate_lastprivate() {
734 #pragma omp taskloop lastprivate(s) firstprivate(s)
735 for (int i = 0; i < 16; ++i)