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 handling of comments



details:   https://anonhg.NetBSD.org/src/rev/291d784bcee7
branches:  trunk
changeset: 376367:291d784bcee7
user:      rillig <rillig%NetBSD.org@localhost>
date:      Wed Jun 14 09:31:05 2023 +0000

description:
indent: clean up handling of comments

One less moving part in the parser state.

No functional change.

diffstat:

 tests/usr.bin/indent/lsym_comment.c |  21 ++++++++++++--
 tests/usr.bin/indent/psym_decl.c    |  27 +++++++++++++++---
 usr.bin/indent/debug.c              |  12 ++++----
 usr.bin/indent/indent.h             |  21 ++++++--------
 usr.bin/indent/io.c                 |  14 +++++----
 usr.bin/indent/pr_comment.c         |  52 +++++++++++++++++-------------------
 6 files changed, 86 insertions(+), 61 deletions(-)

diffs (truncated from 393 to 300 lines):

diff -r f0063f894f42 -r 291d784bcee7 tests/usr.bin/indent/lsym_comment.c
--- a/tests/usr.bin/indent/lsym_comment.c       Wed Jun 14 08:36:51 2023 +0000
+++ b/tests/usr.bin/indent/lsym_comment.c       Wed Jun 14 09:31:05 2023 +0000
@@ -1,4 +1,4 @@
-/* $NetBSD: lsym_comment.c,v 1.20 2023/06/10 16:43:56 rillig Exp $ */
+/* $NetBSD: lsym_comment.c,v 1.21 2023/06/14 09:31:05 rillig Exp $ */
 
 /*
  * Tests for the token lsym_comment, which starts a comment.
@@ -43,7 +43,6 @@
  * - with varying opt.comment_column (-c0, -c1, -c33, -c80)
  * - with varying opt.decl_comment_column (-cd0, -cd1, -cd20, -cd33, -cd80)
  * - with/without ps.line_has_decl
- * - with/without ps.next_col_1
  *
  * - very long comments that overflow the buffer 'com'
  * - comments that come from save_com
@@ -1020,8 +1019,8 @@ f(void)
 
 
 /*
- * Test two completely empty lines in a wrap comment. The second empty line
- * covers the condition ps.next_col_1 in copy_comment_wrap.
+ * In a comment that is wrapped, one or more empty lines separate paragraphs.
+ * All of these empty lines are preserved.
  */
 //indent input
 /* line 1
@@ -1131,3 +1130,17 @@ int line;                        // comment line 1
 // comment line 2
 int block;                     /* comment line 1 comment line 2 */
 //indent end
+
+
+//indent input
+/*/ comment? or:not; /* */
+//indent end
+
+//indent run
+/* / comment? or:not; /* */
+//indent end
+
+//indent run -nfc1
+// $ FIXME: It's a comment, not code.
+/*/ comment ? or : not;                /* */
+//indent end
diff -r f0063f894f42 -r 291d784bcee7 tests/usr.bin/indent/psym_decl.c
--- a/tests/usr.bin/indent/psym_decl.c  Wed Jun 14 08:36:51 2023 +0000
+++ b/tests/usr.bin/indent/psym_decl.c  Wed Jun 14 09:31:05 2023 +0000
@@ -1,20 +1,17 @@
-/* $NetBSD: psym_decl.c,v 1.4 2022/04/24 10:36:37 rillig Exp $ */
+/* $NetBSD: psym_decl.c,v 1.5 2023/06/14 09:31:05 rillig Exp $ */
 
 /*
  * Tests for the parser symbol psym_decl, which represents a declaration.
  *
  * Since C99, declarations and statements can be mixed in blocks.
  *
- * A label can be followed by a statement but not by a declaration.
+ * In C, a label can be followed by a statement but not by a declaration.
  *
  * Indent distinguishes global and local declarations.
  *
  * Declarations can be for functions or for variables.
  */
 
-// TODO: prove that psym_decl can only ever occur at the top of the stack.
-// TODO: delete decl_level if the above is proven.
-
 //indent input
 int global_var;
 int global_array = [1,2,3,4];
@@ -36,3 +33,23 @@ int global_array = [
                    4,
 ];
 //indent end
+
+
+// Declarations can be nested.
+//indent input
+struct level_1 {
+       union level_2 {
+               enum level_3 {
+                       level_3_c_1,
+                       level_3_c_2,
+               }               level_3;
+       }               level_2;
+} level_1;
+//indent end
+
+// The outermost declarator 'level_1' is indented as a global variable.
+// The inner declarators 'level_2' and 'level_3' are indented as local
+// variables.
+// XXX: This is inconsistent, as in practice, struct members are usually
+// aligned, while local variables aren't.
+//indent run-equals-input -ldi0
diff -r f0063f894f42 -r 291d784bcee7 usr.bin/indent/debug.c
--- a/usr.bin/indent/debug.c    Wed Jun 14 08:36:51 2023 +0000
+++ b/usr.bin/indent/debug.c    Wed Jun 14 09:31:05 2023 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: debug.c,v 1.56 2023/06/14 08:36:51 rillig Exp $        */
+/*     $NetBSD: debug.c,v 1.57 2023/06/14 09:31:05 rillig Exp $        */
 
 /*-
  * Copyright (c) 2023 The NetBSD Foundation, Inc.
@@ -30,7 +30,7 @@
  */
 
 #include <sys/cdefs.h>
-__RCSID("$NetBSD: debug.c,v 1.56 2023/06/14 08:36:51 rillig Exp $");
+__RCSID("$NetBSD: debug.c,v 1.57 2023/06/14 09:31:05 rillig Exp $");
 
 #include <stdarg.h>
 #include <string.h>
@@ -363,10 +363,10 @@ debug_parser_state(void)
        debug_ps_int(nparen);
        debug_ps_paren();
 
-       state.heading = "horizontal spacing for comments";
-       debug_ps_int(comment_delta);
-       debug_ps_int(n_comment_delta);
-       debug_ps_int(com_ind);
+       state.heading = "indentation of comments";
+       debug_ps_int(comment_ind);
+       debug_ps_int(comment_shift);
+       debug_ps_bool(comment_in_first_line);
 
        state.heading = "vertical spacing";
        debug_ps_bool(break_after_comma);
diff -r f0063f894f42 -r 291d784bcee7 usr.bin/indent/indent.h
--- a/usr.bin/indent/indent.h   Wed Jun 14 08:36:51 2023 +0000
+++ b/usr.bin/indent/indent.h   Wed Jun 14 09:31:05 2023 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: indent.h,v 1.191 2023/06/14 08:36:51 rillig Exp $      */
+/*     $NetBSD: indent.h,v 1.192 2023/06/14 09:31:05 rillig Exp $      */
 
 /*-
  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
@@ -71,7 +71,7 @@
 typedef enum lexer_symbol {
        lsym_eof,
        lsym_preprocessing,     /* the initial '#' of a preprocessing line */
-       lsym_newline,
+       lsym_newline,           /* outside block comments */
        lsym_comment,           /* the initial '/ *' or '//' of a comment */
 
        lsym_lparen,
@@ -229,8 +229,8 @@ extern struct options {
                                         * lined-up code within the margin */
        bool lineup_to_parens;  /* whether continued code within parens will be
                                 * lined up to the open paren */
-       bool proc_calls_space;  /* whether function calls look like: foo (bar)
-                                * rather than foo(bar) */
+       bool proc_calls_space;  /* whether function calls look like 'foo (bar)'
+                                * rather than 'foo(bar)' */
        bool procnames_start_line;      /* whether the names of functions being
                                         * defined get placed in column 1 (i.e.
                                         * a newline is placed between the type
@@ -376,15 +376,12 @@ extern struct parser_state {
                                 * initializer or declaration */
        struct paren_level paren[20];
 
-       /* Horizontal spacing for comments */
+       /* Indentation of comments */
 
-       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 before
-                                * the start of a box comment so that
-                                * forthcoming lines of the comment are
-                                * indented properly */
-       int com_ind;            /* indentation of the current comment */
+       int comment_ind;        /* indentation of the current comment */
+       int comment_shift;      /* all but the first line of a boxed comment
+                                * are shifted this much to the right */
+       bool comment_in_first_line;
 
        /* Vertical spacing */
 
diff -r f0063f894f42 -r 291d784bcee7 usr.bin/indent/io.c
--- a/usr.bin/indent/io.c       Wed Jun 14 08:36:51 2023 +0000
+++ b/usr.bin/indent/io.c       Wed Jun 14 09:31:05 2023 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: io.c,v 1.217 2023/06/10 21:36:38 rillig Exp $  */
+/*     $NetBSD: io.c,v 1.218 2023/06/14 09:31:05 rillig Exp $  */
 
 /*-
  * SPDX-License-Identifier: BSD-4-Clause
@@ -38,7 +38,7 @@
  */
 
 #include <sys/cdefs.h>
-__RCSID("$NetBSD: io.c,v 1.217 2023/06/10 21:36:38 rillig Exp $");
+__RCSID("$NetBSD: io.c,v 1.218 2023/06/14 09:31:05 rillig Exp $");
 
 #include <stdio.h>
 
@@ -294,10 +294,14 @@ output_line_code(void)
 static void
 output_comment(void)
 {
-       int target_ind = ps.com_ind + ps.comment_delta;
+       int target_ind = ps.comment_ind;
        const char *p;
 
-       /* consider original indentation in case this is a box comment */
+       if (!ps.comment_in_first_line)
+               target_ind += ps.comment_shift;
+       ps.comment_in_first_line = false;
+
+       /* consider the original indentation in case this is a box comment */
        for (p = com.s; *p == '\t'; p++)
                target_ind += opt.tabsize;
 
@@ -321,8 +325,6 @@ output_comment(void)
 
        write_indent(target_ind);
        write_range(p, com.len - (size_t)(p - com.s));
-
-       ps.comment_delta = ps.n_comment_delta;
 }
 
 static void
diff -r f0063f894f42 -r 291d784bcee7 usr.bin/indent/pr_comment.c
--- a/usr.bin/indent/pr_comment.c       Wed Jun 14 08:36:51 2023 +0000
+++ b/usr.bin/indent/pr_comment.c       Wed Jun 14 09:31:05 2023 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: pr_comment.c,v 1.163 2023/06/14 08:36:51 rillig Exp $  */
+/*     $NetBSD: pr_comment.c,v 1.164 2023/06/14 09:31:05 rillig Exp $  */
 
 /*-
  * SPDX-License-Identifier: BSD-4-Clause
@@ -38,7 +38,7 @@
  */
 
 #include <sys/cdefs.h>
-__RCSID("$NetBSD: pr_comment.c,v 1.163 2023/06/14 08:36:51 rillig Exp $");
+__RCSID("$NetBSD: pr_comment.c,v 1.164 2023/06/14 09:31:05 rillig Exp $");
 
 #include <string.h>
 
@@ -58,7 +58,7 @@ com_add_delim(void)
 }
 
 static bool
-fits_in_one_line(int com_ind, int max_line_length)
+fits_in_one_line(int max_line_length)
 {
        for (const char *start = inp_p, *p = start; *p != '\n'; p++) {
                if (p[0] == '*' && p[1] == '/') {
@@ -66,18 +66,17 @@ fits_in_one_line(int com_ind, int max_li
                            && ch_isblank(p[-1])
                            && ch_isblank(p[-2]))
                                p--;
-                       int len = ind_add(com_ind + 3,
+                       int ind = ind_add(ps.comment_ind + 3,
                            start, (size_t)(p - start));
-                       len += p == start || ch_isblank(p[-1]) ? 2 : 3;
-                       return len <= max_line_length;
+                       ind += p == start || ch_isblank(p[-1]) ? 2 : 3;
+                       return ind <= max_line_length;
                }
        }
        return false;
 }
 
 static void
-analyze_comment(bool *p_may_wrap, bool *p_delim,
-    int *p_ind, int *p_line_length)
+analyze_comment(bool *p_may_wrap, bool *p_delim, int *p_line_length)
 {
        bool may_wrap = true;
        bool delim = false;
@@ -122,30 +121,28 @@ analyze_comment(bool *p_may_wrap, bool *
                }
        }
 
-       ps.com_ind = ind;
-
        if (!may_wrap) {
                /* Find out how much indentation there was originally, because
                 * that much will have to be ignored by output_line. */
                size_t len = (size_t)(inp_p - 2 - inp.s);
-               ps.n_comment_delta = -ind_add(0, inp.s, len);
+               ps.comment_shift = -ind_add(0, inp.s, len);
        } else {
-               ps.n_comment_delta = 0;
+               ps.comment_shift = 0;
                if (!(inp_p[0] == '\t' && !ch_isblank(inp_p[1])))
                        while (ch_isblank(inp_p[0]))
                                inp_p++;
        }
 
+       ps.comment_ind = ind;
        *p_may_wrap = may_wrap;
        *p_delim = delim;
-       *p_ind = ind;
        *p_line_length = line_length;
 }
 



Home | Main Index | Thread Index | Old Index