xref: /netbsd-src/usr.bin/indent/debug.c (revision f68d28ccb5df2dfe20ddb2c038cc8840e47295bd)
1 /*	$NetBSD: debug.c,v 1.23 2023/05/23 16:53:57 rillig Exp $	*/
2 
3 /*-
4  * Copyright (c) 2023 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Roland Illig <rillig@NetBSD.org>.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29  * POSSIBILITY OF SUCH DAMAGE.
30  */
31 
32 #include <sys/cdefs.h>
33 __RCSID("$NetBSD: debug.c,v 1.23 2023/05/23 16:53:57 rillig Exp $");
34 
35 #include <stdarg.h>
36 
37 #include "indent.h"
38 
39 #ifdef debug
40 
41 /*-
42  * false	show only the changes to the parser state
43  * true		show unchanged parts of the parser state as well
44  */
45 static bool debug_full_parser_state = true;
46 
47 const char *const lsym_name[] = {
48 	"eof",
49 	"preprocessing",
50 	"newline",
51 	"comment",
52 	"lparen_or_lbracket",
53 	"rparen_or_rbracket",
54 	"lbrace",
55 	"rbrace",
56 	"period",
57 	"unary_op",
58 	"binary_op",
59 	"postfix_op",
60 	"question",
61 	"colon",
62 	"comma",
63 	"semicolon",
64 	"typedef",
65 	"storage_class",
66 	"type_outside_parentheses",
67 	"type_in_parentheses",
68 	"tag",
69 	"case_label",
70 	"sizeof",
71 	"offsetof",
72 	"word",
73 	"funcname",
74 	"do",
75 	"else",
76 	"for",
77 	"if",
78 	"switch",
79 	"while",
80 	"return",
81 };
82 
83 const char *const psym_name[] = {
84 	"0",
85 	"lbrace",
86 	"rbrace",
87 	"decl",
88 	"stmt",
89 	"stmt_list",
90 	"for_exprs",
91 	"if_expr",
92 	"if_expr_stmt",
93 	"if_expr_stmt_else",
94 	"else",
95 	"switch_expr",
96 	"do",
97 	"do_stmt",
98 	"while_expr",
99 };
100 
101 static const char *const declaration_name[] = {
102 	"no",
103 	"begin",
104 	"end",
105 };
106 
107 static const char *const in_enum_name[] = {
108 	"no",
109 	"enum",
110 	"type",
111 	"brace",
112 };
113 
114 const char *const paren_level_cast_name[] = {
115 	"(unknown cast)",
116 	"(maybe cast)",
117 	"(no cast)",
118 };
119 
120 const char *const line_kind_name[] = {
121 	"other",
122 	"blank",
123 	"#if",
124 	"#endif",
125 	"stmt head",
126 	"}",
127 	"block comment",
128 };
129 
130 static const char *const decl_ptr_name[] = {
131 	"start",
132 	"word",
133 	"word *",
134 	"other",
135 };
136 
137 static unsigned wrote_newlines = 1;
138 
139 void
140 debug_printf(const char *fmt, ...)
141 {
142 	FILE *f = output == stdout ? stderr : stdout;
143 	va_list ap;
144 
145 	va_start(ap, fmt);
146 	vfprintf(f, fmt, ap);
147 	va_end(ap);
148 	wrote_newlines = 0;
149 }
150 
151 void
152 debug_println(const char *fmt, ...)
153 {
154 	FILE *f = output == stdout ? stderr : stdout;
155 	va_list ap;
156 
157 	va_start(ap, fmt);
158 	vfprintf(f, fmt, ap);
159 	va_end(ap);
160 	fprintf(f, "\n");
161 	wrote_newlines = fmt[0] == '\0' ? wrote_newlines + 1 : 1;
162 }
163 
164 void
165 debug_blank_line(void)
166 {
167 	while (wrote_newlines < 2)
168 		debug_println("");
169 }
170 
171 void
172 debug_vis_range(const char *prefix, const char *s, size_t len,
173     const char *suffix)
174 {
175 	debug_printf("%s", prefix);
176 	for (size_t i = 0; i < len; i++) {
177 		const char *p = s + i;
178 		if (*p == '\\' || *p == '"')
179 			debug_printf("\\%c", *p);
180 		else if (isprint((unsigned char)*p))
181 			debug_printf("%c", *p);
182 		else if (*p == '\n')
183 			debug_printf("\\n");
184 		else if (*p == '\t')
185 			debug_printf("\\t");
186 		else
187 			debug_printf("\\x%02x", (unsigned char)*p);
188 	}
189 	debug_printf("%s", suffix);
190 }
191 
192 static void
193 debug_print_buf(const char *name, const struct buffer *buf)
194 {
195 	if (buf->len > 0) {
196 		debug_printf(" %s ", name);
197 		debug_vis_range("\"", buf->st, buf->len, "\"");
198 	}
199 }
200 
201 void
202 debug_buffers(void)
203 {
204 	debug_print_buf("token", &token);
205 	debug_print_buf("label", &lab);
206 	debug_print_buf("code", &code);
207 	debug_print_buf("comment", &com);
208 	debug_println("");
209 }
210 
211 #define debug_ps_bool(name) \
212 	if (ps.name != prev_ps.name) \
213 	    debug_println("[%c] -> [%c] ps." #name, \
214 		prev_ps.name ? 'x' : ' ', ps.name ? 'x' : ' '); \
215 	else if (debug_full_parser_state) \
216 	    debug_println("       [%c] ps." #name, ps.name ? 'x' : ' ')
217 #define debug_ps_int(name) \
218 	if (ps.name != prev_ps.name) \
219 	    debug_println("%3d -> %3d ps." #name, prev_ps.name, ps.name); \
220 	else if (debug_full_parser_state) \
221 	    debug_println("       %3d ps." #name, ps.name)
222 #define debug_ps_enum(name, names) \
223 	if (ps.name != prev_ps.name) \
224 	    debug_println("%3s -> %3s ps." #name, \
225 		(names)[prev_ps.name], (names)[ps.name]); \
226 	else if (debug_full_parser_state) \
227 	    debug_println("%10s ps." #name, (names)[ps.name])
228 
229 static bool
230 ps_paren_has_changed(const struct parser_state *prev_ps)
231 {
232 	if (prev_ps->nparen != ps.nparen)
233 		return true;
234 
235 	const paren_level_props *prev = prev_ps->paren, *curr = ps.paren;
236 	for (int i = 0; i < ps.nparen; i++)
237 		if (curr[i].indent != prev[i].indent
238 		    || curr[i].cast != prev[i].cast)
239 			return true;
240 	return false;
241 }
242 
243 static void
244 debug_ps_paren(const struct parser_state *prev_ps)
245 {
246 	if (!debug_full_parser_state && !ps_paren_has_changed(prev_ps))
247 		return;
248 
249 	debug_printf("           ps.paren:");
250 	for (int i = 0; i < ps.nparen; i++) {
251 		debug_printf(" %s%d",
252 		    paren_level_cast_name[ps.paren[i].cast],
253 		    ps.paren[i].indent);
254 	}
255 	if (ps.nparen == 0)
256 		debug_printf(" none");
257 	debug_println("");
258 }
259 
260 static bool
261 ps_di_stack_has_changed(const struct parser_state *prev_ps)
262 {
263 	if (prev_ps->decl_level != ps.decl_level)
264 		return true;
265 	for (int i = 0; i < ps.decl_level; i++)
266 		if (prev_ps->di_stack[i] != ps.di_stack[i])
267 			return true;
268 	return false;
269 }
270 
271 static void
272 debug_ps_di_stack(const struct parser_state *prev_ps)
273 {
274 	bool changed = ps_di_stack_has_changed(prev_ps);
275 	if (!debug_full_parser_state && !changed)
276 		return;
277 
278 	debug_printf("    %s     ps.di_stack:", changed ? "->" : "  ");
279 	for (int i = 0; i < ps.decl_level; i++)
280 		debug_printf(" %d", ps.di_stack[i]);
281 	if (ps.decl_level == 0)
282 		debug_printf(" none");
283 	debug_println("");
284 }
285 
286 void
287 debug_parser_state(void)
288 {
289 	static struct parser_state prev_ps;
290 
291 	debug_blank_line();
292 	debug_println("           ps.prev_token = %s",
293 	    lsym_name[ps.prev_token]);
294 	debug_ps_bool(curr_col_1);
295 	debug_ps_bool(next_col_1);
296 	debug_ps_bool(next_unary);
297 	debug_ps_bool(is_function_definition);
298 	debug_ps_bool(want_blank);
299 	debug_ps_bool(force_nl);
300 	debug_ps_int(line_start_nparen);
301 	debug_ps_int(nparen);
302 	debug_ps_paren(&prev_ps);
303 
304 	debug_ps_int(comment_delta);
305 	debug_ps_int(n_comment_delta);
306 	debug_ps_int(com_ind);
307 
308 	debug_ps_bool(block_init);
309 	debug_ps_int(block_init_level);
310 	debug_ps_bool(init_or_struct);
311 
312 	debug_ps_int(ind_level);
313 	debug_ps_int(ind_level_follow);
314 
315 	debug_ps_int(decl_level);
316 	debug_ps_di_stack(&prev_ps);
317 	debug_ps_bool(decl_on_line);
318 	debug_ps_bool(in_decl);
319 	debug_ps_enum(declaration, declaration_name);
320 	debug_ps_bool(blank_line_after_decl);
321 	debug_ps_bool(in_func_def_params);
322 	debug_ps_enum(in_enum, in_enum_name);
323 	debug_ps_enum(decl_ptr, decl_ptr_name);
324 	debug_ps_bool(decl_indent_done);
325 	debug_ps_int(decl_ind);
326 	debug_ps_bool(tabs_to_var);
327 
328 	debug_ps_bool(in_stmt_or_decl);
329 	debug_ps_bool(in_stmt_cont);
330 	debug_ps_bool(is_case_label);
331 	debug_ps_bool(seen_case);
332 
333 	// The debug output for the parser symbols is done in 'parse' instead.
334 
335 	debug_ps_enum(spaced_expr_psym, psym_name);
336 	debug_ps_int(quest_level);
337 	debug_blank_line();
338 
339 	prev_ps = ps;
340 }
341 
342 void
343 debug_parse_stack(const char *situation)
344 {
345 	printf("parse stack %s:", situation);
346 	for (int i = 1; i <= ps.tos; ++i)
347 		printf(" %s %d", psym_name[ps.s_sym[i]], ps.s_ind_level[i]);
348 	if (ps.tos == 0)
349 		printf(" empty");
350 	printf("\n");
351 }
352 #endif
353