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: allow more than 20 nested parentheses...



details:   https://anonhg.NetBSD.org/src/rev/61e00c8be901
branches:  trunk
changeset: 376375:61e00c8be901
user:      rillig <rillig%NetBSD.org@localhost>
date:      Wed Jun 14 14:11:28 2023 +0000

description:
indent: allow more than 20 nested parentheses or brackets

diffstat:

 tests/usr.bin/indent/lsym_lparen_or_lbracket.c |    4 +-
 tests/usr.bin/indent/opt_bc.c                  |   21 ++++-
 tests/usr.bin/indent/opt_pcs.c                 |    5 +-
 usr.bin/indent/debug.c                         |   29 +++---
 usr.bin/indent/indent.c                        |  115 +++++++++++++++----------
 usr.bin/indent/indent.h                        |   15 +-
 usr.bin/indent/io.c                            |   24 ++--
 usr.bin/indent/lexi.c                          |   22 ++--
 usr.bin/indent/pr_comment.c                    |    6 +-
 9 files changed, 146 insertions(+), 95 deletions(-)

diffs (truncated from 628 to 300 lines):

diff -r 5f0359ebbff3 -r 61e00c8be901 tests/usr.bin/indent/lsym_lparen_or_lbracket.c
--- a/tests/usr.bin/indent/lsym_lparen_or_lbracket.c    Wed Jun 14 13:15:30 2023 +0000
+++ b/tests/usr.bin/indent/lsym_lparen_or_lbracket.c    Wed Jun 14 14:11:28 2023 +0000
@@ -1,4 +1,4 @@
-/* $NetBSD: lsym_lparen_or_lbracket.c,v 1.16 2023/06/10 08:17:04 rillig Exp $ */
+/* $NetBSD: lsym_lparen_or_lbracket.c,v 1.17 2023/06/14 14:11:28 rillig Exp $ */
 
 /*
  * Tests for the token lsym_lparen_or_lbracket, which represents a '(' or '['
@@ -144,9 +144,9 @@ function(void)
 //indent run-equals-input
 
 
-/* This is the maximum supported number of parentheses. */
 //indent input
 int zero = (((((((((((((((((((0)))))))))))))))))));
+int many = ((((((((((((((((((((((((((((((((0))))))))))))))))))))))))))))))));
 //indent end
 
 //indent run-equals-input -di0
diff -r 5f0359ebbff3 -r 61e00c8be901 tests/usr.bin/indent/opt_bc.c
--- a/tests/usr.bin/indent/opt_bc.c     Wed Jun 14 13:15:30 2023 +0000
+++ b/tests/usr.bin/indent/opt_bc.c     Wed Jun 14 14:11:28 2023 +0000
@@ -1,4 +1,4 @@
-/* $NetBSD: opt_bc.c,v 1.10 2023/06/10 06:52:35 rillig Exp $ */
+/* $NetBSD: opt_bc.c,v 1.11 2023/06/14 14:11:28 rillig Exp $ */
 
 /*
  * Tests for the options '-bc' and '-nbc'.
@@ -119,3 +119,22 @@ int                a = 1,
 int            a = (1),
                b = 2;
 //indent end
+
+
+/*
+ * When declarations are too long to fit in a single line, they should not be
+ * joined.
+ */
+//indent input
+{
+       const struct paren_level *prev = state.prev_ps.paren.item,
+           *curr = ps.paren.item;
+}
+//indent end
+
+//indent run
+// $ FIXME: The line becomes too long.
+{
+       const struct paren_level *prev = state.prev_ps.paren.item, *curr = ps.paren.item;
+}
+//indent end
diff -r 5f0359ebbff3 -r 61e00c8be901 tests/usr.bin/indent/opt_pcs.c
--- a/tests/usr.bin/indent/opt_pcs.c    Wed Jun 14 13:15:30 2023 +0000
+++ b/tests/usr.bin/indent/opt_pcs.c    Wed Jun 14 14:11:28 2023 +0000
@@ -1,4 +1,4 @@
-/* $NetBSD: opt_pcs.c,v 1.16 2023/05/18 08:09:28 rillig Exp $ */
+/* $NetBSD: opt_pcs.c,v 1.17 2023/06/14 14:11:28 rillig Exp $ */
 
 /*
  * Tests for the options '-pcs' and '-npcs'.
@@ -114,6 +114,8 @@ int offset = offsetof(struct s, member);
 
 //indent input
 int unary = +call();
+// $ FIXME: Unusual, but there should be no space.
+int postfix = step++ ();
 int binary = 1 + call();
 //indent end
 
@@ -121,5 +123,6 @@ int binary = 1 + call();
 
 //indent run -pcs -di0
 int unary = +call ();
+int postfix = step++ ();
 int binary = 1 + call ();
 //indent end
diff -r 5f0359ebbff3 -r 61e00c8be901 usr.bin/indent/debug.c
--- a/usr.bin/indent/debug.c    Wed Jun 14 13:15:30 2023 +0000
+++ b/usr.bin/indent/debug.c    Wed Jun 14 14:11:28 2023 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: debug.c,v 1.58 2023/06/14 09:57:02 rillig Exp $        */
+/*     $NetBSD: debug.c,v 1.59 2023/06/14 14:11:28 rillig Exp $        */
 
 /*-
  * Copyright (c) 2023 The NetBSD Foundation, Inc.
@@ -30,7 +30,7 @@
  */
 
 #include <sys/cdefs.h>
-__RCSID("$NetBSD: debug.c,v 1.58 2023/06/14 09:57:02 rillig Exp $");
+__RCSID("$NetBSD: debug.c,v 1.59 2023/06/14 14:11:28 rillig Exp $");
 
 #include <stdarg.h>
 #include <string.h>
@@ -255,15 +255,14 @@ debug_ps_enum_member(const char *name, c
 }
 
 static bool
-ps_paren_has_changed(void)
+paren_stack_equal(const struct paren_stack *a, const struct paren_stack *b)
 {
-       if (state.prev_ps.nparen != ps.nparen)
-               return true;
+       if (a->len != b->len)
+               return false;
 
-       const struct paren_level *prev = state.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)
+       for (size_t i = 0, n = a->len; i < n; i++)
+               if (a->item[i].indent != b->item[i].indent
+                   || a->item[i].cast != b->item[i].cast)
                        return true;
        return false;
 }
@@ -271,17 +270,18 @@ ps_paren_has_changed(void)
 static void
 debug_ps_paren(void)
 {
-       if (!config.full_parser_state && !ps_paren_has_changed()
+       if (!config.full_parser_state
+           && paren_stack_equal(&state.prev_ps.paren, &ps.paren)
            && !state.ps_first)
                return;
 
        debug_printf("             ps.paren:");
-       for (int i = 0; i < ps.nparen; i++) {
+       for (size_t i = 0; i < ps.paren.len; i++) {
                debug_printf(" %s%d",
-                   paren_level_cast_name[ps.paren[i].cast],
-                   ps.paren[i].indent);
+                   paren_level_cast_name[ps.paren.item[i].cast],
+                   ps.paren.item[i].indent);
        }
-       if (ps.nparen == 0)
+       if (ps.paren.len == 0)
                debug_printf(" none");
        debug_println("");
 }
@@ -358,7 +358,6 @@ debug_parser_state(void)
        debug_ps_bool(next_unary);
        debug_ps_bool(want_blank);
        debug_ps_int(ind_paren_level);
-       debug_ps_int(nparen);
        debug_ps_paren();
 
        state.heading = "indentation of comments";
diff -r 5f0359ebbff3 -r 61e00c8be901 usr.bin/indent/indent.c
--- a/usr.bin/indent/indent.c   Wed Jun 14 13:15:30 2023 +0000
+++ b/usr.bin/indent/indent.c   Wed Jun 14 14:11:28 2023 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: indent.c,v 1.363 2023/06/14 13:15:30 rillig Exp $      */
+/*     $NetBSD: indent.c,v 1.364 2023/06/14 14:11:28 rillig Exp $      */
 
 /*-
  * SPDX-License-Identifier: BSD-4-Clause
@@ -38,7 +38,7 @@
  */
 
 #include <sys/cdefs.h>
-__RCSID("$NetBSD: indent.c,v 1.363 2023/06/14 13:15:30 rillig Exp $");
+__RCSID("$NetBSD: indent.c,v 1.364 2023/06/14 14:11:28 rillig Exp $");
 
 #include <sys/param.h>
 #include <err.h>
@@ -444,6 +444,49 @@ read_preprocessing_line(void)
 }
 
 static void
+paren_stack_push(struct paren_stack *s, int indent, enum paren_level_cast cast)
+{
+       if (s->len == s->cap) {
+               s->cap = 10 + s->cap;
+               s->item = nonnull(realloc(s->item,
+                       sizeof(s->item[0]) * s->cap));
+       }
+       s->item[s->len++] = (struct paren_level){indent, cast};
+}
+
+static void
+parser_state_backup(struct parser_state *dst)
+{
+       *dst = ps;
+
+       dst->paren.item = nonnull(
+           malloc(sizeof(dst->paren.item[0]) * ps.paren.cap));
+       dst->paren.len = ps.paren.len;
+       dst->paren.cap = ps.paren.cap;
+       memcpy(dst->paren.item, ps.paren.item,
+           sizeof(dst->paren.item[0]) * ps.paren.len);
+}
+
+static void
+parser_state_restore(const struct parser_state *src)
+{
+       struct paren_stack dst_paren = ps.paren;
+       ps = *src;
+       ps.paren = dst_paren;
+
+       ps.paren.len = 0;
+       for (size_t i = 0; i < src->paren.len; i++)
+               paren_stack_push(&ps.paren,
+                   src->paren.item[i].indent, src->paren.item[i].cast);
+}
+
+static void
+parser_state_free(struct parser_state *pst)
+{
+       free(pst->paren.item);
+}
+
+static void
 process_preprocessing(void)
 {
        if (lab.len > 0 || code.len > 0 || com.len > 0)
@@ -464,20 +507,20 @@ process_preprocessing(void)
                        ifdef.item = nonnull(realloc(ifdef.item,
                                sizeof(ifdef.item[0]) * ifdef.cap));
                }
-               ifdef.item[ifdef.len++] = ps;
+               parser_state_backup(ifdef.item + ifdef.len++);
                out.line_kind = lk_if;
 
        } else if (dir_len >= 2 && memcmp(dir, "el", 2) == 0) {
                if (ifdef.len == 0)
                        diag(1, "Unmatched #%.*s", (int)dir_len, dir);
                else
-                       ps = ifdef.item[ifdef.len - 1];
+                       parser_state_restore(ifdef.item + ifdef.len - 1);
 
        } else if (dir_len == 5 && memcmp(dir, "endif", 5) == 0) {
                if (ifdef.len == 0)
                        diag(1, "Unmatched #endif");
                else
-                       ifdef.len--;
+                       parser_state_free(ifdef.item + --ifdef.len);
                out.line_kind = lk_endif;
        }
 }
@@ -486,7 +529,7 @@ static void
 process_newline(void)
 {
        if (ps.prev_lsym == lsym_comma
-           && ps.nparen == 0 && !ps.in_init
+           && ps.paren.len == 0 && !ps.in_init
            && !opt.break_after_comma && ps.break_after_comma
            && lab.len == 0     /* for preprocessing lines */
            && com.len == 0)
@@ -507,8 +550,6 @@ stay_in_line:
 static bool
 want_blank_before_lparen(void)
 {
-       if (!ps.want_blank)
-               return false;
        if (opt.proc_calls_space)
                return true;
        if (ps.prev_lsym == lsym_rparen || ps.prev_lsym == lsym_rbracket)
@@ -523,24 +564,12 @@ want_blank_before_lparen(void)
 }
 
 static void
-ps_paren_push(int indent, enum paren_level_cast cast)
-{
-       if (++ps.nparen == array_length(ps.paren)) {
-               diag(0, "Reached internal limit of %zu unclosed parentheses",
-                    array_length(ps.paren));
-               ps.nparen--;
-       }
-       ps.paren[ps.nparen - 1].indent = indent;
-       ps.paren[ps.nparen - 1].cast = cast;
-}
-
-static void
 process_lparen(void)
 {
 
        if (is_function_pointer_declaration())
                indent_declarator(ps.decl_ind, ps.tabs_to_var);
-       else if (want_blank_before_lparen())
+       else if (ps.want_blank && want_blank_before_lparen())
                buf_add_char(&code, ' ');
        ps.want_blank = false;
        buf_add_char(&code, token.s[0]);
@@ -553,8 +582,6 @@ process_lparen(void)
                ps.in_var_decl = false;
        }
 
-       int indent = ind_add(0, code.s, code.len);
-
        enum paren_level_cast cast = cast_unknown;
        if (ps.prev_lsym == lsym_offsetof
            || ps.prev_lsym == lsym_sizeof
@@ -565,20 +592,18 @@ process_lparen(void)
            || ps.line_has_func_def)
                cast = cast_no;
 
-       ps_paren_push(indent, cast);
-       debug_println("paren_indents[%d] is now %s%d",



Home | Main Index | Thread Index | Old Index