debug.c revision 1.27 1 /* $NetBSD: debug.c,v 1.27 2023/06/04 11:09:18 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 (at) 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.27 2023/06/04 11:09:18 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",
53 "lbracket",
54 "rparen",
55 "rbracket",
56 "lbrace",
57 "rbrace",
58 "period",
59 "unary_op",
60 "binary_op",
61 "postfix_op",
62 "question",
63 "colon",
64 "comma",
65 "semicolon",
66 "typedef",
67 "storage_class",
68 "type_outside_parentheses",
69 "type_in_parentheses",
70 "tag",
71 "case_label",
72 "sizeof",
73 "offsetof",
74 "word",
75 "funcname",
76 "do",
77 "else",
78 "for",
79 "if",
80 "switch",
81 "while",
82 "return",
83 };
84
85 const char *const psym_name[] = {
86 "0",
87 "lbrace",
88 "rbrace",
89 "decl",
90 "stmt",
91 "stmt_list",
92 "for_exprs",
93 "if_expr",
94 "if_expr_stmt",
95 "if_expr_stmt_else",
96 "else",
97 "switch_expr",
98 "do",
99 "do_stmt",
100 "while_expr",
101 };
102
103 static const char *const declaration_name[] = {
104 "no",
105 "begin",
106 "end",
107 };
108
109 static const char *const in_enum_name[] = {
110 "no",
111 "enum",
112 "type",
113 "brace",
114 };
115
116 const char *const paren_level_cast_name[] = {
117 "(unknown cast)",
118 "(maybe cast)",
119 "(no cast)",
120 };
121
122 const char *const line_kind_name[] = {
123 "other",
124 "blank",
125 "#if",
126 "#endif",
127 "stmt head",
128 "}",
129 "block comment",
130 "case/default",
131 };
132
133 static const char *const decl_ptr_name[] = {
134 "start",
135 "word",
136 "word *",
137 "other",
138 };
139
140 static unsigned wrote_newlines = 1;
141
142 void
143 debug_printf(const char *fmt, ...)
144 {
145 FILE *f = output == stdout ? stderr : stdout;
146 va_list ap;
147
148 va_start(ap, fmt);
149 vfprintf(f, fmt, ap);
150 va_end(ap);
151 wrote_newlines = 0;
152 }
153
154 void
155 debug_println(const char *fmt, ...)
156 {
157 FILE *f = output == stdout ? stderr : stdout;
158 va_list ap;
159
160 va_start(ap, fmt);
161 vfprintf(f, fmt, ap);
162 va_end(ap);
163 fprintf(f, "\n");
164 wrote_newlines = fmt[0] == '\0' ? wrote_newlines + 1 : 1;
165 }
166
167 void
168 debug_blank_line(void)
169 {
170 while (wrote_newlines < 2)
171 debug_println("");
172 }
173
174 void
175 debug_vis_range(const char *prefix, const char *s, size_t len,
176 const char *suffix)
177 {
178 debug_printf("%s", prefix);
179 for (size_t i = 0; i < len; i++) {
180 const char *p = s + i;
181 if (*p == '\\' || *p == '"')
182 debug_printf("\\%c", *p);
183 else if (isprint((unsigned char)*p))
184 debug_printf("%c", *p);
185 else if (*p == '\n')
186 debug_printf("\\n");
187 else if (*p == '\t')
188 debug_printf("\\t");
189 else
190 debug_printf("\\x%02x", (unsigned char)*p);
191 }
192 debug_printf("%s", suffix);
193 }
194
195 void
196 debug_print_buf(const char *name, const struct buffer *buf)
197 {
198 if (buf->len > 0) {
199 debug_printf(" %s ", name);
200 debug_vis_range("\"", buf->st, buf->len, "\"");
201 }
202 }
203
204 void
205 debug_buffers(void)
206 {
207 debug_print_buf("label", &lab);
208 debug_print_buf("code", &code);
209 debug_print_buf("comment", &com);
210 debug_println("");
211 }
212
213 #define debug_ps_bool(name) \
214 if (ps.name != prev_ps.name) \
215 debug_println("[%c] -> [%c] ps." #name, \
216 prev_ps.name ? 'x' : ' ', ps.name ? 'x' : ' '); \
217 else if (debug_full_parser_state) \
218 debug_println(" [%c] ps." #name, ps.name ? 'x' : ' ')
219 #define debug_ps_int(name) \
220 if (ps.name != prev_ps.name) \
221 debug_println("%3d -> %3d ps." #name, prev_ps.name, ps.name); \
222 else if (debug_full_parser_state) \
223 debug_println(" %3d ps." #name, ps.name)
224 #define debug_ps_enum(name, names) \
225 if (ps.name != prev_ps.name) \
226 debug_println("%3s -> %3s ps." #name, \
227 (names)[prev_ps.name], (names)[ps.name]); \
228 else if (debug_full_parser_state) \
229 debug_println("%10s ps." #name, (names)[ps.name])
230
231 static bool
232 ps_paren_has_changed(const struct parser_state *prev_ps)
233 {
234 if (prev_ps->nparen != ps.nparen)
235 return true;
236
237 const paren_level_props *prev = prev_ps->paren, *curr = ps.paren;
238 for (int i = 0; i < ps.nparen; i++)
239 if (curr[i].indent != prev[i].indent
240 || curr[i].cast != prev[i].cast)
241 return true;
242 return false;
243 }
244
245 static void
246 debug_ps_paren(const struct parser_state *prev_ps)
247 {
248 if (!debug_full_parser_state && !ps_paren_has_changed(prev_ps))
249 return;
250
251 debug_printf(" ps.paren:");
252 for (int i = 0; i < ps.nparen; i++) {
253 debug_printf(" %s%d",
254 paren_level_cast_name[ps.paren[i].cast],
255 ps.paren[i].indent);
256 }
257 if (ps.nparen == 0)
258 debug_printf(" none");
259 debug_println("");
260 }
261
262 static bool
263 ps_di_stack_has_changed(const struct parser_state *prev_ps)
264 {
265 if (prev_ps->decl_level != ps.decl_level)
266 return true;
267 for (int i = 0; i < ps.decl_level; i++)
268 if (prev_ps->di_stack[i] != ps.di_stack[i])
269 return true;
270 return false;
271 }
272
273 static void
274 debug_ps_di_stack(const struct parser_state *prev_ps)
275 {
276 bool changed = ps_di_stack_has_changed(prev_ps);
277 if (!debug_full_parser_state && !changed)
278 return;
279
280 debug_printf(" %s ps.di_stack:", changed ? "->" : " ");
281 for (int i = 0; i < ps.decl_level; i++)
282 debug_printf(" %d", ps.di_stack[i]);
283 if (ps.decl_level == 0)
284 debug_printf(" none");
285 debug_println("");
286 }
287
288 void
289 debug_parser_state(void)
290 {
291 static struct parser_state prev_ps;
292
293 debug_blank_line();
294 debug_println(" ps.prev_token = %s",
295 lsym_name[ps.prev_token]);
296 debug_ps_bool(curr_col_1);
297 debug_ps_bool(next_col_1);
298 debug_ps_bool(next_unary);
299 debug_ps_bool(is_function_definition);
300 debug_ps_bool(want_blank);
301 debug_ps_bool(break_after_comma);
302 debug_ps_bool(force_nl);
303 debug_ps_int(line_start_nparen);
304 debug_ps_int(nparen);
305 debug_ps_paren(&prev_ps);
306
307 debug_ps_int(comment_delta);
308 debug_ps_int(n_comment_delta);
309 debug_ps_int(com_ind);
310
311 debug_ps_bool(block_init);
312 debug_ps_int(block_init_level);
313 debug_ps_bool(init_or_struct);
314
315 debug_ps_int(ind_level);
316 debug_ps_int(ind_level_follow);
317
318 debug_ps_int(decl_level);
319 debug_ps_di_stack(&prev_ps);
320 debug_ps_bool(decl_on_line);
321 debug_ps_bool(in_decl);
322 debug_ps_enum(declaration, declaration_name);
323 debug_ps_bool(blank_line_after_decl);
324 debug_ps_bool(in_func_def_params);
325 debug_ps_enum(in_enum, in_enum_name);
326 debug_ps_enum(decl_ptr, decl_ptr_name);
327 debug_ps_bool(decl_indent_done);
328 debug_ps_int(decl_ind);
329 debug_ps_bool(tabs_to_var);
330
331 debug_ps_bool(in_stmt_or_decl);
332 debug_ps_bool(in_stmt_cont);
333 debug_ps_bool(seen_case);
334
335 // The debug output for the parser symbols is done in 'parse' instead.
336
337 debug_ps_enum(spaced_expr_psym, psym_name);
338 debug_ps_int(quest_level);
339 debug_blank_line();
340
341 prev_ps = ps;
342 }
343
344 void
345 debug_parse_stack(const char *situation)
346 {
347 printf("parse stack %s:", situation);
348 for (int i = 1; i <= ps.tos; ++i)
349 printf(" %s %d", psym_name[ps.s_sym[i]], ps.s_ind_level[i]);
350 if (ps.tos == 0)
351 printf(" empty");
352 printf("\n");
353 }
354 #endif
355