switch.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  1. // RUN: %clang_cc1 -fsyntax-only -verify -Wswitch-enum -Wcovered-switch-default -triple x86_64-linux-gnu %s
  2. void f (int z) {
  3. while (z) {
  4. default: z--; // expected-error {{statement not in switch}}
  5. }
  6. }
  7. void foo(int X) {
  8. switch (X) {
  9. case 42: ; // expected-note {{previous case}}
  10. case 5000000000LL: // expected-warning {{overflow}}
  11. case 42: // expected-error {{duplicate case value '42'}}
  12. ;
  13. case 100 ... 99: ; // expected-warning {{empty case range}}
  14. case 43: ; // expected-note {{previous case}}
  15. case 43 ... 45: ; // expected-error {{duplicate case value}}
  16. case 100 ... 20000:; // expected-note {{previous case}}
  17. case 15000 ... 40000000:; // expected-error {{duplicate case value}}
  18. }
  19. }
  20. void test3(void) {
  21. // empty switch;
  22. switch (0); // expected-warning {{no case matching constant switch condition '0'}} \
  23. // expected-warning {{switch statement has empty body}} \
  24. // expected-note{{put the semicolon on a separate line to silence this warning}}
  25. }
  26. extern int g();
  27. void test4()
  28. {
  29. int cond;
  30. switch (cond) {
  31. case 0 && g():
  32. case 1 || g():
  33. break;
  34. }
  35. switch(cond) {
  36. case g(): // expected-error {{expression is not an integer constant expression}}
  37. case 0 ... g(): // expected-error {{expression is not an integer constant expression}}
  38. break;
  39. }
  40. switch (cond) {
  41. case 0 && g() ... 1 || g():
  42. break;
  43. }
  44. switch (cond) {
  45. case g() // expected-error {{expression is not an integer constant expression}}
  46. && 0:
  47. break;
  48. }
  49. switch (cond) {
  50. case 0 ...
  51. g() // expected-error {{expression is not an integer constant expression}}
  52. || 1:
  53. break;
  54. }
  55. }
  56. void test5(int z) {
  57. switch(z) {
  58. default: // expected-note {{previous case defined here}}
  59. default: // expected-error {{multiple default labels in one switch}}
  60. break;
  61. }
  62. }
  63. void test6() {
  64. char ch = 'a';
  65. switch(ch) {
  66. case 1234: // expected-warning {{overflow converting case value}}
  67. break;
  68. }
  69. }
  70. // PR5606
  71. int f0(int var) {
  72. switch (va) { // expected-error{{use of undeclared identifier 'va'}}
  73. case 1:
  74. break;
  75. case 2:
  76. return 1;
  77. }
  78. return 2;
  79. }
  80. void test7() {
  81. enum {
  82. A = 1,
  83. B
  84. } a;
  85. switch(a) { //expected-warning{{enumeration value 'B' not handled in switch}}
  86. case A:
  87. break;
  88. }
  89. switch(a) {
  90. case B:
  91. case A:
  92. break;
  93. }
  94. switch(a) {
  95. case A:
  96. case B:
  97. case 3: // expected-warning{{case value not in enumerated type 'enum (anonymous enum}}
  98. break;
  99. }
  100. switch(a) {
  101. case A:
  102. case B:
  103. case 3 ... //expected-warning{{case value not in enumerated type 'enum (anonymous enum}}
  104. 4: //expected-warning{{case value not in enumerated type 'enum (anonymous enum}}
  105. break;
  106. }
  107. switch(a) {
  108. case 1 ... 2:
  109. break;
  110. }
  111. switch(a) {
  112. case 0 ... 2: //expected-warning{{case value not in enumerated type 'enum (anonymous enum}}
  113. break;
  114. }
  115. switch(a) {
  116. case 1 ... 3: //expected-warning{{case value not in enumerated type 'enum (anonymous enum}}
  117. break;
  118. }
  119. switch(a) {
  120. case 0 ... //expected-warning{{case value not in enumerated type 'enum (anonymous enum}}
  121. 3: //expected-warning{{case value not in enumerated type 'enum (anonymous enum}}
  122. break;
  123. }
  124. }
  125. void test8() {
  126. enum {
  127. A,
  128. B,
  129. C = 1
  130. } a;
  131. switch(a) {
  132. case A:
  133. case B:
  134. break;
  135. }
  136. switch(a) {
  137. case A:
  138. case C:
  139. break;
  140. }
  141. switch(a) { //expected-warning{{enumeration value 'B' not handled in switch}}
  142. case A:
  143. break;
  144. }
  145. }
  146. void test9() {
  147. enum {
  148. A = 3,
  149. C = 1
  150. } a;
  151. switch(a) {
  152. case 0: //expected-warning{{case value not in enumerated type 'enum (anonymous enum}}
  153. case 1:
  154. case 2: //expected-warning{{case value not in enumerated type 'enum (anonymous enum}}
  155. case 3:
  156. case 4: //expected-warning{{case value not in enumerated type 'enum (anonymous enum}}
  157. break;
  158. }
  159. }
  160. void test10() {
  161. enum {
  162. A = 10,
  163. C = 2,
  164. B = 4,
  165. D = 12
  166. } a;
  167. switch(a) {
  168. case 0 ... //expected-warning{{case value not in enumerated type 'enum (anonymous enum}}
  169. 1: //expected-warning{{case value not in enumerated type 'enum (anonymous enum}}
  170. case 2 ... 4:
  171. case 5 ... //expected-warning{{case value not in enumerated type 'enum (anonymous enum}}
  172. 9: //expected-warning{{case value not in enumerated type 'enum (anonymous enum}}
  173. case 10 ... 12:
  174. case 13 ... //expected-warning{{case value not in enumerated type 'enum (anonymous enum}}
  175. 16: //expected-warning{{case value not in enumerated type 'enum (anonymous enum}}
  176. break;
  177. }
  178. }
  179. void test11() {
  180. enum {
  181. A = -1,
  182. B,
  183. C
  184. } a;
  185. switch(a) { //expected-warning{{enumeration value 'A' not handled in switch}}
  186. case B:
  187. case C:
  188. break;
  189. }
  190. switch(a) { //expected-warning{{enumeration value 'A' not explicitly handled in switch}}
  191. case B:
  192. case C:
  193. break;
  194. default:
  195. break;
  196. }
  197. }
  198. void test12() {
  199. enum {
  200. A = -1,
  201. B = 4294967286
  202. } a;
  203. switch(a) {
  204. case A:
  205. case B:
  206. break;
  207. }
  208. }
  209. // <rdar://problem/7643909>
  210. typedef enum {
  211. val1,
  212. val2,
  213. val3
  214. } my_type_t;
  215. int test13(my_type_t t) {
  216. switch(t) { // expected-warning{{enumeration value 'val3' not handled in switch}}
  217. case val1:
  218. return 1;
  219. case val2:
  220. return 2;
  221. }
  222. return -1;
  223. }
  224. // <rdar://problem/7658121>
  225. enum {
  226. EC0 = 0xFFFF0000,
  227. EC1 = 0xFFFF0001,
  228. };
  229. int test14(int a) {
  230. switch(a) {
  231. case EC0: return 0;
  232. case EC1: return 1;
  233. }
  234. return 0;
  235. }
  236. void f1(unsigned x) {
  237. switch (x) {
  238. case -1: break;
  239. default: break;
  240. }
  241. }
  242. void test15() {
  243. int i = 0;
  244. switch (1) { // expected-warning {{no case matching constant switch condition '1'}}
  245. case 0: i = 0; break;
  246. case 2: i++; break;
  247. }
  248. }
  249. void test16() {
  250. const char c = '5';
  251. switch (c) { // expected-warning {{no case matching constant switch condition '53'}}
  252. case '6': return;
  253. }
  254. }
  255. // PR7359
  256. void test17(int x) {
  257. switch (x >= 17) { // expected-warning {{switch condition has boolean value}}
  258. case 0: return;
  259. }
  260. switch ((int) (x <= 17)) {
  261. case 0: return;
  262. }
  263. }
  264. int test18() {
  265. enum { A, B } a;
  266. switch (a) {
  267. case A: return 0;
  268. case B: return 1;
  269. case 7: return 1; // expected-warning {{case value not in enumerated type}}
  270. default: return 2; // expected-warning {{default label in switch which covers all enumeration values}}
  271. }
  272. }
  273. // rdar://110822110
  274. typedef enum {
  275. kOne = 1,
  276. } Ints;
  277. void rdar110822110(Ints i)
  278. {
  279. switch (i) {
  280. case kOne:
  281. break;
  282. case 2: // expected-warning {{case value not in enumerated type 'Ints'}}
  283. break;
  284. default: // expected-warning {{default label in switch which covers all enumeration values}}
  285. break;
  286. }
  287. }
  288. // PR9243
  289. #define TEST19MACRO 5
  290. void test19(int i) {
  291. enum {
  292. kTest19Enum1 = 7,
  293. kTest19Enum2 = kTest19Enum1
  294. };
  295. const int a = 3;
  296. switch (i) {
  297. case 5: // expected-note {{previous case}}
  298. case TEST19MACRO: // expected-error {{duplicate case value '5'}}
  299. case 7: // expected-note {{previous case}}
  300. case kTest19Enum1: // expected-error {{duplicate case value: '7' and 'kTest19Enum1' both equal '7'}} \
  301. // expected-note {{previous case}}
  302. case kTest19Enum1: // expected-error {{duplicate case value 'kTest19Enum1'}} \
  303. // expected-note {{previous case}}
  304. case kTest19Enum2: // expected-error {{duplicate case value: 'kTest19Enum1' and 'kTest19Enum2' both equal '7'}} \
  305. // expected-note {{previous case}}
  306. case (int)kTest19Enum2: //expected-error {{duplicate case value 'kTest19Enum2'}}
  307. case 3: // expected-note {{previous case}}
  308. case a: // expected-error {{duplicate case value: '3' and 'a' both equal '3'}} \
  309. // expected-note {{previous case}}
  310. case a: // expected-error {{duplicate case value 'a'}}
  311. break;
  312. }
  313. }
  314. // Allow the warning 'case value not in enumerated type' to be silenced with
  315. // the following pattern.
  316. //
  317. // If 'case' expression refers to a static const variable of the correct enum
  318. // type, then we count this as a sufficient declaration of intent by the user,
  319. // so we silence the warning.
  320. enum ExtendedEnum1 {
  321. EE1_a,
  322. EE1_b
  323. };
  324. enum ExtendedEnum1_unrelated { EE1_misc };
  325. static const enum ExtendedEnum1 EE1_c = 100;
  326. static const enum ExtendedEnum1_unrelated EE1_d = 101;
  327. void switch_on_ExtendedEnum1(enum ExtendedEnum1 e) {
  328. switch(e) {
  329. case EE1_a: break;
  330. case EE1_b: break;
  331. case EE1_c: break; // no-warning
  332. case EE1_d: break; // expected-warning {{case value not in enumerated type 'enum ExtendedEnum1'}}
  333. }
  334. }
  335. void PR11778(char c, int n, long long ll) {
  336. // Do not reject this; we don't have duplicate case values because we
  337. // check for duplicates in the promoted type.
  338. switch (c) case 1: case 257: ; // expected-warning {{overflow}}
  339. switch (n) case 0x100000001LL: case 1: ; // expected-warning {{overflow}} expected-error {{duplicate}} expected-note {{previous}}
  340. switch ((int)ll) case 0x100000001LL: case 1: ; // expected-warning {{overflow}} expected-error {{duplicate}} expected-note {{previous}}
  341. switch ((long long)n) case 0x100000001LL: case 1: ;
  342. switch (ll) case 0x100000001LL: case 1: ;
  343. }