1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
|
/* $NetBSD: debug.c,v 1.70 2023/06/27 04:41:23 rillig Exp $ */
/*-
* Copyright (c) 2023 The NetBSD Foundation, Inc.
* All rights reserved.
*
* This code is derived from software contributed to The NetBSD Foundation
* by Roland Illig <rillig@NetBSD.org>.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
#include <sys/cdefs.h>
__RCSID("$NetBSD: debug.c,v 1.70 2023/06/27 04:41:23 rillig Exp $");
#include <stdarg.h>
#include <string.h>
#include "indent.h"
#ifdef debug
static struct {
// false show only the changes to the parser state
// true show unchanged parts of the parser state as well
bool full_parser_state;
} config = {
.full_parser_state = false,
};
const char *const lsym_name[] = {
"eof",
"preprocessing",
"newline",
"comment",
"lparen",
"rparen",
"lbracket",
"rbracket",
"lbrace",
"rbrace",
"period",
"unary_op",
"sizeof",
"offsetof",
"postfix_op",
"binary_op",
"question",
"question_colon",
"comma",
"typedef",
"modifier",
"tag",
"type",
"word",
"funcname",
"label_colon",
"other_colon",
"semicolon",
"case",
"default",
"do",
"else",
"for",
"if",
"switch",
"while",
"return",
};
const char *const psym_name[] = {
"-",
"{block",
"{struct",
"{union",
"{enum",
"}",
"decl",
"stmt",
"for_exprs",
"if_expr",
"if_expr_stmt",
"if_expr_stmt_else",
"else",
"switch_expr",
"do",
"do_stmt",
"while_expr",
};
static const char *const declaration_name[] = {
"no",
"begin",
"end",
};
static const char *const badp_name[] = {
"none",
"seen{",
"decl",
"seen_decl",
"yes",
};
const char *const paren_level_cast_name[] = {
"(unknown cast)",
"(maybe cast)",
"(no cast)",
};
const char *const line_kind_name[] = {
"other",
"blank",
"#if",
"#endif",
"#other",
"stmt head",
"}",
"block comment",
"case/default",
};
static const char *const extra_expr_indent_name[] = {
"no",
"maybe",
"last",
};
static struct {
struct parser_state prev_ps;
bool ps_first;
const char *heading;
unsigned wrote_newlines;
} state = {
.ps_first = true,
.wrote_newlines = 1,
};
void
debug_printf(const char *fmt, ...)
{
FILE *f = output == stdout ? stderr : stdout;
va_list ap;
if (state.heading != NULL) {
fprintf(f, "%s\n", state.heading);
state.heading = NULL;
}
va_start(ap, fmt);
vfprintf(f, fmt, ap);
va_end(ap);
state.wrote_newlines = 0;
}
void
debug_println(const char *fmt, ...)
{
FILE *f = output == stdout ? stderr : stdout;
va_list ap;
if (state.heading != NULL) {
fprintf(f, "%s\n", state.heading);
state.heading = NULL;
state.wrote_newlines = 1;
}
va_start(ap, fmt);
vfprintf(f, fmt, ap);
va_end(ap);
fprintf(f, "\n");
state.wrote_newlines = fmt[0] == '\0' ? state.wrote_newlines + 1 : 1;
}
void
debug_blank_line(void)
{
while (state.wrote_newlines < 2)
debug_println("");
}
void
debug_vis_range(const char *s, size_t len)
{
debug_printf("\"");
for (size_t i = 0; i < len; i++) {
const char *p = s + i;
if (*p == '\\' || *p == '"')
debug_printf("\\%c", *p);
else if (isprint((unsigned char)*p))
debug_printf("%c", *p);
else if (*p == '\n')
debug_printf("\\n");
else if (*p == '\t')
debug_printf("\\t");
else
debug_printf("\\x%02x", (unsigned char)*p);
}
debug_printf("\"");
}
void
debug_print_buf(const char *name, const struct buffer *buf)
{
if (buf->len > 0) {
debug_printf(" %s ", name);
debug_vis_range(buf->s, buf->len);
}
}
void
debug_buffers(void)
{
debug_print_buf("label", &lab);
debug_print_buf("code", &code);
debug_print_buf("comment", &com);
debug_blank_line();
}
static void
debug_ps_bool_member(const char *name, bool prev, bool curr)
{
if (!state.ps_first && curr != prev) {
char diff = " -+x"[(prev ? 1 : 0) + (curr ? 2 : 0)];
debug_println(" [%c] ps.%s", diff, name);
} else if (config.full_parser_state || state.ps_first)
debug_println(" [%c] ps.%s", curr ? 'x' : ' ', name);
}
static void
debug_ps_int_member(const char *name, int prev, int curr)
{
if (!state.ps_first && curr != prev)
debug_println(" %3d -> %3d ps.%s", prev, curr, name);
else if (config.full_parser_state || state.ps_first)
debug_println(" %3d ps.%s", curr, name);
}
static void
debug_ps_enum_member(const char *name, const char *prev, const char *curr)
{
if (!state.ps_first && strcmp(prev, curr) != 0)
debug_println(" %3s -> %3s ps.%s", prev, curr, name);
else if (config.full_parser_state || state.ps_first)
debug_println(" %10s ps.%s", curr, name);
}
static bool
paren_stack_equal(const struct paren_stack *a, const struct paren_stack *b)
{
if (a->len != b->len)
return false;
for (size_t i = 0, n = a->len; i < n; i++)
if (a->item[i].indent != b->item[i].indent
|| a->item[i].cast != b->item[i].cast)
return false;
return true;
}
static void
debug_ps_paren(void)
{
if (!config.full_parser_state
&& paren_stack_equal(&state.prev_ps.paren, &ps.paren)
&& !state.ps_first)
return;
debug_printf(" ps.paren:");
for (size_t i = 0; i < ps.paren.len; i++) {
debug_printf(" %s%d",
paren_level_cast_name[ps.paren.item[i].cast],
ps.paren.item[i].indent);
}
if (ps.paren.len == 0)
debug_printf(" none");
debug_println("");
}
static bool
ps_di_stack_has_changed(void)
{
if (state.prev_ps.decl_level != ps.decl_level)
return true;
for (int i = 0; i < ps.decl_level; i++)
if (state.prev_ps.di_stack[i] != ps.di_stack[i])
return true;
return false;
}
static void
debug_ps_di_stack(void)
{
bool changed = ps_di_stack_has_changed();
if (!config.full_parser_state && !changed && !state.ps_first)
return;
debug_printf(" %s ps.di_stack:", changed ? "->" : " ");
for (int i = 0; i < ps.decl_level; i++)
debug_printf(" %d", ps.di_stack[i]);
if (ps.decl_level == 0)
debug_printf(" none");
debug_println("");
}
#define debug_ps_bool(name) \
debug_ps_bool_member(#name, state.prev_ps.name, ps.name)
#define debug_ps_int(name) \
debug_ps_int_member(#name, state.prev_ps.name, ps.name)
#define debug_ps_enum(name, names) \
debug_ps_enum_member(#name, (names)[state.prev_ps.name], \
(names)[ps.name])
void
debug_parser_state(void)
{
debug_blank_line();
state.heading = "token classification";
debug_ps_enum(prev_lsym, lsym_name);
debug_ps_bool(in_stmt_or_decl);
debug_ps_bool(in_decl);
debug_ps_bool(in_typedef_decl);
debug_ps_bool(in_var_decl);
debug_ps_bool(in_init);
debug_ps_int(init_level);
debug_ps_bool(line_has_func_def);
debug_ps_bool(in_func_def_params);
debug_ps_bool(line_has_decl);
debug_ps_enum(lbrace_kind, psym_name);
debug_ps_enum(spaced_expr_psym, psym_name);
debug_ps_bool(seen_case);
debug_ps_bool(prev_paren_was_cast);
debug_ps_int(quest_level);
state.heading = "indentation of statements and declarations";
debug_ps_int(ind_level);
debug_ps_int(ind_level_follow);
debug_ps_bool(line_is_stmt_cont);
debug_ps_int(decl_level);
debug_ps_di_stack();
debug_ps_bool(decl_indent_done);
debug_ps_int(decl_ind);
debug_ps_bool(tabs_to_var);
debug_ps_enum(extra_expr_indent, extra_expr_indent_name);
// The parser symbol stack is printed in debug_psyms_stack instead.
state.heading = "spacing inside a statement or declaration";
debug_ps_bool(next_unary);
debug_ps_bool(want_blank);
debug_ps_int(ind_paren_level);
debug_ps_paren();
state.heading = "indentation of comments";
debug_ps_int(comment_ind);
debug_ps_int(comment_shift);
debug_ps_bool(comment_cont);
state.heading = "vertical spacing";
debug_ps_bool(break_after_comma);
debug_ps_bool(want_newline);
debug_ps_enum(declaration, declaration_name);
debug_ps_bool(blank_line_after_decl);
debug_ps_enum(badp, badp_name);
state.heading = NULL;
debug_blank_line();
parser_state_free(&state.prev_ps);
parser_state_back_up(&state.prev_ps);
state.ps_first = false;
}
void
debug_psyms_stack(const char *situation)
{
debug_printf("parse stack %s:", situation);
const struct psym_stack *psyms = &ps.psyms;
for (size_t i = 0; i < psyms->len; i++)
debug_printf(" %d %s",
psyms->ind_level[i], psym_name[psyms->sym[i]]);
debug_println("");
}
#endif
|