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