xref: /netbsd-src/tests/usr.bin/indent/opt_bad.c (revision 0fce8157b28d3190f9767e73df95eed6ca63d873)
1 /* $NetBSD: opt_bad.c,v 1.13 2023/10/22 21:03:08 rillig Exp $ */
2 
3 /*
4  * Tests for the options '-bad' and '-nbad'.
5  *
6  * The option '-bad' forces a blank line after every block of declarations.
7  * It only affects declarations of local variables.  It does not affect
8  * file-scoped declarations or definitions.
9  *
10  * The option '-nbad' leaves everything as is.
11  */
12 
13 /* Test global declarations. */
14 //indent input
15 int		global_variable;
16 void		function_declaration(void);
17 #if 0
18 #endif
19 /* comment */
20 //indent end
21 
22 //indent run-equals-input -bad
23 
24 //indent run-equals-input -nbad
25 
26 
27 /* See FreeBSD r303599. */
28 //indent input
29 #if defined(__i386__)
30 int a;
31 #elif defined(__amd64__)
32 int b;
33 #else
34 #error "Port me"
35 #endif
36 //indent end
37 
38 //indent run -bad
39 #if defined(__i386__)
40 int		a;
41 #elif defined(__amd64__)
42 int		b;
43 #else
44 #error "Port me"
45 #endif
46 //indent end
47 
48 
49 /* Test local declarations. */
50 //indent input
function_definition(void)51 void function_definition(void) {
52     int local_variable;
53     function_call();
54     int local_variable_after_statement;
55     function_call();
56 }
57 //indent end
58 
59 //indent run -bad
60 void
function_definition(void)61 function_definition(void)
62 {
63 	int		local_variable;
64 
65 	function_call();
66 	int		local_variable_after_statement;
67 
68 	function_call();
69 }
70 //indent end
71 
72 //indent run -nbad
73 void
function_definition(void)74 function_definition(void)
75 {
76 	int		local_variable;
77 	/* $ No blank line here. */
78 	function_call();
79 	int		local_variable_after_statement;
80 	/* $ No blank line here. */
81 	function_call();
82 }
83 //indent end
84 
85 
86 /*
87  * A comment after a declaration does not change whether there should be a
88  * blank line below the declaration.
89  */
90 //indent input
91 void
comments(void)92 comments(void)
93 {
94 	int local_var_1;	/* comment */
95 	int local_var_2;	/* comment */
96 	/* comment line */
97 	function_call();
98 }
99 //indent end
100 
101 //indent run -ldi0 -bad
102 void
comments(void)103 comments(void)
104 {
105 	int local_var_1;	/* comment */
106 	int local_var_2;	/* comment */
107 // $ Indent does not look ahead much, so it doesn't know whether this comment
108 // $ will be followed by a declaration or a statement.
109 	/* comment line */
110 
111 	function_call();
112 }
113 //indent end
114 
115 //indent run-equals-input -ldi0 -nbad
116 
117 
118 /*
119  * A declaration that has a braced initializer is still a declaration and
120  * therefore needs a blank line below.
121  */
122 //indent input
123 void
initializer(void)124 initializer(void)
125 {
126 	int local_var_init_1[] = {1};
127 	int local_var_init_2[] = {1};
128 	function_call();
129 }
130 
131 void
initializer_with_blank(void)132 initializer_with_blank(void)
133 {
134 	int local_var_init_1[] = {1};
135 
136 	int local_var_init_2[] = {1};
137 
138 	function_call();
139 }
140 //indent end
141 
142 //indent run -ldi0 -bad
143 void
initializer(void)144 initializer(void)
145 {
146 	int local_var_init_1[] = {1};
147 	int local_var_init_2[] = {1};
148 
149 	function_call();
150 }
151 
152 void
initializer_with_blank(void)153 initializer_with_blank(void)
154 {
155 	int local_var_init_1[] = {1};
156 
157 	int local_var_init_2[] = {1};
158 
159 	function_call();
160 }
161 //indent end
162 
163 //indent run-equals-input -ldi0 -nbad
164 
165 
166 //indent input
167 {
168 	// $ The '}' in an initializer does not finish a declaration,
169 	// $ only a semicolon does.
170 	int decl1[2][2] = {
171 		{1, 2},
172 		{3, 4},
173 	};
174 	/* comment */
175 	int decl2;
176 	// $ If the declaration is followed by a '}' that terminates the block
177 	// $ statement, there is no need for a blank line before the '}'.
178 }
179 //indent end
180 
181 //indent run-equals-input -bad -di0
182