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