summaryrefslogtreecommitdiff
path: root/tests/usr.bin/indent/lsym_lbrace.c
blob: 4f284503e17662e17288425a592277506e9e1f3d (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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
/* $NetBSD: lsym_lbrace.c,v 1.10 2023/06/16 23:19:01 rillig Exp $ */

/*
 * Tests for the token lsym_lbrace, which represents a '{' in these contexts:
 *
 * In an initializer, '{' starts an inner group of initializers, usually to
 * initialize a nested struct, union or array.
 *
 * In a function body, '{' starts a block.
 *
 * In an expression, '(type){' starts a compound literal that is typically
 * used in an assignment to a struct or array.
 *
 * In macro arguments, a '{' is an ordinary character, it does not need to be
 * balanced.  This is in contrast to '(', which must be balanced with ')'.
 *
 * TODO: try to split this token into lsym_lbrace_block and lsym_lbrace_init.
 */

/* Brace level in an initializer */
//indent input
void
function(void)
{
	struct person	p = {
		.name = "Name",
		.age = {{{35}}},	/* C11 6.7.9 allows this. */
	};
}
//indent end

//indent run-equals-input


/* Begin of a block of statements */
//indent input
void function(void) {{{ body(); }}}
//indent end

//indent run
void
function(void)
{
	{
		{
			body();
		}
	}
}
//indent end


/* Compound literal */
//indent input
struct point
origin(void)
{
	return (struct point){
		.x = 0,
		.y = 0,
	}, actual_return_value;
}
//indent end

//indent run-equals-input

/* Ensure that the comma is not interpreted as separator for declarators. */
//indent run-equals-input -bc


//indent input
{
	const char *hello = (const char[]){
		'h', 'e', 'l', 'l', 'o',
	}, *world = (const char[]){
		'w', 'o', 'r', 'l', 'd',
	};
}
//indent end

//indent run-equals-input -ldi0

//indent run-equals-input -ldi0 -bc


//indent input
{
	if (cond rparen {
	}
	switch (expr rparen {
	}
}
//indent end

//indent run
{
		if (cond rparen {
		}
		switch (expr rparen {
		}
}
// exit 1
// error: Standard Input:2: Unbalanced parentheses
// error: Standard Input:4: Unbalanced parentheses
//indent end


/*
 * The -bl option does not force initializer braces on separate lines.
 */
//indent input
struct {int member;} var = {1};
//indent end

//indent run -bl
struct
{
	int		member;
}		var = {1};
//indent end


/*
 * A comment in a single-line function definition is not a declaration comment
 * and thus not in column 25.
 */
//indent input
void function(void); /* comment */
void function(void) { /* comment */ }
//indent end

//indent run -di0
void function(void);		/* comment */
void
function(void)
{				/* comment */
}
//indent end

//indent run -di0 -nfbs
void function(void);		/* comment */
void
function(void) {		/* comment */
}
//indent end