xref: /netbsd-src/tests/usr.bin/indent/opt_badp.c (revision f6c6195c8b4708c30e3e542ca839085be9da440b)
1 /* $NetBSD: opt_badp.c,v 1.16 2023/06/27 04:41:23 rillig Exp $ */
2 
3 /*
4  * Tests for the options '-badp' and '-nbadp'.
5  *
6  * The option '-badp' forces a blank line between the first set of declarations
7  * in a function and the next comment or statement. It produces a blank line
8  * even if there are no declarations.
9  */
10 
11 
12 /* An empty function body does not need a blank line. */
13 //indent input
14 void
empty(void)15 empty(void)
16 {
17 }
18 //indent end
19 
20 //indent run-equals-input -badp
21 
22 //indent run-equals-input -nbadp
23 
24 
25 /* If an empty function body already has a blank line, it is kept. */
26 //indent input
27 void
blank(void)28 blank(void)
29 {
30 
31 }
32 //indent end
33 
34 //indent run-equals-input -badp
35 
36 //indent run-equals-input -nbadp
37 
38 
39 /*
40  * If a function body has only declarations (doesn't occur in practice), it
41  * does not need an empty line.
42  */
43 //indent input
44 void
declaration(void)45 declaration(void)
46 {
47 	int		decl;
48 }
49 //indent end
50 
51 //indent run-equals-input -badp
52 
53 //indent run-equals-input -nbadp
54 
55 
56 /*
57  * A function body without declarations gets an empty line above the first
58  * statement.
59  */
60 //indent input
61 void
statement(void)62 statement(void)
63 {
64 	stmt();
65 }
66 //indent end
67 
68 //indent run -badp
69 void
statement(void)70 statement(void)
71 {
72 
73 	stmt();
74 }
75 //indent end
76 
77 //indent run-equals-input -nbadp
78 
79 
80 /*
81  * A function body with a declaration and a statement gets a blank line between
82  * those.
83  */
84 //indent input
85 void
declaration_statement(void)86 declaration_statement(void)
87 {
88 	int		decl;
89 	stmt();
90 }
91 //indent end
92 
93 //indent run -badp
94 void
declaration_statement(void)95 declaration_statement(void)
96 {
97 	int		decl;
98 
99 	stmt();
100 }
101 //indent end
102 
103 //indent run-equals-input -nbadp
104 
105 
106 /* If there already is a blank line in the right place, it is kept. */
107 //indent input
108 static void
declaration_blank_statement(void)109 declaration_blank_statement(void)
110 {
111 	int		decl;
112 
113 	stmt();
114 }
115 //indent end
116 
117 //indent run-equals-input -badp
118 
119 //indent run-equals-input -nbadp
120 
121 
122 /* Additional blank lines are kept. To remove them, see the '-sob' option. */
123 //indent input
124 static void
declaration_blank_blank_statement(void)125 declaration_blank_blank_statement(void)
126 {
127 	int		decl;
128 
129 
130 
131 	stmt();
132 }
133 //indent end
134 
135 //indent run-equals-input -badp
136 
137 //indent run-equals-input -nbadp
138 
139 
140 /*
141  * The blank line is only inserted at the top of a function body, not in nested
142  * block statements.
143  */
144 //indent input
145 static void
nested(void)146 nested(void)
147 {
148 	{
149 		int		decl;
150 		stmt();
151 	}
152 }
153 //indent end
154 
155 //indent run -badp
156 static void
nested(void)157 nested(void)
158 {
159 
160 	{
161 		int		decl;
162 		stmt();
163 	}
164 }
165 //indent end
166 
167 //indent run-equals-input -nbadp
168 
169 
170 /*
171  * A struct declaration or an initializer are not function bodies, so don't
172  * add a blank line after them.
173  */
174 //indent input
175 struct {
176 	int member[2];
177 } s = {
178 	{
179 		0,
180 		0,
181 	}
182 };
183 //indent end
184 
185 //indent run-equals-input -di0 -badp
186 
187 //indent run-equals-input -di0 -nbadp
188 
189 
190 /* Single-line function definitions must be handled correctly as well. */
191 //indent input
f(void)192 void f(void) { int decl; stmt; }
193 //indent end
194 
195 //indent run -badp
196 void
f(void)197 f(void)
198 {
199 	int		decl;
200 
201 	stmt;
202 }
203 //indent end
204 
205 //indent run -nfbs -badp
206 void
f(void)207 f(void) {
208 	int		decl;
209 
210 	stmt;
211 }
212 //indent end
213 
214 
215 /* The '}' of an initializer does not end a block. */
216 //indent input
217 void
f(void)218 f(void)
219 {
220 	int decl1[2][2] = {
221 		{1, 2},
222 		{3, 4},
223 	};
224 	int decl2 = 5;
225 	stmt;
226 }
227 //indent end
228 
229 //indent run -di0 -badp
230 void
f(void)231 f(void)
232 {
233 	int decl1[2][2] = {
234 		{1, 2},
235 		{3, 4},
236 	};
237 	int decl2 = 5;
238 
239 	stmt;
240 }
241 //indent end
242 
243 
244 /*
245  * Due to its limited lookahead, indent cannot know whether the comment is
246  * followed by a declaration or a statement, so it assumes that the comment is
247  * part of the declaration block.
248  */
249 //indent input
f(void)250 void f(void) {
251 	int		decl1;
252 	/* comment */
253 	int		decl2;
254 	stmt;
255 }
256 //indent end
257 
258 //indent run -badp
259 void
f(void)260 f(void)
261 {
262 	int		decl1;
263 	/* comment */
264 	int		decl2;
265 
266 	stmt;
267 }
268 //indent end
269 
270 
271 /* Combining -bad and -badp only adds a single blank line. */
272 //indent input
f(void)273 void f(void) { int decl; stmt1; stmt2; }
274 //indent end
275 
276 //indent run -bad -badp
277 void
f(void)278 f(void)
279 {
280 	int		decl;
281 
282 	stmt1;
283 	stmt2;
284 }
285 //indent end
286