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