Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/usr.bin/indent indent: add debug logging for brace indentation



details:   https://anonhg.NetBSD.org/src/rev/e62e9de1c824
branches:  trunk
changeset: 375897:e62e9de1c824
user:      rillig <rillig%NetBSD.org@localhost>
date:      Sat May 20 02:47:35 2023 +0000

description:
indent: add debug logging for brace indentation

No functional change outside debug mode, as the initialization of
di_stack[0] was redundant.

diffstat:

 usr.bin/indent/debug.c  |  35 ++++++++++++++++++++++++++++++-----
 usr.bin/indent/indent.c |   7 ++-----
 usr.bin/indent/indent.h |   4 ++--
 3 files changed, 34 insertions(+), 12 deletions(-)

diffs (129 lines):

diff -r 2703369f8ebc -r e62e9de1c824 usr.bin/indent/debug.c
--- a/usr.bin/indent/debug.c    Sat May 20 01:28:14 2023 +0000
+++ b/usr.bin/indent/debug.c    Sat May 20 02:47:35 2023 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: debug.c,v 1.14 2023/05/18 05:33:27 rillig Exp $        */
+/*     $NetBSD: debug.c,v 1.15 2023/05/20 02:47:35 rillig Exp $        */
 
 /*-
  * Copyright (c) 2023 The NetBSD Foundation, Inc.
@@ -30,7 +30,7 @@
  */
 
 #include <sys/cdefs.h>
-__RCSID("$NetBSD: debug.c,v 1.14 2023/05/18 05:33:27 rillig Exp $");
+__RCSID("$NetBSD: debug.c,v 1.15 2023/05/20 02:47:35 rillig Exp $");
 
 #include <stdarg.h>
 
@@ -208,11 +208,10 @@ debug_buffers(void)
 static bool
 ps_paren_has_changed(const struct parser_state *prev_ps)
 {
-       const paren_level_props *prev = prev_ps->paren, *curr = ps.paren;
-
        if (prev_ps->nparen != ps.nparen)
                return true;
 
+       const paren_level_props *prev = prev_ps->paren, *curr = ps.paren;
        for (int i = 0; i < ps.nparen; i++)
                if (curr[i].indent != prev[i].indent
                    || curr[i].cast != prev[i].cast)
@@ -237,6 +236,32 @@ debug_ps_paren(const struct parser_state
        debug_println("");
 }
 
+static bool
+ps_di_stack_has_changed(const struct parser_state *prev_ps)
+{
+       if (prev_ps->decl_level != ps.decl_level)
+               return true;
+       for (int i = 0; i < ps.decl_level; i++)
+               if (prev_ps->di_stack[i] != ps.di_stack[i])
+                       return true;
+       return false;
+}
+
+static void
+debug_ps_di_stack(const struct parser_state *prev_ps)
+{
+       bool changed = ps_di_stack_has_changed(prev_ps);
+       if (!debug_full_parser_state && !changed)
+               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("");
+}
+
 void
 debug_parser_state(lexer_symbol lsym)
 {
@@ -274,6 +299,7 @@ debug_parser_state(lexer_symbol lsym)
        debug_ps_int(ind_level_follow);
 
        debug_ps_int(decl_level);
+       debug_ps_di_stack(&prev_ps);
        debug_ps_bool(decl_on_line);
        debug_ps_bool(in_decl);
        debug_ps_enum(declaration, declaration_name);
@@ -282,7 +308,6 @@ debug_parser_state(lexer_symbol lsym)
        debug_ps_enum(in_enum, in_enum_name);
        debug_ps_bool(decl_indent_done);
        debug_ps_int(decl_ind);
-       // No debug output for di_stack.
        debug_ps_bool(tabs_to_var);
 
        debug_ps_bool(in_stmt_or_decl);
diff -r 2703369f8ebc -r e62e9de1c824 usr.bin/indent/indent.c
--- a/usr.bin/indent/indent.c   Sat May 20 01:28:14 2023 +0000
+++ b/usr.bin/indent/indent.c   Sat May 20 02:47:35 2023 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: indent.c,v 1.295 2023/05/18 07:13:05 rillig Exp $      */
+/*     $NetBSD: indent.c,v 1.296 2023/05/20 02:47:35 rillig Exp $      */
 
 /*-
  * SPDX-License-Identifier: BSD-4-Clause
@@ -38,7 +38,7 @@
  */
 
 #include <sys/cdefs.h>
-__RCSID("$NetBSD: indent.c,v 1.295 2023/05/18 07:13:05 rillig Exp $");
+__RCSID("$NetBSD: indent.c,v 1.296 2023/05/20 02:47:35 rillig Exp $");
 
 #include <sys/param.h>
 #include <err.h>
@@ -1009,9 +1009,6 @@ process_preprocessing(void)
 static int
 indent(void)
 {
-
-       ps.di_stack[ps.decl_level = 0] = 0;
-
        for (;;) {              /* loop until we reach eof */
                lexer_symbol lsym = lexi();
 
diff -r 2703369f8ebc -r e62e9de1c824 usr.bin/indent/indent.h
--- a/usr.bin/indent/indent.h   Sat May 20 01:28:14 2023 +0000
+++ b/usr.bin/indent/indent.h   Sat May 20 02:47:35 2023 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: indent.h,v 1.151 2023/05/20 00:17:56 rillig Exp $      */
+/*     $NetBSD: indent.h,v 1.152 2023/05/20 02:47:35 rillig Exp $      */
 
 /*-
  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
@@ -328,10 +328,10 @@ extern struct parser_state {
                                 * the middle of a statement */
        int decl_level;         /* current nesting level for a structure
                                 * declaration or an initializer */
+       int di_stack[20];       /* a stack of structure indentation levels */
        bool decl_indent_done;  /* whether the indentation for a declaration
                                 * has been added to the code buffer. */
        int decl_ind;           /* current indentation for declarations */
-       int di_stack[20];       /* a stack of structure indentation levels */
        bool tabs_to_var;       /* true if using tabs to indent to var name */
 
        enum {



Home | Main Index | Thread Index | Old Index