blob: 8817c77acf1586d86f2a51c970c1a408b298c6dc [file] [log] [blame]
Alexey Bataevdb390212015-05-20 04:24:19 +00001// RUN: %clang_cc1 -fsyntax-only -fopenmp -x c++ -std=c++11 -fexceptions -fcxx-exceptions -verify %s
Alexey Bataevf29276e2014-06-18 04:14:57 +00002
Alexey Bataeva8a9153a2017-12-29 18:07:07 +00003// RUN: %clang_cc1 -fsyntax-only -fopenmp-simd -x c++ -std=c++11 -fexceptions -fcxx-exceptions -verify %s
4
Alexey Bataevf29276e2014-06-18 04:14:57 +00005class S {
6 int a;
7 S() : a(0) {}
8
9public:
10 S(int v) : a(v) {}
11 S(const S &s) : a(s.a) {}
12};
13
14static int sii;
Alexey Bataeve648e802015-12-25 13:38:08 +000015// expected-note@+1 {{defined as threadprivate or thread local}}
Alexey Bataev0c024df2015-05-12 09:02:07 +000016#pragma omp threadprivate(sii)
Alexey Bataev9aba41c2014-11-14 04:08:45 +000017static int globalii;
Alexey Bataevf29276e2014-06-18 04:14:57 +000018
Akira Hatanaka8c26ea62015-11-18 00:15:28 +000019// Currently, we cannot use "0" for global register variables.
20// register int reg0 __asm__("0");
21int reg0;
Alexey Bataev1a8b3f12015-05-06 06:34:55 +000022
Alexey Bataevf29276e2014-06-18 04:14:57 +000023int test_iteration_spaces() {
24 const int N = 100;
25 float a[N], b[N], c[N];
26 int ii, jj, kk;
27 float fii;
28 double dii;
Alexey Bataev1a8b3f12015-05-06 06:34:55 +000029 register int reg; // expected-warning {{'register' storage class specifier is deprecated}}
Alexey Bataevf29276e2014-06-18 04:14:57 +000030#pragma omp parallel
31#pragma omp for
32 for (int i = 0; i < 10; i += 1) {
33 c[i] = a[i] + b[i];
34 }
35#pragma omp parallel
36#pragma omp for
37 for (char i = 0; i < 10; i++) {
38 c[i] = a[i] + b[i];
39 }
40#pragma omp parallel
41#pragma omp for
42 for (char i = 0; i < 10; i += '\1') {
43 c[i] = a[i] + b[i];
44 }
45#pragma omp parallel
46#pragma omp for
47 for (long long i = 0; i < 10; i++) {
48 c[i] = a[i] + b[i];
49 }
50#pragma omp parallel
51// expected-error@+2 {{expression must have integral or unscoped enumeration type, not 'double'}}
52#pragma omp for
53 for (long long i = 0; i < 10; i += 1.5) {
54 c[i] = a[i] + b[i];
55 }
56#pragma omp parallel
57#pragma omp for
58 for (long long i = 0; i < 'z'; i += 1u) {
59 c[i] = a[i] + b[i];
60 }
61#pragma omp parallel
62// expected-error@+2 {{variable must be of integer or random access iterator type}}
63#pragma omp for
64 for (float fi = 0; fi < 10.0; fi++) {
65 c[(int)fi] = a[(int)fi] + b[(int)fi];
66 }
67#pragma omp parallel
68// expected-error@+2 {{variable must be of integer or random access iterator type}}
69#pragma omp for
70 for (double fi = 0; fi < 10.0; fi++) {
71 c[(int)fi] = a[(int)fi] + b[(int)fi];
72 }
73#pragma omp parallel
Alexey Bataeva8899172015-08-06 12:30:57 +000074// expected-error@+2 {{initialization clause of OpenMP for loop is not in canonical form ('var = init' or 'T var = init')}}
Alexey Bataevf29276e2014-06-18 04:14:57 +000075#pragma omp for
76 for (int &ref = ii; ref < 10; ref++) {
77 }
78#pragma omp parallel
Alexey Bataeva8899172015-08-06 12:30:57 +000079// expected-error@+2 {{initialization clause of OpenMP for loop is not in canonical form ('var = init' or 'T var = init')}}
Alexey Bataevf29276e2014-06-18 04:14:57 +000080#pragma omp for
81 for (int i; i < 10; i++)
82 c[i] = a[i];
83
84#pragma omp parallel
Alexey Bataeva8899172015-08-06 12:30:57 +000085// expected-error@+2 {{initialization clause of OpenMP for loop is not in canonical form ('var = init' or 'T var = init')}}
Alexey Bataevf29276e2014-06-18 04:14:57 +000086#pragma omp for
87 for (int i = 0, j = 0; i < 10; ++i)
88 c[i] = a[i];
89
90#pragma omp parallel
Alexey Bataeva8899172015-08-06 12:30:57 +000091// expected-error@+2 {{initialization clause of OpenMP for loop is not in canonical form ('var = init' or 'T var = init')}}
Alexey Bataevf29276e2014-06-18 04:14:57 +000092#pragma omp for
93 for (; ii < 10; ++ii)
94 c[ii] = a[ii];
95
96#pragma omp parallel
97// expected-warning@+3 {{expression result unused}}
Alexey Bataeva8899172015-08-06 12:30:57 +000098// expected-error@+2 {{initialization clause of OpenMP for loop is not in canonical form ('var = init' or 'T var = init')}}
Alexey Bataevf29276e2014-06-18 04:14:57 +000099#pragma omp for
100 for (ii + 1; ii < 10; ++ii)
101 c[ii] = a[ii];
102
103#pragma omp parallel
Alexey Bataeva8899172015-08-06 12:30:57 +0000104// expected-error@+2 {{initialization clause of OpenMP for loop is not in canonical form ('var = init' or 'T var = init')}}
Alexey Bataevf29276e2014-06-18 04:14:57 +0000105#pragma omp for
106 for (c[ii] = 0; ii < 10; ++ii)
107 c[ii] = a[ii];
108
109#pragma omp parallel
110// Ok to skip parenthesises.
111#pragma omp for
112 for (((ii)) = 0; ii < 10; ++ii)
113 c[ii] = a[ii];
114
115#pragma omp parallel
116// expected-error@+2 {{condition of OpenMP for loop must be a relational comparison ('<', '<=', '>', or '>=') of loop variable 'i'}}
117#pragma omp for
118 for (int i = 0; i; i++)
119 c[i] = a[i];
120
121#pragma omp parallel
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'}}
124#pragma omp for
125 for (int i = 0; jj < kk; ii++)
126 c[i] = a[i];
127
128#pragma omp parallel
129// expected-error@+2 {{condition of OpenMP for loop must be a relational comparison ('<', '<=', '>', or '>=') of loop variable 'i'}}
130#pragma omp for
131 for (int i = 0; !!i; i++)
132 c[i] = a[i];
133
Kelvin Liefbe4af2018-11-21 19:10:48 +0000134// Ok
Alexey Bataevf29276e2014-06-18 04:14:57 +0000135#pragma omp parallel
Alexey Bataevf29276e2014-06-18 04:14:57 +0000136#pragma omp for
137 for (int i = 0; i != 1; i++)
138 c[i] = a[i];
139
140#pragma omp parallel
141// expected-error@+2 {{condition of OpenMP for loop must be a relational comparison ('<', '<=', '>', or '>=') of loop variable 'i'}}
142#pragma omp for
143 for (int i = 0;; i++)
144 c[i] = a[i];
145
146#pragma omp parallel
147// Ok.
148#pragma omp for
149 for (int i = 11; i > 10; i--)
150 c[i] = a[i];
151
152#pragma omp parallel
153// Ok.
154#pragma omp for
155 for (int i = 0; i < 10; ++i)
156 c[i] = a[i];
157
158#pragma omp parallel
159// Ok.
160#pragma omp for
161 for (ii = 0; ii < 10; ++ii)
162 c[ii] = a[ii];
163
164#pragma omp parallel
165// expected-error@+2 {{increment clause of OpenMP for loop must perform simple addition or subtraction on loop variable 'ii'}}
166#pragma omp for
167 for (ii = 0; ii < 10; ++jj)
168 c[ii] = a[jj];
169
170#pragma omp parallel
171// expected-error@+2 {{increment clause of OpenMP for loop must perform simple addition or subtraction on loop variable 'ii'}}
172#pragma omp for
173 for (ii = 0; ii < 10; ++++ii)
174 c[ii] = a[ii];
175
176#pragma omp parallel
177// Ok but undefined behavior (in general, cannot check that incr
178// is really loop-invariant).
179#pragma omp for
180 for (ii = 0; ii < 10; ii = ii + ii)
181 c[ii] = a[ii];
182
183#pragma omp parallel
184// expected-error@+2 {{expression must have integral or unscoped enumeration type, not 'float'}}
185#pragma omp for
186 for (ii = 0; ii < 10; ii = ii + 1.0f)
187 c[ii] = a[ii];
188
189#pragma omp parallel
190// Ok - step was converted to integer type.
191#pragma omp for
192 for (ii = 0; ii < 10; ii = ii + (int)1.1f)
193 c[ii] = a[ii];
194
195#pragma omp parallel
196// expected-error@+2 {{increment clause of OpenMP for loop must perform simple addition or subtraction on loop variable 'ii'}}
197#pragma omp for
198 for (ii = 0; ii < 10; jj = ii + 2)
199 c[ii] = a[ii];
200
201#pragma omp parallel
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'}}
204#pragma omp for
205 for (ii = 0; ii<10; jj> kk + 2)
206 c[ii] = a[ii];
207
208#pragma omp parallel
209// expected-error@+2 {{increment clause of OpenMP for loop must perform simple addition or subtraction on loop variable 'ii'}}
210#pragma omp for
211 for (ii = 0; ii < 10;)
212 c[ii] = a[ii];
213
214#pragma omp parallel
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'}}
217#pragma omp for
218 for (ii = 0; ii < 10; !ii)
219 c[ii] = a[ii];
220
221#pragma omp parallel
222// expected-error@+2 {{increment clause of OpenMP for loop must perform simple addition or subtraction on loop variable 'ii'}}
223#pragma omp for
224 for (ii = 0; ii < 10; ii ? ++ii : ++jj)
225 c[ii] = a[ii];
226
227#pragma omp parallel
228// expected-error@+2 {{increment clause of OpenMP for loop must perform simple addition or subtraction on loop variable 'ii'}}
229#pragma omp for
230 for (ii = 0; ii < 10; ii = ii < 10)
231 c[ii] = a[ii];
232
233#pragma omp parallel
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}}
236#pragma omp for
237 for (ii = 0; ii < 10; ii = ii + 0)
238 c[ii] = a[ii];
239
240#pragma omp parallel
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}}
243#pragma omp for
244 for (ii = 0; ii < 10; ii = ii + (int)(0.8 - 0.45))
245 c[ii] = a[ii];
246
247#pragma omp parallel
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}}
250#pragma omp for
251 for (ii = 0; (ii) < 10; ii -= 25)
252 c[ii] = a[ii];
253
254#pragma omp parallel
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}}
257#pragma omp for
258 for (ii = 0; (ii < 10); ii -= 0)
259 c[ii] = a[ii];
260
261#pragma omp parallel
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}}
264#pragma omp for
265 for (ii = 0; ii > 10; (ii += 0))
266 c[ii] = a[ii];
267
268#pragma omp parallel
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}}
271#pragma omp for
272 for (ii = 0; ii < 10; (ii) = (1 - 1) + (ii))
273 c[ii] = a[ii];
274
275#pragma omp parallel
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}}
278#pragma omp for
279 for ((ii = 0); ii > 10; (ii -= 0))
280 c[ii] = a[ii];
281
282#pragma omp parallel
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}}
285#pragma omp for
286 for (ii = 0; (ii < 10); (ii -= 0))
287 c[ii] = a[ii];
288
289#pragma omp parallel
290// expected-note@+2 {{defined as firstprivate}}
Alexey Bataev4acb8592014-07-07 13:01:15 +0000291// expected-error@+2 {{loop iteration variable in the associated loop of 'omp for' directive may not be firstprivate, predetermined as private}}
Alexey Bataevf29276e2014-06-18 04:14:57 +0000292#pragma omp for firstprivate(ii)
293 for (ii = 0; ii < 10; ii++)
294 c[ii] = a[ii];
295
296#pragma omp parallel
Alexey Bataevf29276e2014-06-18 04:14:57 +0000297// expected-note@+2 {{defined as linear}}
Alexey Bataev4acb8592014-07-07 13:01:15 +0000298// expected-error@+2 {{loop iteration variable in the associated loop of 'omp for' directive may not be linear, predetermined as private}}
Alexey Bataevf29276e2014-06-18 04:14:57 +0000299#pragma omp for linear(ii)
300 for (ii = 0; ii < 10; ii++)
301 c[ii] = a[ii];
302
303#pragma omp parallel
304#pragma omp for private(ii)
305 for (ii = 0; ii < 10; ii++)
306 c[ii] = a[ii];
307
308#pragma omp parallel
309#pragma omp for lastprivate(ii)
310 for (ii = 0; ii < 10; ii++)
311 c[ii] = a[ii];
312
313#pragma omp parallel
314 {
Alexey Bataeve648e802015-12-25 13:38:08 +0000315// expected-error@+2 {{loop iteration variable in the associated loop of 'omp for' directive may not be threadprivate or thread local, predetermined as private}}
Alexey Bataevf29276e2014-06-18 04:14:57 +0000316#pragma omp for
317 for (sii = 0; sii < 10; sii += 1)
318 c[sii] = a[sii];
319 }
320
321#pragma omp parallel
Alexey Bataev9aba41c2014-11-14 04:08:45 +0000322 {
Alexey Bataev1a8b3f12015-05-06 06:34:55 +0000323#pragma omp for
324 for (reg0 = 0; reg0 < 10; reg0 += 1)
325 c[reg0] = a[reg0];
326 }
327
328#pragma omp parallel
329 {
330#pragma omp for
331 for (reg = 0; reg < 10; reg += 1)
332 c[reg] = a[reg];
333 }
334
335#pragma omp parallel
336 {
Alexey Bataev9aba41c2014-11-14 04:08:45 +0000337#pragma omp for
338 for (globalii = 0; globalii < 10; globalii += 1)
339 c[globalii] = a[globalii];
340 }
341
342#pragma omp parallel
343 {
Alexey Bataev9aba41c2014-11-14 04:08:45 +0000344#pragma omp for collapse(2)
345 for (ii = 0; ii < 10; ii += 1)
346 for (globalii = 0; globalii < 10; globalii += 1)
347 c[globalii] += a[globalii] + ii;
348 }
349
350#pragma omp parallel
Alexey Bataevf29276e2014-06-18 04:14:57 +0000351// expected-error@+2 {{statement after '#pragma omp for' must be a for loop}}
352#pragma omp for
353 for (auto &item : a) {
354 item = item + 1;
355 }
356
357#pragma omp parallel
358// expected-note@+3 {{loop step is expected to be positive due to this condition}}
359// expected-error@+2 {{increment expression must cause 'i' to increase on each iteration of OpenMP for loop}}
360#pragma omp for
361 for (unsigned i = 9; i < 10; i--) {
362 c[i] = a[i] + b[i];
363 }
364
365 int(*lb)[4] = nullptr;
366#pragma omp parallel
367#pragma omp for
368 for (int(*p)[4] = lb; p < lb + 8; ++p) {
369 }
370
371#pragma omp parallel
372// expected-warning@+2 {{initialization clause of OpenMP for loop is not in canonical form ('var = init' or 'T var = init')}}
373#pragma omp for
374 for (int a{0}; a < 10; ++a) {
375 }
376
377 return 0;
378}
379
380// Iterators allowed in openmp for-loops.
381namespace std {
382struct random_access_iterator_tag {};
383template <class Iter>
384struct iterator_traits {
385 typedef typename Iter::difference_type difference_type;
386 typedef typename Iter::iterator_category iterator_category;
387};
388template <class Iter>
389typename iterator_traits<Iter>::difference_type
390distance(Iter first, Iter last) { return first - last; }
391}
392class Iter0 {
393public:
394 Iter0() {}
395 Iter0(const Iter0 &) {}
396 Iter0 operator++() { return *this; }
397 Iter0 operator--() { return *this; }
398 bool operator<(Iter0 a) { return true; }
399};
Alexander Musmana5f070a2014-10-01 06:03:56 +0000400// expected-note@+2 {{candidate function not viable: no known conversion from 'GoodIter' to 'Iter0' for 1st argument}}
401// expected-note@+1 2 {{candidate function not viable: no known conversion from 'Iter1' to 'Iter0' for 1st argument}}
Alexey Bataevf29276e2014-06-18 04:14:57 +0000402int operator-(Iter0 a, Iter0 b) { return 0; }
403class Iter1 {
404public:
405 Iter1(float f = 0.0f, double d = 0.0) {}
406 Iter1(const Iter1 &) {}
407 Iter1 operator++() { return *this; }
408 Iter1 operator--() { return *this; }
409 bool operator<(Iter1 a) { return true; }
410 bool operator>=(Iter1 a) { return false; }
411};
412class GoodIter {
413public:
414 GoodIter() {}
415 GoodIter(const GoodIter &) {}
416 GoodIter(int fst, int snd) {}
417 GoodIter &operator=(const GoodIter &that) { return *this; }
418 GoodIter &operator=(const Iter0 &that) { return *this; }
419 GoodIter &operator+=(int x) { return *this; }
Alexander Musmana5f070a2014-10-01 06:03:56 +0000420 GoodIter &operator-=(int x) { return *this; }
Alexey Bataevf29276e2014-06-18 04:14:57 +0000421 explicit GoodIter(void *) {}
422 GoodIter operator++() { return *this; }
423 GoodIter operator--() { return *this; }
424 bool operator!() { return true; }
425 bool operator<(GoodIter a) { return true; }
426 bool operator<=(GoodIter a) { return true; }
427 bool operator>=(GoodIter a) { return false; }
428 typedef int difference_type;
429 typedef std::random_access_iterator_tag iterator_category;
430};
Alexey Bataevc0214e02016-02-16 12:13:49 +0000431class GoodIter1 {
432public:
433 GoodIter1() {}
434 GoodIter1(const GoodIter1 &) {}
435 GoodIter1 &operator++(int) { return *this; }
436 GoodIter1 &operator=(const GoodIter1 &that) { return *this; }
437 GoodIter1 &operator+=(int x) { return *this; }
438 friend long operator-(const GoodIter1 &, const GoodIter1 &);
439 GoodIter1 &operator-(int) { return *this; }
440 bool operator<(GoodIter1 a) { return true; }
441 typedef int difference_type;
442 typedef std::random_access_iterator_tag iterator_category;
443};
Alexey Bataev5a3af132016-03-29 08:58:54 +0000444// expected-note@+2 {{candidate function not viable: no known conversion from 'const Iter0' to 'GoodIter' for 2nd argument}}
Alexander Musmana5f070a2014-10-01 06:03:56 +0000445// expected-note@+1 2 {{candidate function not viable: no known conversion from 'Iter1' to 'GoodIter' for 1st argument}}
Alexey Bataevf29276e2014-06-18 04:14:57 +0000446int operator-(GoodIter a, GoodIter b) { return 0; }
Alexander Musmana5f070a2014-10-01 06:03:56 +0000447// expected-note@+1 3 {{candidate function not viable: requires single argument 'a', but 2 arguments were provided}}
Alexey Bataevf29276e2014-06-18 04:14:57 +0000448GoodIter operator-(GoodIter a) { return a; }
Alexey Bataev5a3af132016-03-29 08:58:54 +0000449// expected-note@+2 {{candidate function not viable: no known conversion from 'const Iter0' to 'int' for 2nd argument}}
Alexander Musmana5f070a2014-10-01 06:03:56 +0000450// expected-note@+1 2 {{candidate function not viable: no known conversion from 'Iter1' to 'GoodIter' for 1st argument}}
Alexey Bataevf29276e2014-06-18 04:14:57 +0000451GoodIter operator-(GoodIter a, int v) { return GoodIter(); }
Alexander Musmana5f070a2014-10-01 06:03:56 +0000452// expected-note@+1 2 {{candidate function not viable: no known conversion from 'Iter0' to 'GoodIter' for 1st argument}}
Alexey Bataevf29276e2014-06-18 04:14:57 +0000453GoodIter operator+(GoodIter a, int v) { return GoodIter(); }
Alexander Musmana5f070a2014-10-01 06:03:56 +0000454// expected-note@+2 {{candidate function not viable: no known conversion from 'GoodIter' to 'int' for 1st argument}}
455// expected-note@+1 2 {{candidate function not viable: no known conversion from 'Iter1' to 'int' for 1st argument}}
Alexey Bataevf29276e2014-06-18 04:14:57 +0000456GoodIter operator-(int v, GoodIter a) { return GoodIter(); }
Alexander Musmana5f070a2014-10-01 06:03:56 +0000457// expected-note@+1 2 {{candidate function not viable: no known conversion from 'Iter0' to 'int' for 1st argument}}
Alexey Bataevf29276e2014-06-18 04:14:57 +0000458GoodIter operator+(int v, GoodIter a) { return GoodIter(); }
459
460int test_with_random_access_iterator() {
461 GoodIter begin, end;
462 Iter0 begin0, end0;
463#pragma omp parallel
464#pragma omp for
465 for (GoodIter I = begin; I < end; ++I)
466 ++I;
467#pragma omp parallel
Alexey Bataeva8899172015-08-06 12:30:57 +0000468// expected-error@+2 {{initialization clause of OpenMP for loop is not in canonical form ('var = init' or 'T var = init')}}
Alexey Bataevf29276e2014-06-18 04:14:57 +0000469#pragma omp for
470 for (GoodIter &I = begin; I < end; ++I)
471 ++I;
472#pragma omp parallel
473#pragma omp for
474 for (GoodIter I = begin; I >= end; --I)
475 ++I;
476#pragma omp parallel
477// expected-warning@+2 {{initialization clause of OpenMP for loop is not in canonical form ('var = init' or 'T var = init')}}
478#pragma omp for
479 for (GoodIter I(begin); I < end; ++I)
480 ++I;
481#pragma omp parallel
482// expected-warning@+2 {{initialization clause of OpenMP for loop is not in canonical form ('var = init' or 'T var = init')}}
483#pragma omp for
484 for (GoodIter I(nullptr); I < end; ++I)
485 ++I;
486#pragma omp parallel
487// expected-warning@+2 {{initialization clause of OpenMP for loop is not in canonical form ('var = init' or 'T var = init')}}
488#pragma omp for
489 for (GoodIter I(0); I < end; ++I)
490 ++I;
491#pragma omp parallel
492// expected-warning@+2 {{initialization clause of OpenMP for loop is not in canonical form ('var = init' or 'T var = init')}}
493#pragma omp for
494 for (GoodIter I(1, 2); I < end; ++I)
495 ++I;
496#pragma omp parallel
497#pragma omp for
498 for (begin = GoodIter(0); begin < end; ++begin)
499 ++begin;
Alexey Bataev5a3af132016-03-29 08:58:54 +0000500// expected-error@+4 {{invalid operands to binary expression ('GoodIter' and 'const Iter0')}}
Alexander Musmana5f070a2014-10-01 06:03:56 +0000501// expected-error@+3 {{could not calculate number of iterations calling 'operator-' with upper and lower loop bounds}}
Alexey Bataevf29276e2014-06-18 04:14:57 +0000502#pragma omp parallel
503#pragma omp for
504 for (begin = begin0; begin < end; ++begin)
505 ++begin;
506#pragma omp parallel
Alexey Bataeva8899172015-08-06 12:30:57 +0000507// expected-error@+2 {{initialization clause of OpenMP for loop is not in canonical form ('var = init' or 'T var = init')}}
Alexey Bataevf29276e2014-06-18 04:14:57 +0000508#pragma omp for
509 for (++begin; begin < end; ++begin)
510 ++begin;
511#pragma omp parallel
512#pragma omp for
513 for (begin = end; begin < end; ++begin)
514 ++begin;
515#pragma omp parallel
516// expected-error@+2 {{condition of OpenMP for loop must be a relational comparison ('<', '<=', '>', or '>=') of loop variable 'I'}}
517#pragma omp for
518 for (GoodIter I = begin; I - I; ++I)
519 ++I;
520#pragma omp parallel
521// expected-error@+2 {{condition of OpenMP for loop must be a relational comparison ('<', '<=', '>', or '>=') of loop variable 'I'}}
522#pragma omp for
523 for (GoodIter I = begin; begin < end; ++I)
524 ++I;
525#pragma omp parallel
526// expected-error@+2 {{condition of OpenMP for loop must be a relational comparison ('<', '<=', '>', or '>=') of loop variable 'I'}}
527#pragma omp for
528 for (GoodIter I = begin; !I; ++I)
529 ++I;
530#pragma omp parallel
531// expected-note@+3 {{loop step is expected to be negative due to this condition}}
532// expected-error@+2 {{increment expression must cause 'I' to decrease on each iteration of OpenMP for loop}}
533#pragma omp for
534 for (GoodIter I = begin; I >= end; I = I + 1)
535 ++I;
536#pragma omp parallel
537#pragma omp for
538 for (GoodIter I = begin; I >= end; I = I - 1)
539 ++I;
540#pragma omp parallel
541// expected-error@+2 {{increment clause of OpenMP for loop must perform simple addition or subtraction on loop variable 'I'}}
542#pragma omp for
543 for (GoodIter I = begin; I >= end; I = -I)
544 ++I;
545#pragma omp parallel
546// expected-note@+3 {{loop step is expected to be negative due to this condition}}
547// expected-error@+2 {{increment expression must cause 'I' to decrease on each iteration of OpenMP for loop}}
548#pragma omp for
549 for (GoodIter I = begin; I >= end; I = 2 + I)
550 ++I;
551#pragma omp parallel
552// expected-error@+2 {{increment clause of OpenMP for loop must perform simple addition or subtraction on loop variable 'I'}}
553#pragma omp for
554 for (GoodIter I = begin; I >= end; I = 2 - I)
555 ++I;
Alexander Musmana5f070a2014-10-01 06:03:56 +0000556// In the following example, we cannot update the loop variable using '+='
557// expected-error@+3 {{invalid operands to binary expression ('Iter0' and 'int')}}
Alexey Bataevf29276e2014-06-18 04:14:57 +0000558#pragma omp parallel
559#pragma omp for
560 for (Iter0 I = begin0; I < end0; ++I)
561 ++I;
562#pragma omp parallel
563// Initializer is constructor without params.
Alexander Musmana5f070a2014-10-01 06:03:56 +0000564// expected-error@+3 {{invalid operands to binary expression ('Iter0' and 'int')}}
Alexey Bataevf29276e2014-06-18 04:14:57 +0000565// expected-warning@+2 {{initialization clause of OpenMP for loop is not in canonical form ('var = init' or 'T var = init')}}
566#pragma omp for
567 for (Iter0 I; I < end0; ++I)
568 ++I;
569 Iter1 begin1, end1;
Alexander Musmana5f070a2014-10-01 06:03:56 +0000570// expected-error@+4 {{invalid operands to binary expression ('Iter1' and 'Iter1')}}
571// expected-error@+3 {{could not calculate number of iterations calling 'operator-' with upper and lower loop bounds}}
Alexey Bataevf29276e2014-06-18 04:14:57 +0000572#pragma omp parallel
573#pragma omp for
574 for (Iter1 I = begin1; I < end1; ++I)
575 ++I;
576#pragma omp parallel
577// expected-note@+3 {{loop step is expected to be negative due to this condition}}
578// expected-error@+2 {{increment expression must cause 'I' to decrease on each iteration of OpenMP for loop}}
579#pragma omp for
580 for (Iter1 I = begin1; I >= end1; ++I)
581 ++I;
582#pragma omp parallel
Alexey Bataev0d08a7f2015-07-16 04:19:43 +0000583// expected-error@+5 {{invalid operands to binary expression ('Iter1' and 'float')}}
Alexander Musmana5f070a2014-10-01 06:03:56 +0000584// expected-error@+4 {{could not calculate number of iterations calling 'operator-' with upper and lower loop bounds}}
Alexey Bataevf29276e2014-06-18 04:14:57 +0000585// Initializer is constructor with all default params.
586// expected-warning@+2 {{initialization clause of OpenMP for loop is not in canonical form ('var = init' or 'T var = init')}}
587#pragma omp for
588 for (Iter1 I; I < end1; ++I) {
589 }
Alexey Bataevc0214e02016-02-16 12:13:49 +0000590 GoodIter1 I1, E1;
591#pragma omp for
592 for (GoodIter1 I = I1; I < E1; I++)
593 ;
Alexey Bataevf29276e2014-06-18 04:14:57 +0000594 return 0;
595}
596
597template <typename IT, int ST>
598class TC {
599public:
600 int dotest_lt(IT begin, IT end) {
601#pragma omp parallel
602// expected-note@+3 {{loop step is expected to be positive due to this condition}}
603// expected-error@+2 {{increment expression must cause 'I' to increase on each iteration of OpenMP for loop}}
604#pragma omp for
605 for (IT I = begin; I < end; I = I + ST) {
606 ++I;
607 }
608#pragma omp parallel
609// expected-note@+3 {{loop step is expected to be positive due to this condition}}
610// expected-error@+2 {{increment expression must cause 'I' to increase on each iteration of OpenMP for loop}}
611#pragma omp for
612 for (IT I = begin; I <= end; I += ST) {
613 ++I;
614 }
615#pragma omp parallel
616#pragma omp for
617 for (IT I = begin; I < end; ++I) {
618 ++I;
619 }
620 }
621
622 static IT step() {
623 return IT(ST);
624 }
625};
626template <typename IT, int ST = 0>
627int dotest_gt(IT begin, IT end) {
628#pragma omp parallel
629// expected-note@+3 2 {{loop step is expected to be negative due to this condition}}
630// expected-error@+2 2 {{increment expression must cause 'I' to decrease on each iteration of OpenMP for loop}}
631#pragma omp for
632 for (IT I = begin; I >= end; I = I + ST) {
633 ++I;
634 }
635#pragma omp parallel
636// expected-note@+3 2 {{loop step is expected to be negative due to this condition}}
637// expected-error@+2 2 {{increment expression must cause 'I' to decrease on each iteration of OpenMP for loop}}
638#pragma omp for
639 for (IT I = begin; I >= end; I += ST) {
640 ++I;
641 }
642
643#pragma omp parallel
644// expected-note@+3 {{loop step is expected to be negative due to this condition}}
645// expected-error@+2 {{increment expression must cause 'I' to decrease on each iteration of OpenMP for loop}}
646#pragma omp for
647 for (IT I = begin; I >= end; ++I) {
648 ++I;
649 }
650
651#pragma omp parallel
652#pragma omp for
653 for (IT I = begin; I < end; I += TC<int, ST>::step()) {
654 ++I;
655 }
656}
657
658void test_with_template() {
659 GoodIter begin, end;
660 TC<GoodIter, 100> t1;
661 TC<GoodIter, -100> t2;
662 t1.dotest_lt(begin, end);
663 t2.dotest_lt(begin, end); // expected-note {{in instantiation of member function 'TC<GoodIter, -100>::dotest_lt' requested here}}
664 dotest_gt(begin, end); // expected-note {{in instantiation of function template specialization 'dotest_gt<GoodIter, 0>' requested here}}
Alexey Bataev5372fb82017-08-31 23:06:52 +0000665 dotest_gt<unsigned, 10>(0, 100); // expected-note {{in instantiation of function template specialization 'dotest_gt<unsigned int, 10>' requested here}}
Alexey Bataevf29276e2014-06-18 04:14:57 +0000666}
667
668void test_loop_break() {
669 const int N = 100;
670 float a[N], b[N], c[N];
671#pragma omp parallel
672#pragma omp for
673 for (int i = 0; i < 10; i++) {
674 c[i] = a[i] + b[i];
675 for (int j = 0; j < 10; ++j) {
676 if (a[i] > b[j])
677 break; // OK in nested loop
678 }
679 switch (i) {
680 case 1:
681 b[i]++;
682 break;
683 default:
684 break;
685 }
686 if (c[i] > 10)
687 break; // expected-error {{'break' statement cannot be used in OpenMP for loop}}
688
689 if (c[i] > 11)
690 break; // expected-error {{'break' statement cannot be used in OpenMP for loop}}
691 }
692
693#pragma omp parallel
694#pragma omp for
695 for (int i = 0; i < 10; i++) {
696 for (int j = 0; j < 10; j++) {
697 c[i] = a[i] + b[i];
698 if (c[i] > 10) {
699 if (c[i] < 20) {
700 break; // OK
701 }
702 }
703 }
704 }
705}
706
707void test_loop_eh() {
708 const int N = 100;
709 float a[N], b[N], c[N];
710#pragma omp parallel
711#pragma omp for
712 for (int i = 0; i < 10; i++) {
713 c[i] = a[i] + b[i];
714 try {
715 for (int j = 0; j < 10; ++j) {
716 if (a[i] > b[j])
717 throw a[i];
718 }
719 throw a[i];
720 } catch (float f) {
721 if (f > 0.1)
722 throw a[i];
723 return; // expected-error {{cannot return from OpenMP region}}
724 }
725 switch (i) {
726 case 1:
727 b[i]++;
728 break;
729 default:
730 break;
731 }
732 for (int j = 0; j < 10; j++) {
733 if (c[i] > 10)
734 throw c[i];
735 }
736 }
737 if (c[9] > 10)
738 throw c[9]; // OK
739
740#pragma omp parallel
741#pragma omp for
742 for (int i = 0; i < 10; ++i) {
743 struct S {
744 void g() { throw 0; }
745 };
746 }
747}
748
749void test_loop_firstprivate_lastprivate() {
750 S s(4);
751#pragma omp parallel
752#pragma omp for lastprivate(s) firstprivate(s)
753 for (int i = 0; i < 16; ++i)
754 ;
755}
Alexey Bataev4c904ad2014-07-21 02:45:36 +0000756
757void test_ordered() {
758#pragma omp parallel
759#pragma omp for ordered ordered // expected-error {{directive '#pragma omp for' cannot contain more than one 'ordered' clause}}
760 for (int i = 0; i < 16; ++i)
761 ;
762}
763
764void test_nowait() {
765#pragma omp parallel
766#pragma omp for nowait nowait // expected-error {{directive '#pragma omp for' cannot contain more than one 'nowait' clause}}
767 for (int i = 0; i < 16; ++i)
768 ;
769}