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: various cleanups



details:   https://anonhg.NetBSD.org/src/rev/5f61d10bac1a
branches:  trunk
changeset: 1024876:5f61d10bac1a
user:      rillig <rillig%NetBSD.org@localhost>
date:      Sun Nov 07 18:26:17 2021 +0000

description:
indent: various cleanups

Make several comments more precise.

Rename process_end_of_file to process_eof to match the token name.

Change the order of assignments in analyze_comment to keep the com_ind
computations closer together.

In copy_comment_wrap, use pointer difference instead of pointer addition
to stay away from undefined behavior.

No functional change.

diffstat:

 usr.bin/indent/indent.c     |  16 ++++++++--------
 usr.bin/indent/indent.h     |  22 +++++++++++++++-------
 usr.bin/indent/lexi.c       |   8 ++++----
 usr.bin/indent/parse.c      |   7 +++----
 usr.bin/indent/pr_comment.c |  24 +++++++++++-------------
 5 files changed, 41 insertions(+), 36 deletions(-)

diffs (271 lines):

diff -r 81cf0ababdff -r 5f61d10bac1a usr.bin/indent/indent.c
--- a/usr.bin/indent/indent.c   Sun Nov 07 18:09:56 2021 +0000
+++ b/usr.bin/indent/indent.c   Sun Nov 07 18:26:17 2021 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: indent.c,v 1.215 2021/11/07 15:18:25 rillig Exp $      */
+/*     $NetBSD: indent.c,v 1.216 2021/11/07 18:26:17 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.215 2021/11/07 15:18:25 rillig Exp $");
+__RCSID("$NetBSD: indent.c,v 1.216 2021/11/07 18:26:17 rillig Exp $");
 #elif defined(__FreeBSD__)
 __FBSDID("$FreeBSD: head/usr.bin/indent/indent.c 340138 2018-11-04 19:24:49Z oshogbo $");
 #endif
@@ -294,8 +294,8 @@
     if (sc_end == NULL) {
        /*
         * Copy everything from the start of the line, because
-        * process_comment() will use that to calculate original indentation
-        * of a boxed comment.
+        * process_comment() will use that to calculate the original
+        * indentation of a boxed comment.
         */
        /*
         * FIXME: This '4' needs an explanation. For example, in the snippet
@@ -665,7 +665,7 @@
 }
 
 static void __attribute__((__noreturn__))
-process_end_of_file(void)
+process_eof(void)
 {
     if (lab.s != lab.e || code.s != code.e || com.s != com.e)
        dump_line();
@@ -940,8 +940,8 @@
     }
 
     ps.in_decl = ps.decl_level > 0;    /* if we were in a first level
-                                        * structure declaration, we aren't
-                                        * anymore */
+                                        * structure declaration before, we
+                                        * aren't anymore */
 
     if ((!*spaced_expr || hd != hd_for) && ps.p_l_follow > 0) {
 
@@ -1395,7 +1395,7 @@
        search_stmt(&lsym, &force_nl, &last_else);
 
        if (lsym == lsym_eof) {
-           process_end_of_file();
+           process_eof();
            /* NOTREACHED */
        }
 
diff -r 81cf0ababdff -r 5f61d10bac1a usr.bin/indent/indent.h
--- a/usr.bin/indent/indent.h   Sun Nov 07 18:09:56 2021 +0000
+++ b/usr.bin/indent/indent.h   Sun Nov 07 18:26:17 2021 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: indent.h,v 1.85 2021/11/07 15:18:25 rillig Exp $       */
+/*     $NetBSD: indent.h,v 1.86 2021/11/07 18:26:17 rillig Exp $       */
 
 /*-
  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
@@ -150,7 +150,8 @@
 extern FILE *output;
 
 extern struct buffer inp;      /* one line of input, ready to be split into
-                                * tokens */
+                                * tokens; occasionally this buffer switches
+                                * to sc_buf */
 
 extern struct buffer token;    /* the current token to be processed, is
                                 * typically copied to the buffer 'code',
@@ -271,7 +272,9 @@
     bool next_unary;           /* whether the following operator should be
                                 * unary */
 
-    char procname[100];                /* The name of the current procedure */
+    char procname[100];                /* The name of the current procedure; TODO:
+                                * document the difference between procname[0]
+                                * being '\0', ' ' and a real character */
 
 
     bool want_blank;           /* whether the following token should be
@@ -279,10 +282,11 @@
                                 * ignored in some cases.) */
 
     int paren_level;           /* parenthesization level. used to indent
-                                * within statements */
+                                * within statements, initializers and
+                                * declarations */
     /* TODO: rename to next_line_paren_level */
     int p_l_follow;            /* how to indent the remaining lines of the
-                                * statement */
+                                * 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,
@@ -327,7 +331,9 @@
     bool decl_indent_done;     /* whether the indentation for a declaration
                                 * has been added to the code buffer. */
 
-    bool in_stmt;
+    bool in_stmt;              /* TODO: rename to something appropriate; this
+                                * is set to true in struct declarations as
+                                * well, so 'stmt' isn't accurate */
     bool ind_stmt;             /* whether the next line should have an extra
                                 * indentation level because we are in the
                                 * middle of a statement */
@@ -336,7 +342,9 @@
 
     bool search_stmt;          /* whether it is necessary to buffer up all
                                 * text up to the start of a statement after
-                                * an 'if', 'while', etc. */
+                                * an 'if (expr)', 'while (expr)', etc., to
+                                * move the comments after the opening brace
+                                * of the following statement */
 
     int tos;                   /* pointer to top of stack */
     parser_symbol s_sym[STACKSIZE];
diff -r 81cf0ababdff -r 5f61d10bac1a usr.bin/indent/lexi.c
--- a/usr.bin/indent/lexi.c     Sun Nov 07 18:09:56 2021 +0000
+++ b/usr.bin/indent/lexi.c     Sun Nov 07 18:26:17 2021 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: lexi.c,v 1.137 2021/11/07 15:18:25 rillig Exp $        */
+/*     $NetBSD: lexi.c,v 1.138 2021/11/07 18:26:17 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.137 2021/11/07 15:18:25 rillig Exp $");
+__RCSID("$NetBSD: lexi.c,v 1.138 2021/11/07 18:26:17 rillig Exp $");
 #elif defined(__FreeBSD__)
 __FBSDID("$FreeBSD: head/usr.bin/indent/lexi.c 337862 2018-08-15 18:19:45Z pstef $");
 #endif
@@ -362,7 +362,7 @@
             * lex_number_state[0][s - 'A'] now indicates the type:
             * f = floating, i = integer, u = unknown
             */
-           break;
+           return;
        }
 
        s = lex_number_state[row][s - 'A'];
@@ -464,7 +464,7 @@
     return strcmp(key, ((const struct keyword *)elem)->name);
 }
 
-/* Read an alphanumeric token into 'token', or return end_of_file. */
+/* Read an alphanumeric token into 'token', or return lsym_eof. */
 static lexer_symbol
 lexi_alnum(void)
 {
diff -r 81cf0ababdff -r 5f61d10bac1a usr.bin/indent/parse.c
--- a/usr.bin/indent/parse.c    Sun Nov 07 18:09:56 2021 +0000
+++ b/usr.bin/indent/parse.c    Sun Nov 07 18:26:17 2021 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: parse.c,v 1.47 2021/10/29 23:48:50 rillig Exp $        */
+/*     $NetBSD: parse.c,v 1.48 2021/11/07 18:26:17 rillig Exp $        */
 
 /*-
  * SPDX-License-Identifier: BSD-4-Clause
@@ -160,7 +160,7 @@
                 * for a switch, brace should be two levels out from the code
                 */
                if (ps.s_sym[ps.tos] == psym_switch_expr &&
-                       opt.case_indent >= 1)
+                       opt.case_indent >= 1.0F)
                    --ps.ind_level;
            }
        }
@@ -191,11 +191,10 @@
        if (ps.s_sym[ps.tos] != psym_if_expr_stmt)
            diag(1, "Unmatched 'else'");
        else {
-           /* The indentation for 'else' should be the same as for 'if'. */
            ps.ind_level = ps.s_ind_level[ps.tos];
            ps.ind_level_follow = ps.ind_level + 1;
            ps.s_sym[ps.tos] = psym_if_expr_stmt_else;
-           /* remember if with else */
+
            ps.search_stmt = opt.brace_same_line || opt.else_if;
        }
        break;
diff -r 81cf0ababdff -r 5f61d10bac1a usr.bin/indent/pr_comment.c
--- a/usr.bin/indent/pr_comment.c       Sun Nov 07 18:09:56 2021 +0000
+++ b/usr.bin/indent/pr_comment.c       Sun Nov 07 18:26:17 2021 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: pr_comment.c,v 1.115 2021/11/07 13:38:32 rillig Exp $  */
+/*     $NetBSD: pr_comment.c,v 1.116 2021/11/07 18:26:17 rillig Exp $  */
 
 /*-
  * SPDX-License-Identifier: BSD-4-Clause
@@ -43,7 +43,7 @@
 
 #include <sys/cdefs.h>
 #if defined(__NetBSD__)
-__RCSID("$NetBSD: pr_comment.c,v 1.115 2021/11/07 13:38:32 rillig Exp $");
+__RCSID("$NetBSD: pr_comment.c,v 1.116 2021/11/07 18:26:17 rillig Exp $");
 #elif defined(__FreeBSD__)
 __FBSDID("$FreeBSD: head/usr.bin/indent/pr_comment.c 334927 2018-06-10 16:44:18Z pstef $");
 #endif
@@ -124,8 +124,8 @@
        }
 
        if (lab.s == lab.e && code.s == code.e) {
+           adj_max_line_length = opt.block_comment_max_line_length;
            com_ind = (ps.ind_level - opt.unindent_displace) * opt.indent_size;
-           adj_max_line_length = opt.block_comment_max_line_length;
            if (com_ind <= 0)
                com_ind = opt.format_col1_comments ? 0 : 1;
 
@@ -150,20 +150,16 @@
     if (!may_wrap) {
        /*
         * Find out how much indentation there was originally, because that
-        * much will have to be ignored by dump_line(). This is a box comment,
-        * so nothing changes -- not even indentation.
+        * much will have to be ignored by dump_line().
         *
         * The comment we're about to read usually comes from inp.buf, unless
         * it has been copied into save_com.
-        */
-       const char *start;
-
-       /*
+        *
         * XXX: ordered comparison between pointers from different objects
         * invokes undefined behavior (C99 6.5.8).
         */
-       start = inp.s >= sc_buf && inp.s < sc_buf + sc_size ?
-           sc_buf : inp.buf;
+       const char *start = inp.s >= sc_buf && inp.s < sc_buf + sc_size
+           ? sc_buf : inp.buf;
        ps.n_comment_delta = -ind_add(0, start, inp.s - 2);
     } else {
        ps.n_comment_delta = 0;
@@ -174,6 +170,8 @@
     ps.comment_delta = 0;
     com_add_char('/');
     com_add_char(token.e[-1]); /* either '*' or '/' */
+
+    /* TODO: Maybe preserve a single '\t' as well. */
     if (*inp.s != ' ' && may_wrap)
        com_add_char(' ');
 
@@ -227,7 +225,7 @@
            last_blank = -1;
            if (ps.next_col_1) {
                if (com.s == com.e)
-                   com_add_char(' ');
+                   com_add_char(' ');  /* force empty line of output */
                if (com.e - com.s > 3) {
                    dump_line();
                    com_add_delim();
@@ -264,7 +262,7 @@
                inp_skip();
 
                if (break_delim) {
-                   if (com.e > com.s + 3)
+                   if (com.e - com.s > 3)
                        dump_line();
                    else
                        com.e = com.s;



Home | Main Index | Thread Index | Old Index