Module Name: src Committed By: rillig Date: Wed Jun 14 16:14:30 UTC 2023
Modified Files: src/usr.bin/indent: debug.c indent.c indent.h io.c lexi.c parse.c Log Message: 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. To generate a diff of this commit: cvs rdiff -u -r1.59 -r1.60 src/usr.bin/indent/debug.c cvs rdiff -u -r1.364 -r1.365 src/usr.bin/indent/indent.c cvs rdiff -u -r1.194 -r1.195 src/usr.bin/indent/indent.h cvs rdiff -u -r1.220 -r1.221 src/usr.bin/indent/io.c cvs rdiff -u -r1.228 -r1.229 src/usr.bin/indent/lexi.c cvs rdiff -u -r1.73 -r1.74 src/usr.bin/indent/parse.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.59 src/usr.bin/indent/debug.c:1.60 --- src/usr.bin/indent/debug.c:1.59 Wed Jun 14 14:11:28 2023 +++ src/usr.bin/indent/debug.c Wed Jun 14 16:14:30 2023 @@ -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(""); Index: src/usr.bin/indent/indent.c diff -u src/usr.bin/indent/indent.c:1.364 src/usr.bin/indent/indent.c:1.365 --- src/usr.bin/indent/indent.c:1.364 Wed Jun 14 14:11:28 2023 +++ src/usr.bin/indent/indent.c Wed Jun 14 16:14:30 2023 @@ -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 && Index: src/usr.bin/indent/indent.h diff -u src/usr.bin/indent/indent.h:1.194 src/usr.bin/indent/indent.h:1.195 --- src/usr.bin/indent/indent.h:1.194 Wed Jun 14 14:11:28 2023 +++ src/usr.bin/indent/indent.h Wed Jun 14 16:14:30 2023 @@ -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]; }; Index: src/usr.bin/indent/io.c diff -u src/usr.bin/indent/io.c:1.220 src/usr.bin/indent/io.c:1.221 --- src/usr.bin/indent/io.c:1.220 Wed Jun 14 14:11:28 2023 +++ src/usr.bin/indent/io.c Wed Jun 14 16:14:30 2023 @@ -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; Index: src/usr.bin/indent/lexi.c diff -u src/usr.bin/indent/lexi.c:1.228 src/usr.bin/indent/lexi.c:1.229 --- src/usr.bin/indent/lexi.c:1.228 Wed Jun 14 14:11:28 2023 +++ src/usr.bin/indent/lexi.c Wed Jun 14 16:14:30 2023 @@ -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()) { Index: src/usr.bin/indent/parse.c diff -u src/usr.bin/indent/parse.c:1.73 src/usr.bin/indent/parse.c:1.74 --- src/usr.bin/indent/parse.c:1.73 Wed Jun 14 07:20:55 2023 +++ src/usr.bin/indent/parse.c Wed Jun 14 16:14:30 2023 @@ -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 @@ __RCSID("$NetBSD: parse.c,v 1.73 2023/06 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; } @@ -116,11 +116,12 @@ static void psyms_reduce(struct psym_stack *psyms) { again: - if (psyms->sym[psyms->top] == psym_stmt && psyms_reduce_stmt(psyms)) + if (psyms->sym[psyms->len - 1] == psym_stmt + && psyms_reduce_stmt(psyms)) goto again; - if (psyms->sym[psyms->top] == psym_while_expr && - psyms->sym[psyms->top - 1] == psym_do_stmt) { - psyms->top -= 2; + if (psyms->sym[psyms->len - 1] == psym_while_expr && + psyms->sym[psyms->len - 2] == psym_do_stmt) { + psyms->len -= 2; goto again; } } @@ -146,8 +147,8 @@ parse(parser_symbol psym) struct psym_stack *psyms = &ps.psyms; if (psym != psym_else) { - while (psyms->sym[psyms->top] == psym_if_expr_stmt) { - psyms->sym[psyms->top] = psym_stmt; + while (psyms->sym[psyms->len - 1] == psym_if_expr_stmt) { + psyms->sym[psyms->len - 1] = psym_stmt; psyms_reduce(&ps.psyms); } } @@ -159,8 +160,8 @@ parse(parser_symbol psym) case psym_lbrace_union: case psym_lbrace_enum: ps.break_after_comma = false; - if (psyms->sym[psyms->top] == psym_decl - || psyms->sym[psyms->top] == psym_stmt) + if (psyms->sym[psyms->len - 1] == psym_decl + || psyms->sym[psyms->len - 1] == psym_stmt) ++ps.ind_level_follow; else if (code.len == 0) { /* It is part of a while, for, etc. */ @@ -168,7 +169,7 @@ parse(parser_symbol psym) /* for a switch, brace should be two levels out from * the code */ - if (psyms->sym[psyms->top] == psym_switch_expr + if (psyms->sym[psyms->len - 1] == psym_switch_expr && opt.case_indent >= 1.0F) --ps.ind_level; } @@ -179,18 +180,18 @@ parse(parser_symbol psym) case psym_rbrace: /* stack should have <lbrace> <stmt> or <lbrace> <stmt_list> */ - if (!(psyms->top > 0 - && is_lbrace(psyms->sym[psyms->top - 1]))) { + if (!(psyms->len >= 2 + && is_lbrace(psyms->sym[psyms->len - 2]))) { diag(1, "Statement nesting error"); break; } ps.ind_level = ps.ind_level_follow = - psyms->ind_level[--psyms->top]; - psyms->sym[psyms->top] = psym_stmt; + psyms->ind_level[psyms->len-- - 2]; + psyms->sym[psyms->len - 1] = psym_stmt; break; case psym_decl: - if (psyms->sym[psyms->top] == psym_decl) + if (psyms->sym[psyms->len - 1] == psym_decl) break; /* only put one declaration onto stack */ ps.break_after_comma = true; @@ -206,9 +207,10 @@ parse(parser_symbol psym) break; case psym_if_expr: - if (psyms->sym[psyms->top] == psym_if_expr_stmt_else + if (psyms->sym[psyms->len - 1] == psym_if_expr_stmt_else && opt.else_if_in_same_line) - ps.ind_level_follow = psyms->ind_level[psyms->top--]; + ps.ind_level_follow = + psyms->ind_level[psyms->len-- - 1]; /* FALLTHROUGH */ case psym_do: case psym_for_exprs: @@ -217,13 +219,13 @@ parse(parser_symbol psym) break; case psym_else: - if (psyms->sym[psyms->top] != psym_if_expr_stmt) { + if (psyms->sym[psyms->len - 1] != psym_if_expr_stmt) { diag(1, "Unmatched 'else'"); break; } - ps.ind_level = psyms->ind_level[psyms->top]; + ps.ind_level = psyms->ind_level[psyms->len - 1]; ps.ind_level_follow = ps.ind_level + 1; - psyms->sym[psyms->top] = psym_if_expr_stmt_else; + psyms->sym[psyms->len - 1] = psym_if_expr_stmt_else; break; case psym_switch_expr: @@ -232,9 +234,9 @@ parse(parser_symbol psym) break; case psym_while_expr: - if (psyms->sym[psyms->top] == psym_do_stmt) { + if (psyms->sym[psyms->len - 1] == psym_do_stmt) { ps.ind_level = ps.ind_level_follow = - psyms->ind_level[psyms->top]; + psyms->ind_level[psyms->len - 1]; ps_push(psym_while_expr, false); } else { ps_push(psym_while_expr, true);