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: replace bitmasking code with struct



details:   https://anonhg.NetBSD.org/src/rev/a909729ea9d5
branches:  trunk
changeset: 361534:a909729ea9d5
user:      rillig <rillig%NetBSD.org@localhost>
date:      Sun Feb 13 12:20:09 2022 +0000

description:
indent: replace bitmasking code with struct

The struct directly represents the properties of a pair of parentheses,
without forcing the human reader to decode any bitset.  This makes it
easier to find the remaining bugs in the heuristic for determining the
kind of parentheses.

No functional change outside debug mode.

diffstat:

 usr.bin/indent/indent.c |  29 +++++++++++++--------------
 usr.bin/indent/indent.h |  24 ++++++++++++----------
 usr.bin/indent/io.c     |  14 ++++++------
 usr.bin/indent/lexi.c   |  52 ++++++++++++++++++++++++++++++++++++++----------
 4 files changed, 75 insertions(+), 44 deletions(-)

diffs (260 lines):

diff -r 750810fe1720 -r a909729ea9d5 usr.bin/indent/indent.c
--- a/usr.bin/indent/indent.c   Sun Feb 13 12:09:19 2022 +0000
+++ b/usr.bin/indent/indent.c   Sun Feb 13 12:20:09 2022 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: indent.c,v 1.240 2022/02/13 12:09:19 rillig Exp $      */
+/*     $NetBSD: indent.c,v 1.241 2022/02/13 12:20:09 rillig Exp $      */
 
 /*-
  * SPDX-License-Identifier: BSD-4-Clause
@@ -43,7 +43,7 @@
 
 #include <sys/cdefs.h>
 #if defined(__NetBSD__)
-__RCSID("$NetBSD: indent.c,v 1.240 2022/02/13 12:09:19 rillig Exp $");
+__RCSID("$NetBSD: indent.c,v 1.241 2022/02/13 12:20:09 rillig Exp $");
 #elif defined(__FreeBSD__)
 __FBSDID("$FreeBSD: head/usr.bin/indent/indent.c 340138 2018-11-04 19:24:49Z oshogbo $");
 #endif
@@ -683,9 +683,9 @@
 static void
 process_lparen_or_lbracket(int decl_ind, bool tabs_to_var, bool spaced_expr)
 {
-    if (++ps.p_l_follow == array_length(ps.paren_indents)) {
+    if (++ps.p_l_follow == array_length(ps.paren)) {
        diag(0, "Reached internal limit of %zu unclosed parentheses",
-           array_length(ps.paren_indents));
+           array_length(ps.paren));
        ps.p_l_follow--;
     }
 
@@ -700,14 +700,14 @@
     ps.want_blank = false;
     *code.e++ = token.s[0];
 
-    ps.paren_indents[ps.p_l_follow - 1] = (short)ind_add(0, code.s, code.e);
+    ps.paren[ps.p_l_follow - 1].indent = (short)ind_add(0, code.s, code.e);
     debug_println("paren_indents[%d] is now %d",
-       ps.p_l_follow - 1, ps.paren_indents[ps.p_l_follow - 1]);
+       ps.p_l_follow - 1, ps.paren[ps.p_l_follow - 1].indent);
 
     if (spaced_expr && ps.p_l_follow == 1 && opt.extra_expr_indent
-           && ps.paren_indents[0] < 2 * opt.indent_size) {
-       ps.paren_indents[0] = (short)(2 * opt.indent_size);
-       debug_println("paren_indents[0] is now %d", ps.paren_indents[0]);
+           && ps.paren[0].indent < 2 * opt.indent_size) {
+       ps.paren[0].indent = (short)(2 * opt.indent_size);
+       debug_println("paren_indents[0] is now %d", ps.paren[0].indent);
     }
 
     if (ps.init_or_struct && *token.s == '(' && ps.tos <= 2) {
@@ -721,19 +721,20 @@
 
     /* parenthesized type following sizeof or offsetof is not a cast */
     if (ps.prev_token == lsym_offsetof || ps.prev_token == lsym_sizeof)
-       ps.not_cast_mask0 |= 1 << (ps.p_l_follow - 1);
+       ps.paren[ps.p_l_follow - 1].no_cast = true;
 }
 
 static void
 process_rparen_or_rbracket(bool *spaced_expr, bool *force_nl, stmt_head hd)
 {
-    if ((ps.cast_mask0 & (1 << (ps.p_l_follow - 1)) & ~ps.not_cast_mask0) != 0) {
+    if (ps.paren[ps.p_l_follow - 1].maybe_cast &&
+       !ps.paren[ps.p_l_follow - 1].no_cast) {
        ps.next_unary = true;
-       ps.cast_mask0 &= (1 << (ps.p_l_follow - 1)) - 1;
+       ps.paren[ps.p_l_follow - 1].maybe_cast = false;
        ps.want_blank = opt.space_after_cast;
     } else
        ps.want_blank = true;
-    ps.not_cast_mask0 &= (1 << (ps.p_l_follow - 1)) - 1;
+    ps.paren[ps.p_l_follow - 1].no_cast = false;
 
     if (ps.p_l_follow > 0)
        ps.p_l_follow--;
@@ -855,8 +856,6 @@
     *quest_level = 0;
     if (ps.prev_token == lsym_rparen_or_rbracket)
        ps.in_func_def_params = false;
-    ps.cast_mask0 = 0;
-    ps.not_cast_mask0 = 0;
     ps.block_init = false;
     ps.block_init_level = 0;
     ps.just_saw_decl--;
diff -r 750810fe1720 -r a909729ea9d5 usr.bin/indent/indent.h
--- a/usr.bin/indent/indent.h   Sun Feb 13 12:09:19 2022 +0000
+++ b/usr.bin/indent/indent.h   Sun Feb 13 12:20:09 2022 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: indent.h,v 1.109 2022/02/13 12:09:19 rillig Exp $      */
+/*     $NetBSD: indent.h,v 1.110 2022/02/13 12:20:09 rillig Exp $      */
 
 /*-
  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
@@ -247,6 +247,17 @@
 
 #define        STACKSIZE 256
 
+/* Properties of each level of parentheses or brackets. */
+typedef struct paren_level_props {
+    short indent;              /* indentation of the operand/argument,
+                                * relative to the enclosing statement; if
+                                * negative, reflected at -1 */
+    bool maybe_cast;           /* whether the parentheses may form a type
+                                * cast */
+    bool no_cast;              /* whether the parentheses definitely do not
+                                * form a type cast */
+} paren_level_props;
+
 extern struct parser_state {
     lexer_symbol prev_token;   /* the previous token, but never comment,
                                 * newline or preprocessing line */
@@ -268,16 +279,7 @@
     /* TODO: rename to next_line_paren_level */
     int p_l_follow;            /* how to indent the remaining lines of the
                                 * statement or initializer or declaration */
-    short paren_indents[20];   /* indentation of the operand/argument of each
-                                * level of parentheses or brackets, relative
-                                * to the enclosing statement; if negative,
-                                * reflected at -1 */
-    int cast_mask0;            /* indicates which close parentheses
-                                * potentially close off casts */
-    int not_cast_mask0;                /* indicates which close parentheses
-                                * definitely close off something else than
-                                * casts */
-
+    paren_level_props paren[20];
     int comment_delta;         /* used to set up indentation for all lines of
                                 * a boxed comment after the first one */
     int n_comment_delta;       /* remembers how many columns there were
diff -r 750810fe1720 -r a909729ea9d5 usr.bin/indent/io.c
--- a/usr.bin/indent/io.c       Sun Feb 13 12:09:19 2022 +0000
+++ b/usr.bin/indent/io.c       Sun Feb 13 12:20:09 2022 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: io.c,v 1.144 2022/02/12 19:56:52 rillig Exp $  */
+/*     $NetBSD: io.c,v 1.145 2022/02/13 12:20:09 rillig Exp $  */
 
 /*-
  * SPDX-License-Identifier: BSD-4-Clause
@@ -43,7 +43,7 @@
 
 #include <sys/cdefs.h>
 #if defined(__NetBSD__)
-__RCSID("$NetBSD: io.c,v 1.144 2022/02/12 19:56:52 rillig Exp $");
+__RCSID("$NetBSD: io.c,v 1.145 2022/02/13 12:20:09 rillig Exp $");
 #elif defined(__FreeBSD__)
 __FBSDID("$FreeBSD: head/usr.bin/indent/io.c 334927 2018-06-10 16:44:18Z pstef $");
 #endif
@@ -450,12 +450,12 @@
 
     int target_ind = compute_code_indent();
     for (int i = 0; i < ps.p_l_follow; i++) {
-       if (ps.paren_indents[i] >= 0) {
-           int paren_ind = ps.paren_indents[i];
-           ps.paren_indents[i] = (short)(-1 - (paren_ind + target_ind));
+       if (ps.paren[i].indent >= 0) {
+           int paren_ind = ps.paren[i].indent;
+           ps.paren[i].indent = (short)(-1 - (paren_ind + target_ind));
            debug_println(
                "setting paren_indents[%d] from %d to %d for column %d",
-               i, paren_ind, ps.paren_indents[i], target_ind + 1);
+               i, paren_ind, ps.paren[i].indent, target_ind + 1);
        }
     }
 
@@ -575,7 +575,7 @@
 
     if (ps.paren_level > 0) {
        /* TODO: explain what negative indentation means */
-       paren_indent = -1 - ps.paren_indents[ps.paren_level - 1];
+       paren_indent = -1 - ps.paren[ps.paren_level - 1].indent;
        debug_println("paren_indent is now %d", paren_indent);
     }
 
diff -r 750810fe1720 -r a909729ea9d5 usr.bin/indent/lexi.c
--- a/usr.bin/indent/lexi.c     Sun Feb 13 12:09:19 2022 +0000
+++ b/usr.bin/indent/lexi.c     Sun Feb 13 12:20:09 2022 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: lexi.c,v 1.170 2022/02/13 12:09:19 rillig Exp $        */
+/*     $NetBSD: lexi.c,v 1.171 2022/02/13 12:20:09 rillig Exp $        */
 
 /*-
  * SPDX-License-Identifier: BSD-4-Clause
@@ -43,7 +43,7 @@
 
 #include <sys/cdefs.h>
 #if defined(__NetBSD__)
-__RCSID("$NetBSD: lexi.c,v 1.170 2022/02/13 12:09:19 rillig Exp $");
+__RCSID("$NetBSD: lexi.c,v 1.171 2022/02/13 12:20:09 rillig Exp $");
 #elif defined(__FreeBSD__)
 __FBSDID("$FreeBSD: head/usr.bin/indent/lexi.c 337862 2018-08-15 18:19:45Z pstef $");
 #endif
@@ -262,6 +262,42 @@
        else if (debug_full_parser_state()) \
            debug_println("       %3d ps." #name, ps.name)
 
+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->p_l_follow != ps.p_l_follow)
+       return true;
+
+    for (int i = 0; i < ps.p_l_follow; i++) {
+       if (curr[i].indent != prev[i].indent ||
+           curr[i].maybe_cast != prev[i].maybe_cast ||
+           curr[i].no_cast != prev[i].no_cast)
+           return true;
+    }
+    return false;
+}
+
+static void
+debug_ps_paren(const struct parser_state *prev_ps)
+{
+    if (!debug_full_parser_state() && !ps_paren_has_changed(prev_ps))
+       return;
+
+    debug_printf("           ps.paren:");
+    for (int i = 0; i < ps.p_l_follow; i++) {
+       const paren_level_props *props = ps.paren + i;
+       const char *cast = props->no_cast ? "(no cast)"
+           : props->maybe_cast ? "(cast)"
+           : "";
+       debug_printf(" %s%d", cast, props->indent);
+    }
+    if (ps.p_l_follow == 0)
+       debug_printf(" none");
+    debug_println("");
+}
+
 static void
 debug_lexi(lexer_symbol lsym)
 {
@@ -287,14 +323,7 @@
     debug_ps_bool(want_blank);
     debug_ps_int(paren_level);
     debug_ps_int(p_l_follow);
-    if (ps.paren_level != prev_ps.paren_level) {
-       debug_printf("           ps.paren_indents:");
-       for (int i = 0; i < ps.paren_level; i++)
-           debug_printf(" %d", ps.paren_indents[i]);
-       debug_println("");
-    }
-    debug_ps_int(cast_mask0);
-    debug_ps_int(not_cast_mask0);
+    debug_ps_paren(&prev_ps);
 
     debug_ps_int(comment_delta);
     debug_ps_int(n_comment_delta);
@@ -556,7 +585,8 @@
 found_typename:
        if (ps.p_l_follow > 0) {
            /* inside parentheses: cast, param list, offsetof or sizeof */
-           ps.cast_mask0 |= (1 << (ps.p_l_follow - 1)) & ~ps.not_cast_mask0;
+           if (!ps.paren[ps.p_l_follow - 1].no_cast)
+               ps.paren[ps.p_l_follow - 1].maybe_cast = true;
        }
        if (ps.prev_token != lsym_period && ps.prev_token != lsym_unary_op) {
            if (kw != NULL && kw->lsym == lsym_tag) {



Home | Main Index | Thread Index | Old Index