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