xref: /llvm-project/clang/test/SemaOpenACC/no-branch-in-out.c (revision 39351f8e46e3e42b945ed686537f182b4c313289)
1 // RUN: %clang_cc1 %s -verify -fopenacc
2 
3 void BreakContinue() {
4 
5 #pragma acc parallel
6   for(int i =0; i < 5; ++i) {
7     switch(i) {
8       case 0:
9       break; // leaves switch, not 'for'.
10       default:
11       i +=2;
12       break;
13     }
14     if (i == 2)
15       continue;
16 
17     break;  // expected-error{{invalid branch out of OpenACC Compute/Combined Construct}}
18   }
19 
20 #pragma acc parallel loop
21   for(int i =0; i < 5; ++i) {
22     switch(i) {
23       case 0:
24       break; // leaves switch, not 'for'.
25       default:
26       i +=2;
27       break;
28     }
29     if (i == 2)
30       continue;
31 
32     break;  // expected-error{{invalid branch out of OpenACC Compute/Combined Construct}}
33   }
34 
35   int j;
36   switch(j) {
37     case 0:
38 #pragma acc parallel
39     {
40       break; // expected-error{{invalid branch out of OpenACC Compute/Combined Construct}}
41     }
42     case 1:
43 #pragma acc parallel
44     {
45     }
46     break;
47   }
48 
49 #pragma acc parallel
50   for(int i = 0; i < 5; ++i) {
51     if (i > 1)
52       break; // expected-error{{invalid branch out of OpenACC Compute/Combined Construct}}
53   }
54 
55 #pragma acc parallel loop
56   for(int i = 0; i < 5; ++i) {
57     if (i > 1)
58       break; // expected-error{{invalid branch out of OpenACC Compute/Combined Construct}}
59   }
60 
61 #pragma acc serial
62   for(int i = 0; i < 5; ++i) {
63     if (i > 1)
64       break; // expected-error{{invalid branch out of OpenACC Compute/Combined Construct}}
65   }
66 
67 #pragma acc serial loop
68   for(int i = 0; i < 5; ++i) {
69     if (i > 1)
70       break; // expected-error{{invalid branch out of OpenACC Compute/Combined Construct}}
71   }
72 
73 #pragma acc kernels
74   for(int i = 0; i < 5; ++i) {
75     if (i > 1)
76       break; // expected-error{{invalid branch out of OpenACC Compute/Combined Construct}}
77   }
78 
79 #pragma acc kernels loop
80   for(int i = 0; i < 5; ++i) {
81     if (i > 1)
82       break; // expected-error{{invalid branch out of OpenACC Compute/Combined Construct}}
83   }
84 
85 #pragma acc parallel
86   switch(j) {
87     case 1:
88       break;
89   }
90 
91 #pragma acc parallel
92   {
93     for(int i = 1; i < 100; i++) {
94       if (i > 4)
95         break;
96     }
97   }
98 
99   for (int i =0; i < 5; ++i) {
100 #pragma acc parallel
101     {
102       continue; // expected-error{{invalid branch out of OpenACC Compute/Combined Construct}}
103     }
104   }
105 
106 #pragma acc parallel
107   for (int i =0; i < 5; ++i) {
108     continue;
109   }
110 
111 #pragma acc parallel loop
112   for (int i =0; i < 5; ++i) {
113     continue;
114   }
115 
116 #pragma acc parallel
117   for (int i =0; i < 5; ++i) {
118     {
119       continue;
120     }
121   }
122 
123 #pragma acc parallel loop
124   for (int i =0; i < 5; ++i) {
125     {
126       continue;
127     }
128   }
129 
130   for (int i =0; i < 5; ++i) {
131 #pragma acc parallel
132     {
133       break; // expected-error{{invalid branch out of OpenACC Compute/Combined Construct}}
134     }
135   }
136 
137 #pragma acc parallel
138   while (j) {
139     --j;
140     if (j > 4)
141       break; // expected-error{{invalid branch out of OpenACC Compute/Combined Construct}}
142   }
143 #pragma acc parallel
144   do {
145     --j;
146     if (j > 4)
147       break; // expected-error{{invalid branch out of OpenACC Compute/Combined Construct}}
148   } while (j );
149 }
150 
151 void Return() {
152 #pragma acc parallel
153   {
154     return;// expected-error{{invalid return out of OpenACC Compute/Combined Construct}}
155   }
156 
157 #pragma acc parallel loop
158   for (unsigned i = 0; i < 5; ++i) {
159     return;// expected-error{{invalid return out of OpenACC Compute/Combined Construct}}
160   }
161 
162 #pragma acc serial
163   {
164     return;// expected-error{{invalid return out of OpenACC Compute/Combined Construct}}
165   }
166 
167 #pragma acc serial loop
168   for (unsigned i = 0; i < 5; ++i) {
169     return;// expected-error{{invalid return out of OpenACC Compute/Combined Construct}}
170   }
171 
172 #pragma acc kernels
173   {
174     return;// expected-error{{invalid return out of OpenACC Compute/Combined Construct}}
175   }
176 
177 #pragma acc kernels loop
178   for (unsigned i = 0; i < 5; ++i) {
179     return;// expected-error{{invalid return out of OpenACC Compute/Combined Construct}}
180   }
181 
182 #pragma acc parallel
183   {
184     {
185       return;// expected-error{{invalid return out of OpenACC Compute/Combined Construct}}
186     }
187   }
188 
189 #pragma acc parallel loop
190   for (unsigned i = 0; i < 5; ++i) {
191     {
192       return;// expected-error{{invalid return out of OpenACC Compute/Combined Construct}}
193     }
194   }
195 
196 #pragma acc parallel
197   {
198     for (int i = 0; i < 5; ++i) {
199       return;// expected-error{{invalid return out of OpenACC Compute/Combined Construct}}
200     }
201   }
202 
203 #pragma acc parallel loop
204   for (unsigned i = 0; i < 5; ++i) {
205     for (int i = 0; i < 5; ++i) {
206       return;// expected-error{{invalid return out of OpenACC Compute/Combined Construct}}
207     }
208   }
209 }
210 
211 void Goto() {
212   int j;
213 #pragma acc parallel // expected-note{{invalid branch out of OpenACC Compute/Combined Construct}}
214   while(j) {
215     if (j <3)
216       goto LABEL; // expected-error{{cannot jump from this goto statement to its label}}
217   }
218 
219 LABEL:
220   {}
221 
222   goto LABEL_IN; // expected-error{{cannot jump from this goto statement to its label}}
223 #pragma acc parallel // expected-note{{invalid branch into OpenACC Compute/Combined Construct}}
224   for(int i = 0; i < 5; ++i) {
225 LABEL_IN:
226     {}
227   }
228 
229   int i;
230   goto LABEL_IN2; // expected-error{{cannot jump from this goto statement to its label}}
231 #pragma acc parallel loop // expected-note{{invalid branch into OpenACC Compute/Combined Construct}}
232   for(i = 0; i < 5; ++i) {
233 LABEL_IN2:
234     {}
235   }
236 
237 #pragma acc parallel
238   for(int i = 0; i < 5; ++i) {
239 LABEL_NOT_CALLED:
240     {}
241   }
242 
243 #pragma acc parallel loop
244   for(int i = 0; i < 5; ++i) {
245 LABEL_NOT_CALLED2:
246     {}
247   }
248 
249 #pragma acc parallel
250   {
251     goto ANOTHER_LOOP; // expected-error{{cannot jump from this goto statement to its label}}
252 
253   }
254 
255 #pragma acc parallel loop
256   for (unsigned i = 0; i < 5; ++i) {
257     goto ANOTHER_LOOP2; // expected-error{{cannot jump from this goto statement to its label}}
258 
259   }
260 
261 #pragma acc parallel// expected-note{{invalid branch into OpenACC Compute/Combined Construct}}
262   {
263 ANOTHER_LOOP:
264     {}
265   }
266 
267 #pragma acc parallel loop// expected-note{{invalid branch into OpenACC Compute/Combined Construct}}
268   for (unsigned i = 0; i < 5; ++i) {
269 ANOTHER_LOOP2:
270     {}
271   }
272 
273 #pragma acc parallel
274   {
275   while (j) {
276     --j;
277     if (j < 3)
278       goto LABEL2;
279 
280     if (j > 4)
281       break;
282   }
283 LABEL2:
284   {}
285   }
286 
287 #pragma acc parallel loop
288   for (unsigned i = 0; i < 5; ++i) {
289   while (j) {
290     --j;
291     if (j < 3)
292       goto LABEL2_2;
293 
294     if (j > 4)
295       break;
296   }
297 LABEL2_2:
298   {}
299   }
300 
301 
302 #pragma acc parallel
303   do {
304     if (j < 3)
305       goto LABEL3;
306 
307     if (j > 4)
308       break; // expected-error{{invalid branch out of OpenACC Compute/Combined Construct}}
309 
310 LABEL3:
311   {}
312   } while (j);
313 
314 
315 LABEL4:
316   {}
317 
318 #pragma acc parallel// expected-note{{invalid branch out of OpenACC Compute/Combined Construct}}
319   {
320     goto LABEL4;// expected-error{{cannot jump from this goto statement to its label}}
321   }
322 
323 #pragma acc parallel loop// expected-note{{invalid branch out of OpenACC Compute/Combined Construct}}
324   for (unsigned i = 0; i < 5; ++i) {
325     goto LABEL4;// expected-error{{cannot jump from this goto statement to its label}}
326   }
327 
328 
329 #pragma acc parallel// expected-note{{invalid branch into OpenACC Compute/Combined Construct}}
330   {
331 LABEL5:
332     {}
333   }
334 
335 #pragma acc parallel loop// expected-note{{invalid branch into OpenACC Compute/Combined Construct}}
336   for (unsigned i = 0; i < 5; ++i) {
337 LABEL5_2:
338     {}
339   }
340 
341   {
342     goto LABEL5;// expected-error{{cannot jump from this goto statement to its label}}
343     goto LABEL5_2;// expected-error{{cannot jump from this goto statement to its label}}
344   }
345 
346 #pragma acc parallel
347   {
348 LABEL6:
349     {}
350     goto LABEL6;
351 
352   }
353 
354 #pragma acc parallel loop
355   for (unsigned i = 0; i < 5; ++i) {
356 LABEL6_2:
357     {}
358     goto LABEL6_2;
359 
360   }
361 
362 #pragma acc parallel
363   goto LABEL7; // expected-error{{cannot jump from this goto statement to its label}}
364 #pragma acc parallel// expected-note{{invalid branch into OpenACC Compute/Combined Construct}}
365   {
366 LABEL7:{}
367   }
368 
369 #pragma acc parallel
370   LABEL8:{}
371 #pragma acc parallel// expected-note{{invalid branch out of OpenACC Compute/Combined Construct}}
372   {
373     goto LABEL8;// expected-error{{cannot jump from this goto statement to its label}}
374   }
375 
376 #pragma acc parallel// expected-note{{invalid branch into OpenACC Compute/Combined Construct}}
377   {
378 LABEL9:{}
379   }
380 
381   ({goto LABEL9;});// expected-error{{cannot jump from this goto statement to its label}}
382 
383 #pragma acc parallel loop// expected-note{{invalid branch into OpenACC Compute/Combined Construct}}
384   for (unsigned i = 0; i < 5; ++i) {
385 LABEL9_2:{}
386   }
387 
388   ({goto LABEL9_2;});// expected-error{{cannot jump from this goto statement to its label}}
389 
390 
391 #pragma acc parallel// expected-note{{invalid branch out of OpenACC Compute/Combined Construct}}
392   for (unsigned i = 0; i < 5; ++i) {
393   ({goto LABEL10;});// expected-error{{cannot jump from this goto statement to its label}}
394   }
395 
396 LABEL10:{}
397 
398 #pragma acc parallel loop// expected-note{{invalid branch out of OpenACC Compute/Combined Construct}}
399   for (unsigned i = 0; i < 5; ++i) {
400   ({goto LABEL10_2;});// expected-error{{cannot jump from this goto statement to its label}}
401   }
402 
403 LABEL10_2:{}
404 
405   ({goto LABEL11;});// expected-error{{cannot jump from this goto statement to its label}}
406 #pragma acc parallel// expected-note{{invalid branch into OpenACC Compute/Combined Construct}}
407   {
408 LABEL11:{}
409   }
410 
411   ({goto LABEL11_2;});// expected-error{{cannot jump from this goto statement to its label}}
412 #pragma acc parallel loop// expected-note{{invalid branch into OpenACC Compute/Combined Construct}}
413   for (unsigned i = 0; i < 5; ++i) {
414 LABEL11_2:{}
415   }
416 
417 LABEL12:{}
418 #pragma acc parallel// expected-note{{invalid branch out of OpenACC Compute/Combined Construct}}
419   {
420   ({goto LABEL12;});// expected-error{{cannot jump from this goto statement to its label}}
421   }
422 
423 LABEL12_2:{}
424 #pragma acc parallel loop// expected-note{{invalid branch out of OpenACC Compute/Combined Construct}}
425   for (unsigned i = 0; i < 5; ++i) {
426   ({goto LABEL12_2;});// expected-error{{cannot jump from this goto statement to its label}}
427   }
428 
429 #pragma acc parallel
430   {
431   ({goto LABEL13;});
432 LABEL13:{}
433   }
434 
435 #pragma acc parallel loop
436   for (unsigned i = 0; i < 5; ++i) {
437   ({goto LABEL13_2;});
438 LABEL13_2:{}
439   }
440 
441 #pragma acc parallel
442   {
443   LABEL14:{}
444   ({goto LABEL14;});
445   }
446 
447 #pragma acc parallel loop
448   for (unsigned i = 0; i < 5; ++i) {
449   LABEL14_2:{}
450   ({goto LABEL14_2;});
451   }
452 
453 
454 
455   ({goto LABEL15;});// expected-error{{cannot jump from this goto statement to its label}}
456 #pragma acc serial// expected-note{{invalid branch into OpenACC Compute/Combined Construct}}
457   {
458 LABEL15:{}
459   }
460   ({goto LABEL15_2;});// expected-error{{cannot jump from this goto statement to its label}}
461 #pragma acc serial loop// expected-note{{invalid branch into OpenACC Compute/Combined Construct}}
462   for (unsigned i = 0; i < 5; ++i) {
463 LABEL15_2:{}
464   }
465 
466 LABEL16:{}
467 #pragma acc serial// expected-note{{invalid branch out of OpenACC Compute/Combined Construct}}
468   {
469   ({goto LABEL16;});// expected-error{{cannot jump from this goto statement to its label}}
470   }
471 
472 LABEL16_2:{}
473 #pragma acc serial loop// expected-note{{invalid branch out of OpenACC Compute/Combined Construct}}
474   for (unsigned i = 0; i < 5; ++i) {
475   ({goto LABEL16_2;});// expected-error{{cannot jump from this goto statement to its label}}
476   }
477 
478   ({goto LABEL17;});// expected-error{{cannot jump from this goto statement to its label}}
479 #pragma acc kernels// expected-note{{invalid branch into OpenACC Compute/Combined Construct}}
480   {
481 LABEL17:{}
482   }
483 
484   ({goto LABEL17_2;});// expected-error{{cannot jump from this goto statement to its label}}
485 #pragma acc kernels loop// expected-note{{invalid branch into OpenACC Compute/Combined Construct}}
486   for (unsigned i = 0; i < 5; ++i) {
487 LABEL17_2:{}
488   }
489 
490 LABEL18:{}
491 #pragma acc kernels// expected-note{{invalid branch out of OpenACC Compute/Combined Construct}}
492   {
493   ({goto LABEL18;});// expected-error{{cannot jump from this goto statement to its label}}
494   }
495 
496 LABEL18_2:{}
497 #pragma acc kernels loop// expected-note{{invalid branch out of OpenACC Compute/Combined Construct}}
498   for (unsigned i = 0; i < 5; ++i) {
499   ({goto LABEL18_2;});// expected-error{{cannot jump from this goto statement to its label}}
500   }
501 }
502 
503 void IndirectGoto1() {
504   void* ptr;
505 #pragma acc parallel
506   {
507 LABEL1:{}
508     ptr = &&LABEL1;
509     goto *ptr;
510 
511   }
512 }
513 
514 void IndirectGoto1_Loop() {
515   void *ptr;
516 #pragma acc parallel loop
517   for (unsigned i = 0; i < 5; ++i) {
518 LABEL1:{}
519     ptr = &&LABEL1;
520 
521     goto *ptr;
522 
523   }
524 }
525 
526 void IndirectGoto2() {
527   void* ptr;
528 LABEL2:{} // #GOTOLBL2
529     ptr = &&LABEL2;
530 #pragma acc parallel // #GOTOPAR2
531   {
532 // expected-error@+3{{cannot jump from this indirect goto statement to one of its possible targets}}
533 // expected-note@#GOTOLBL2{{possible target of indirect goto statement}}
534 // expected-note@#GOTOPAR2{{invalid branch out of OpenACC Compute/Combined Construct}}
535     goto *ptr;
536   }
537 
538 #pragma acc parallel loop // #GOTOPAR_LOOP2
539   for (unsigned i = 0; i < 5; ++i) {
540 // expected-error@+3{{cannot jump from this indirect goto statement to one of its possible targets}}
541 // expected-note@#GOTOLBL2{{possible target of indirect goto statement}}
542 // expected-note@#GOTOPAR_LOOP2{{invalid branch out of OpenACC Compute/Combined Construct}}
543     goto *ptr;
544   }
545 }
546 
547 void IndirectGoto3() {
548   void* ptr;
549 #pragma acc parallel // #GOTOPAR3
550   {
551 LABEL3:{} // #GOTOLBL3
552     ptr = &&LABEL3;
553   }
554 // expected-error@+3{{cannot jump from this indirect goto statement to one of its possible targets}}
555 // expected-note@#GOTOLBL3{{possible target of indirect goto statement}}
556 // expected-note@#GOTOPAR3{{invalid branch into OpenACC Compute/Combined Construct}}
557   goto *ptr;
558 }
559 
560 void IndirectGoto3_Loop() {
561   void* ptr;
562 #pragma acc parallel loop// #GOTOPAR_LOOP3
563   for (unsigned i = 0; i < 5; ++i) {
564 LABEL3:{} // #GOTOLBL3_2
565     ptr = &&LABEL3;
566   }
567 // expected-error@+3{{cannot jump from this indirect goto statement to one of its possible targets}}
568 // expected-note@#GOTOLBL3_2{{possible target of indirect goto statement}}
569 // expected-note@#GOTOPAR_LOOP3{{invalid branch into OpenACC Compute/Combined Construct}}
570   goto *ptr;
571 }
572 
573 void IndirectGoto4() {
574   void* ptr;
575 #pragma acc parallel // #GOTOPAR4
576   {
577 LABEL4:{}
578     ptr = &&LABEL4;
579 // expected-error@+3{{cannot jump from this indirect goto statement to one of its possible targets}}
580 // expected-note@#GOTOLBL5{{possible target of indirect goto statement}}
581 // expected-note@#GOTOPAR4{{invalid branch out of OpenACC Compute/Combined Construct}}
582     goto *ptr;
583   }
584 LABEL5:// #GOTOLBL5
585 
586   ptr=&&LABEL5;
587 }
588 
589 void IndirectGoto4_2() {
590   void* ptr;
591 #pragma acc parallel loop // #GOTOPAR_LOOP4
592   for (unsigned i = 0; i < 5; ++i) {
593 LABEL4:{}
594     ptr = &&LABEL4;
595 // expected-error@+3{{cannot jump from this indirect goto statement to one of its possible targets}}
596 // expected-note@#GOTOLBL_LOOP5{{possible target of indirect goto statement}}
597 // expected-note@#GOTOPAR_LOOP4{{invalid branch out of OpenACC Compute/Combined Construct}}
598     goto *ptr;
599   }
600 LABEL5:// #GOTOLBL_LOOP5
601 
602   ptr=&&LABEL5;
603 }
604 
605 void DuffsDevice() {
606   int j;
607   switch (j) {
608 #pragma acc parallel
609   for(int i =0; i < 5; ++i) {
610     case 0: // expected-error{{invalid branch into OpenACC Compute/Combined Construct}}
611       {}
612   }
613   }
614 
615   switch (j) {
616 #pragma acc parallel
617   for(int i =0; i < 5; ++i) {
618     default: // expected-error{{invalid branch into OpenACC Compute/Combined Construct}}
619       {}
620   }
621   }
622 
623   switch (j) {
624 #pragma acc kernels
625   for(int i =0; i < 5; ++i) {
626     default: // expected-error{{invalid branch into OpenACC Compute/Combined Construct}}
627       {}
628   }
629   }
630 
631   switch (j) {
632 #pragma acc parallel
633   for(int i =0; i < 5; ++i) {
634     case 'a' ... 'z': // expected-error{{invalid branch into OpenACC Compute/Combined Construct}}
635       {}
636   }
637   }
638 
639   switch (j) {
640 #pragma acc serial
641   for(int i =0; i < 5; ++i) {
642     case 'a' ... 'z': // expected-error{{invalid branch into OpenACC Compute/Combined Construct}}
643       {}
644   }
645   }
646 }
647 
648 void DuffsDeviceLoop() {
649   int j;
650   switch (j) {
651 #pragma acc parallel loop
652   for(int i =0; i < 5; ++i) {
653     case 0: // expected-error{{invalid branch into OpenACC Compute/Combined Construct}}
654       {}
655   }
656   }
657 
658   switch (j) {
659 #pragma acc parallel loop
660   for(int i =0; i < 5; ++i) {
661     default: // expected-error{{invalid branch into OpenACC Compute/Combined Construct}}
662       {}
663   }
664   }
665 
666   switch (j) {
667 #pragma acc kernels loop
668   for(int i =0; i < 5; ++i) {
669     default: // expected-error{{invalid branch into OpenACC Compute/Combined Construct}}
670       {}
671   }
672   }
673 
674   switch (j) {
675 #pragma acc parallel loop
676   for(int i =0; i < 5; ++i) {
677     case 'a' ... 'z': // expected-error{{invalid branch into OpenACC Compute/Combined Construct}}
678       {}
679   }
680   }
681 
682   switch (j) {
683 #pragma acc serial loop
684   for(int i =0; i < 5; ++i) {
685     case 'a' ... 'z': // expected-error{{invalid branch into OpenACC Compute/Combined Construct}}
686       {}
687   }
688   }
689 }
690