Module Name:    src
Committed By:   rillig
Date:           Sat Jan  4 10:28:08 UTC 2025

Modified Files:
        src/usr.bin/indent: debug.c indent.c indent.h io.c parse.c pr_comment.c

Log Message:
indent: make debug output easier readable

The previous format had the values of the parser state on the left side
and the corresponding names on the right side.  While it looked nicely
aligned, it was not suitable for focusing on the actual data.  Replace
this format with the more common "key: value" format.

Use the names of the enum constants in the debug log, instead of the
previous "nice" names that needed one more level of mental translation
and in some cases contained unbalanced punctuation such as '{'.


To generate a diff of this commit:
cvs rdiff -u -r1.72 -r1.73 src/usr.bin/indent/debug.c
cvs rdiff -u -r1.392 -r1.393 src/usr.bin/indent/indent.c
cvs rdiff -u -r1.208 -r1.209 src/usr.bin/indent/indent.h
cvs rdiff -u -r1.236 -r1.237 src/usr.bin/indent/io.c
cvs rdiff -u -r1.79 -r1.80 src/usr.bin/indent/parse.c
cvs rdiff -u -r1.173 -r1.174 src/usr.bin/indent/pr_comment.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/usr.bin/indent/debug.c
diff -u src/usr.bin/indent/debug.c:1.72 src/usr.bin/indent/debug.c:1.73
--- src/usr.bin/indent/debug.c:1.72	Fri Jan  3 23:37:18 2025
+++ src/usr.bin/indent/debug.c	Sat Jan  4 10:28:08 2025
@@ -1,4 +1,4 @@
-/*	$NetBSD: debug.c,v 1.72 2025/01/03 23:37:18 rillig Exp $	*/
+/*	$NetBSD: debug.c,v 1.73 2025/01/04 10:28:08 rillig Exp $	*/
 
 /*-
  * Copyright (c) 2023 The NetBSD Foundation, Inc.
@@ -30,7 +30,7 @@
  */
 
 #include <sys/cdefs.h>
-__RCSID("$NetBSD: debug.c,v 1.72 2025/01/03 23:37:18 rillig Exp $");
+__RCSID("$NetBSD: debug.c,v 1.73 2025/01/04 10:28:08 rillig Exp $");
 
 #include <stdarg.h>
 #include <string.h>
@@ -48,133 +48,151 @@ static struct {
 };
 
 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",
+	"lsym_eof",
+	"lsym_preprocessing",
+	"lsym_newline",
+	"lsym_comment",
+	"lsym_lparen",
+	"lsym_rparen",
+	"lsym_lbracket",
+	"lsym_rbracket",
+	"lsym_lbrace",
+	"lsym_rbrace",
+	"lsym_period",
+	"lsym_unary_op",
+	"lsym_sizeof",
+	"lsym_offsetof",
+	"lsym_postfix_op",
+	"lsym_binary_op",
+	"lsym_question",
+	"lsym_question_colon",
+	"lsym_comma",
+	"lsym_typedef",
+	"lsym_modifier",
+	"lsym_tag",
+	"lsym_type",
+	"lsym_word",
+	"lsym_funcname",
+	"lsym_label_colon",
+	"lsym_other_colon",
+	"lsym_semicolon",
+	"lsym_case",
+	"lsym_default",
+	"lsym_do",
+	"lsym_else",
+	"lsym_for",
+	"lsym_if",
+	"lsym_switch",
+	"lsym_while",
+	"lsym_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",
+	"psym_0",
+	"psym_lbrace_block",
+	"psym_lbrace_struct",
+	"psym_lbrace_union",
+	"psym_lbrace_enum",
+	"psym_rbrace",
+	"psym_decl",
+	"psym_stmt",
+	"psym_for_exprs",
+	"psym_if_expr",
+	"psym_if_expr_stmt",
+	"psym_if_expr_stmt_else",
+	"psym_else",
+	"psym_switch_expr",
+	"psym_do",
+	"psym_do_stmt",
+	"psym_while_expr",
 };
 
 static const char *const newline_name[] = {
-	"no",
-	"no-if",
-	"no-lbrace",
-	"no-semicolon",
-	"yes",
+	"nl_no",
+	"nl_unless_if",
+	"nl_unless_lbrace",
+	"nl_unless_semicolon",
+	"nl_yes",
 };
 
 static const char *const declaration_name[] = {
-	"no",
-	"begin",
-	"end",
+	"decl_no",
+	"decl_begin",
+	"decl_end",
 };
 
 static const char *const badp_name[] = {
-	"none",
-	"seen{",
-	"decl",
-	"seen_decl",
-	"yes",
+	"badp_none",
+	"badp_seen_lbrace",
+	"badp_decl",
+	"badp_seen_decl",
+	"badp_yes",
 };
 
 const char *const paren_level_cast_name[] = {
-	"(unknown cast)",
-	"(maybe cast)",
-	"(no cast)",
+	"cast_unknown",
+	"cast_maybe",
+	"cast_no",
 };
 
 const char *const line_kind_name[] = {
-	"other",
-	"blank",
-	"#if",
-	"#endif",
-	"#other",
-	"stmt head",
-	"}",
-	"block comment",
-	"case/default",
+	"lk_other",
+	"lk_blank",
+	"lk_pre_if",
+	"lk_pre_endif",
+	"lk_pre_other",
+	"lk_stmt_head",
+	"lk_func_end",
+	"lk_block_comment",
+	"lk_case_or_default",
 };
 
 static const char *const extra_expr_indent_name[] = {
-	"no",
-	"maybe",
-	"last",
+	"eei_no",
+	"eei_maybe",
+	"eei_last"
 };
 
 static struct {
 	struct parser_state prev_ps;
 	bool ps_first;
-	const char *heading;
+	const char *heading1;
+	const char *heading2;
 	unsigned wrote_newlines;
 } state = {
 	.ps_first = true,
 	.wrote_newlines = 1,
 };
 
+static FILE *
+debug_file(void)
+{
+	return output == stdout ? stderr : stdout;
+}
+
+static void
+debug_print_headings(void)
+{
+	if (state.heading1 != NULL) {
+		(void)fprintf(debug_file(), "%s:\n", state.heading1);
+		state.heading1 = NULL;
+		state.wrote_newlines = 1;
+	}
+	if (state.heading2 != NULL) {
+		(void)fprintf(debug_file(), "\t%s:\n", state.heading2);
+		state.heading2 = NULL;
+		state.wrote_newlines = 1;
+	}
+}
+
 void
 debug_printf(const char *fmt, ...)
 {
-	FILE *f = output == stdout ? stderr : stdout;
 	va_list ap;
 
-	if (state.heading != NULL) {
-		(void)fprintf(f, "%s\n", state.heading);
-		state.heading = NULL;
-	}
+	debug_print_headings();
 	va_start(ap, fmt);
-	(void)vfprintf(f, fmt, ap);
+	(void)vfprintf(debug_file(), fmt, ap);
 	va_end(ap);
 	state.wrote_newlines = 0;
 }
@@ -182,18 +200,13 @@ debug_printf(const char *fmt, ...)
 void
 debug_println(const char *fmt, ...)
 {
-	FILE *f = output == stdout ? stderr : stdout;
 	va_list ap;
 
-	if (state.heading != NULL) {
-		(void)fprintf(f, "%s\n", state.heading);
-		state.heading = NULL;
-		state.wrote_newlines = 1;
-	}
+	debug_print_headings();
 	va_start(ap, fmt);
-	(void)vfprintf(f, fmt, ap);
+	(void)vfprintf(debug_file(), fmt, ap);
 	va_end(ap);
-	(void)fprintf(f, "\n");
+	(void)fprintf(debug_file(), "\n");
 	state.wrote_newlines = fmt[0] == '\0' ? state.wrote_newlines + 1 : 1;
 }
 
@@ -234,12 +247,15 @@ debug_print_buf(const char *name, const 
 }
 
 void
-debug_buffers(void)
+debug_buffers(const char *descr)
 {
-	debug_print_buf("label", &lab);
-	debug_print_buf("code", &code);
-	debug_print_buf("comment", &com);
-	debug_blank_line();
+	if (lab.len > 0 || code.len > 0 || com.len > 0) {
+		debug_printf("%s:", descr);
+		debug_print_buf("label", &lab);
+		debug_print_buf("code", &code);
+		debug_print_buf("comment", &com);
+		debug_blank_line();
+	}
 }
 
 static void
@@ -247,27 +263,27 @@ debug_ps_bool_member(const char *name, b
 {
 	if (!state.ps_first && curr != prev) {
 		char diff = " -+x"[(prev ? 1 : 0) + (curr ? 2 : 0)];
-		debug_println("        [%c]  ps.%s", diff, name);
+		debug_println("\t\t%s: [%c]", name, diff);
 	} else if (config.full_parser_state || state.ps_first)
-		debug_println("        [%c]  ps.%s", curr ? 'x' : ' ', name);
+		debug_println("\t\t%s: [%c]", name, curr ? 'x' : ' ');
 }
 
 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);
+		debug_println("\t\t%s: %d -> %d", name, prev, curr);
 	else if (config.full_parser_state || state.ps_first)
-		debug_println("        %3d  ps.%s", curr, name);
+		debug_println("\t\t%s: %d", name, curr);
 }
 
 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);
+		debug_println("\t\t%s: %s -> %s", name, prev, curr);
 	else if (config.full_parser_state || state.ps_first)
-		debug_println(" %10s  ps.%s", curr, name);
+		debug_println("\t\t%s: %s", name, curr);
 }
 
 static bool
@@ -291,9 +307,9 @@ debug_ps_paren(void)
 	    && !state.ps_first)
 		return;
 
-	debug_printf("             ps.paren:");
+	debug_printf("\t\tparen:");
 	for (size_t i = 0; i < ps.paren.len; i++) {
-		debug_printf(" %s%d",
+		debug_printf("  %s %d",
 		    paren_level_cast_name[ps.paren.item[i].cast],
 		    ps.paren.item[i].indent);
 	}
@@ -320,7 +336,7 @@ debug_ps_di_stack(void)
 	if (!config.full_parser_state && !changed && !state.ps_first)
 		return;
 
-	debug_printf("     %s      ps.di_stack:", changed ? "->" : "  ");
+	debug_printf("\t\tdi_stack: ");
 	for (int i = 0; i < ps.decl_level; i++)
 		debug_printf(" %d", ps.di_stack[i]);
 	if (ps.decl_level == 0)
@@ -341,7 +357,8 @@ debug_parser_state(void)
 {
 	debug_blank_line();
 
-	state.heading = "token classification";
+	state.heading1 = "parser state";
+	state.heading2 = "token classification";
 	debug_ps_enum(prev_lsym, lsym_name);
 	debug_ps_bool(in_stmt_or_decl);
 	debug_ps_bool(in_decl);
@@ -358,7 +375,7 @@ debug_parser_state(void)
 	debug_ps_bool(prev_paren_was_cast);
 	debug_ps_int(quest_level);
 
-	state.heading = "indentation of statements and declarations";
+	state.heading2 = "indentation of statements and declarations";
 	debug_ps_int(ind_level);
 	debug_ps_int(ind_level_follow);
 	debug_ps_bool(line_is_stmt_cont);
@@ -371,25 +388,26 @@ debug_parser_state(void)
 
 	// The parser symbol stack is printed in debug_psyms_stack instead.
 
-	state.heading = "spacing inside a statement or declaration";
+	state.heading2 = "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";
+	state.heading2 = "indentation of comments";
 	debug_ps_int(comment_ind);
 	debug_ps_int(comment_shift);
 	debug_ps_bool(comment_cont);
 
-	state.heading = "vertical spacing";
+	state.heading2 = "vertical spacing";
 	debug_ps_bool(break_after_comma);
 	debug_ps_enum(newline, newline_name);
 	debug_ps_enum(declaration, declaration_name);
 	debug_ps_bool(blank_line_after_decl);
 	debug_ps_enum(badp, badp_name);
 
-	state.heading = NULL;
+	state.heading1 = NULL;
+	state.heading2 = NULL;
 	debug_blank_line();
 
 	parser_state_free(&state.prev_ps);
@@ -398,13 +416,14 @@ debug_parser_state(void)
 }
 
 void
-debug_psyms_stack(const char *situation)
+debug_fmt_psyms_stack(struct buffer *buf)
 {
-	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("");
+	buf_clear(buf);
+	for (size_t i = 0; i < ps.psyms.len; i++) {
+		char num[64];
+		snprintf(num, sizeof(num), "  %d ", ps.psyms.ind_level[i]);
+		buf_add_str(buf, num);
+		buf_add_str(buf, psym_name[ps.psyms.sym[i]]);
+	}
 }
 #endif

Index: src/usr.bin/indent/indent.c
diff -u src/usr.bin/indent/indent.c:1.392 src/usr.bin/indent/indent.c:1.393
--- src/usr.bin/indent/indent.c:1.392	Fri Jan  3 23:37:18 2025
+++ src/usr.bin/indent/indent.c	Sat Jan  4 10:28:08 2025
@@ -1,4 +1,4 @@
-/*	$NetBSD: indent.c,v 1.392 2025/01/03 23:37:18 rillig Exp $	*/
+/*	$NetBSD: indent.c,v 1.393 2025/01/04 10:28:08 rillig Exp $	*/
 
 /*-
  * SPDX-License-Identifier: BSD-4-Clause
@@ -38,7 +38,7 @@
  */
 
 #include <sys/cdefs.h>
-__RCSID("$NetBSD: indent.c,v 1.392 2025/01/03 23:37:18 rillig Exp $");
+__RCSID("$NetBSD: indent.c,v 1.393 2025/01/04 10:28:08 rillig Exp $");
 
 #include <sys/param.h>
 #include <err.h>
@@ -1126,10 +1126,12 @@ indent(void)
 		lexer_symbol lsym = lexi();
 
 		debug_blank_line();
-		debug_printf("line %d: %s",
+		debug_printf("line %d: next token is %s",
 		    in.token_start_line, lsym_name[lsym]);
-		debug_print_buf("token", &token);
-		debug_buffers();
+		debug_print_buf("with text", &token);
+		debug_println("");
+		if (lab.len > 0 || code.len > 0 || com.len > 0)
+			debug_buffers("the buffers contain");
 		debug_blank_line();
 
 		if (lsym == lsym_eof)

Index: src/usr.bin/indent/indent.h
diff -u src/usr.bin/indent/indent.h:1.208 src/usr.bin/indent/indent.h:1.209
--- src/usr.bin/indent/indent.h:1.208	Fri Jan  3 23:37:18 2025
+++ src/usr.bin/indent/indent.h	Sat Jan  4 10:28:08 2025
@@ -1,4 +1,4 @@
-/*	$NetBSD: indent.h,v 1.208 2025/01/03 23:37:18 rillig Exp $	*/
+/*	$NetBSD: indent.h,v 1.209 2025/01/04 10:28:08 rillig Exp $	*/
 
 /*-
  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
@@ -67,6 +67,7 @@
 #include <ctype.h>
 #include <stdbool.h>
 #include <stdio.h>
+#include <string.h>
 
 typedef enum lexer_symbol {
 	lsym_eof,
@@ -460,9 +461,9 @@ void debug_println(const char *, ...) __
 void debug_blank_line(void);
 void debug_vis_range(const char *, size_t);
 void debug_parser_state(void);
-void debug_psyms_stack(const char *);
+void debug_fmt_psyms_stack(struct buffer *);
 void debug_print_buf(const char *, const struct buffer *);
-void debug_buffers(void);
+void debug_buffers(const char *);
 void parser_state_back_up(struct parser_state *);
 void parser_state_free(struct parser_state *);
 extern const char *const lsym_name[];
@@ -479,7 +480,7 @@ extern const char *const line_kind_name[
 #define	debug_parser_state() debug_noop()
 #define	debug_psyms_stack(situation) debug_noop()
 #define debug_print_buf(name, buf) debug_noop()
-#define	debug_buffers() debug_noop()
+#define	debug_buffers(descr) debug_noop()
 #define static_unless_debug static
 #endif
 
@@ -507,6 +508,12 @@ void *nonnull(void *);
 void buf_add_char(struct buffer *, char);
 void buf_add_chars(struct buffer *, const char *, size_t);
 
+static inline void
+buf_add_str(struct buffer *buf, const char *str)
+{
+	buf_add_chars(buf, str, strlen(str));
+}
+
 static inline bool
 ch_isalnum(char ch)
 {

Index: src/usr.bin/indent/io.c
diff -u src/usr.bin/indent/io.c:1.236 src/usr.bin/indent/io.c:1.237
--- src/usr.bin/indent/io.c:1.236	Thu Dec 12 05:51:50 2024
+++ src/usr.bin/indent/io.c	Sat Jan  4 10:28:08 2025
@@ -1,4 +1,4 @@
-/*	$NetBSD: io.c,v 1.236 2024/12/12 05:51:50 rillig Exp $	*/
+/*	$NetBSD: io.c,v 1.237 2025/01/04 10:28:08 rillig Exp $	*/
 
 /*-
  * SPDX-License-Identifier: BSD-4-Clause
@@ -38,7 +38,7 @@
  */
 
 #include <sys/cdefs.h>
-__RCSID("$NetBSD: io.c,v 1.236 2024/12/12 05:51:50 rillig Exp $");
+__RCSID("$NetBSD: io.c,v 1.237 2025/01/04 10:28:08 rillig Exp $");
 
 #include <err.h>
 #include <stdio.h>
@@ -176,6 +176,7 @@ want_blank_line(void)
 {
 	debug_println("%s: %s -> %s", __func__,
 	    line_kind_name[out.prev_line_kind], line_kind_name[out.line_kind]);
+	debug_blank_line();
 
 	if (((ps.blank_line_after_decl && ps.declaration == decl_no)
 	    || ps.badp == badp_yes)
@@ -426,8 +427,7 @@ void
 output_line(void)
 {
 	debug_blank_line();
-	debug_printf("%s", __func__);
-	debug_buffers();
+	debug_buffers(__func__);
 
 	if (indent_enabled == indent_on)
 		output_indented_line();

Index: src/usr.bin/indent/parse.c
diff -u src/usr.bin/indent/parse.c:1.79 src/usr.bin/indent/parse.c:1.80
--- src/usr.bin/indent/parse.c:1.79	Sun Jun 18 06:56:32 2023
+++ src/usr.bin/indent/parse.c	Sat Jan  4 10:28:08 2025
@@ -1,4 +1,4 @@
-/*	$NetBSD: parse.c,v 1.79 2023/06/18 06:56:32 rillig Exp $	*/
+/*	$NetBSD: parse.c,v 1.80 2025/01/04 10:28:08 rillig Exp $	*/
 
 /*-
  * SPDX-License-Identifier: BSD-4-Clause
@@ -38,7 +38,7 @@
  */
 
 #include <sys/cdefs.h>
-__RCSID("$NetBSD: parse.c,v 1.79 2023/06/18 06:56:32 rillig Exp $");
+__RCSID("$NetBSD: parse.c,v 1.80 2025/01/04 10:28:08 rillig Exp $");
 
 #include <stdlib.h>
 
@@ -264,7 +264,17 @@ parse(parser_symbol psym)
 		return;
 	}
 
-	debug_psyms_stack("before reduction");
+#if debug
+	static struct buffer before, after;
+	debug_fmt_psyms_stack(&before);
+#endif
 	psyms_reduce();
-	debug_psyms_stack("after reduction");
+#if debug
+	debug_fmt_psyms_stack(&after);
+	if (before.len != after.len
+	    || memcmp(before.s, after.s, before.len) != 0) {
+		debug_println("psyms before:%s", before.s);
+		debug_println("psyms after: %s", after.s);
+	}
+#endif
 }

Index: src/usr.bin/indent/pr_comment.c
diff -u src/usr.bin/indent/pr_comment.c:1.173 src/usr.bin/indent/pr_comment.c:1.174
--- src/usr.bin/indent/pr_comment.c:1.173	Sun Dec  3 21:44:42 2023
+++ src/usr.bin/indent/pr_comment.c	Sat Jan  4 10:28:08 2025
@@ -1,4 +1,4 @@
-/*	$NetBSD: pr_comment.c,v 1.173 2023/12/03 21:44:42 rillig Exp $	*/
+/*	$NetBSD: pr_comment.c,v 1.174 2025/01/04 10:28:08 rillig Exp $	*/
 
 /*-
  * SPDX-License-Identifier: BSD-4-Clause
@@ -38,7 +38,7 @@
  */
 
 #include <sys/cdefs.h>
-__RCSID("$NetBSD: pr_comment.c,v 1.173 2023/12/03 21:44:42 rillig Exp $");
+__RCSID("$NetBSD: pr_comment.c,v 1.174 2025/01/04 10:28:08 rillig Exp $");
 
 #include <string.h>
 
@@ -54,7 +54,7 @@ static void
 com_add_star(void)
 {
 	if (opt.star_comment_cont)
-		buf_add_chars(&com, " * ", 3);
+		buf_add_str(&com, " * ");
 }
 
 static bool
@@ -268,9 +268,9 @@ copy_comment_wrap_finish(int line_length
 
 	in.p += 2;
 	if (com.len > 0 && ch_isblank(com.s[com.len - 1]))
-		buf_add_chars(&com, "*/", 2);
+		buf_add_str(&com, "*/");
 	else
-		buf_add_chars(&com, " */", 3);
+		buf_add_str(&com, " */");
 }
 
 static void

Reply via email to