xref: /netbsd-src/tests/usr.bin/indent/lsym_case_label.c (revision 7d62b00eb9ad855ffcd7da46b41e23feb5476fac)
1 /* $NetBSD: lsym_case_label.c,v 1.6 2022/04/24 09:04:12 rillig Exp $ */
2 
3 /*
4  * Tests for the token lsym_case_label, which represents either the keyword
5  * 'case' or the keyword 'default', which are both used in 'switch'
6  * statements.
7  *
8  * Since C11, the keyword 'default' is used in _Generic selections as well.
9  *
10  * See also:
11  *	opt_cli.c
12  *	psym_switch_expr.c
13  *	C11 6.5.1.1		"Generic selection"
14  */
15 
16 /*
17  * A case label can be used in a 'switch' statement.
18  */
19 //indent input
20 void function(void){switch(expr){case 1:;case 2:break;default:switch(inner){case 4:break;}}}
21 //indent end
22 
23 //indent run
24 void
25 function(void)
26 {
27 	switch (expr) {
28 	case 1:	;
29 	case 2:
30 		break;
31 	default:
32 		switch (inner) {
33 		case 4:
34 			break;
35 		}
36 	}
37 }
38 //indent end
39 
40 
41 /*
42  * If there is a '{' after a case label, it gets indented using tabs instead
43  * of spaces. Indent does not necessarily insert a space in this situation,
44  * which looks strange.
45  */
46 //indent input
47 void
48 function(void)
49 {
50 	switch (expr) {
51 	case 1: {
52 		break;
53 	}
54 	case 11: {
55 		break;
56 	}
57 	}
58 }
59 //indent end
60 
61 //indent run
62 void
63 function(void)
64 {
65 	switch (expr) {
66 	/* $ The space between the ':' and the '{' is actually a tab. */
67 	case 1:	{
68 			break;
69 		}
70 	/* $ FIXME: missing space between ':' and '{'. */
71 	case 11:{
72 			break;
73 		}
74 	}
75 }
76 //indent end
77 
78 
79 /*
80  * Since C11, the _Generic selection expression allows a switch on the data
81  * type of an expression.
82  */
83 //indent input
84 const char *type_name = _Generic(
85 	' ',
86 	int: "character constants have type 'int'",
87 	char: "character constants have type 'char'",
88 	default: "character constants have some other type"
89 );
90 //indent end
91 
92 //indent run -di0
93 const char *type_name = _Generic(
94 // $ XXX: It's strange to align the arguments at the parenthesis even though
95 // $ XXX: the first argument is already on a separate line.
96 				 ' ',
97 // $ TODO: indent the type names
98 int:				 "character constants have type 'int'",
99 char:				 "character constants have type 'char'",
100 default:
101 // $ TODO: remove the newline after 'default:'
102 				 "character constants have some other type"
103 );
104 //indent end
105 
106 //indent run -di0 -nlp
107 const char *type_name = _Generic(
108 	' ',
109 // $ TODO: indent the type names
110 int:	"character constants have type 'int'",
111 char:	"character constants have type 'char'",
112 default:
113 	"character constants have some other type"
114 );
115 //indent end
116