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