xref: /netbsd-src/tests/usr.bin/indent/lsym_case_label.c (revision 0982fd88d1a4358af669ce182835c8c00eae5dae)
1 /* $NetBSD: lsym_case_label.c,v 1.11 2023/06/15 09:19:07 rillig Exp $ */
2 
3 /*
4  * Tests for the tokens lsym_case and lsym_default, which represent the
5  * keywords 'case' and 'default', which are both used in 'switch' statements.
6  *
7  * Since C11, the keyword 'default' is used in _Generic selections as well.
8  *
9  * See also:
10  *	opt_cli.c
11  *	psym_switch_expr.c
12  *	C11 6.5.1.1		"Generic selection"
13  */
14 
15 /*
16  * A case label can be used in a 'switch' statement.
17  */
18 //indent input
function(void)19 void function(void){switch(expr){case 1:;case 2:break;default:switch(inner){case 4:break;}}}
20 //indent end
21 
22 //indent run
23 void
function(void)24 function(void)
25 {
26 	switch (expr) {
27 	case 1:	;
28 	case 2:
29 		break;
30 	default:
31 		switch (inner) {
32 		case 4:
33 			break;
34 		}
35 	}
36 }
37 //indent end
38 
39 
40 /*
41  * If there is a '{' after a case label, it gets indented using tabs instead
42  * of spaces. Indent does not necessarily insert a space in this situation,
43  * which looks strange.
44  */
45 //indent input
46 void
function(void)47 function(void)
48 {
49 	switch (expr) {
50 	case 1: {
51 		break;
52 	}
53 	case 11: {
54 		break;
55 	}
56 	}
57 }
58 //indent end
59 
60 //indent run
61 void
function(void)62 function(void)
63 {
64 	switch (expr) {
65 	/* $ The space between the ':' and the '{' is actually a tab. */
66 	case 1:	{
67 			break;
68 		}
69 	case 11: {
70 			break;
71 		}
72 	}
73 }
74 //indent end
75 
76 
77 /*
78  * Since C11, the _Generic selection expression allows a switch on the data
79  * type of an expression.
80  */
81 //indent input
82 const char *type_name = _Generic(
83 	' ',
84 	int: "character constants have type 'int'",
85 	char: "character constants have type 'char'",
86 	default: "character constants have some other type"
87 );
88 //indent end
89 
90 //indent run -di0
91 const char *type_name = _Generic(
92 // $ XXX: It's strange to align the arguments at the parenthesis even though
93 // $ XXX: the first argument is already on a separate line.
94 				 ' ',
95 // $ The indentation is so large that the strings would spill over the right
96 // $ margin.  To prevent that, the code is right-aligned.  Since the base
97 // $ indentation of this declaration is 0, the code might even start at the
98 // $ beginning of the line.
99 				 int: "character constants have type 'int'",
100 			       char: "character constants have type 'char'",
101 			 default: "character constants have some other type"
102 );
103 //indent end
104 
105 //indent run-equals-input -di0 -nlp
106 
107 
108 /*
109  * Multi-line case expressions are rare but still should be processed in a
110  * sensible way.
111  */
112 //indent input
113 {
114 	switch (expr) {
115 // $ FIXME: The line containing the 'case' must be indented like a 'case'.
116 		case 1
117 		    + 2
118 // $ FIXME: This continuation line must be indented by 4 columns.
119 	+ 3:
120 		stmt;
121 	}
122 }
123 //indent end
124 
125 //indent run-equals-input -ci4
126