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: clean up array indexing for parser sy...



details:   https://anonhg.NetBSD.org/src/rev/9198232ca28a
branches:  trunk
changeset: 376376:9198232ca28a
user:      rillig <rillig%NetBSD.org@localhost>
date:      Wed Jun 14 16:14:30 2023 +0000

description:
indent: clean up array indexing for parser symbols

With 'top' pointing to the actual top element, the array was indexed in
the closed range from 0 to top. All other arrays are indexed by the
usual half-open interval from 0 to len.

No functional change.

diffstat:

 usr.bin/indent/debug.c  |   6 ++--
 usr.bin/indent/indent.c |  20 ++++++------
 usr.bin/indent/indent.h |   7 +++-
 usr.bin/indent/io.c     |  16 +++++-----
 usr.bin/indent/lexi.c   |   6 ++--
 usr.bin/indent/parse.c  |  72 +++++++++++++++++++++++++-----------------------
 6 files changed, 66 insertions(+), 61 deletions(-)

diffs (truncated from 414 to 300 lines):

diff -r 61e00c8be901 -r 9198232ca28a usr.bin/indent/debug.c
--- a/usr.bin/indent/debug.c    Wed Jun 14 14:11:28 2023 +0000
+++ b/usr.bin/indent/debug.c    Wed Jun 14 16:14:30 2023 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: debug.c,v 1.59 2023/06/14 14:11:28 rillig Exp $        */
+/*     $NetBSD: debug.c,v 1.60 2023/06/14 16:14:30 rillig Exp $        */
 
 /*-
  * Copyright (c) 2023 The NetBSD Foundation, Inc.
@@ -30,7 +30,7 @@
  */
 
 #include <sys/cdefs.h>
-__RCSID("$NetBSD: debug.c,v 1.59 2023/06/14 14:11:28 rillig Exp $");
+__RCSID("$NetBSD: debug.c,v 1.60 2023/06/14 16:14:30 rillig Exp $");
 
 #include <stdarg.h>
 #include <string.h>
@@ -383,7 +383,7 @@ debug_psyms_stack(const char *situation)
 {
        debug_printf("parse stack %s:", situation);
        const struct psym_stack *psyms = &ps.psyms;
-       for (int i = 0; i <= psyms->top; ++i)
+       for (size_t i = 0; i < psyms->len; ++i)
                debug_printf(" %d %s",
                    psyms->ind_level[i], psym_name[psyms->sym[i]]);
        debug_println("");
diff -r 61e00c8be901 -r 9198232ca28a usr.bin/indent/indent.c
--- a/usr.bin/indent/indent.c   Wed Jun 14 14:11:28 2023 +0000
+++ b/usr.bin/indent/indent.c   Wed Jun 14 16:14:30 2023 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: indent.c,v 1.364 2023/06/14 14:11:28 rillig Exp $      */
+/*     $NetBSD: indent.c,v 1.365 2023/06/14 16:14:30 rillig Exp $      */
 
 /*-
  * SPDX-License-Identifier: BSD-4-Clause
@@ -38,7 +38,7 @@
  */
 
 #include <sys/cdefs.h>
-__RCSID("$NetBSD: indent.c,v 1.364 2023/06/14 14:11:28 rillig Exp $");
+__RCSID("$NetBSD: indent.c,v 1.365 2023/06/14 16:14:30 rillig Exp $");
 
 #include <sys/param.h>
 #include <err.h>
@@ -187,7 +187,7 @@ ind_add(int ind, const char *s, size_t l
 static void
 init_globals(void)
 {
-       ps.psyms.sym[0] = psym_stmt;
+       ps.psyms.sym[ps.psyms.len++] = psym_stmt;
        ps.prev_lsym = lsym_semicolon;
        ps.lbrace_kind = psym_lbrace_block;
 
@@ -388,7 +388,7 @@ process_eof(void)
 {
        finish_output();
 
-       if (ps.psyms.top > 1)   /* check for balanced braces */
+       if (ps.psyms.len > 2)   /* check for balanced braces */
                diag(1, "Stuff missing from end of file");
 
        return found_err ? EXIT_FAILURE : EXIT_SUCCESS;
@@ -534,7 +534,7 @@ process_newline(void)
            && lab.len == 0     /* for preprocessing lines */
            && com.len == 0)
                goto stay_in_line;
-       if (ps.psyms.sym[ps.psyms.top] == psym_switch_expr
+       if (ps.psyms.sym[ps.psyms.len - 1] == psym_switch_expr
            && opt.brace_same_line
            && com.len == 0) {
                ps.force_nl = true;
@@ -577,7 +577,7 @@ process_lparen(void)
        if (opt.extra_expr_indent && ps.spaced_expr_psym != psym_0)
                ps.extra_expr_indent = eei_maybe;
 
-       if (ps.in_var_decl && ps.psyms.top <= 2 && !ps.in_init) {
+       if (ps.in_var_decl && ps.psyms.len <= 3 && !ps.in_init) {
                parse(psym_stmt);       /* prepare for function definition */
                ps.in_var_decl = false;
        }
@@ -762,14 +762,14 @@ process_rbrace(void)
                ps.in_decl = true;
        }
 
-       if (ps.psyms.top == 2)
+       if (ps.psyms.len == 3)
                out.line_kind = lk_func_end;
 
        parse(psym_rbrace);
 
        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.psyms.sym[ps.psyms.len - 1] != psym_do_stmt
+           && ps.psyms.sym[ps.psyms.len - 1] != psym_if_expr_stmt)
                ps.force_nl = true;
 }
 
@@ -905,7 +905,7 @@ process_type_outside_parentheses(void)
 {
        parse(psym_decl);       /* let the parser worry about indentation */
 
-       if (ps.prev_lsym == lsym_rparen && ps.psyms.top <= 1 && code.len > 0)
+       if (ps.prev_lsym == lsym_rparen && ps.psyms.len <= 2 && code.len > 0)
                output_line();
 
        if (ps.in_func_def_params && opt.indent_parameters &&
diff -r 61e00c8be901 -r 9198232ca28a usr.bin/indent/indent.h
--- a/usr.bin/indent/indent.h   Wed Jun 14 14:11:28 2023 +0000
+++ b/usr.bin/indent/indent.h   Wed Jun 14 16:14:30 2023 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: indent.h,v 1.194 2023/06/14 14:11:28 rillig Exp $      */
+/*     $NetBSD: indent.h,v 1.195 2023/06/14 16:14:30 rillig Exp $      */
 
 /*-
  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
@@ -272,7 +272,10 @@ struct paren_level {
 };
 
 struct psym_stack {
-       int top;                /* pointer to top of stack */
+       size_t len;             /* points to one behind the top of the stack;
+                                * 1 at the top level of the file outside a
+                                * declaration or statement; 2 at the top
+                                * level */
        parser_symbol sym[STACKSIZE];
        int ind_level[STACKSIZE];
 };
diff -r 61e00c8be901 -r 9198232ca28a usr.bin/indent/io.c
--- a/usr.bin/indent/io.c       Wed Jun 14 14:11:28 2023 +0000
+++ b/usr.bin/indent/io.c       Wed Jun 14 16:14:30 2023 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: io.c,v 1.220 2023/06/14 14:11:28 rillig Exp $  */
+/*     $NetBSD: io.c,v 1.221 2023/06/14 16:14:30 rillig Exp $  */
 
 /*-
  * SPDX-License-Identifier: BSD-4-Clause
@@ -38,7 +38,7 @@
  */
 
 #include <sys/cdefs.h>
-__RCSID("$NetBSD: io.c,v 1.220 2023/06/14 14:11:28 rillig Exp $");
+__RCSID("$NetBSD: io.c,v 1.221 2023/06/14 16:14:30 rillig Exp $");
 
 #include <stdio.h>
 
@@ -196,7 +196,7 @@ is_blank_line_optional(void)
 {
        if (out.prev_line_kind == lk_stmt_head)
                return newlines >= 1;
-       if (ps.psyms.top >= 2)
+       if (ps.psyms.len >= 3)
                return newlines >= 2;
        return newlines >= 3;
 }
@@ -204,7 +204,7 @@ is_blank_line_optional(void)
 static int
 compute_case_label_indent(void)
 {
-       int i = ps.psyms.top;
+       size_t i = ps.psyms.len - 1;
        while (i > 0 && ps.psyms.sym[i] != psym_switch_expr)
                i--;
        float case_ind = (float)ps.psyms.ind_level[i] + opt.case_indent;
@@ -252,8 +252,8 @@ compute_code_indent(void)
        int base_ind = ps.ind_level * opt.indent_size;
 
        if (ps.ind_paren_level == 0) {
-               if (ps.psyms.top >= 1
-                   && ps.psyms.sym[ps.psyms.top - 1] == psym_lbrace_enum)
+               if (ps.psyms.len >= 2
+                   && ps.psyms.sym[ps.psyms.len - 2] == psym_lbrace_enum)
                        return base_ind;
                if (ps.in_stmt_cont)
                        return base_ind + opt.continuation_indent;
@@ -346,7 +346,7 @@ output_indented_line(void)
                ps.in_stmt_cont = false;
 
        if (opt.blank_line_after_decl && ps.declaration == decl_end
-           && ps.psyms.top > 1) {
+           && ps.psyms.len > 2) {
                ps.declaration = decl_no;
                ps.blank_line_after_decl = true;
        }
@@ -400,7 +400,7 @@ output_line(void)
        ps.decl_indent_done = false;
        if (ps.extra_expr_indent == eei_last)
                ps.extra_expr_indent = eei_no;
-       if (!(ps.psyms.sym[ps.psyms.top] == psym_if_expr_stmt_else
+       if (!(ps.psyms.sym[ps.psyms.len - 1] == psym_if_expr_stmt_else
                && ps.paren.len > 0))
                ps.ind_level = ps.ind_level_follow;
        ps.ind_paren_level = (int)ps.paren.len;
diff -r 61e00c8be901 -r 9198232ca28a usr.bin/indent/lexi.c
--- a/usr.bin/indent/lexi.c     Wed Jun 14 14:11:28 2023 +0000
+++ b/usr.bin/indent/lexi.c     Wed Jun 14 16:14:30 2023 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: lexi.c,v 1.228 2023/06/14 14:11:28 rillig Exp $        */
+/*     $NetBSD: lexi.c,v 1.229 2023/06/14 16:14:30 rillig Exp $        */
 
 /*-
  * SPDX-License-Identifier: BSD-4-Clause
@@ -38,7 +38,7 @@
  */
 
 #include <sys/cdefs.h>
-__RCSID("$NetBSD: lexi.c,v 1.228 2023/06/14 14:11:28 rillig Exp $");
+__RCSID("$NetBSD: lexi.c,v 1.229 2023/06/14 16:14:30 rillig Exp $");
 
 #include <stdlib.h>
 #include <string.h>
@@ -435,7 +435,7 @@ found_typename:
                }
        }
 
-       if (inp_p[0] == '(' && ps.psyms.top <= 1 && ps.ind_level == 0 &&
+       if (inp_p[0] == '(' && ps.psyms.len <= 2 && ps.ind_level == 0 &&
            !ps.in_func_def_params && !ps.in_init) {
 
                if (ps.paren.len == 0 && probably_function_definition()) {
diff -r 61e00c8be901 -r 9198232ca28a usr.bin/indent/parse.c
--- a/usr.bin/indent/parse.c    Wed Jun 14 14:11:28 2023 +0000
+++ b/usr.bin/indent/parse.c    Wed Jun 14 16:14:30 2023 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: parse.c,v 1.73 2023/06/14 07:20:55 rillig Exp $        */
+/*     $NetBSD: parse.c,v 1.74 2023/06/14 16:14:30 rillig Exp $        */
 
 /*-
  * SPDX-License-Identifier: BSD-4-Clause
@@ -38,7 +38,7 @@
  */
 
 #include <sys/cdefs.h>
-__RCSID("$NetBSD: parse.c,v 1.73 2023/06/14 07:20:55 rillig Exp $");
+__RCSID("$NetBSD: parse.c,v 1.74 2023/06/14 16:14:30 rillig Exp $");
 
 #include <err.h>
 
@@ -51,20 +51,20 @@
 static bool
 psyms_reduce_stmt(struct psym_stack *psyms)
 {
-       switch (psyms->sym[psyms->top - 1]) {
+       switch (psyms->sym[psyms->len - 2]) {
 
        case psym_stmt:
-               psyms->sym[--psyms->top] = psym_stmt;
+               psyms->sym[psyms->len-- - 2] = psym_stmt;
                return true;
 
        case psym_do:
-               psyms->sym[--psyms->top] = psym_do_stmt;
-               ps.ind_level_follow = psyms->ind_level[psyms->top];
+               psyms->sym[psyms->len-- - 2] = psym_do_stmt;
+               ps.ind_level_follow = psyms->ind_level[psyms->len - 1];
                return true;
 
        case psym_if_expr:
-               psyms->sym[--psyms->top] = psym_if_expr_stmt;
-               int i = psyms->top - 1;
+               psyms->sym[psyms->len-- - 2] = psym_if_expr_stmt;
+               size_t i = psyms->len - 2;
                while (psyms->sym[i] != psym_stmt &&
                    psyms->sym[i] != psym_lbrace_block)
                        --i;
@@ -79,8 +79,8 @@ psyms_reduce_stmt(struct psym_stack *psy
        case psym_if_expr_stmt_else:
        case psym_for_exprs:
        case psym_while_expr:
-               psyms->sym[--psyms->top] = psym_stmt;
-               ps.ind_level_follow = psyms->ind_level[psyms->top];
+               psyms->sym[psyms->len-- - 2] = psym_stmt;
+               ps.ind_level_follow = psyms->ind_level[psyms->len - 1];
                return true;
 
        default:
@@ -92,7 +92,7 @@ static int
 decl_level(void)
 {
        int level = 0;
-       for (int i = ps.psyms.top - 1; i > 0; i--)
+       for (size_t i = ps.psyms.len - 2; i > 0; i--)
                if (ps.psyms.sym[i] == psym_decl)
                        level++;
        return level;
@@ -101,10 +101,10 @@ decl_level(void)
 static void
 ps_push(parser_symbol psym, bool follow)
 {
-       if (ps.psyms.top + 1 >= STACKSIZE)
+       if (ps.psyms.len >= STACKSIZE)
                errx(1, "Parser stack overflow");
-       ps.psyms.sym[++ps.psyms.top] = psym;
-       ps.psyms.ind_level[ps.psyms.top] =
+       ps.psyms.sym[++ps.psyms.len - 1] = psym;
+       ps.psyms.ind_level[ps.psyms.len - 1] =
            follow ? ps.ind_level_follow : ps.ind_level;
 }
 



Home | Main Index | Thread Index | Old Index