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