blob: 08ab0e0ebd7355ccfa62359dfed6b037a583fb29 [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}