debug.c revision 1.4 1 /* $NetBSD: debug.c,v 1.4 2023/05/13 15:34:22 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.4 2023/05/13 15:34:22 rillig Exp $");
34
35 #include "indent.h"
36
37 #ifdef debug
38 const char *const lsym_name[] = {
39 "eof",
40 "preprocessing",
41 "newline",
42 "form_feed",
43 "comment",
44 "lparen_or_lbracket",
45 "rparen_or_rbracket",
46 "lbrace",
47 "rbrace",
48 "period",
49 "unary_op",
50 "binary_op",
51 "postfix_op",
52 "question",
53 "colon",
54 "comma",
55 "semicolon",
56 "typedef",
57 "storage_class",
58 "type_outside_parentheses",
59 "type_in_parentheses",
60 "tag",
61 "case_label",
62 "sizeof",
63 "offsetof",
64 "word",
65 "funcname",
66 "do",
67 "else",
68 "for",
69 "if",
70 "switch",
71 "while",
72 "return",
73 };
74
75 const char *const psym_name[] = {
76 "0",
77 "lbrace",
78 "rbrace",
79 "decl",
80 "stmt",
81 "stmt_list",
82 "for_exprs",
83 "if_expr",
84 "if_expr_stmt",
85 "if_expr_stmt_else",
86 "else",
87 "switch_expr",
88 "do",
89 "do_stmt",
90 "while_expr",
91 };
92
93 static const char *declaration_name[] = {
94 "no",
95 "begin",
96 "end",
97 };
98
99 static const char *in_enum_name[] = {
100 "no",
101 "enum",
102 "type",
103 "brace",
104 };
105
106 static bool debug_full_parser_state = true;
107
108 static void
109 debug_print_buf(const char *name, const struct buffer *buf)
110 {
111 if (buf->s < buf->e) {
112 debug_printf("%s ", name);
113 debug_vis_range("\"", buf->s, buf->e, "\"\n");
114 }
115 }
116
117 void
118 debug_buffers(void)
119 {
120 if (lab.e != lab.s) {
121 debug_printf(" label ");
122 debug_vis_range("\"", lab.s, lab.e, "\"");
123 }
124 if (code.e != code.s) {
125 debug_printf(" code ");
126 debug_vis_range("\"", code.s, code.e, "\"");
127 }
128 if (com.e < com.s) {
129 debug_printf(" comment ");
130 debug_vis_range("\"", com.s, com.e, "\"");
131 }
132 }
133
134 #define debug_ps_bool(name) \
135 if (ps.name != prev_ps.name) \
136 debug_println("[%c] -> [%c] ps." #name, \
137 prev_ps.name ? 'x' : ' ', ps.name ? 'x' : ' '); \
138 else if (debug_full_parser_state) \
139 debug_println(" [%c] ps." #name, ps.name ? 'x' : ' ')
140 #define debug_ps_int(name) \
141 if (ps.name != prev_ps.name) \
142 debug_println("%3d -> %3d ps." #name, prev_ps.name, ps.name); \
143 else if (debug_full_parser_state) \
144 debug_println(" %3d ps." #name, ps.name)
145 #define debug_ps_enum(name, names) \
146 if (ps.name != prev_ps.name) \
147 debug_println("%3s -> %3s ps." #name, \
148 (names)[prev_ps.name], (names)[ps.name]); \
149 else if (debug_full_parser_state) \
150 debug_println("%10s ps." #name, (names)[ps.name])
151
152 static bool
153 ps_paren_has_changed(const struct parser_state *prev_ps)
154 {
155 const paren_level_props *prev = prev_ps->paren, *curr = ps.paren;
156
157 if (prev_ps->nparen != ps.nparen)
158 return true;
159
160 for (int i = 0; i < ps.nparen; i++) {
161 if (curr[i].indent != prev[i].indent ||
162 curr[i].maybe_cast != prev[i].maybe_cast ||
163 curr[i].no_cast != prev[i].no_cast)
164 return true;
165 }
166 return false;
167 }
168
169 static void
170 debug_ps_paren(const struct parser_state *prev_ps)
171 {
172 if (!debug_full_parser_state && !ps_paren_has_changed(prev_ps))
173 return;
174
175 debug_printf(" ps.paren:");
176 for (int i = 0; i < ps.nparen; i++) {
177 const paren_level_props *props = ps.paren + i;
178 const char *cast = props->no_cast ? "(no cast)"
179 : props->maybe_cast ? "(cast)"
180 : "";
181 debug_printf(" %s%d", cast, props->indent);
182 }
183 if (ps.nparen == 0)
184 debug_printf(" none");
185 debug_println("");
186 }
187
188 void
189 debug_parser_state(lexer_symbol lsym)
190 {
191 static struct parser_state prev_ps;
192
193 debug_println("");
194 debug_printf("line %d: %s", line_no, lsym_name[lsym]);
195 debug_vis_range(" \"", token.s, token.e, "\"\n");
196
197 debug_print_buf("label", &lab);
198 debug_print_buf("code", &code);
199 debug_print_buf("comment", &com);
200
201 debug_println(" ps.prev_token = %s", lsym_name[ps.prev_token]);
202 debug_ps_bool(curr_col_1);
203 debug_ps_bool(next_col_1);
204 debug_ps_bool(next_unary);
205 debug_ps_bool(is_function_definition);
206 debug_ps_bool(want_blank);
207 debug_ps_bool(force_nl);
208 debug_ps_int(line_start_nparen);
209 debug_ps_int(nparen);
210 debug_ps_paren(&prev_ps);
211
212 debug_ps_int(comment_delta);
213 debug_ps_int(n_comment_delta);
214 debug_ps_int(com_ind);
215
216 debug_ps_bool(block_init);
217 debug_ps_int(block_init_level);
218 debug_ps_bool(init_or_struct);
219
220 debug_ps_int(ind_level);
221 debug_ps_int(ind_level_follow);
222
223 debug_ps_int(decl_level);
224 debug_ps_bool(decl_on_line);
225 debug_ps_bool(in_decl);
226 debug_ps_enum(declaration, declaration_name);
227 debug_ps_bool(blank_line_after_decl);
228 debug_ps_bool(in_func_def_params);
229 debug_ps_enum(in_enum, in_enum_name);
230 debug_ps_bool(decl_indent_done);
231 debug_ps_int(decl_ind);
232 // No debug output for di_stack.
233 debug_ps_bool(tabs_to_var);
234
235 debug_ps_bool(in_stmt_or_decl);
236 debug_ps_bool(in_stmt_cont);
237 debug_ps_bool(is_case_label);
238 debug_ps_bool(seen_case);
239
240 // The debug output for the parser symbols is done in 'parse' instead.
241
242 debug_ps_enum(spaced_expr_psym, psym_name);
243 debug_ps_int(quest_level);
244
245 prev_ps = ps;
246 }
247
248 void
249 debug_parse_stack(const char *situation)
250 {
251 printf("parse stack %s:", situation);
252 for (int i = 1; i <= ps.tos; ++i)
253 printf(" %s %d", psym_name[ps.s_sym[i]], ps.s_ind_level[i]);
254 if (ps.tos == 0)
255 printf(" empty");
256 printf("\n");
257 }
258 #endif
259