fmt_decl.c revision 1.47 1 /* $NetBSD: fmt_decl.c,v 1.47 2023/06/02 14:21:55 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 //indent run-equals-input
574
575
576 /*
577 * Before lexi.c 1.153 from 2021-11-25, indent did not recognize 'Token' as a
578 * type name and then messed up the positioning of the '{'.
579 */
580 //indent input
581 static Token
582 ToToken(bool cond)
583 {
584 }
585 //indent end
586
587 //indent run-equals-input -TToken
588
589 /* Since lexi.c 1.153 from 2021-11-25. */
590 //indent run-equals-input
591
592
593 /*
594 * Before indent.c 1.309 from 2023-05-23, indent easily got confused by unknown
595 * type names in struct declarations, as a ';' did not finish a declaration.
596 */
597 //indent input
598 typedef struct OpenDirs {
599 CachedDirList list;
600 HashTable /* of CachedDirListNode */ table;
601 } OpenDirs;
602 //indent end
603
604 //indent run-equals-input -THashTable
605
606 //indent run-equals-input
607
608
609 /*
610 * Before lexi.c 1.153 from 2021-11-25, indent easily got confused by unknown
611 * type names, even in declarations that are syntactically unambiguous.
612 */
613 //indent input
614 static CachedDir *dot = NULL;
615 //indent end
616
617 //indent run-equals-input -TCachedDir
618
619 //indent run-equals-input
620
621
622 /*
623 * Before lexi.c 1.153 from 2021-11-25, indent easily got confused by unknown
624 * type names in declarations.
625 */
626 //indent input
627 static CachedDir *
628 CachedDir_New(const char *name)
629 {
630 }
631 //indent end
632
633 //indent run-equals-input
634
635
636 /*
637 * Before lexi.c 1.156 from 2021-11-25, indent easily got confused by unknown
638 * type names in declarations and generated 'CachedDir * dir' with an extra
639 * space.
640 */
641 //indent input
642 static CachedDir *
643 CachedDir_Ref(CachedDir *dir)
644 {
645 }
646 //indent end
647
648 //indent run-equals-input
649
650
651 /*
652 * Before lexi.c 1.156 from 2021-11-25, indent easily got confused by unknown
653 * type names in declarations and generated 'HashEntry * he' with an extra
654 * space.
655 *
656 * Before lexi.c 1.153 from 2021-11-25, indent also placed the '{' at the end
657 * of the line.
658 */
659 //indent input
660 static bool
661 HashEntry_KeyEquals(const HashEntry *he, Substring key)
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 didn't notice that the two '*'
671 * are in a declaration, instead it interpreted the first '*' as a binary
672 * operator, therefore generating 'CachedDir * *var' with an extra space.
673 */
674 //indent input
675 static void
676 CachedDir_Assign(CachedDir **var, CachedDir *dir)
677 {
678 }
679 //indent end
680
681 //indent run-equals-input -TCachedDir
682
683 //indent run-equals-input
684
685
686 /*
687 * Before lexi.c 1.153 from 2021-11-25, all initializer expressions after the
688 * first one were indented as if they were statement continuations. This was
689 * caused by the token 'Shell' being identified as a word, not as a type name.
690 */
691 //indent input
692 static Shell shells[] = {
693 {
694 first,
695 second,
696 },
697 };
698 //indent end
699
700 //indent run-equals-input
701
702
703 /*
704 * Before lexi.c 1.158 from 2021-11-25, indent easily got confused by function
705 * attribute macros that followed the function declaration. Its primitive
706 * heuristic between deciding between a function declaration and a function
707 * definition only looked for ')' immediately followed by ',' or ';'. This was
708 * sufficient for well-formatted code before 1990. With the addition of
709 * function prototypes and GCC attributes, the situation became more
710 * complicated, and it took indent 31 years to adapt to this new reality.
711 */
712 //indent input
713 static void JobInterrupt(bool, int) MAKE_ATTR_DEAD;
714 static void JobRestartJobs(void);
715 //indent end
716
717 //indent run
718 static void JobInterrupt(bool, int) MAKE_ATTR_DEAD;
719 static void JobRestartJobs(void);
720 //indent end
721
722
723 /*
724 * Before lexi.c 1.158 from 2021-11-25, indent easily got confused by the
725 * tokens ')' and ';' in the function body. It wrongly regarded them as
726 * finishing a function declaration.
727 */
728 //indent input
729 MAKE_INLINE const char *
730 GNode_VarTarget(GNode *gn) { return GNode_ValueDirect(gn, TARGET); }
731 //indent end
732
733 /*
734 * Before lexi.c 1.156 from 2021-11-25, indent generated 'GNode * gn' with an
735 * extra space.
736 *
737 * Before lexi.c 1.158 from 2021-11-25, indent wrongly placed the function
738 * name in line 1, together with the '{'.
739 */
740 //indent run
741 MAKE_INLINE const char *
742 GNode_VarTarget(GNode *gn)
743 {
744 return GNode_ValueDirect(gn, TARGET);
745 }
746 //indent end
747
748 //indent run-equals-prev-output -TGNode
749
750
751 /*
752 * Ensure that '*' in declarations is interpreted (or at least formatted) as
753 * a 'pointer to' type derivation, not as a binary or unary operator.
754 */
755 //indent input
756 number *var = a * b;
757
758 void
759 function(void)
760 {
761 number *var = a * b;
762 }
763 //indent end
764
765 //indent run-equals-input -di0
766
767
768 /*
769 * In declarations, most occurrences of '*' are pointer type derivations.
770 * There are a few exceptions though. Some of these are hard to detect
771 * without knowing which identifiers are type names.
772 */
773 //indent input
774 char str[expr * expr];
775 char str[expr**ptr];
776 char str[*ptr**ptr];
777 char str[sizeof(expr * expr)];
778 char str[sizeof(int) * expr];
779 char str[sizeof(*ptr)];
780 char str[sizeof(type**)];
781 char str[sizeof(**ptr)];
782 //indent end
783
784 //indent run -di0
785 char str[expr * expr];
786 char str[expr * *ptr];
787 char str[*ptr * *ptr];
788 char str[sizeof(expr * expr)];
789 char str[sizeof(int) * expr];
790 char str[sizeof(*ptr)];
791 /* $ FIXME: should be 'type **' */
792 char str[sizeof(type * *)];
793 char str[sizeof(**ptr)];
794 //indent end
795
796
797 /*
798 * Since lexi.c 1.158 from 2021-11-25, whether the function 'a' was considered
799 * a declaration or a definition depended on the preceding struct, in
800 * particular the length of the 'pn' line. This didn't make sense at all and
801 * was due to an out-of-bounds memory access.
802 *
803 * Seen amongst others in args.c 1.72, function add_typedefs_from_file.
804 * Fixed in lexi.c 1.165 from 2021-11-27.
805 */
806 //indent input
807 struct {
808 } v = {
809 pn("ta"),
810 };
811
812 static void
813 a(char *fe)
814 {
815 }
816
817 struct {
818 } v = {
819 pn("t"),
820 };
821
822 static void
823 a(char *fe)
824 {
825 }
826 //indent end
827
828 //indent run -di0
829 struct {
830 } v = {
831 pn("ta"),
832 };
833
834 static void
835 a(char *fe)
836 {
837 }
838
839 struct {
840 } v = {
841 pn("t"),
842 };
843
844 static void
845 a(char *fe)
846 {
847 }
848 //indent end
849
850
851 /*
852 * Before NetBSD indent.c 1.178 from 2021-10-29, indent removed the blank
853 * before the '=', in the second and third of these function pointer
854 * declarations. This was because indent interpreted the prototype parameters
855 * 'int' and 'int, int' as type casts, which doesn't make sense at all. Fixing
856 * this properly requires large style changes since indent is based on simple
857 * heuristics all over. This didn't change in indent.c 1.178; instead, the
858 * rule for inserting a blank before a binary operator was changed to always
859 * insert a blank, except at the beginning of a line.
860 */
861 //indent input
862 char *(*fn)() = NULL;
863 char *(*fn)(int) = NULL;
864 char *(*fn)(int, int) = NULL;
865 //indent end
866
867 /* XXX: The parameter '(int)' is wrongly interpreted as a type cast. */
868 /* XXX: The parameter '(int, int)' is wrongly interpreted as a type cast. */
869 //indent run-equals-input -di0
870
871
872 /*
873 * Depending on whether there was a line break in the function header, the
874 * spaces around the '||' operator were erroneously removed.
875 */
876 //indent input
877 bool is_identifier_start(char ch)
878 {
879 return ch_isalpha(ch) || ch == '_';
880 }
881
882 bool
883 is_identifier_start(char ch)
884 {
885 return ch_isalpha(ch) || ch == '_';
886 }
887 //indent end
888
889 //indent run
890 bool
891 is_identifier_start(char ch)
892 {
893 return ch_isalpha(ch) || ch == '_';
894 }
895
896 bool
897 is_identifier_start(char ch)
898 {
899 return ch_isalpha(ch) || ch == '_';
900 }
901 //indent end
902
903
904 //indent input
905 void buf_add_chars(struct buffer *, const char *, size_t);
906
907 static inline bool
908 ch_isalnum(char ch)
909 {
910 return isalnum((unsigned char)ch) != 0;
911 }
912
913 static inline bool
914 ch_isalpha(char ch)
915 {
916 return isalpha((unsigned char)ch) != 0;
917 }
918 //indent end
919
920 //indent run -i4 -di0
921 // $ FIXME: 'buffer' is classified as 'word'.
922 // $
923 // $ XXX: 'char' is classified as 'type_in_parentheses'; check whether this
924 // $ XXX: lexer symbol should only be used for types in cast expressions.
925 // $
926 // $ FIXME: 'size_t' is classified as 'word'.
927 void buf_add_chars(struct buffer *, const char *, size_t);
928
929 static inline bool
930 ch_isalnum(char ch)
931 {
932 return isalnum((unsigned char)ch) != 0;
933 }
934
935 static inline bool
936 ch_isalpha(char ch)
937 {
938 return isalpha((unsigned char)ch) != 0;
939 }
940 //indent end
941
942 //indent run-equals-input -i4 -di0
943
944
945 //indent input
946 void __printflike(1, 2)
947 debug_printf(const char *fmt, ...)
948 {
949 }
950 //indent end
951
952 //indent run
953 void
954 // $ FIXME: No line break here.
955 __printflike(1, 2)
956 debug_printf(const char *fmt, ...)
957 {
958 }
959 //indent end
960
961
962 //indent input
963 void
964 (error_at)(int msgid, const pos_t *pos, ...)
965 {
966 }
967 //indent end
968
969 //indent run -ci4 -di0 -ndj -nlp
970 void
971 // $ FIXME: Wrong indentation, should be 0 instead.
972 (error_at)(int msgid, const pos_t *pos, ...)
973 {
974 }
975 //indent end
976