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