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: rename and sort variables in parser s...



details:   https://anonhg.NetBSD.org/src/rev/01eca2e0eac1
branches:  trunk
changeset: 376305:01eca2e0eac1
user:      rillig <rillig%NetBSD.org@localhost>
date:      Sat Jun 10 06:38:20 2023 +0000

description:
indent: rename and sort variables in parser state

No functional change.

diffstat:

 tests/usr.bin/indent/fmt_decl.c     |  23 +++++++++++-
 tests/usr.bin/indent/lsym_comment.c |   4 +-
 tests/usr.bin/indent/opt_bc.c       |  19 +++++++++-
 usr.bin/indent/debug.c              |  20 +++++-----
 usr.bin/indent/indent.c             |  70 ++++++++++++++++++------------------
 usr.bin/indent/indent.h             |  52 +++++++++++++-------------
 usr.bin/indent/io.c                 |  10 ++--
 usr.bin/indent/lexi.c               |  18 ++++----
 usr.bin/indent/pr_comment.c         |   6 +-
 9 files changed, 130 insertions(+), 92 deletions(-)

diffs (truncated from 561 to 300 lines):

diff -r 3e172a721b49 -r 01eca2e0eac1 tests/usr.bin/indent/fmt_decl.c
--- a/tests/usr.bin/indent/fmt_decl.c   Sat Jun 10 04:45:25 2023 +0000
+++ b/tests/usr.bin/indent/fmt_decl.c   Sat Jun 10 06:38:20 2023 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: fmt_decl.c,v 1.52 2023/06/09 09:49:07 rillig Exp $     */
+/*     $NetBSD: fmt_decl.c,v 1.53 2023/06/10 06:38:21 rillig Exp $     */
 
 /*
  * Tests for declarations of global variables, external functions, and local
@@ -1048,3 +1048,24 @@ typedef struct {
 // $ FIXME: Remove this line break.
 mod_t;
 //indent end
+
+
+//indent input
+int a[] = {1, 2},
+b[] = {1, 2};
+{
+int a[] = {1, 2},
+b[] = {1, 2};
+}
+//indent end
+
+//indent run -di0
+int a[] = {1, 2},
+// $ FIXME: Missing indentation.
+b[] = {1, 2};
+{
+       int a[] = {1, 2},
+       // $ FIXME: Missing indentation.
+       b[] = {1, 2};
+}
+//indent end
diff -r 3e172a721b49 -r 01eca2e0eac1 tests/usr.bin/indent/lsym_comment.c
--- a/tests/usr.bin/indent/lsym_comment.c       Sat Jun 10 04:45:25 2023 +0000
+++ b/tests/usr.bin/indent/lsym_comment.c       Sat Jun 10 06:38:20 2023 +0000
@@ -1,4 +1,4 @@
-/* $NetBSD: lsym_comment.c,v 1.18 2023/06/06 07:51:35 rillig Exp $ */
+/* $NetBSD: lsym_comment.c,v 1.19 2023/06/10 06:38:21 rillig Exp $ */
 
 /*
  * Tests for the token lsym_comment, which starts a comment.
@@ -42,7 +42,7 @@
  * - with varying opt.block_comment_max_line_length (-lc60, -lc78, -lc90)
  * - with varying opt.comment_column (-c0, -c1, -c33, -c80)
  * - with varying opt.decl_comment_column (-cd0, -cd1, -cd20, -cd33, -cd80)
- * - with/without ps.decl_on_line
+ * - with/without ps.line_has_decl
  * - with/without ps.next_col_1
  *
  * - very long comments that overflow the buffer 'com'
diff -r 3e172a721b49 -r 01eca2e0eac1 tests/usr.bin/indent/opt_bc.c
--- a/tests/usr.bin/indent/opt_bc.c     Sat Jun 10 04:45:25 2023 +0000
+++ b/tests/usr.bin/indent/opt_bc.c     Sat Jun 10 06:38:20 2023 +0000
@@ -1,4 +1,4 @@
-/* $NetBSD: opt_bc.c,v 1.8 2023/06/02 11:43:07 rillig Exp $ */
+/* $NetBSD: opt_bc.c,v 1.9 2023/06/10 06:38:21 rillig Exp $ */
 
 /*
  * Tests for the options '-bc' and '-nbc'.
@@ -102,3 +102,20 @@ int                d;
 int            d;
 #endif
 //indent end
+
+
+/*
+ * A '(' at the top level is taken to start a function definition, leaving
+ * variable declaration mode.
+ */
+//indent input
+int a = 1, b = 2;
+int a = (1), b = 2;
+//indent end
+
+//indent run -bc
+int            a = 1,
+               b = 2;
+// $ FIXME: Missing line break.
+int            a = (1), b = 2;
+//indent end
diff -r 3e172a721b49 -r 01eca2e0eac1 usr.bin/indent/debug.c
--- a/usr.bin/indent/debug.c    Sat Jun 10 04:45:25 2023 +0000
+++ b/usr.bin/indent/debug.c    Sat Jun 10 06:38:20 2023 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: debug.c,v 1.43 2023/06/09 18:09:30 rillig Exp $        */
+/*     $NetBSD: debug.c,v 1.44 2023/06/10 06:38:20 rillig Exp $        */
 
 /*-
  * Copyright (c) 2023 The NetBSD Foundation, Inc.
@@ -30,7 +30,7 @@
  */
 
 #include <sys/cdefs.h>
-__RCSID("$NetBSD: debug.c,v 1.43 2023/06/09 18:09:30 rillig Exp $");
+__RCSID("$NetBSD: debug.c,v 1.44 2023/06/10 06:38:20 rillig Exp $");
 
 #include <stdarg.h>
 
@@ -294,19 +294,19 @@ debug_parser_state(void)
            lsym_name[ps.prev_lsym]);
 
        debug_println("token classification");
-       debug_ps_int(quest_level);
-       debug_ps_bool(is_function_definition);
-       debug_ps_bool(block_init);
-       debug_ps_int(block_init_level);
-       debug_ps_bool(init_or_struct);
-       debug_ps_bool(decl_on_line);
        debug_ps_bool(in_stmt_or_decl);
        debug_ps_bool(in_decl);
+       debug_ps_bool(in_var_decl);
+       debug_ps_bool(in_init);
+       debug_ps_int(init_level);
+       debug_ps_bool(in_func_def_line);
        debug_ps_bool(in_func_def_params);
-       debug_ps_bool(seen_case);
+       debug_ps_bool(line_has_decl);
+       debug_ps_enum(lbrace_kind, psym_name);
        debug_ps_enum(spaced_expr_psym, psym_name);
-       debug_ps_enum(lbrace_kind, psym_name);
+       debug_ps_bool(seen_case);
        debug_ps_bool(prev_paren_was_cast);
+       debug_ps_int(quest_level);
 
        debug_println("indentation of statements and declarations");
        debug_ps_int(ind_level);
diff -r 3e172a721b49 -r 01eca2e0eac1 usr.bin/indent/indent.c
--- a/usr.bin/indent/indent.c   Sat Jun 10 04:45:25 2023 +0000
+++ b/usr.bin/indent/indent.c   Sat Jun 10 06:38:20 2023 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: indent.c,v 1.348 2023/06/09 22:01:26 rillig Exp $      */
+/*     $NetBSD: indent.c,v 1.349 2023/06/10 06:38:21 rillig Exp $      */
 
 /*-
  * SPDX-License-Identifier: BSD-4-Clause
@@ -38,7 +38,7 @@
  */
 
 #include <sys/cdefs.h>
-__RCSID("$NetBSD: indent.c,v 1.348 2023/06/09 22:01:26 rillig Exp $");
+__RCSID("$NetBSD: indent.c,v 1.349 2023/06/10 06:38:21 rillig Exp $");
 
 #include <sys/param.h>
 #include <err.h>
@@ -370,9 +370,9 @@ static bool
 is_function_pointer_declaration(void)
 {
        return ps.in_decl
-           && !ps.block_init
+           && !ps.in_init
            && !ps.decl_indent_done
-           && !ps.is_function_definition
+           && !ps.in_func_def_line
            && ps.line_start_nparen == 0;
 }
 
@@ -477,7 +477,7 @@ static void
 process_newline(void)
 {
        if (ps.prev_lsym == lsym_comma
-           && ps.nparen == 0 && !ps.block_init
+           && ps.nparen == 0 && !ps.in_init
            && !opt.break_after_comma && ps.break_after_comma
            && lab.len == 0     /* for preprocessing lines */
            && com.len == 0)
@@ -531,10 +531,10 @@ process_lparen(void)
        if (opt.extra_expr_indent && ps.spaced_expr_psym != psym_0)
                ps.extra_expr_indent = eei_maybe;
 
-       if (ps.init_or_struct && ps.psyms.top <= 2) {
+       if (ps.in_var_decl && ps.psyms.top <= 2) {
                /* A kludge to correctly align function definitions. */
                parse(psym_stmt);
-               ps.init_or_struct = false;
+               ps.in_var_decl = false;
        }
 
        int indent = ind_add(0, code.s, code.len);
@@ -546,7 +546,7 @@ process_lparen(void)
            || ps.prev_lsym == lsym_if
            || ps.prev_lsym == lsym_switch
            || ps.prev_lsym == lsym_while
-           || ps.is_function_definition)
+           || ps.in_func_def_line)
                cast = cast_no;
 
        ps.paren[ps.nparen - 1].indent = indent;
@@ -564,7 +564,7 @@ process_rparen(void)
        }
 
        enum paren_level_cast cast = ps.paren[--ps.nparen].cast;
-       if (ps.in_func_def_params || (ps.decl_on_line && !ps.block_init))
+       if (ps.in_func_def_params || (ps.line_has_decl && !ps.in_init))
                cast = cast_no;
 
        ps.prev_paren_was_cast = cast == cast_maybe;
@@ -636,8 +636,8 @@ static void
 process_lbrace(void)
 {
        if (ps.prev_lsym == lsym_rparen && ps.prev_paren_was_cast) {
-               ps.block_init = true;
-               ps.init_or_struct = true;
+               ps.in_var_decl = true;  // XXX: not really
+               ps.in_init = true;
        }
 
        if (out.line_kind == lk_stmt_head)
@@ -645,16 +645,16 @@ process_lbrace(void)
 
        ps.in_stmt_or_decl = false;     /* don't indent the {} */
 
-       if (!ps.block_init)
+       if (!ps.in_init)
                ps.force_nl = true;
        else
-               ps.block_init_level++;
+               ps.init_level++;
 
-       if (code.len > 0 && !ps.block_init) {
+       if (code.len > 0 && !ps.in_init) {
                if (!opt.brace_same_line ||
                    (code.len > 0 && code.s[code.len - 1] == '}'))
                        output_line();
-               else if (ps.in_func_def_params && !ps.init_or_struct) {
+               else if (ps.in_func_def_params && !ps.in_var_decl) {
                        ps.ind_level_follow = 0;
                        if (opt.function_brace_split)
                                output_line();
@@ -663,7 +663,7 @@ process_lbrace(void)
                }
        }
 
-       if (ps.nparen > 0 && ps.block_init_level == 0) {
+       if (ps.nparen > 0 && ps.init_level == 0) {
                diag(1, "Unbalanced parentheses");
                ps.nparen = 0;
                if (ps.spaced_expr_psym != psym_0) {
@@ -676,15 +676,15 @@ process_lbrace(void)
        if (code.len == 0)
                ps.in_stmt_cont = false;        /* don't indent the '{' itself
                                                 */
-       if (ps.in_decl && ps.init_or_struct) {
+       if (ps.in_decl && ps.in_var_decl) {
                ps.di_stack[ps.decl_level] = ps.decl_ind;
                if (++ps.decl_level == (int)array_length(ps.di_stack)) {
-                       diag(0, "Reached internal limit of %d struct levels",
-                           (int)array_length(ps.di_stack));
+                       diag(0, "Reached internal limit of %zu struct levels",
+                           array_length(ps.di_stack));
                        ps.decl_level--;
                }
        } else {
-               ps.decl_on_line = false;        /* we can't be in the middle of
+               ps.line_has_decl = false;       /* we can't be in the middle of
                                                 * a declaration, so don't do
                                                 * special indentation of
                                                 * comments */
@@ -704,17 +704,17 @@ process_lbrace(void)
 static void
 process_rbrace(void)
 {
-       if (ps.nparen > 0 && ps.block_init_level == 0) {
+       if (ps.nparen > 0 && ps.init_level == 0) {
                diag(1, "Unbalanced parentheses");
                ps.nparen = 0;
                ps.spaced_expr_psym = psym_0;
        }
 
        ps.declaration = decl_no;
-       if (ps.block_init_level > 0)
-               ps.block_init_level--;
+       if (ps.init_level > 0)
+               ps.init_level--;
 
-       if (code.len > 0 && !ps.block_init)
+       if (code.len > 0 && !ps.in_init)
                output_line();
 
        buf_add_char(&code, '}');
@@ -737,7 +737,7 @@ process_rbrace(void)
 
        parse(psym_rbrace);
 
-       if (!ps.init_or_struct
+       if (!ps.in_var_decl
            && ps.psyms.sym[ps.psyms.top] != psym_do_stmt
            && ps.psyms.sym[ps.psyms.top] != psym_if_expr_stmt)
                ps.force_nl = true;
@@ -803,7 +803,7 @@ process_comma(void)
        ps.want_blank = code.len > 0;   /* only put blank after comma if comma
                                         * does not start the line */
 
-       if (ps.in_decl && !ps.is_function_definition && !ps.block_init &&
+       if (ps.in_decl && !ps.in_func_def_line && !ps.in_init &&
            !ps.decl_indent_done && ps.line_start_nparen == 0) {
                /* indent leading commas and not the actual identifiers */
                indent_declarator(ps.decl_ind - 1, ps.tabs_to_var);
@@ -812,8 +812,8 @@ process_comma(void)



Home | Main Index | Thread Index | Old Index