summaryrefslogtreecommitdiff
path: root/tests/usr.bin/indent/opt_badp.c
blob: c31c3a9f70b5fb8ad8f2f8eec8b9e35b605c48da (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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
/* $NetBSD: opt_badp.c,v 1.16 2023/06/27 04:41:23 rillig Exp $ */

/*
 * Tests for the options '-badp' and '-nbadp'.
 *
 * The option '-badp' forces a blank line between the first set of declarations
 * in a function and the next comment or statement. It produces a blank line
 * even if there are no declarations.
 */


/* An empty function body does not need a blank line. */
//indent input
void
empty(void)
{
}
//indent end

//indent run-equals-input -badp

//indent run-equals-input -nbadp


/* If an empty function body already has a blank line, it is kept. */
//indent input
void
blank(void)
{

}
//indent end

//indent run-equals-input -badp

//indent run-equals-input -nbadp


/*
 * If a function body has only declarations (doesn't occur in practice), it
 * does not need an empty line.
 */
//indent input
void
declaration(void)
{
	int		decl;
}
//indent end

//indent run-equals-input -badp

//indent run-equals-input -nbadp


/*
 * A function body without declarations gets an empty line above the first
 * statement.
 */
//indent input
void
statement(void)
{
	stmt();
}
//indent end

//indent run -badp
void
statement(void)
{

	stmt();
}
//indent end

//indent run-equals-input -nbadp


/*
 * A function body with a declaration and a statement gets a blank line between
 * those.
 */
//indent input
void
declaration_statement(void)
{
	int		decl;
	stmt();
}
//indent end

//indent run -badp
void
declaration_statement(void)
{
	int		decl;

	stmt();
}
//indent end

//indent run-equals-input -nbadp


/* If there already is a blank line in the right place, it is kept. */
//indent input
static void
declaration_blank_statement(void)
{
	int		decl;

	stmt();
}
//indent end

//indent run-equals-input -badp

//indent run-equals-input -nbadp


/* Additional blank lines are kept. To remove them, see the '-sob' option. */
//indent input
static void
declaration_blank_blank_statement(void)
{
	int		decl;



	stmt();
}
//indent end

//indent run-equals-input -badp

//indent run-equals-input -nbadp


/*
 * The blank line is only inserted at the top of a function body, not in nested
 * block statements.
 */
//indent input
static void
nested(void)
{
	{
		int		decl;
		stmt();
	}
}
//indent end

//indent run -badp
static void
nested(void)
{

	{
		int		decl;
		stmt();
	}
}
//indent end

//indent run-equals-input -nbadp


/*
 * A struct declaration or an initializer are not function bodies, so don't
 * add a blank line after them.
 */
//indent input
struct {
	int member[2];
} s = {
	{
		0,
		0,
	}
};
//indent end

//indent run-equals-input -di0 -badp

//indent run-equals-input -di0 -nbadp


/* Single-line function definitions must be handled correctly as well. */
//indent input
void f(void) { int decl; stmt; }
//indent end

//indent run -badp
void
f(void)
{
	int		decl;

	stmt;
}
//indent end

//indent run -nfbs -badp
void
f(void) {
	int		decl;

	stmt;
}
//indent end


/* The '}' of an initializer does not end a block. */
//indent input
void
f(void)
{
	int decl1[2][2] = {
		{1, 2},
		{3, 4},
	};
	int decl2 = 5;
	stmt;
}
//indent end

//indent run -di0 -badp
void
f(void)
{
	int decl1[2][2] = {
		{1, 2},
		{3, 4},
	};
	int decl2 = 5;

	stmt;
}
//indent end


/*
 * Due to its limited lookahead, indent cannot know whether the comment is
 * followed by a declaration or a statement, so it assumes that the comment is
 * part of the declaration block.
 */
//indent input
void f(void) {
	int		decl1;
	/* comment */
	int		decl2;
	stmt;
}
//indent end

//indent run -badp
void
f(void)
{
	int		decl1;
	/* comment */
	int		decl2;

	stmt;
}
//indent end


/* Combining -bad and -badp only adds a single blank line. */
//indent input
void f(void) { int decl; stmt1; stmt2; }
//indent end

//indent run -bad -badp
void
f(void)
{
	int		decl;

	stmt1;
	stmt2;
}
//indent end