lsym_case_label.c revision 1.11
11.11Srillig/* $NetBSD: lsym_case_label.c,v 1.11 2023/06/15 09:19:07 rillig Exp $ */ 21.1Srillig 31.1Srillig/* 41.7Srillig * Tests for the tokens lsym_case and lsym_default, which represent the 51.7Srillig * keywords 'case' and 'default', which are both used in 'switch' statements. 61.1Srillig * 71.1Srillig * Since C11, the keyword 'default' is used in _Generic selections as well. 81.1Srillig * 91.1Srillig * See also: 101.2Srillig * opt_cli.c 111.1Srillig * psym_switch_expr.c 121.1Srillig * C11 6.5.1.1 "Generic selection" 131.1Srillig */ 141.1Srillig 151.4Srillig/* 161.4Srillig * A case label can be used in a 'switch' statement. 171.4Srillig */ 181.6Srillig//indent input 191.4Srilligvoid function(void){switch(expr){case 1:;case 2:break;default:switch(inner){case 4:break;}}} 201.6Srillig//indent end 211.1Srillig 221.6Srillig//indent run 231.4Srilligvoid 241.4Srilligfunction(void) 251.4Srillig{ 261.4Srillig switch (expr) { 271.4Srillig case 1: ; 281.4Srillig case 2: 291.4Srillig break; 301.4Srillig default: 311.4Srillig switch (inner) { 321.4Srillig case 4: 331.4Srillig break; 341.4Srillig } 351.4Srillig } 361.4Srillig} 371.6Srillig//indent end 381.3Srillig 391.3Srillig 401.3Srillig/* 411.3Srillig * If there is a '{' after a case label, it gets indented using tabs instead 421.3Srillig * of spaces. Indent does not necessarily insert a space in this situation, 431.3Srillig * which looks strange. 441.3Srillig */ 451.6Srillig//indent input 461.3Srilligvoid 471.3Srilligfunction(void) 481.3Srillig{ 491.3Srillig switch (expr) { 501.3Srillig case 1: { 511.3Srillig break; 521.3Srillig } 531.3Srillig case 11: { 541.3Srillig break; 551.3Srillig } 561.3Srillig } 571.3Srillig} 581.6Srillig//indent end 591.3Srillig 601.6Srillig//indent run 611.3Srilligvoid 621.3Srilligfunction(void) 631.3Srillig{ 641.3Srillig switch (expr) { 651.3Srillig /* $ The space between the ':' and the '{' is actually a tab. */ 661.3Srillig case 1: { 671.3Srillig break; 681.3Srillig } 691.8Srillig case 11: { 701.3Srillig break; 711.3Srillig } 721.3Srillig } 731.3Srillig} 741.6Srillig//indent end 751.4Srillig 761.4Srillig 771.4Srillig/* 781.4Srillig * Since C11, the _Generic selection expression allows a switch on the data 791.4Srillig * type of an expression. 801.4Srillig */ 811.6Srillig//indent input 821.4Srilligconst char *type_name = _Generic( 831.4Srillig ' ', 841.4Srillig int: "character constants have type 'int'", 851.4Srillig char: "character constants have type 'char'", 861.4Srillig default: "character constants have some other type" 871.4Srillig); 881.6Srillig//indent end 891.4Srillig 901.6Srillig//indent run -di0 911.4Srilligconst char *type_name = _Generic( 921.4Srillig// $ XXX: It's strange to align the arguments at the parenthesis even though 931.4Srillig// $ XXX: the first argument is already on a separate line. 941.4Srillig ' ', 951.10Srillig// $ The indentation is so large that the strings would spill over the right 961.10Srillig// $ margin. To prevent that, the code is right-aligned. Since the base 971.10Srillig// $ indentation of this declaration is 0, the code might even start at the 981.10Srillig// $ beginning of the line. 991.9Srillig int: "character constants have type 'int'", 1001.9Srillig char: "character constants have type 'char'", 1011.9Srillig default: "character constants have some other type" 1021.4Srillig); 1031.6Srillig//indent end 1041.4Srillig 1051.9Srillig//indent run-equals-input -di0 -nlp 1061.11Srillig 1071.11Srillig 1081.11Srillig/* 1091.11Srillig * Multi-line case expressions are rare but still should be processed in a 1101.11Srillig * sensible way. 1111.11Srillig */ 1121.11Srillig//indent input 1131.11Srillig{ 1141.11Srillig switch (expr) { 1151.11Srillig// $ FIXME: The line containing the 'case' must be indented like a 'case'. 1161.11Srillig case 1 1171.11Srillig + 2 1181.11Srillig// $ FIXME: This continuation line must be indented by 4 columns. 1191.11Srillig + 3: 1201.11Srillig stmt; 1211.11Srillig } 1221.11Srillig} 1231.11Srillig//indent end 1241.11Srillig 1251.11Srillig//indent run-equals-input -ci4 126