fmt_decl.c revision 1.38 1 /* $NetBSD: fmt_decl.c,v 1.38 2023/05/13 06:52:48 rillig Exp $ */
2
3 /*
4 * Tests for declarations of global variables, external functions, and local
5 * variables.
6 *
7 * See also:
8 * opt_di.c
9 */
10
11 /* See FreeBSD r303570 */
12
13 /*
14 * A type definition usually declares a single type, so there is no need to
15 * align the newly declared type name with the other variables.
16 */
17 //indent input
18 typedef void ( * function_ptr ) ( int * ) ;
19 //indent end
20
21 //indent run
22 typedef void (*function_ptr)(int *);
23 //indent end
24
25
26 /*
27 * In variable declarations, the names of the first declarators are indented
28 * by the amount given in '-di', which defaults to 16.
29 */
30 //indent input
31 extern void ( * function_pointer ) ( int * ) ;
32 extern void * pointer;
33 //indent end
34
35 //indent run
36 /* $ XXX: Why is the token 'function_pointer' not aligned with 'pointer'? */
37 extern void (*function_pointer)(int *);
38 extern void *pointer;
39 //indent end
40
41
42 //indent input
43 static const struct
44 {
45 double x;
46 double y, z;
47 } n[m + 1] =
48 {
49 {
50 .0,
51 .9,
52 5
53 }
54 };
55 //indent end
56
57 //indent run
58 static const struct
59 {
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 {
84 double x;
85 double y;
86 } Complex;
87 //indent end
88
89
90 /*
91 * Ensure that function definitions are reasonably indented. Before
92 * 2023-05-11, tokens were repeatedly read, and the line numbers were wrong.
93 */
94 //indent input
95 void
96 t1 (char *a, int b,
97 void (*fn)(void))
98 {}
99 //indent end
100
101 //indent run
102 void
103 t1(char *a, int b,
104 void (*fn)(void))
105 {
106 }
107 //indent end
108
109
110 /* See opt_bc.c. */
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 /*
163 * Ensure that the usual GCC-style function attributes are formatted in a
164 * sensible way.
165 */
166 //indent input
167 void function(const char *, ...) __attribute__((format(printf, 1, 2)));
168 //indent end
169
170 /* FIXME: missing space before '__attribute__' */
171 //indent run -di0
172 void function(const char *, ...)__attribute__((format(printf, 1, 2)));
173 //indent end
174
175 /* FIXME: missing space before '__attribute__' */
176 //indent run
177 void function(const char *, ...)__attribute__((format(printf, 1, 2)));
178 //indent end
179
180
181 //indent input
182 static
183 _attribute_printf(1, 2)
184 void
185 print_error(const char *fmt,...)
186 {
187 }
188 //indent end
189
190 //indent run
191 static
192 _attribute_printf(1, 2)
193 void
194 print_error(const char *fmt, ...)
195 {
196 }
197 //indent end
198
199
200 //indent input
201 static _attribute_printf(1, 2)
202 void
203 print_error(const char *fmt,...)
204 {
205 }
206 //indent end
207
208 //indent run
209 static _attribute_printf(1, 2)
210 void
211 print_error(const char *fmt, ...)
212 {
213 }
214 //indent end
215
216
217 //indent input
218 static void _attribute_printf(1, 2)
219 print_error(const char *fmt,...)
220 {
221 }
222 //indent end
223
224 //indent run
225 static void
226 _attribute_printf(1, 2)
227 print_error(const char *fmt, ...)
228 {
229 }
230 //indent end
231
232
233 /* See FreeBSD r309380 */
234 //indent input
235 static LIST_HEAD(, alq) ald_active;
236 static int ald_shutting_down = 0;
237 struct thread *ald_thread;
238 //indent end
239
240 //indent run
241 static LIST_HEAD(, alq) ald_active;
242 static int ald_shutting_down = 0;
243 struct thread *ald_thread;
244 //indent end
245
246
247 //indent input
248 static int
249 old_style_definition(a, b, c)
250 struct thread *a;
251 int b;
252 double ***c;
253 {
254
255 }
256 //indent end
257
258 //indent run
259 static int
260 old_style_definition(a, b, c)
261 struct thread *a;
262 int b;
263 double ***c;
264 {
265
266 }
267 //indent end
268
269
270 /*
271 * Demonstrate how variable declarations are broken into several lines when
272 * the line length limit is set quite low.
273 */
274 //indent input
275 struct s a,b;
276 struct s0 a,b;
277 struct s01 a,b;
278 struct s012 a,b;
279 struct s0123 a,b;
280 struct s01234 a,b;
281 struct s012345 a,b;
282 struct s0123456 a,b;
283 struct s01234567 a,b;
284 struct s012345678 a,b;
285 struct s0123456789 a,b;
286 struct s01234567890 a,b;
287 struct s012345678901 a,b;
288 struct s0123456789012 a,b;
289 struct s01234567890123 a,b;
290 //indent end
291
292 //indent run -l20 -di0
293 struct s a, b;
294 /* $ XXX: See process_comma, varname_len for why this line is broken. */
295 struct s0 a,
296 b;
297 /* $ XXX: The indentation of the second line is wrong. The variable names */
298 /* $ XXX: 'a' and 'b' should be in the same column; the word 'struct' is */
299 /* $ XXX: missing in the calculation for the indentation. */
300 struct s01 a,
301 b;
302 struct s012 a,
303 b;
304 struct s0123 a,
305 b;
306 struct s01234 a,
307 b;
308 struct s012345 a,
309 b;
310 struct s0123456 a,
311 b;
312 struct s01234567 a,
313 b;
314 struct s012345678 a,
315 b;
316 struct s0123456789 a,
317 b;
318 struct s01234567890 a,
319 b;
320 struct s012345678901 a,
321 b;
322 struct s0123456789012 a,
323 b;
324 struct s01234567890123 a,
325 b;
326 //indent end
327
328
329 //indent input
330 char * x(void)
331 {
332 type identifier;
333 type *pointer;
334 unused * value;
335 (void)unused * value;
336
337 dmax = (double)3 * 10.0;
338 dmin = (double)dmax * 10.0;
339 davg = (double)dmax * dmin;
340
341 return NULL;
342 }
343 //indent end
344
345 //indent run
346 char *
347 x(void)
348 {
349 type identifier;
350 type *pointer;
351 unused *value;
352 (void)unused * value;
353
354 dmax = (double)3 * 10.0;
355 dmin = (double)dmax * 10.0;
356 davg = (double)dmax * dmin;
357
358 return NULL;
359 }
360 //indent end
361
362
363 //indent input
364 int *
365 y(void) {
366
367 }
368
369 int
370 z(void) {
371
372 }
373 //indent end
374
375 //indent run
376 int *
377 y(void)
378 {
379
380 }
381
382 int
383 z(void)
384 {
385
386 }
387 //indent end
388
389
390 //indent input
391 int x;
392 int *y;
393 int * * * * z;
394 //indent end
395
396 //indent run
397 int x;
398 int *y;
399 int ****z;
400 //indent end
401
402
403 //indent input
404 int main(void) {
405 char (*f1)() = NULL;
406 char *(*f1)() = NULL;
407 char *(*f2)();
408 }
409 //indent end
410
411 /*
412 * Before NetBSD io.c 1.103 from 2021-10-27, indent wrongly placed the second
413 * and third variable declaration in column 1. This bug has been introduced
414 * to NetBSD when FreeBSD indent was imported in 2019.
415 */
416 //indent run -ldi0
417 int
418 main(void)
419 {
420 char (*f1)() = NULL;
421 char *(*f1)() = NULL;
422 char *(*f2)();
423 }
424 //indent end
425
426 //indent run
427 int
428 main(void)
429 {
430 /* $ XXX: Not really pretty, the name 'f1' should be aligned, if at all. */
431 char (*f1)() = NULL;
432 /* $ XXX: Not really pretty, the name 'f1' should be aligned, if at all. */
433 char *(* f1)() = NULL;
434 /* $ XXX: Not really pretty, the name 'f2' should be aligned, if at all. */
435 char *(* f2)();
436 }
437 //indent end
438
439
440 /*
441 * In some ancient time long before ISO C90, variable declarations with
442 * initializer could be written without '='. The C Programming Language from
443 * 1978 doesn't mention this form anymore.
444 *
445 * Before NetBSD lexi.c 1.123 from 2021-10-31, indent treated the '-' as a
446 * unary operator.
447 */
448 //indent input
449 int a - 1;
450 {
451 int a - 1;
452 }
453 //indent end
454
455 //indent run -di0
456 int a - 1;
457 {
458 int a - 1;
459 }
460 //indent end
461
462
463 /*
464 * Between 2019-04-04 and before lexi.c 1.146 from 2021-11-19, the indentation
465 * of the '*' depended on the function name, which did not make sense. For
466 * function names that matched [A-Za-z]+, the '*' was placed correctly, for
467 * all other function names (containing [$0-9_]) the '*' was right-aligned on
468 * the declaration indentation, which defaults to 16.
469 */
470 //indent input
471 int *
472 f2(void)
473 {
474 }
475
476 int *
477 yy(void)
478 {
479 }
480
481 int *
482 int_create(void)
483 {
484 }
485 //indent end
486
487 //indent run-equals-input
488
489
490 /*
491 * Since 2019-04-04, the space between the '){' is missing.
492 */
493 //indent input
494 int *
495 function_name_____20________30________40________50
496 (void)
497 {}
498 //indent end
499
500 /*
501 * Before 2023-05-11, indent moved the '{' right after the '(void)', without
502 * any space in between.
503 */
504 //indent run
505 int *function_name_____20________30________40________50
506 (void)
507 {
508 }
509 //indent end
510
511
512 /*
513 * Since 2019-04-04 and before lexi.c 1.144 from 2021-11-19, some function
514 * names were preserved while others were silently discarded.
515 */
516 //indent input
517 int *
518 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
519 (void)
520 {}
521 //indent end
522
523 /*
524 * Before 2023-05-11, indent moved the '{' right after the '(void)', without
525 * any space in between.
526 */
527 //indent run
528 int *aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
529 (void)
530 {
531 }
532 //indent end
533
534
535 /*
536 * Before 1990, when C90 standardized function prototypes, a function
537 * declaration or definition did not contain a '*' that may have looked
538 * similar to the binary operator '*' because it was surrounded by two
539 * identifiers.
540 *
541 * As of 2021-11-21, indent interpreted the '*' in the function declaration in
542 * line 1 as a binary operator, even though the '*' was followed by a ','
543 * directly. This was not visible in the output though since indent never
544 * outputs a space before a comma.
545 *
546 * In the function declaration in line 2 and the function definition in line
547 * 5, indent interpreted the '*' as a binary operator as well and accordingly
548 * placed spaces around the '*'. On a very low syntactical analysis level,
549 * this may have made sense since the '*' was surrounded by words, but still
550 * the '*' is part of a declaration, where a binary operator does not make
551 * sense.
552 *
553 * Essentially, as of 2021, indent had missed the last 31 years of advances in
554 * the C programming language, in particular the invention of function
555 * prototypes. Instead, the workaround had been to require all type names to
556 * be specified via the options '-ta' and '-T'. This put the burden on the
557 * user instead of the implementer.
558 *
559 * Fixed in lexi.c 1.156 from 2021-11-25.
560 */
561 //indent input
562 void buffer_add(buffer *, char);
563 void buffer_add(buffer *buf, char ch);
564
565 void
566 buffer_add(buffer *buf, char ch)
567 {
568 *buf->e++ = ch;
569 }
570 //indent end
571
572 /* Before lexi.c 1.156 from 2021-11-25, indent generated 'buffer * buf'. */
573 //indent run
574 void buffer_add(buffer *, char);
575 /* $ FIXME: space after '*' */
576 void buffer_add(buffer * buf, char ch);
577
578 void
579 buffer_add(buffer *buf, char ch)
580 {
581 *buf->e++ = ch;
582 }
583 //indent end
584
585
586 /*
587 * Before lexi.c 1.153 from 2021-11-25, indent did not recognize 'Token' as a
588 * type name and then messed up the positioning of the '{'.
589 */
590 //indent input
591 static Token
592 ToToken(bool cond)
593 {
594 }
595 //indent end
596
597 //indent run-equals-input -TToken
598
599 /* Since lexi.c 1.153 from 2021-11-25. */
600 //indent run-equals-input
601
602
603 /*
604 * Indent gets easily confused by unknown type names in struct declarations.
605 */
606 //indent input
607 typedef struct OpenDirs {
608 CachedDirList list;
609 HashTable /* of CachedDirListNode */ table;
610 } OpenDirs;
611 //indent end
612
613 /* FIXME: The word 'HashTable' must not be aligned like a member name. */
614 //indent run
615 typedef struct OpenDirs {
616 CachedDirList list;
617 HashTable /* of CachedDirListNode */ table;
618 } OpenDirs;
619 //indent end
620
621 //indent run-equals-input -THashTable
622
623
624 /*
625 * Indent gets easily confused by unknown type names, even in declarations
626 * that are syntactically unambiguous.
627 */
628 //indent input
629 static CachedDir *dot = NULL;
630 //indent end
631
632 //indent run-equals-input -TCachedDir
633
634 /* Since lexi.c 1.153 from 2021-11-25. */
635 //indent run-equals-input
636
637
638 /*
639 * Before lexi.c 1.156 from 2021-11-25, indent easily got confused by unknown
640 * type names in declarations and generated 'HashEntry * he' with an extra
641 * space.
642 */
643 //indent input
644 static CachedDir *
645 CachedDir_New(const char *name)
646 {
647 }
648 //indent end
649
650 /* Since lexi.c 1.153 from 2021-11-25. */
651 //indent run-equals-input
652
653
654 /*
655 * Before lexi.c 1.156 from 2021-11-25, indent easily got confused by unknown
656 * type names in declarations and generated 'CachedDir * dir' with an extra
657 * space.
658 */
659 //indent input
660 static CachedDir *
661 CachedDir_Ref(CachedDir *dir)
662 {
663 }
664 //indent end
665
666 //indent run-equals-input
667
668
669 /*
670 * Before lexi.c 1.156 from 2021-11-25, indent easily got confused by unknown
671 * type names in declarations and generated 'HashEntry * he' with an extra
672 * space.
673 *
674 * Before lexi.c 1.153 from 2021-11-25, indent also placed the '{' at the end
675 * of the line.
676 */
677 //indent input
678 static bool
679 HashEntry_KeyEquals(const HashEntry *he, Substring key)
680 {
681 }
682 //indent end
683
684 //indent run-equals-input
685
686
687 /*
688 * Before lexi.c 1.156 from 2021-11-25, indent didn't notice that the two '*'
689 * are in a declaration, instead it interpreted the first '*' as a binary
690 * operator, therefore generating 'CachedDir * *var' with an extra space.
691 */
692 //indent input
693 static void
694 CachedDir_Assign(CachedDir **var, CachedDir *dir)
695 {
696 }
697 //indent end
698
699 //indent run-equals-input
700
701 //indent run-equals-input -TCachedDir
702
703
704 /*
705 * Before lexi.c 1.153 from 2021-11-25, all initializer expressions after the
706 * first one were indented as if they would be statement continuations. This
707 * was because the token 'Shell' was identified as a word, not as a type name.
708 */
709 //indent input
710 static Shell shells[] = {
711 {
712 first,
713 second,
714 },
715 };
716 //indent end
717
718 /* Since lexi.c 1.153 from 2021-11-25. */
719 //indent run-equals-input
720
721
722 /*
723 * Before lexi.c 1.158 from 2021-11-25, indent easily got confused by function
724 * attribute macros that followed the function declaration. Its primitive
725 * heuristic between deciding between a function declaration and a function
726 * definition only looked for ')' immediately followed by ',' or ';'. This was
727 * sufficient for well-formatted code before 1990. With the addition of
728 * function prototypes and GCC attributes, the situation became more
729 * complicated, and it took indent 31 years to adapt to this new reality.
730 */
731 //indent input
732 static void JobInterrupt(bool, int) MAKE_ATTR_DEAD;
733 static void JobRestartJobs(void);
734 //indent end
735
736 //indent run
737 /* $ FIXME: Missing space before 'MAKE_ATTR_DEAD'. */
738 static void JobInterrupt(bool, int)MAKE_ATTR_DEAD;
739 static void JobRestartJobs(void);
740 //indent end
741
742
743 /*
744 * Before lexi.c 1.158 from 2021-11-25, indent easily got confused by the
745 * tokens ')' and ';' in the function body. It wrongly regarded them as
746 * finishing a function declaration.
747 */
748 //indent input
749 MAKE_INLINE const char *
750 GNode_VarTarget(GNode *gn) { return GNode_ValueDirect(gn, TARGET); }
751 //indent end
752
753 /*
754 * Before lexi.c 1.156 from 2021-11-25, indent generated 'GNode * gn' with an
755 * extra space.
756 *
757 * Before lexi.c 1.158 from 2021-11-25, indent wrongly placed the function
758 * name in line 1, together with the '{'.
759 */
760 //indent run
761 MAKE_INLINE const char *
762 GNode_VarTarget(GNode *gn)
763 {
764 return GNode_ValueDirect(gn, TARGET);
765 }
766 //indent end
767
768 //indent run-equals-prev-output -TGNode
769
770
771 /*
772 * Ensure that '*' in declarations is interpreted (or at least formatted) as
773 * a 'pointer to' type derivation, not as a binary or unary operator.
774 */
775 //indent input
776 number *var = a * b;
777
778 void
779 function(void)
780 {
781 number *var = a * b;
782 }
783 //indent end
784
785 //indent run-equals-input -di0
786
787
788 /*
789 * In declarations, most occurrences of '*' are pointer type derivations.
790 * There are a few exceptions though. Some of these are hard to detect
791 * without knowing which identifiers are type names.
792 */
793 //indent input
794 char str[expr * expr];
795 char str[expr**ptr];
796 char str[*ptr**ptr];
797 char str[sizeof(expr * expr)];
798 char str[sizeof(int) * expr];
799 char str[sizeof(*ptr)];
800 char str[sizeof(type**)];
801 char str[sizeof(**ptr)];
802 //indent end
803
804 //indent run -di0
805 char str[expr * expr];
806 char str[expr * *ptr];
807 char str[*ptr * *ptr];
808 char str[sizeof(expr * expr)];
809 char str[sizeof(int) * expr];
810 char str[sizeof(*ptr)];
811 /* $ FIXME: should be 'type **' */
812 char str[sizeof(type * *)];
813 char str[sizeof(**ptr)];
814 //indent end
815
816
817 /*
818 * Since lexi.c 1.158 from 2021-11-25, whether the function 'a' was considered
819 * a declaration or a definition depended on the preceding struct, in
820 * particular the length of the 'pn' line. This didn't make sense at all and
821 * was due to an out-of-bounds memory access.
822 *
823 * Seen amongst others in args.c 1.72, function add_typedefs_from_file.
824 * Fixed in lexi.c 1.165 from 2021-11-27.
825 */
826 //indent input
827 struct {
828 } v = {
829 pn("ta"),
830 };
831
832 static void
833 a(char *fe)
834 {
835 }
836
837 struct {
838 } v = {
839 pn("t"),
840 };
841
842 static void
843 a(char *fe)
844 {
845 }
846 //indent end
847
848 //indent run -di0
849 struct {
850 } v = {
851 pn("ta"),
852 };
853
854 static void
855 a(char *fe)
856 {
857 }
858
859 struct {
860 } v = {
861 pn("t"),
862 };
863
864 static void
865 a(char *fe)
866 {
867 }
868 //indent end
869
870
871 /*
872 * Before NetBSD indent.c 1.178 from 2021-10-29, indent removed the blank
873 * before the '=', in the second and third of these function pointer
874 * declarations. This was because indent interpreted the prototype parameters
875 * 'int' and 'int, int' as type casts, which doesn't make sense at all. Fixing
876 * this properly requires large style changes since indent is based on simple
877 * heuristics all over. This didn't change in indent.c 1.178; instead, the
878 * rule for inserting a blank before a binary operator was changed to always
879 * insert a blank, except at the beginning of a line.
880 */
881 //indent input
882 char *(*fn)() = NULL;
883 char *(*fn)(int) = NULL;
884 char *(*fn)(int, int) = NULL;
885 //indent end
886
887 /* XXX: The parameter '(int)' is wrongly interpreted as a type cast. */
888 /* XXX: The parameter '(int, int)' is wrongly interpreted as a type cast. */
889 //indent run-equals-input -di0
890