xref: /netbsd-src/tests/usr.bin/indent/opt_bacc.c (revision 0982fd88d1a4358af669ce182835c8c00eae5dae)
1 /* $NetBSD: opt_bacc.c,v 1.13 2023/06/15 09:19:07 rillig Exp $ */
2 
3 /*
4  * Tests for the options '-bacc' and '-nbacc' ("blank line around conditional
5  * compilation").
6  *
7  * The option '-bacc' forces a blank line around every conditional compilation
8  * block.  For example, in front of every #ifdef and after every #endif.
9  * Other blank lines surrounding such blocks are swallowed.
10  *
11  * The option '-nbacc' leaves the vertical spacing as-is.
12  */
13 
14 
15 /* Example code without surrounding blank lines. */
16 //indent input
17 int		a;
18 #if 0
19 int		b;
20 #endif
21 int		c;
22 //indent end
23 
24 //indent run -bacc
25 int		a;
26 
27 #if 0
28 int		b;
29 #endif
30 
31 int		c;
32 //indent end
33 
34 /* The option '-nbacc' does not remove anything. */
35 //indent run-equals-input -nbacc
36 
37 
38 /* Example code containing blank lines. */
39 //indent input
40 int		space_a;
41 
42 #if 0
43 
44 int		space_b;
45 
46 #endif
47 
48 int		space_c;
49 //indent end
50 
51 //indent run-equals-input -bacc
52 
53 /* The option '-nbacc' does not remove anything. */
54 //indent run-equals-input -nbacc
55 
56 
57 /*
58  * Preprocessing directives can also occur in function bodies.
59  */
60 //indent input
61 const char *
os_name(void)62 os_name(void)
63 {
64 #if defined(__NetBSD__) || defined(__FreeBSD__)
65 	return "BSD";
66 #else
67 	return "unknown";
68 #endif
69 }
70 //indent end
71 
72 //indent run -bacc
73 const char *
os_name(void)74 os_name(void)
75 {
76 
77 #if defined(__NetBSD__) || defined(__FreeBSD__)
78 	return "BSD";
79 #else
80 	return "unknown";
81 #endif
82 
83 }
84 //indent end
85 
86 //indent run-equals-input -nbacc
87 
88 
89 /*
90  * Test nested preprocessor directives.
91  */
92 //indent input
93 #if outer
94 #if inner
95 int decl;
96 #endif
97 #endif
98 //indent end
99 
100 //indent run-equals-input -di0 -bacc
101 
102 //indent run-equals-input -di0 -nbacc
103 
104 
105 /*
106  * Test nested preprocessor directives that are interleaved with declarations.
107  */
108 //indent input
109 #ifdef outer
110 int outer_above;
111 #ifdef inner
112 int inner;
113 #endif
114 int outer_below;
115 #endif
116 //indent end
117 
118 //indent run -di0 -bacc
119 #ifdef outer
120 int outer_above;
121 
122 #ifdef inner
123 int inner;
124 #endif
125 
126 int outer_below;
127 #endif
128 //indent end
129 
130 //indent run-equals-input -di0 -nbacc
131 
132 
133 //indent input
134 /* before */
135 #if 0
136 /* between if and else */
137 #else
138 #if 1
139 #endif
140 #endif
141 /* after */
142 //indent end
143 
144 //indent run -bacc
145 /* before */
146 // $ XXX: The 'before' comment may refer to the '#if', so it is not obvious
147 // $ XXX: that this blank line is useful.
148 
149 #if 0
150 /* between if and else */
151 #else
152 // $ XXX: This blank line looks unintended, as both lines are preprocessing
153 // $ XXX: directives.
154 
155 #if 1
156 #endif
157 #endif
158 
159 /* after */
160 //indent end
161