fmt_decl.c revision 1.11 1 /* $NetBSD: fmt_decl.c,v 1.11 2021/10/31 19:13:41 rillig Exp $ */
2 /* $FreeBSD: head/usr.bin/indent/tests/declarations.0 334478 2018-06-01 09:41:15Z pstef $ */
3
4 /* See FreeBSD r303570 */
5
6 #indent input
7 typedef void (*voidptr) (int *);
8 #indent end
9
10 #indent run
11 typedef void (*voidptr)(int *);
12 #indent end
13
14
15 #indent input
16 static const struct
17 {
18 double x;
19 double y, z;
20 } n[m + 1] =
21 {
22 {
23 .0,
24 .9,
25 5
26 }
27 };
28 #indent end
29
30 #indent run
31 static const struct {
32 double x;
33 double y, z;
34 } n[m + 1] =
35 {
36 {
37 .0,
38 .9,
39 5
40 }
41 };
42 #indent end
43
44
45 #indent input
46 typedef struct Complex
47 {
48 double x;
49 double y;
50 } Complex;
51 #indent end
52
53 #indent run
54 typedef struct Complex {
55 double x;
56 double y;
57 } Complex;
58 #indent end
59
60
61 /*
62 * As of 2021-10-31, indent parses the following function definition as these
63 * tokens:
64 *
65 * line 1: type type "void"
66 * line 1: newline "\n"
67 * line 2: funcname "t1"
68 * line 2: newline "\n" repeated, see search_stmt
69 * line 3: funcname "t1" XXX: wrong line_no
70 * line 3: lparen_or_lbracket "("
71 * line 3: ident type "char"
72 * line 3: unary_op "*"
73 * line 3: ident "a"
74 * line 3: comma ","
75 * line 3: ident type "int"
76 * line 3: ident "b"
77 * line 3: comma ","
78 * line 3: newline "\n"
79 * line 4: ident type "void"
80 * line 4: lparen_or_lbracket "("
81 * line 4: unary_op "*"
82 * line 4: ident "fn"
83 * line 4: rparen_or_rbracket ")"
84 * line 4: lparen_or_lbracket "("
85 * line 4: ident type "void"
86 * line 4: rparen_or_rbracket ")"
87 * line 4: rparen_or_rbracket ")"
88 * line 4: newline "\n"
89 * line 5: lbrace "{"
90 * line 5: lbrace "{" repeated, see search_stmt
91 * line 5: newline "\n" FIXME: there is no newline in the source
92 * line 6: rbrace "}"
93 * line 6: eof "\n"
94 */
95 #indent input
96 void
97 t1 (char *a, int b,
98 void (*fn)(void))
99 {}
100 #indent end
101
102 #indent run
103 void
104 t1(char *a, int b,
105 void (*fn)(void))
106 {
107 }
108 #indent end
109
110
111 #indent input
112 void t2 (char *x, int y)
113 {
114 int a,
115 b,
116 c;
117 int
118 *d,
119 *e,
120 *f;
121 int (*g)(),
122 (*h)(),
123 (*i)();
124 int j,
125 k,
126 l;
127 int m
128 ,n
129 ,o
130 ;
131 int chars[ /* push the comma beyond column 74 .... */ ], x;
132 }
133 #indent end
134
135 #indent run
136 void
137 t2(char *x, int y)
138 {
139 int a, b, c;
140 int
141 *d, *e, *f;
142 int (*g)(), (*h)(), (*i)();
143 int j, k, l;
144 int m
145 ,n
146 ,o
147 ;
148 int chars[ /* push the comma beyond column 74 .... */ ],
149 x;
150 }
151 #indent end
152
153
154 #indent input
155 const int int_minimum_size =
156 MAXALIGN(offsetof(int, test)) + MAXIMUM_ALIGNOF;
157 #indent end
158
159 #indent run-equals-input
160
161
162 #indent input
163 int *int_create(void)
164 {
165
166 }
167 #indent end
168
169 #indent run
170 int *
171 int_create(void)
172 {
173
174 }
175 #indent end
176
177
178 #indent input
179 static
180 _attribute_printf(1, 2)
181 void
182 print_error(const char *fmt,...)
183 {
184 }
185 #indent end
186
187 #indent run
188 static
189 _attribute_printf(1, 2)
190 void
191 print_error(const char *fmt, ...)
192 {
193 }
194 #indent end
195
196
197 #indent input
198 static _attribute_printf(1, 2)
199 void
200 print_error(const char *fmt,...)
201 {
202 }
203 #indent end
204
205 #indent run
206 static _attribute_printf(1, 2)
207 void
208 print_error(const char *fmt, ...)
209 {
210 }
211 #indent end
212
213
214 #indent input
215 static void _attribute_printf(1, 2)
216 print_error(const char *fmt,...)
217 {
218 }
219 #indent end
220
221 #indent run
222 static void
223 _attribute_printf(1, 2)
224 print_error(const char *fmt, ...)
225 {
226 }
227 #indent end
228
229
230 /* See FreeBSD r309380 */
231 #indent input
232 static LIST_HEAD(, alq) ald_active;
233 static int ald_shutingdown = 0;
234 struct thread *ald_thread;
235 #indent end
236
237 #indent run
238 static LIST_HEAD(, alq) ald_active;
239 static int ald_shutingdown = 0;
240 struct thread *ald_thread;
241 #indent end
242
243
244 #indent input
245 static int
246 old_style_definition(a, b, c)
247 struct thread *a;
248 int b;
249 double ***c;
250 {
251
252 }
253 #indent end
254
255 #indent run
256 static int
257 old_style_definition(a, b, c)
258 struct thread *a;
259 int b;
260 double ***c;
261 {
262
263 }
264 #indent end
265
266
267 /*
268 * Demonstrate how variable declarations are broken into several lines when
269 * the line length limit is set quite low.
270 */
271 #indent input
272 struct s a,b;
273 struct s0 a,b;
274 struct s01 a,b;
275 struct s012 a,b;
276 struct s0123 a,b;
277 struct s01234 a,b;
278 struct s012345 a,b;
279 struct s0123456 a,b;
280 struct s01234567 a,b;
281 struct s012345678 a,b;
282 struct s0123456789 a,b;
283 struct s01234567890 a,b;
284 struct s012345678901 a,b;
285 struct s0123456789012 a,b;
286 struct s01234567890123 a,b;
287 #indent end
288
289 #indent run -l20 -di0
290 struct s a, b;
291 /* $ XXX: See process_comma, varname_len for why this line is broken. */
292 struct s0 a,
293 b;
294 /* $ XXX: The indentation of the second line is wrong. The variable names */
295 /* $ XXX: 'a' and 'b' should be in the same column; the word 'struct' is */
296 /* $ XXX: missing in the calculation for the indentation. */
297 struct s01 a,
298 b;
299 struct s012 a,
300 b;
301 struct s0123 a,
302 b;
303 struct s01234 a,
304 b;
305 struct s012345 a,
306 b;
307 struct s0123456 a,
308 b;
309 struct s01234567 a,
310 b;
311 struct s012345678 a,
312 b;
313 struct s0123456789 a,
314 b;
315 struct s01234567890 a,
316 b;
317 struct s012345678901 a,
318 b;
319 struct s0123456789012 a,
320 b;
321 struct s01234567890123 a,
322 b;
323 #indent end
324
325
326 #indent input
327 char * x(void)
328 {
329 type identifier;
330 type *pointer;
331 unused * value;
332 (void)unused * value;
333
334 dmax = (double)3 * 10.0;
335 dmin = (double)dmax * 10.0;
336 davg = (double)dmax * dmin;
337
338 return NULL;
339 }
340 #indent end
341
342 #indent run
343 char *
344 x(void)
345 {
346 type identifier;
347 type *pointer;
348 unused *value;
349 (void)unused * value;
350
351 dmax = (double)3 * 10.0;
352 dmin = (double)dmax * 10.0;
353 davg = (double)dmax * dmin;
354
355 return NULL;
356 }
357 #indent end
358
359 #indent input
360 int *
361 y(void) {
362
363 }
364
365 int
366 z(void) {
367
368 }
369 #indent end
370
371 #indent run
372 int *
373 y(void)
374 {
375
376 }
377
378 int
379 z(void)
380 {
381
382 }
383 #indent end
384
385
386 #indent input
387 int x;
388 int *y;
389 int * * * * z;
390 #indent end
391
392 #indent run
393 int x;
394 int *y;
395 int ****z;
396 #indent end
397
398
399 #indent input
400 int main(void) {
401 char (*f1)() = NULL;
402 char *(*f1)() = NULL;
403 char *(*f2)();
404 }
405 #indent end
406
407 /*
408 * Before NetBSD io.c 1.103 from 2021-10-27, indent wrongly placed the second
409 * and third variable declaration in column 1. This bug has been introduced
410 * to NetBSD when FreeBSD indent was imported in 2019.
411 */
412 #indent run -ldi0
413 int
414 main(void)
415 {
416 char (*f1)() = NULL;
417 char *(*f1)() = NULL;
418 char *(*f2)();
419 }
420 #indent end
421
422 #indent run
423 int
424 main(void)
425 {
426 /* $ XXX: Not really pretty, the name 'f1' should be aligned, if at all. */
427 char (*f1)() = NULL;
428 /* $ XXX: Not really pretty, the name 'f1' should be aligned, if at all. */
429 char *(* f1)() = NULL;
430 /* $ XXX: Not really pretty, the name 'f2' should be aligned, if at all. */
431 char *(* f2)();
432 }
433 #indent end
434
435
436 /*
437 * In some ancient time long before ISO C90, variable declarations with
438 * initializer could be written without '='. The C Programming Language from
439 * 1978 doesn't mention this form anymore.
440 */
441 #indent input
442 int a - 1;
443 {
444 int a - 1;
445 }
446 #indent end
447
448 #indent run -di0
449 int a - 1;
450 {
451 int a - 1;
452 }
453 #indent end
454