Debian Codegraph @

clang/test/Sema/labeled-break-continue.c

raw
1 // RUN: %clang_cc1 -std=c2y -verify -Wunused -fsyntax-only -fblocks %s
2 // RUN: %clang_cc1 -std=c23 -verify -Wunused -fsyntax-only -fblocks -fnamed-loops %s
3 // RUN: %clang_cc1 -x c++ -verify -Wunused -fsyntax-only -fblocks -fnamed-loops %s
4
5 void f1() {
6 l1: while (true) {
7 break l1;
8 continue l1;
9 }
10
11 l2: for (;;) {
12 break l2;
13 continue l2;
14 }
15
16 l3: do {
17 break l3;
18 continue l3;
19 } while (true);
20
21 l4: switch (1) {
22 case 1:
23 break l4;
24 }
25 }
26
27 void f2() {
28 l1:;
29 break l1; // expected-error {{'break' label does not name an enclosing loop or 'switch'}}
30 continue l1; // expected-error {{'continue' label does not name an enclosing loop}}
31
32 l2: while (true) {
33 break l1; // expected-error {{'break' label does not name an enclosing loop or 'switch'}}
34 continue l1; // expected-error {{'continue' label does not name an enclosing loop}}
35 }
36
37 while (true) {
38 break l2; // expected-error {{'break' label does not name an enclosing loop or 'switch'}}
39 continue l2; // expected-error {{'continue' label does not name an enclosing loop}}
40 }
41
42 break l3; // expected-error {{'break' label does not name an enclosing loop or 'switch'}}
43 continue l3; // expected-error {{'continue' label does not name an enclosing loop}}
44 l3: while (true) {}
45 }
46
47 void f3() {
48 a: b: c: d: while (true) {
49 break a; // expected-error {{'break' label does not name an enclosing loop or 'switch'}}
50 break b; // expected-error {{'break' label does not name an enclosing loop or 'switch'}}
51 break c; // expected-error {{'break' label does not name an enclosing loop or 'switch'}}
52 break d;
53
54 continue a; // expected-error {{'continue' label does not name an enclosing loop}}
55 continue b; // expected-error {{'continue' label does not name an enclosing loop}}
56 continue c; // expected-error {{'continue' label does not name an enclosing loop}}
57 continue d;
58
59 e: while (true) {
60 break a; // expected-error {{'break' label does not name an enclosing loop or 'switch'}}
61 break b; // expected-error {{'break' label does not name an enclosing loop or 'switch'}}
62 break c; // expected-error {{'break' label does not name an enclosing loop or 'switch'}}
63 break d;
64 break e;
65
66 continue a; // expected-error {{'continue' label does not name an enclosing loop}}
67 continue b; // expected-error {{'continue' label does not name an enclosing loop}}
68 continue c; // expected-error {{'continue' label does not name an enclosing loop}}
69 continue d;
70 continue e;
71 }
72
73 break e; // expected-error {{'break' label does not name an enclosing loop or 'switch'}}
74 continue e; // expected-error {{'continue' label does not name an enclosing loop}}
75 }
76 }
77
78 void f4() {
79 a: switch (1) {
80 case 1: {
81 continue a; // expected-error {{label of 'continue' refers to a switch statement}}
82 }
83 }
84 }
85
86 void f5() {
87 a: {
88 break a; // expected-error {{'break' label does not name an enclosing loop or 'switch'}}
89 }
90
91 b: {
92 while (true)
93 break b; // expected-error {{'break' label does not name an enclosing loop or 'switch'}}
94 }
95 }
96
97 void f6() {
98 a: while (({
99 break a; // expected-error {{'break' label does not name an enclosing loop or 'switch'}}
100 continue a; // expected-error {{'continue' label does not name an enclosing loop}}
101 1;
102 })) {
103 ({ break a; });
104 ({ continue a; });
105 }
106
107 b: for (
108 int x = ({
109 break b; // expected-error {{'break' label does not name an enclosing loop or 'switch'}}
110 continue b; // expected-error {{'continue' label does not name an enclosing loop}}
111 1;
112 });
113 ({
114 break b; // expected-error {{'break' label does not name an enclosing loop or 'switch'}}
115 continue b; // expected-error {{'continue' label does not name an enclosing loop}}
116 1;
117 });
118 (void) ({
119 break b; // expected-error {{'break' label does not name an enclosing loop or 'switch'}}
120 continue b; // expected-error {{'continue' label does not name an enclosing loop}}
121 1;
122 })
123 ) {
124 ({ break b; });
125 ({ continue b; });
126 }
127
128 c: do {
129 ({ break c; });
130 ({ continue c; });
131 } while (({
132 break c; // expected-error {{'break' label does not name an enclosing loop or 'switch'}}
133 continue c; // expected-error {{'continue' label does not name an enclosing loop}}
134 1;
135 }));
136
137 d: switch (({
138 break d; // expected-error {{'break' label does not name an enclosing loop or 'switch'}}
139 continue d; // expected-error {{'continue' label does not name an enclosing loop}}
140 1;
141 })) {
142 case 1: {
143 ({ break d; });
144 ({ continue d; }); // expected-error {{label of 'continue' refers to a switch statement}}
145 }
146 }
147 }
148
149 void f7() {
150 a: while (true) {
151 (void) ^{
152 break a; // expected-error {{'break' label does not name an enclosing loop or 'switch'}}
153 continue a; // expected-error {{'continue' label does not name an enclosing loop}}
154 };
155 }
156
157 while (true) {
158 break c; // expected-error {{'break' label does not name an enclosing loop or 'switch'}}
159 continue d; // expected-error {{'continue' label does not name an enclosing loop}}
160 }
161 }
162
163 void f8() {
164 l1: // no-warning
165 while (true) {
166 break l1;
167 }
168
169 l2: // no-warning
170 while (true) {
171 continue l2;
172 }
173 }