debug.c revision 1.12
1/*	$NetBSD: debug.c,v 1.12 2023/05/17 18:03:25 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.12 2023/05/17 18:03:25 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 */
45static bool debug_full_parser_state = true;
46
47const 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
83const 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
101static const char *const declaration_name[] = {
102    "no",
103    "begin",
104    "end",
105};
106
107static const char *const in_enum_name[] = {
108    "no",
109    "enum",
110    "type",
111    "brace",
112};
113
114const char *const paren_level_cast_name[] = {
115    "(unknown cast)",
116    "(maybe cast)",
117    "(no cast)",
118};
119
120void
121debug_printf(const char *fmt, ...)
122{
123    FILE *f = output == stdout ? stderr : stdout;
124    va_list ap;
125
126    va_start(ap, fmt);
127    vfprintf(f, fmt, ap);
128    va_end(ap);
129}
130
131void
132debug_println(const char *fmt, ...)
133{
134    FILE *f = output == stdout ? stderr : stdout;
135    va_list ap;
136
137    va_start(ap, fmt);
138    vfprintf(f, fmt, ap);
139    va_end(ap);
140    fprintf(f, "\n");
141}
142
143void
144debug_vis_range(const char *prefix, const char *s, size_t len,
145    const char *suffix)
146{
147    debug_printf("%s", prefix);
148    for (size_t i = 0; i < len; i++) {
149	const char *p = s + i;
150	if (*p == '\\' || *p == '"')
151	    debug_printf("\\%c", *p);
152	else if (isprint((unsigned char)*p))
153	    debug_printf("%c", *p);
154	else if (*p == '\n')
155	    debug_printf("\\n");
156	else if (*p == '\t')
157	    debug_printf("\\t");
158	else
159	    debug_printf("\\x%02x", (unsigned char)*p);
160    }
161    debug_printf("%s", suffix);
162}
163
164static void
165debug_print_buf(const char *name, const struct buffer *buf)
166{
167    if (buf->len > 0) {
168	debug_printf("%s ", name);
169	debug_vis_range("\"", buf->st, buf->len, "\"\n");
170    }
171}
172
173void
174debug_buffers(void)
175{
176    if (lab.len > 0) {
177	debug_printf(" label ");
178	debug_vis_range("\"", lab.st, lab.len, "\"");
179    }
180    if (code.len > 0) {
181	debug_printf(" code ");
182	debug_vis_range("\"", code.st, code.len, "\"");
183    }
184    if (com.len > 0) {
185	debug_printf(" comment ");
186	debug_vis_range("\"", com.st, com.len, "\"");
187    }
188}
189
190#define debug_ps_bool(name) \
191	if (ps.name != prev_ps.name) \
192	    debug_println("[%c] -> [%c] ps." #name, \
193		prev_ps.name ? 'x' : ' ', ps.name ? 'x' : ' '); \
194	else if (debug_full_parser_state) \
195	    debug_println("       [%c] ps." #name, ps.name ? 'x' : ' ')
196#define debug_ps_int(name) \
197	if (ps.name != prev_ps.name) \
198	    debug_println("%3d -> %3d ps." #name, prev_ps.name, ps.name); \
199	else if (debug_full_parser_state) \
200	    debug_println("       %3d ps." #name, ps.name)
201#define debug_ps_enum(name, names) \
202	if (ps.name != prev_ps.name) \
203	    debug_println("%3s -> %3s ps." #name, \
204		(names)[prev_ps.name], (names)[ps.name]); \
205	else if (debug_full_parser_state) \
206	    debug_println("%10s ps." #name, (names)[ps.name])
207
208static bool
209ps_paren_has_changed(const struct parser_state *prev_ps)
210{
211    const paren_level_props *prev = prev_ps->paren, *curr = ps.paren;
212
213    if (prev_ps->nparen != ps.nparen)
214	return true;
215
216    for (int i = 0; i < ps.nparen; i++)
217	if (curr[i].indent != prev[i].indent || curr[i].cast != prev[i].cast)
218	    return true;
219    return false;
220}
221
222static void
223debug_ps_paren(const struct parser_state *prev_ps)
224{
225    if (!debug_full_parser_state && !ps_paren_has_changed(prev_ps))
226	return;
227
228    debug_printf("           ps.paren:");
229    for (int i = 0; i < ps.nparen; i++) {
230	debug_printf(" %s%d",
231	    paren_level_cast_name[ps.paren[i].cast], ps.paren[i].indent);
232    }
233    if (ps.nparen == 0)
234	debug_printf(" none");
235    debug_println("");
236}
237
238void
239debug_parser_state(lexer_symbol lsym)
240{
241    static struct parser_state prev_ps;
242
243    debug_println("");
244    debug_printf("line %d: %s", line_no, lsym_name[lsym]);
245    debug_vis_range(" \"", token.st, token.len, "\"\n");
246
247    debug_print_buf("label", &lab);
248    debug_print_buf("code", &code);
249    debug_print_buf("comment", &com);
250
251    debug_println("           ps.prev_token = %s", lsym_name[ps.prev_token]);
252    debug_ps_bool(curr_col_1);
253    debug_ps_bool(next_col_1);
254    debug_ps_bool(next_unary);
255    debug_ps_bool(is_function_definition);
256    debug_ps_bool(want_blank);
257    debug_ps_bool(force_nl);
258    debug_ps_int(line_start_nparen);
259    debug_ps_int(nparen);
260    debug_ps_paren(&prev_ps);
261
262    debug_ps_int(comment_delta);
263    debug_ps_int(n_comment_delta);
264    debug_ps_int(com_ind);
265
266    debug_ps_bool(block_init);
267    debug_ps_int(block_init_level);
268    debug_ps_bool(init_or_struct);
269
270    debug_ps_int(ind_level);
271    debug_ps_int(ind_level_follow);
272
273    debug_ps_int(decl_level);
274    debug_ps_bool(decl_on_line);
275    debug_ps_bool(in_decl);
276    debug_ps_enum(declaration, declaration_name);
277    debug_ps_bool(blank_line_after_decl);
278    debug_ps_bool(in_func_def_params);
279    debug_ps_enum(in_enum, in_enum_name);
280    debug_ps_bool(decl_indent_done);
281    debug_ps_int(decl_ind);
282    // No debug output for di_stack.
283    debug_ps_bool(tabs_to_var);
284
285    debug_ps_bool(in_stmt_or_decl);
286    debug_ps_bool(in_stmt_cont);
287    debug_ps_bool(is_case_label);
288    debug_ps_bool(seen_case);
289
290    // The debug output for the parser symbols is done in 'parse' instead.
291
292    debug_ps_enum(spaced_expr_psym, psym_name);
293    debug_ps_int(quest_level);
294
295    prev_ps = ps;
296}
297
298void
299debug_parse_stack(const char *situation)
300{
301    printf("parse stack %s:", situation);
302    for (int i = 1; i <= ps.tos; ++i)
303	printf(" %s %d", psym_name[ps.s_sym[i]], ps.s_ind_level[i]);
304    if (ps.tos == 0)
305	printf(" empty");
306    printf("\n");
307}
308#endif
309