Home | History | Annotate | Line # | Download | only in indent
debug.c revision 1.50
      1 /*	$NetBSD: debug.c,v 1.50 2023/06/10 16:43:55 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.50 2023/06/10 16:43:55 rillig Exp $");
     34 
     35 #include <stdarg.h>
     36 #include <string.h>
     37 
     38 #include "indent.h"
     39 
     40 #ifdef debug
     41 
     42 static struct {
     43 	/*-
     44 	 * false	show only the changes to the parser state
     45 	 * true		show unchanged parts of the parser state as well
     46 	 */
     47 	bool full_parser_state;
     48 } config = {
     49 	.full_parser_state = false,
     50 };
     51 
     52 const char *const lsym_name[] = {
     53 	"eof",
     54 	"preprocessing",
     55 	"newline",
     56 	"comment",
     57 	"lparen",
     58 	"rparen",
     59 	"lbracket",
     60 	"rbracket",
     61 	"lbrace",
     62 	"rbrace",
     63 	"period",
     64 	"unary_op",
     65 	"sizeof",
     66 	"offsetof",
     67 	"postfix_op",
     68 	"binary_op",
     69 	"question",
     70 	"question_colon",
     71 	"comma",
     72 	"typedef",
     73 	"modifier",
     74 	"tag",
     75 	"type_outside_parentheses",
     76 	"type_in_parentheses",
     77 	"word",
     78 	"funcname",
     79 	"label_colon",
     80 	"other_colon",
     81 	"semicolon",
     82 	"case",
     83 	"default",
     84 	"do",
     85 	"else",
     86 	"for",
     87 	"if",
     88 	"switch",
     89 	"while",
     90 	"return",
     91 };
     92 
     93 const char *const psym_name[] = {
     94 	"-",
     95 	"{block",
     96 	"{struct",
     97 	"{union",
     98 	"{enum",
     99 	"}",
    100 	"decl",
    101 	"stmt",
    102 	"stmt_list",
    103 	"for_exprs",
    104 	"if_expr",
    105 	"if_expr_stmt",
    106 	"if_expr_stmt_else",
    107 	"else",
    108 	"switch_expr",
    109 	"do",
    110 	"do_stmt",
    111 	"while_expr",
    112 };
    113 
    114 static const char *const declaration_name[] = {
    115 	"no",
    116 	"begin",
    117 	"end",
    118 };
    119 
    120 const char *const paren_level_cast_name[] = {
    121 	"(unknown cast)",
    122 	"(maybe cast)",
    123 	"(no cast)",
    124 };
    125 
    126 const char *const line_kind_name[] = {
    127 	"other",
    128 	"blank",
    129 	"#if",
    130 	"#endif",
    131 	"stmt head",
    132 	"}",
    133 	"block comment",
    134 	"case/default",
    135 };
    136 
    137 static const char *const extra_expr_indent_name[] = {
    138 	"no",
    139 	"maybe",
    140 	"last",
    141 };
    142 
    143 static struct {
    144 	struct parser_state prev_ps;
    145 	bool ps_first;
    146 	const char *heading;
    147 	unsigned wrote_newlines;
    148 } state = {
    149 	.ps_first = true,
    150 	.wrote_newlines = 1,
    151 };
    152 
    153 void
    154 debug_printf(const char *fmt, ...)
    155 {
    156 	FILE *f = output == stdout ? stderr : stdout;
    157 	va_list ap;
    158 
    159 	if (state.heading != NULL) {
    160 		fprintf(f, "%s\n", state.heading);
    161 		state.heading = NULL;
    162 	}
    163 	va_start(ap, fmt);
    164 	vfprintf(f, fmt, ap);
    165 	va_end(ap);
    166 	state.wrote_newlines = 0;
    167 }
    168 
    169 void
    170 debug_println(const char *fmt, ...)
    171 {
    172 	FILE *f = output == stdout ? stderr : stdout;
    173 	va_list ap;
    174 
    175 	if (state.heading != NULL) {
    176 		fprintf(f, "%s\n", state.heading);
    177 		state.heading = NULL;
    178 		state.wrote_newlines = 1;
    179 	}
    180 	va_start(ap, fmt);
    181 	vfprintf(f, fmt, ap);
    182 	va_end(ap);
    183 	fprintf(f, "\n");
    184 	state.wrote_newlines = fmt[0] == '\0' ? state.wrote_newlines + 1 : 1;
    185 }
    186 
    187 void
    188 debug_blank_line(void)
    189 {
    190 	while (state.wrote_newlines < 2)
    191 		debug_println("");
    192 }
    193 
    194 void
    195 debug_vis_range(const char *prefix, const char *s, size_t len,
    196     const char *suffix)
    197 {
    198 	debug_printf("%s", prefix);
    199 	for (size_t i = 0; i < len; i++) {
    200 		const char *p = s + i;
    201 		if (*p == '\\' || *p == '"')
    202 			debug_printf("\\%c", *p);
    203 		else if (isprint((unsigned char)*p))
    204 			debug_printf("%c", *p);
    205 		else if (*p == '\n')
    206 			debug_printf("\\n");
    207 		else if (*p == '\t')
    208 			debug_printf("\\t");
    209 		else
    210 			debug_printf("\\x%02x", (unsigned char)*p);
    211 	}
    212 	debug_printf("%s", suffix);
    213 }
    214 
    215 void
    216 debug_print_buf(const char *name, const struct buffer *buf)
    217 {
    218 	if (buf->len > 0) {
    219 		debug_printf(" %s ", name);
    220 		debug_vis_range("\"", buf->s, buf->len, "\"");
    221 	}
    222 }
    223 
    224 void
    225 debug_buffers(void)
    226 {
    227 	debug_print_buf("label", &lab);
    228 	debug_print_buf("code", &code);
    229 	debug_print_buf("comment", &com);
    230 	debug_blank_line();
    231 }
    232 
    233 static void
    234 write_ps_bool(const char *name, bool prev, bool curr)
    235 {
    236 	if (curr != prev) {
    237 		char diff = " -+x"[(prev ? 1 : 0) + (curr ? 2 : 0)];
    238 		debug_println("        [%c]  ps.%s", diff, name);
    239 	} else if (config.full_parser_state || state.ps_first)
    240 		debug_println("        [%c]  ps.%s", curr ? 'x' : ' ', name);
    241 }
    242 
    243 static void
    244 write_ps_int(const char *name, int prev, int curr)
    245 {
    246 	if (curr != prev)
    247 		debug_println(" %3d -> %3d  ps.%s", prev, curr, name);
    248 	else if (config.full_parser_state || state.ps_first)
    249 		debug_println("        %3d  ps.%s", curr, name);
    250 }
    251 
    252 static void
    253 write_ps_enum(const char *name, const char *prev, const char *curr)
    254 {
    255 	if (strcmp(prev, curr) != 0)
    256 		debug_println(" %3s -> %3s  ps.%s", prev, curr, name);
    257 	else if (config.full_parser_state || state.ps_first)
    258 		debug_println(" %10s  ps.%s", curr, name);
    259 }
    260 
    261 static bool
    262 ps_paren_has_changed(void)
    263 {
    264 	if (state.prev_ps.nparen != ps.nparen)
    265 		return true;
    266 
    267 	const struct paren_level *prev = state.prev_ps.paren, *curr = ps.paren;
    268 	for (int i = 0; i < ps.nparen; i++)
    269 		if (curr[i].indent != prev[i].indent
    270 		    || curr[i].cast != prev[i].cast)
    271 			return true;
    272 	return false;
    273 }
    274 
    275 static void
    276 debug_ps_paren(void)
    277 {
    278 	if (!config.full_parser_state && !ps_paren_has_changed()
    279 	    && !state.ps_first)
    280 		return;
    281 
    282 	debug_printf("             ps.paren:");
    283 	for (int i = 0; i < ps.nparen; i++) {
    284 		debug_printf(" %s%d",
    285 		    paren_level_cast_name[ps.paren[i].cast],
    286 		    ps.paren[i].indent);
    287 	}
    288 	if (ps.nparen == 0)
    289 		debug_printf(" none");
    290 	debug_println("");
    291 }
    292 
    293 static bool
    294 ps_di_stack_has_changed(void)
    295 {
    296 	if (state.prev_ps.decl_level != ps.decl_level)
    297 		return true;
    298 	for (int i = 0; i < ps.decl_level; i++)
    299 		if (state.prev_ps.di_stack[i] != ps.di_stack[i])
    300 			return true;
    301 	return false;
    302 }
    303 
    304 static void
    305 debug_ps_di_stack(void)
    306 {
    307 	bool changed = ps_di_stack_has_changed();
    308 	if (!config.full_parser_state && !changed && !state.ps_first)
    309 		return;
    310 
    311 	debug_printf("     %s      ps.di_stack:", changed ? "->" : "  ");
    312 	for (int i = 0; i < ps.decl_level; i++)
    313 		debug_printf(" %d", ps.di_stack[i]);
    314 	if (ps.decl_level == 0)
    315 		debug_printf(" none");
    316 	debug_println("");
    317 }
    318 
    319 #define debug_ps_bool(name) \
    320 	write_ps_bool(#name, state.prev_ps.name, ps.name)
    321 #define debug_ps_int(name) \
    322 	write_ps_int(#name, state.prev_ps.name, ps.name)
    323 #define debug_ps_enum(name, names) \
    324         write_ps_enum(#name, (names)[state.prev_ps.name], (names)[ps.name])
    325 
    326 void
    327 debug_parser_state(void)
    328 {
    329 	debug_blank_line();
    330 	debug_println("             ps.prev_lsym = %s",
    331 	    lsym_name[ps.prev_lsym]);
    332 
    333 	state.heading = "token classification";
    334 	debug_ps_bool(in_stmt_or_decl);
    335 	debug_ps_bool(in_decl);
    336 	debug_ps_bool(in_var_decl);
    337 	debug_ps_bool(in_init);
    338 	debug_ps_int(init_level);
    339 	debug_ps_bool(line_has_func_def);
    340 	debug_ps_bool(in_func_def_params);
    341 	debug_ps_bool(line_has_decl);
    342 	debug_ps_enum(lbrace_kind, psym_name);
    343 	debug_ps_enum(spaced_expr_psym, psym_name);
    344 	debug_ps_bool(seen_case);
    345 	debug_ps_bool(prev_paren_was_cast);
    346 	debug_ps_int(quest_level);
    347 
    348 	state.heading = "indentation of statements and declarations";
    349 	debug_ps_int(ind_level);
    350 	debug_ps_int(ind_level_follow);
    351 	debug_ps_bool(in_stmt_cont);
    352 	debug_ps_int(decl_level);
    353 	debug_ps_di_stack();
    354 	debug_ps_bool(decl_indent_done);
    355 	debug_ps_int(decl_ind);
    356 	debug_ps_bool(tabs_to_var);
    357 	debug_ps_enum(extra_expr_indent, extra_expr_indent_name);
    358 
    359 	// The parser symbol stack is printed in debug_psyms_stack instead.
    360 
    361 	state.heading = "spacing inside a statement or declaration";
    362 	debug_ps_bool(next_unary);
    363 	debug_ps_bool(want_blank);
    364 	debug_ps_int(line_start_nparen);
    365 	debug_ps_int(nparen);
    366 	debug_ps_paren();
    367 
    368 	state.heading = "horizontal spacing for comments";
    369 	debug_ps_int(comment_delta);
    370 	debug_ps_int(n_comment_delta);
    371 	debug_ps_int(com_ind);
    372 
    373 	state.heading = "vertical spacing";
    374 	debug_ps_bool(break_after_comma);
    375 	debug_ps_bool(force_nl);
    376 	debug_ps_enum(declaration, declaration_name);
    377 	debug_ps_bool(blank_line_after_decl);
    378 
    379 	state.heading = "comments";
    380 	debug_ps_bool(curr_col_1);
    381 	debug_ps_bool(next_col_1);
    382 
    383 	state.heading = NULL;
    384 	debug_blank_line();
    385 
    386 	state.prev_ps = ps;
    387 	state.ps_first = false;
    388 }
    389 
    390 void
    391 debug_psyms_stack(const char *situation)
    392 {
    393 	debug_printf("parse stack %s:", situation);
    394 	const struct psym_stack *psyms = &ps.psyms;
    395 	for (int i = 0; i <= psyms->top; ++i)
    396 		debug_printf(" %d %s",
    397 		    psyms->ind_level[i], psym_name[psyms->sym[i]]);
    398 	debug_blank_line();
    399 }
    400 #endif
    401