blob: 2690ad28e99a68506cfb02c713373a90d9113092 [file] [log] [blame]
Daniel Dunbara5728872009-12-15 20:14:24 +00001// RUN: %clang_cc1 -fsyntax-only -verify %s
Chris Lattner8a87e572007-07-23 17:05:23 +00002void f (int z) {
3 while (z) {
Chris Lattner0471f5b2007-08-23 18:29:20 +00004 default: z--; // expected-error {{statement not in switch}}
Chris Lattner8a87e572007-07-23 17:05:23 +00005 }
6}
7
Chris Lattnerf4021e72007-08-23 05:46:52 +00008void foo(int X) {
9 switch (X) {
Chris Lattner5f4a6822008-11-23 23:12:31 +000010 case 42: ; // expected-note {{previous case}}
Chris Lattner0471f5b2007-08-23 18:29:20 +000011 case 5000000000LL: // expected-warning {{overflow}}
12 case 42: // expected-error {{duplicate case value}}
Chris Lattnerf4021e72007-08-23 05:46:52 +000013 ;
Chris Lattner6efc4d32007-08-23 17:48:14 +000014
Chris Lattner0471f5b2007-08-23 18:29:20 +000015 case 100 ... 99: ; // expected-warning {{empty case range}}
16
Chris Lattner5f4a6822008-11-23 23:12:31 +000017 case 43: ; // expected-note {{previous case}}
Chris Lattner0471f5b2007-08-23 18:29:20 +000018 case 43 ... 45: ; // expected-error {{duplicate case value}}
19
Chris Lattner5f4a6822008-11-23 23:12:31 +000020 case 100 ... 20000:; // expected-note {{previous case}}
Chris Lattner0471f5b2007-08-23 18:29:20 +000021 case 15000 ... 40000000:; // expected-error {{duplicate case value}}
Chris Lattnerf4021e72007-08-23 05:46:52 +000022 }
23}
24
Chris Lattnerf3348502007-08-23 14:29:07 +000025void test3(void) {
Chris Lattner0471f5b2007-08-23 18:29:20 +000026 // empty switch;
Chris Lattnerf3348502007-08-23 14:29:07 +000027 switch (0);
28}
29
Anders Carlsson51fe9962008-11-22 21:04:56 +000030extern int g();
31
32void test4()
33{
34 switch (1) {
35 case 0 && g():
36 case 1 || g():
37 break;
38 }
39
40 switch(1) {
Anders Carlssond3a61d52008-12-01 02:13:02 +000041 case g(): // expected-error {{expression is not an integer constant expression}}
42 case 0 ... g(): // expected-error {{expression is not an integer constant expression}}
Anders Carlsson51fe9962008-11-22 21:04:56 +000043 break;
44 }
45
46 switch (1) {
47 case 0 && g() ... 1 || g():
48 break;
49 }
Anders Carlsson6dde0d52008-11-22 21:50:49 +000050
51 switch (1) {
Anders Carlssond3a61d52008-12-01 02:13:02 +000052 case g() && 0: // expected-error {{expression is not an integer constant expression}} // expected-note {{subexpression not valid in an integer constant expression}}
Anders Carlsson6dde0d52008-11-22 21:50:49 +000053 break;
54 }
55
56 switch (1) {
Anders Carlssond3a61d52008-12-01 02:13:02 +000057 case 0 ... g() || 1: // expected-error {{expression is not an integer constant expression}} // expected-note {{subexpression not valid in an integer constant expression}}
Anders Carlsson6dde0d52008-11-22 21:50:49 +000058 break;
59 }
Anders Carlsson51fe9962008-11-22 21:04:56 +000060}
61
Chris Lattner5f4a6822008-11-23 23:12:31 +000062void test5(int z) {
63 switch(z) {
64 default: // expected-note {{previous case defined here}}
65 default: // expected-error {{multiple default labels in one switch}}
66 break;
67 }
68}
69
Chris Lattner5f048812009-10-16 16:45:22 +000070void test6() {
71 const char ch = 'a';
72 switch(ch) {
73 case 1234: // expected-warning {{overflow converting case value}}
74 break;
75 }
76}
Douglas Gregorbe724ba2009-11-25 06:20:02 +000077
78// PR5606
Douglas Gregor539c5c32010-01-07 00:31:29 +000079int f0(int var) { // expected-note{{'var' declared here}}
Douglas Gregorbe724ba2009-11-25 06:20:02 +000080 switch (va) { // expected-error{{use of undeclared identifier 'va'}}
81 case 1:
82 break;
83 case 2:
84 return 1;
85 }
86 return 2;
87}
Douglas Gregorba915af2010-02-08 22:24:16 +000088
89void test7() {
90 enum {
91 A = 1,
92 B
93 } a;
94 switch(a) { //expected-warning{{enumeration value 'B' not handled in switch}}
95 case A:
96 break;
97 }
98 switch(a) {
99 case B:
100 case A:
101 break;
102 }
103 switch(a) {
104 case A:
105 case B:
106 case 3: // expected-warning{{case value not in enumerated type ''}}
107 break;
108 }
109 switch(a) {
110 case A:
111 case B:
112 case 3 ... //expected-warning{{case value not in enumerated type ''}}
113 4: //expected-warning{{case value not in enumerated type ''}}
114 break;
115 }
116 switch(a) {
117 case 1 ... 2:
118 break;
119 }
120 switch(a) {
121 case 0 ... 2: //expected-warning{{case value not in enumerated type ''}}
122 break;
123 }
124 switch(a) {
125 case 1 ... 3: //expected-warning{{case value not in enumerated type ''}}
126 break;
127 }
128 switch(a) {
129 case 0 ... //expected-warning{{case value not in enumerated type ''}}
130 3: //expected-warning{{case value not in enumerated type ''}}
131 break;
132 }
133
134}
135
136void test8() {
137 enum {
138 A,
139 B,
140 C = 1
141 } a;
142 switch(a) {
143 case A:
144 case B:
145 break;
146 }
147 switch(a) {
148 case A:
149 case C:
150 break;
151 }
152 switch(a) { //expected-warning{{enumeration value 'B' not handled in switch}}
153 case A:
154 break;
155 }
156}
157
158void test9() {
159 enum {
160 A = 3,
161 C = 1
162 } a;
163 switch(a) {
164 case 0: //expected-warning{{case value not in enumerated type ''}}
165 case 1:
166 case 2: //expected-warning{{case value not in enumerated type ''}}
167 case 3:
168 case 4: //expected-warning{{case value not in enumerated type ''}}
169 break;
170 }
171}
172
173void test10() {
174 enum {
175 A = 10,
176 C = 2,
177 B = 4,
178 D = 12
179 } a;
180 switch(a) {
181 case 0 ... //expected-warning{{case value not in enumerated type ''}}
182 1: //expected-warning{{case value not in enumerated type ''}}
183 case 2 ... 4:
184 case 5 ... //expected-warning{{case value not in enumerated type ''}}
185 9: //expected-warning{{case value not in enumerated type ''}}
186 case 10 ... 12:
187 case 13 ... //expected-warning{{case value not in enumerated type ''}}
188 16: //expected-warning{{case value not in enumerated type ''}}
189 break;
190 }
191}
192
193void test11() {
194 enum {
195 A = -1,
196 B,
197 C
198 } a;
199 switch(a) { //expected-warning{{enumeration value 'A' not handled in switch}}
200 case B:
201 case C:
202 break;
203 }
204
205 switch(a) {
206 case B:
207 case C:
208 break;
209
210 default:
211 break;
212 }
213}
214
215void test12() {
216 enum {
217 A = -1,
218 B = 4294967286
219 } a;
220 switch(a) {
221 case A:
222 case B:
223 break;
224 }
225}