fmt_decl.c revision 1.18 1 /* $NetBSD: fmt_decl.c,v 1.18 2021/11/19 19:37:13 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-11-07, indent parses the following function definition as these
63 * tokens:
64 *
65 * line 1: type_outside_parentheses "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: type_in_parentheses "char"
72 * line 3: unary_op "*"
73 * line 3: word "a"
74 * line 3: comma ","
75 * line 3: type_in_parentheses "int"
76 * line 3: word "b"
77 * line 3: comma ","
78 * line 3: newline "\n"
79 * line 4: type_in_parentheses "void"
80 * line 4: lparen_or_lbracket "("
81 * line 4: unary_op "*"
82 * line 4: word "fn"
83 * line 4: rparen_or_rbracket ")"
84 * line 4: lparen_or_lbracket "("
85 * line 4: type_in_parentheses "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 static
164 _attribute_printf(1, 2)
165 void
166 print_error(const char *fmt,...)
167 {
168 }
169 #indent end
170
171 #indent run
172 static
173 _attribute_printf(1, 2)
174 void
175 print_error(const char *fmt, ...)
176 {
177 }
178 #indent end
179
180
181 #indent input
182 static _attribute_printf(1, 2)
183 void
184 print_error(const char *fmt,...)
185 {
186 }
187 #indent end
188
189 #indent run
190 static _attribute_printf(1, 2)
191 void
192 print_error(const char *fmt, ...)
193 {
194 }
195 #indent end
196
197
198 #indent input
199 static void _attribute_printf(1, 2)
200 print_error(const char *fmt,...)
201 {
202 }
203 #indent end
204
205 #indent run
206 static void
207 _attribute_printf(1, 2)
208 print_error(const char *fmt, ...)
209 {
210 }
211 #indent end
212
213
214 /* See FreeBSD r309380 */
215 #indent input
216 static LIST_HEAD(, alq) ald_active;
217 static int ald_shutingdown = 0;
218 struct thread *ald_thread;
219 #indent end
220
221 #indent run
222 static LIST_HEAD(, alq) ald_active;
223 static int ald_shutingdown = 0;
224 struct thread *ald_thread;
225 #indent end
226
227
228 #indent input
229 static int
230 old_style_definition(a, b, c)
231 struct thread *a;
232 int b;
233 double ***c;
234 {
235
236 }
237 #indent end
238
239 #indent run
240 static int
241 old_style_definition(a, b, c)
242 struct thread *a;
243 int b;
244 double ***c;
245 {
246
247 }
248 #indent end
249
250
251 /*
252 * Demonstrate how variable declarations are broken into several lines when
253 * the line length limit is set quite low.
254 */
255 #indent input
256 struct s a,b;
257 struct s0 a,b;
258 struct s01 a,b;
259 struct s012 a,b;
260 struct s0123 a,b;
261 struct s01234 a,b;
262 struct s012345 a,b;
263 struct s0123456 a,b;
264 struct s01234567 a,b;
265 struct s012345678 a,b;
266 struct s0123456789 a,b;
267 struct s01234567890 a,b;
268 struct s012345678901 a,b;
269 struct s0123456789012 a,b;
270 struct s01234567890123 a,b;
271 #indent end
272
273 #indent run -l20 -di0
274 struct s a, b;
275 /* $ XXX: See process_comma, varname_len for why this line is broken. */
276 struct s0 a,
277 b;
278 /* $ XXX: The indentation of the second line is wrong. The variable names */
279 /* $ XXX: 'a' and 'b' should be in the same column; the word 'struct' is */
280 /* $ XXX: missing in the calculation for the indentation. */
281 struct s01 a,
282 b;
283 struct s012 a,
284 b;
285 struct s0123 a,
286 b;
287 struct s01234 a,
288 b;
289 struct s012345 a,
290 b;
291 struct s0123456 a,
292 b;
293 struct s01234567 a,
294 b;
295 struct s012345678 a,
296 b;
297 struct s0123456789 a,
298 b;
299 struct s01234567890 a,
300 b;
301 struct s012345678901 a,
302 b;
303 struct s0123456789012 a,
304 b;
305 struct s01234567890123 a,
306 b;
307 #indent end
308
309
310 #indent input
311 char * x(void)
312 {
313 type identifier;
314 type *pointer;
315 unused * value;
316 (void)unused * value;
317
318 dmax = (double)3 * 10.0;
319 dmin = (double)dmax * 10.0;
320 davg = (double)dmax * dmin;
321
322 return NULL;
323 }
324 #indent end
325
326 #indent run
327 char *
328 x(void)
329 {
330 type identifier;
331 type *pointer;
332 unused *value;
333 (void)unused * value;
334
335 dmax = (double)3 * 10.0;
336 dmin = (double)dmax * 10.0;
337 davg = (double)dmax * dmin;
338
339 return NULL;
340 }
341 #indent end
342
343 #indent input
344 int *
345 y(void) {
346
347 }
348
349 int
350 z(void) {
351
352 }
353 #indent end
354
355 #indent run
356 int *
357 y(void)
358 {
359
360 }
361
362 int
363 z(void)
364 {
365
366 }
367 #indent end
368
369
370 #indent input
371 int x;
372 int *y;
373 int * * * * z;
374 #indent end
375
376 #indent run
377 int x;
378 int *y;
379 int ****z;
380 #indent end
381
382
383 #indent input
384 int main(void) {
385 char (*f1)() = NULL;
386 char *(*f1)() = NULL;
387 char *(*f2)();
388 }
389 #indent end
390
391 /*
392 * Before NetBSD io.c 1.103 from 2021-10-27, indent wrongly placed the second
393 * and third variable declaration in column 1. This bug has been introduced
394 * to NetBSD when FreeBSD indent was imported in 2019.
395 */
396 #indent run -ldi0
397 int
398 main(void)
399 {
400 char (*f1)() = NULL;
401 char *(*f1)() = NULL;
402 char *(*f2)();
403 }
404 #indent end
405
406 #indent run
407 int
408 main(void)
409 {
410 /* $ XXX: Not really pretty, the name 'f1' should be aligned, if at all. */
411 char (*f1)() = NULL;
412 /* $ XXX: Not really pretty, the name 'f1' should be aligned, if at all. */
413 char *(* f1)() = NULL;
414 /* $ XXX: Not really pretty, the name 'f2' should be aligned, if at all. */
415 char *(* f2)();
416 }
417 #indent end
418
419
420 /*
421 * In some ancient time long before ISO C90, variable declarations with
422 * initializer could be written without '='. The C Programming Language from
423 * 1978 doesn't mention this form anymore.
424 *
425 * Before NetBSD lexi.c 1.123 from 2021-10-31, indent treated the '-' as a
426 * unary operator.
427 */
428 #indent input
429 int a - 1;
430 {
431 int a - 1;
432 }
433 #indent end
434
435 #indent run -di0
436 int a - 1;
437 {
438 int a - 1;
439 }
440 #indent end
441
442
443 /*
444 * Between 2019-04-04 and before lexi.c 1.146 from 2021-11-09, the indentation
445 * of the '*' depended on the function name, which did not make sense. For
446 * function names that matched [A-Za-z]+, the '*' was placed correctly, for
447 * all other function names (containing [$0-9_]) the '*' was right-aligned on
448 * declaration indentation, which defaults to 16.
449 */
450 #indent input
451 int *
452 f2(void)
453 {
454 }
455
456 int *
457 yy(void)
458 {
459 }
460
461 int *
462 int_create(void)
463 {
464 }
465 #indent end
466
467 #indent run-equals-input
468
469
470 /*
471 * Since 2019-04-04, the space between the '){' is missing.
472 */
473 #indent input
474 int *
475 function_name_____20________30________40________50
476 (void)
477 {}
478 #indent end
479
480 /* FIXME: The space between '){' is missing. */
481 #indent run
482 int *function_name_____20________30________40________50
483 (void){
484 }
485 #indent end
486
487
488 /*
489 * Since 2019-04-04 and before lexi.c 1.144 from 2021-11-19, some function
490 * names were preserved while others were silently discarded.
491 */
492 #indent input
493 int *
494 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
495 (void)
496 {}
497 #indent end
498
499 #indent run
500 int *aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
501 (void){
502 }
503 #indent end
504