xref: /netbsd-src/tests/usr.bin/indent/fmt_else_comment.c (revision 53e024a772ef428ea95960e5c2dfe0a8f28396b9)
1 /*	$NetBSD: fmt_else_comment.c,v 1.3 2022/04/22 21:21:20 rillig Exp $	*/
2 
3 /*
4  * Tests for comments after 'if (expr)' and 'else'. If the option '-br' is
5  * given (or rather, if '-bl' is not given), indent looks ahead to the
6  * following significant token to see whether it is a '{', it then moves the
7  * comments after the '{'.
8  *
9  * See also:
10  *	FreeBSD r303484
11  *	FreeBSD r309342
12  */
13 
14 /*
15  * The two 'if' statements below exercise two different code paths, even
16  * though they look very similar.
17  */
18 #indent input
19 void t(void) {
20 	if (1) /* a */ int a; else /* b */ int b;
21 
22 	if (1) /* a */
23 		int a;
24 	else /* b */
25 		int b;
26 }
27 #indent end
28 
29 #indent run
30 void
31 t(void)
32 {
33 	if (1)			/* a */
34 		int		a;
35 	else			/* b */
36 		int		b;
37 
38 	if (1)			/* a */
39 		int		a;
40 	else			/* b */
41 		int		b;
42 }
43 #indent end
44 
45 
46 #indent input
47 void t(void) {
48 	if (1) {
49 
50 	}
51 
52 
53 
54 	/* Old indent would remove the 3 blank lines above, awaiting "else". */
55 
56 	if (1) {
57 		int a;
58 	}
59 
60 
61 	else if (0) {
62 		int b;
63 	}
64 	/* test */
65 	else
66 		;
67 
68 	if (1)
69 		;
70 	else /* Old indent would get very confused here */
71 	/* We also mustn't assume that there's only one comment */
72 	/* before the left brace. */
73 	{
74 
75 
76 	}
77 }
78 #indent end
79 
80 #indent run -bl
81 void
82 t(void)
83 {
84 	if (1)
85 	{
86 
87 	}
88 
89 
90 
91 	/*
92 	 * Old indent would remove the 3 blank lines above, awaiting "else".
93 	 */
94 
95 	if (1)
96 	{
97 		int		a;
98 	} else if (0)
99 	{
100 		int		b;
101 	}
102 	/* test */
103 	else
104 		;
105 
106 	if (1)
107 		;
108 	else			/* Old indent would get very confused here */
109 		/* We also mustn't assume that there's only one comment */
110 		/* before the left brace. */
111 	{
112 
113 
114 	}
115 }
116 #indent end
117