fmt_decl.c revision 1.40 1 /* $NetBSD: fmt_decl.c,v 1.40 2023/05/15 15:04: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 single_param(int) __attribute__((__noreturn__)) ;
168 void function(const char *, ...) __attribute__((format(printf, 1, 2))) ;
169 //indent end
170
171 //indent run -di0
172 void single_param(int) __attribute__((__noreturn__));
173 void function(const char *, ...) __attribute__((format(printf, 1, 2)));
174 //indent end
175
176 //indent run
177 void single_param(int) __attribute__((__noreturn__));
178 void function(const char *, ...) __attribute__((format(printf, 1, 2)));
179 //indent end
180
181
182 //indent input
183 static
184 _attribute_printf(1, 2)
185 void
186 print_error(const char *fmt,...)
187 {
188 }
189 //indent end
190
191 //indent run
192 static
193 _attribute_printf(1, 2)
194 void
195 print_error(const char *fmt, ...)
196 {
197 }
198 //indent end
199
200
201 //indent input
202 static _attribute_printf(1, 2)
203 void
204 print_error(const char *fmt,...)
205 {
206 }
207 //indent end
208
209 //indent run
210 static _attribute_printf(1, 2)
211 void
212 print_error(const char *fmt, ...)
213 {
214 }
215 //indent end
216
217
218 //indent input
219 static void _attribute_printf(1, 2)
220 print_error(const char *fmt,...)
221 {
222 }
223 //indent end
224
225 //indent run
226 static void
227 _attribute_printf(1, 2)
228 print_error(const char *fmt, ...)
229 {
230 }
231 //indent end
232
233
234 /* See FreeBSD r309380 */
235 //indent input
236 static LIST_HEAD(, alq) ald_active;
237 static int ald_shutting_down = 0;
238 struct thread *ald_thread;
239 //indent end
240
241 //indent run
242 static LIST_HEAD(, alq) ald_active;
243 static int ald_shutting_down = 0;
244 struct thread *ald_thread;
245 //indent end
246
247
248 //indent input
249 static int
250 old_style_definition(a, b, c)
251 struct thread *a;
252 int b;
253 double ***c;
254 {
255
256 }
257 //indent end
258
259 //indent run
260 static int
261 old_style_definition(a, b, c)
262 struct thread *a;
263 int b;
264 double ***c;
265 {
266
267 }
268 //indent end
269
270
271 /*
272 * Demonstrate how variable declarations are broken into several lines when
273 * the line length limit is set quite low.
274 */
275 //indent input
276 struct s a,b;
277 struct s0 a,b;
278 struct s01 a,b;
279 struct s012 a,b;
280 struct s0123 a,b;
281 struct s01234 a,b;
282 struct s012345 a,b;
283 struct s0123456 a,b;
284 struct s01234567 a,b;
285 struct s012345678 a,b;
286 struct s0123456789 a,b;
287 struct s01234567890 a,b;
288 struct s012345678901 a,b;
289 struct s0123456789012 a,b;
290 struct s01234567890123 a,b;
291 //indent end
292
293 //indent run -l20 -di0
294 struct s a, b;
295 /* $ XXX: See process_comma, varname_len for why this line is broken. */
296 struct s0 a,
297 b;
298 /* $ XXX: The indentation of the second line is wrong. The variable names */
299 /* $ XXX: 'a' and 'b' should be in the same column; the word 'struct' is */
300 /* $ XXX: missing in the calculation for the indentation. */
301 struct s01 a,
302 b;
303 struct s012 a,
304 b;
305 struct s0123 a,
306 b;
307 struct s01234 a,
308 b;
309 struct s012345 a,
310 b;
311 struct s0123456 a,
312 b;
313 struct s01234567 a,
314 b;
315 struct s012345678 a,
316 b;
317 struct s0123456789 a,
318 b;
319 struct s01234567890 a,
320 b;
321 struct s012345678901 a,
322 b;
323 struct s0123456789012 a,
324 b;
325 struct s01234567890123 a,
326 b;
327 //indent end
328
329
330 //indent input
331 char * x(void)
332 {
333 type identifier;
334 type *pointer;
335 unused * value;
336 (void)unused * value;
337
338 dmax = (double)3 * 10.0;
339 dmin = (double)dmax * 10.0;
340 davg = (double)dmax * dmin;
341
342 return NULL;
343 }
344 //indent end
345
346 //indent run
347 char *
348 x(void)
349 {
350 type identifier;
351 type *pointer;
352 unused *value;
353 (void)unused * value;
354
355 dmax = (double)3 * 10.0;
356 dmin = (double)dmax * 10.0;
357 davg = (double)dmax * dmin;
358
359 return NULL;
360 }
361 //indent end
362
363
364 //indent input
365 int *
366 y(void) {
367
368 }
369
370 int
371 z(void) {
372
373 }
374 //indent end
375
376 //indent run
377 int *
378 y(void)
379 {
380
381 }
382
383 int
384 z(void)
385 {
386
387 }
388 //indent end
389
390
391 //indent input
392 int x;
393 int *y;
394 int * * * * z;
395 //indent end
396
397 //indent run
398 int x;
399 int *y;
400 int ****z;
401 //indent end
402
403
404 //indent input
405 int main(void) {
406 char (*f1)() = NULL;
407 char *(*f1)() = NULL;
408 char *(*f2)();
409 }
410 //indent end
411
412 /*
413 * Before NetBSD io.c 1.103 from 2021-10-27, indent wrongly placed the second
414 * and third variable declaration in column 1. This bug has been introduced
415 * to NetBSD when FreeBSD indent was imported in 2019.
416 */
417 //indent run -ldi0
418 int
419 main(void)
420 {
421 char (*f1)() = NULL;
422 char *(*f1)() = NULL;
423 char *(*f2)();
424 }
425 //indent end
426
427 //indent run
428 int
429 main(void)
430 {
431 /* $ XXX: Not really pretty, the name 'f1' should be aligned, if at all. */
432 char (*f1)() = NULL;
433 /* $ XXX: Not really pretty, the name 'f1' should be aligned, if at all. */
434 char *(* f1)() = NULL;
435 /* $ XXX: Not really pretty, the name 'f2' should be aligned, if at all. */
436 char *(* f2)();
437 }
438 //indent end
439
440
441 /*
442 * In some ancient time long before ISO C90, variable declarations with
443 * initializer could be written without '='. The C Programming Language from
444 * 1978 doesn't mention this form anymore.
445 *
446 * Before NetBSD lexi.c 1.123 from 2021-10-31, indent treated the '-' as a
447 * unary operator.
448 */
449 //indent input
450 int a - 1;
451 {
452 int a - 1;
453 }
454 //indent end
455
456 //indent run -di0
457 int a - 1;
458 {
459 int a - 1;
460 }
461 //indent end
462
463
464 /*
465 * Between 2019-04-04 and before lexi.c 1.146 from 2021-11-19, the indentation
466 * of the '*' depended on the function name, which did not make sense. For
467 * function names that matched [A-Za-z]+, the '*' was placed correctly, for
468 * all other function names (containing [$0-9_]) the '*' was right-aligned on
469 * the declaration indentation, which defaults to 16.
470 */
471 //indent input
472 int *
473 f2(void)
474 {
475 }
476
477 int *
478 yy(void)
479 {
480 }
481
482 int *
483 int_create(void)
484 {
485 }
486 //indent end
487
488 //indent run-equals-input
489
490
491 /*
492 * Since 2019-04-04, the space between the '){' is missing.
493 */
494 //indent input
495 int *
496 function_name_____20________30________40________50
497 (void)
498 {}
499 //indent end
500
501 /*
502 * Before 2023-05-11, indent moved the '{' right after the '(void)', without
503 * any space in between.
504 */
505 //indent run
506 int *function_name_____20________30________40________50
507 (void)
508 {
509 }
510 //indent end
511
512
513 /*
514 * Since 2019-04-04 and before lexi.c 1.144 from 2021-11-19, some function
515 * names were preserved while others were silently discarded.
516 */
517 //indent input
518 int *
519 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
520 (void)
521 {}
522 //indent end
523
524 /*
525 * Before 2023-05-11, indent moved the '{' right after the '(void)', without
526 * any space in between.
527 */
528 //indent run
529 int *aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
530 (void)
531 {
532 }
533 //indent end
534
535
536 /*
537 * Before 1990, when C90 standardized function prototypes, a function
538 * declaration or definition did not contain a '*' that may have looked
539 * similar to the binary operator '*' because it was surrounded by two
540 * identifiers.
541 *
542 * As of 2021-11-21, indent interpreted the '*' in the function declaration in
543 * line 1 as a binary operator, even though the '*' was followed by a ','
544 * directly. This was not visible in the output though since indent never
545 * outputs a space before a comma.
546 *
547 * In the function declaration in line 2 and the function definition in line
548 * 5, indent interpreted the '*' as a binary operator as well and accordingly
549 * placed spaces around the '*'. On a very low syntactical analysis level,
550 * this may have made sense since the '*' was surrounded by words, but still
551 * the '*' is part of a declaration, where a binary operator does not make
552 * sense.
553 *
554 * Essentially, as of 2021, indent had missed the last 31 years of advances in
555 * the C programming language, in particular the invention of function
556 * prototypes. Instead, the workaround had been to require all type names to
557 * be specified via the options '-ta' and '-T'. This put the burden on the
558 * user instead of the implementer.
559 *
560 * Fixed in lexi.c 1.156 from 2021-11-25.
561 */
562 //indent input
563 void buffer_add(buffer *, char);
564 void buffer_add(buffer *buf, char ch);
565
566 void
567 buffer_add(buffer *buf, char ch)
568 {
569 *buf->e++ = ch;
570 }
571 //indent end
572
573 /* Before lexi.c 1.156 from 2021-11-25, indent generated 'buffer * buf'. */
574 //indent run
575 void buffer_add(buffer *, char);
576 /* $ FIXME: space after '*' */
577 void buffer_add(buffer * buf, char ch);
578
579 void
580 buffer_add(buffer *buf, char ch)
581 {
582 *buf->e++ = ch;
583 }
584 //indent end
585
586
587 /*
588 * Before lexi.c 1.153 from 2021-11-25, indent did not recognize 'Token' as a
589 * type name and then messed up the positioning of the '{'.
590 */
591 //indent input
592 static Token
593 ToToken(bool cond)
594 {
595 }
596 //indent end
597
598 //indent run-equals-input -TToken
599
600 /* Since lexi.c 1.153 from 2021-11-25. */
601 //indent run-equals-input
602
603
604 /*
605 * Indent gets easily confused by unknown type names in struct declarations.
606 */
607 //indent input
608 typedef struct OpenDirs {
609 CachedDirList list;
610 HashTable /* of CachedDirListNode */ table;
611 } OpenDirs;
612 //indent end
613
614 /* FIXME: The word 'HashTable' must not be aligned like a member name. */
615 //indent run
616 typedef struct OpenDirs {
617 CachedDirList list;
618 HashTable /* of CachedDirListNode */ table;
619 } OpenDirs;
620 //indent end
621
622 //indent run-equals-input -THashTable
623
624
625 /*
626 * Indent gets easily confused by unknown type names, even in declarations
627 * that are syntactically unambiguous.
628 */
629 //indent input
630 static CachedDir *dot = NULL;
631 //indent end
632
633 //indent run-equals-input -TCachedDir
634
635 /* Since lexi.c 1.153 from 2021-11-25. */
636 //indent run-equals-input
637
638
639 /*
640 * Before lexi.c 1.156 from 2021-11-25, indent easily got confused by unknown
641 * type names in declarations and generated 'HashEntry * he' with an extra
642 * space.
643 */
644 //indent input
645 static CachedDir *
646 CachedDir_New(const char *name)
647 {
648 }
649 //indent end
650
651 /* Since lexi.c 1.153 from 2021-11-25. */
652 //indent run-equals-input
653
654
655 /*
656 * Before lexi.c 1.156 from 2021-11-25, indent easily got confused by unknown
657 * type names in declarations and generated 'CachedDir * dir' with an extra
658 * space.
659 */
660 //indent input
661 static CachedDir *
662 CachedDir_Ref(CachedDir *dir)
663 {
664 }
665 //indent end
666
667 //indent run-equals-input
668
669
670 /*
671 * Before lexi.c 1.156 from 2021-11-25, indent easily got confused by unknown
672 * type names in declarations and generated 'HashEntry * he' with an extra
673 * space.
674 *
675 * Before lexi.c 1.153 from 2021-11-25, indent also placed the '{' at the end
676 * of the line.
677 */
678 //indent input
679 static bool
680 HashEntry_KeyEquals(const HashEntry *he, Substring key)
681 {
682 }
683 //indent end
684
685 //indent run-equals-input
686
687
688 /*
689 * Before lexi.c 1.156 from 2021-11-25, indent didn't notice that the two '*'
690 * are in a declaration, instead it interpreted the first '*' as a binary
691 * operator, therefore generating 'CachedDir * *var' with an extra space.
692 */
693 //indent input
694 static void
695 CachedDir_Assign(CachedDir **var, CachedDir *dir)
696 {
697 }
698 //indent end
699
700 //indent run-equals-input
701
702 //indent run-equals-input -TCachedDir
703
704
705 /*
706 * Before lexi.c 1.153 from 2021-11-25, all initializer expressions after the
707 * first one were indented as if they would be statement continuations. This
708 * was because the token 'Shell' was identified as a word, not as a type name.
709 */
710 //indent input
711 static Shell shells[] = {
712 {
713 first,
714 second,
715 },
716 };
717 //indent end
718
719 /* Since lexi.c 1.153 from 2021-11-25. */
720 //indent run-equals-input
721
722
723 /*
724 * Before lexi.c 1.158 from 2021-11-25, indent easily got confused by function
725 * attribute macros that followed the function declaration. Its primitive
726 * heuristic between deciding between a function declaration and a function
727 * definition only looked for ')' immediately followed by ',' or ';'. This was
728 * sufficient for well-formatted code before 1990. With the addition of
729 * function prototypes and GCC attributes, the situation became more
730 * complicated, and it took indent 31 years to adapt to this new reality.
731 */
732 //indent input
733 static void JobInterrupt(bool, int) MAKE_ATTR_DEAD;
734 static void JobRestartJobs(void);
735 //indent end
736
737 //indent run
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
891
892 /*
893 * Depending on the line break in the function header, the spaces around the
894 * '||' operator were removed.
895 */
896 //indent input
897 bool is_identifier_start(char ch)
898 {
899 return ch_isalpha(ch) || ch == '_';
900 }
901
902 bool
903 is_identifier_start(char ch)
904 {
905 return ch_isalpha(ch) || ch == '_';
906 }
907 //indent end
908
909 //indent run
910 bool
911 is_identifier_start(char ch)
912 {
913 return ch_isalpha(ch) || ch == '_';
914 }
915
916 bool
917 is_identifier_start(char ch)
918 {
919 return ch_isalpha(ch)||ch == '_';
920 }
921 //indent end
922