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