summaryrefslogtreecommitdiff
path: root/tests/usr.bin/indent/lsym_for.c
blob: 08c42c58f4086b0d96aaadfda599fecadfd3b196 (plain)
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
/* $NetBSD: lsym_for.c,v 1.9 2023/06/26 20:23:40 rillig Exp $ */

/*
 * Tests for the token lsym_for, which represents the keyword 'for' that
 * starts a 'for' loop.
 *
 * Most 'for' loops have 3 expressions in their head.  Each of these
 * expressions is optional though.
 *
 * When all 3 expressions are omitted, the 'for' loop is often called a
 * 'forever' loop.
 */

//indent input
void
example(void)
{
	for (;;)
		break;
	for (var = value;;)
		break;
	for (; cond;)
		break;
	for (;; i++)
		break;
}
//indent end

//indent run-equals-input


//indent input
void
function(void)
{
	for (int i = 0; i < 6; i++)
		print_char("hello\n"[i]);
	forever {
		stmt();
	}
}
//indent end

//indent run-equals-input


/*
 * Indent can cope with various syntax errors, which may be caused by
 * syntactic macros like 'forever' or 'foreach'.
 */
//indent input
#define forever for (;;)
#define foreach(list, it) for (it = list.first; it != NULL; it = it->next)

void
function(void)
{
	forever
		stmt();

	forever {
		stmt();
	}

/* $ No space after 'foreach' since it looks like a function name. */
	foreach(list, it)
		println(it->data);

/* $ No space after 'foreach' since it looks like a function name. */
	foreach(list, it) {
		println(it->data);
	}
}
//indent end

//indent run-equals-input


/*
 * Another variant of a 'for' loop, seen in sys/arch/arm/apple/apple_intc.c.
 */
//indent input
{
	for (CPU_INFO_FOREACH(cii, ci)) {
	}
}
//indent end

//indent run-equals-input


/* Ensure that the '*' after 'list_item' is a unary operator. */
//indent input
{
	for (const list_item *i = first; i != NULL; i = i->next) {
	}
	for (list_item **i = first; i != NULL; i = i->next) {
	}
	for (list_item *const *i = first; i != NULL; i = i->next) {
	}
	for (const char *const *i = first; i != NULL; i = i->next) {
	}
}
//indent end

//indent run-equals-input