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



details:   https://anonhg.NetBSD.org/src/rev/6b2b2fca29de
branches:  trunk
changeset: 375305:6b2b2fca29de
user:      rillig <rillig%NetBSD.org@localhost>
date:      Sun May 14 11:29:23 2023 +0000

description:
indent: miscellaneous cleanups

diffstat:

 tests/usr.bin/indent/t_errors.sh |   8 ++--
 usr.bin/indent/args.c            |  13 +++++----
 usr.bin/indent/indent.c          |  55 ++++++++++++++++++---------------------
 usr.bin/indent/indent.h          |  48 +++++++++++++++++++++-------------
 usr.bin/indent/io.c              |  19 ++++++-------
 usr.bin/indent/lexi.c            |   9 ++----
 6 files changed, 79 insertions(+), 73 deletions(-)

diffs (truncated from 428 to 300 lines):

diff -r ed72fe20cb1a -r 6b2b2fca29de tests/usr.bin/indent/t_errors.sh
--- a/tests/usr.bin/indent/t_errors.sh  Sun May 14 11:04:56 2023 +0000
+++ b/tests/usr.bin/indent/t_errors.sh  Sun May 14 11:29:23 2023 +0000
@@ -1,5 +1,5 @@
 #! /bin/sh
-# $NetBSD: t_errors.sh,v 1.27 2023/05/13 16:40:18 rillig Exp $
+# $NetBSD: t_errors.sh,v 1.28 2023/05/14 11:29:23 rillig Exp $
 #
 # Copyright (c) 2021 The NetBSD Foundation, Inc.
 # All rights reserved.
@@ -181,15 +181,15 @@ atf_test_case 'option_special_missing_pa
 option_special_missing_param_body()
 {
        expect_error \
-           'indent: Command line: ``-cli'\'\'' requires an argument' \
+           'indent: Command line: option "-cli" requires an argument' \
            -cli
 
        expect_error \
-           'indent: Command line: ``-T'\'\'' requires an argument' \
+           'indent: Command line: option "-T" requires an argument' \
            -T
 
        expect_error \
-           'indent: Command line: ``-U'\'\'' requires an argument' \
+           'indent: Command line: option "-U" requires an argument' \
            -U
 }
 
diff -r ed72fe20cb1a -r 6b2b2fca29de usr.bin/indent/args.c
--- a/usr.bin/indent/args.c     Sun May 14 11:04:56 2023 +0000
+++ b/usr.bin/indent/args.c     Sun May 14 11:29:23 2023 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: args.c,v 1.75 2023/05/13 13:48:54 rillig Exp $ */
+/*     $NetBSD: args.c,v 1.76 2023/05/14 11:29:23 rillig Exp $ */
 
 /*-
  * SPDX-License-Identifier: BSD-4-Clause
@@ -43,7 +43,7 @@ static char sccsid[] = "@(#)args.c    8.1 (
 
 #include <sys/cdefs.h>
 #if defined(__NetBSD__)
-__RCSID("$NetBSD: args.c,v 1.75 2023/05/13 13:48:54 rillig Exp $");
+__RCSID("$NetBSD: args.c,v 1.76 2023/05/14 11:29:23 rillig Exp $");
 #elif defined(__FreeBSD__)
 __FBSDID("$FreeBSD: head/usr.bin/indent/args.c 336318 2018-07-15 21:04:21Z pstef $");
 #endif
@@ -125,6 +125,7 @@ static const struct pro {
     /* "U" is special */
     bool_options("ut", use_tabs),
     bool_options("v", verbose),
+    /* "-version" is special */
 };
 
 
@@ -138,7 +139,7 @@ add_typedefs_from_file(const char *fname
        fprintf(stderr, "indent: cannot open file %s\n", fname);
        exit(1);
     }
-    while ((fgets(line, BUFSIZ, file)) != NULL) {
+    while ((fgets(line, sizeof(line), file)) != NULL) {
        /* Only keep the first word of the line. */
        line[strcspn(line, " \t\n\r")] = '\0';
        register_typename(line);
@@ -157,7 +158,7 @@ set_special_option(const char *arg, cons
     }
 
     if (arg[0] == 'P' || strcmp(arg, "npro") == 0)
-       return true;
+       return true;            /* see main_load_profiles */
 
     if (strncmp(arg, "cli", 3) == 0) {
        arg_end = arg + 3;
@@ -198,7 +199,7 @@ set_special_option(const char *arg, cons
     return false;
 
 need_arg:
-    errx(1, "%s: ``-%.*s'' requires an argument",
+    errx(1, "%s: option \"-%.*s\" requires an argument",
        option_source, (int)(arg_end - arg), arg);
     /* NOTREACHED */
 }
@@ -277,7 +278,7 @@ load_profile(const char *fname, bool mus
                comment_ch = ch == '/' && comment_ch == '*' ? -1 : ch;
            } else if (ch_isspace((char)ch)) {
                break;
-           } else if (n >= array_length(buf) - 5) {
+           } else if (n >= array_length(buf) - 2) {
                errx(1, "buffer overflow in %s, starting with '%.10s'",
                    fname, buf);
            } else
diff -r ed72fe20cb1a -r 6b2b2fca29de usr.bin/indent/indent.c
--- a/usr.bin/indent/indent.c   Sun May 14 11:04:56 2023 +0000
+++ b/usr.bin/indent/indent.c   Sun May 14 11:29:23 2023 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: indent.c,v 1.264 2023/05/13 17:54:34 rillig Exp $      */
+/*     $NetBSD: indent.c,v 1.265 2023/05/14 11:29:23 rillig Exp $      */
 
 /*-
  * SPDX-License-Identifier: BSD-4-Clause
@@ -43,7 +43,7 @@ static char sccsid[] = "@(#)indent.c  5.1
 
 #include <sys/cdefs.h>
 #if defined(__NetBSD__)
-__RCSID("$NetBSD: indent.c,v 1.264 2023/05/13 17:54:34 rillig Exp $");
+__RCSID("$NetBSD: indent.c,v 1.265 2023/05/14 11:29:23 rillig Exp $");
 #elif defined(__FreeBSD__)
 __FBSDID("$FreeBSD: head/usr.bin/indent/indent.c 340138 2018-11-04 19:24:49Z oshogbo $");
 #endif
@@ -330,9 +330,9 @@ main_parse_command_line(int argc, char *
        opt.comment_column = 2; /* don't put normal comments before column 2 */
     if (opt.block_comment_max_line_length <= 0)
        opt.block_comment_max_line_length = opt.max_line_length;
-    if (opt.local_decl_indent < 0)     /* if not specified by user, set this */
+    if (opt.local_decl_indent < 0)
        opt.local_decl_indent = opt.decl_indent;
-    if (opt.decl_comment_column <= 0)  /* if not specified by user, set this */
+    if (opt.decl_comment_column <= 0)
        opt.decl_comment_column = opt.ljust_decl
            ? (opt.comment_column <= 10 ? 2 : opt.comment_column - 8)
            : opt.comment_column;
@@ -355,8 +355,7 @@ main_prepare_parsing(void)
            break;
     }
 
-    if (ind >= opt.indent_size)
-       ps.ind_level = ps.ind_level_follow = ind / opt.indent_size;
+    ps.ind_level = ps.ind_level_follow = ind / opt.indent_size;
 }
 
 static void
@@ -443,6 +442,17 @@ stay_in_line:
 }
 
 static bool
+is_function_pointer_declaration(void)
+{
+    return token.s[0] == '('
+       && ps.in_decl
+       && !ps.block_init
+       && !ps.decl_indent_done
+       && !ps.is_function_definition
+       && ps.line_start_nparen == 0;
+}
+
+static bool
 want_blank_before_lparen(void)
 {
     if (!ps.want_blank)
@@ -469,10 +479,7 @@ process_lparen_or_lbracket(void)
        ps.nparen--;
     }
 
-    if (token.s[0] == '(' && ps.in_decl
-       && !ps.block_init && !ps.decl_indent_done &&
-       !ps.is_function_definition && ps.line_start_nparen == 0) {
-       /* function pointer declarations */
+    if (is_function_pointer_declaration()) {
        code_add_decl_indent(ps.decl_ind, ps.tabs_to_var);
        ps.decl_indent_done = true;
     } else if (want_blank_before_lparen())
@@ -499,7 +506,6 @@ process_lparen_or_lbracket(void)
        ps.init_or_struct = false;
     }
 
-    /* parenthesized type following sizeof or offsetof is not a cast */
     if (ps.prev_token == lsym_offsetof || ps.prev_token == lsym_sizeof)
        ps.paren[ps.nparen - 1].no_cast = true;
 }
@@ -854,14 +860,6 @@ process_ident(lexer_symbol lsym)
 }
 
 static void
-copy_token(void)
-{
-    if (ps.want_blank)
-       buf_add_char(&code, ' ');
-    buf_add_buf(&code, &token);
-}
-
-static void
 process_period(void)
 {
     if (code.e > code.s && code.e[-1] == ',')
@@ -888,11 +886,10 @@ process_comma(void)
     if (ps.nparen == 0) {
        if (ps.block_init_level <= 0)
            ps.block_init = false;
-       int varname_len = 8;    /* rough estimate for the length of a typical
-                                * variable name */
+       int typical_varname_length = 8;
        if (break_comma && (opt.break_after_comma ||
                ind_add(compute_code_indent(), code.s, code.e)
-               >= opt.max_line_length - varname_len))
+               >= opt.max_line_length - typical_varname_length))
            ps.force_nl = true;
     }
 }
@@ -903,12 +900,10 @@ read_preprocessing_line(void)
 {
     enum {
        PLAIN, STR, CHR, COMM
-    } state;
+    } state = PLAIN;
 
     buf_add_char(&lab, '#');
 
-    state = PLAIN;
-
     while (ch_isblank(inp_peek()))
        buf_add_char(&lab, inp_next());
 
@@ -1036,12 +1031,12 @@ main_loop(void)
     for (;;) {                 /* loop until we reach eof */
        lexer_symbol lsym = lexi();
 
+       if (lsym == lsym_eof)
+           return process_eof();
+
        if (lsym == lsym_if && ps.prev_token == lsym_else && opt.else_if)
            ps.force_nl = false;
 
-       if (lsym == lsym_eof)
-           return process_eof();
-
        if (lsym == lsym_newline || lsym == lsym_form_feed ||
                lsym == lsym_preprocessing)
            ps.force_nl = false;
@@ -1153,7 +1148,9 @@ main_loop(void)
        case lsym_return:
            process_ident(lsym);
     copy_token:
-           copy_token();
+           if (ps.want_blank)
+               buf_add_char(&code, ' ');
+           buf_add_buf(&code, &token);
            if (lsym != lsym_funcname)
                ps.want_blank = true;
            break;
diff -r ed72fe20cb1a -r 6b2b2fca29de usr.bin/indent/indent.h
--- a/usr.bin/indent/indent.h   Sun May 14 11:04:56 2023 +0000
+++ b/usr.bin/indent/indent.h   Sun May 14 11:29:23 2023 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: indent.h,v 1.128 2023/05/13 15:34:22 rillig Exp $      */
+/*     $NetBSD: indent.h,v 1.129 2023/05/14 11:29:23 rillig Exp $      */
 
 /*-
  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
@@ -188,13 +188,12 @@ extern struct options {
                                 * "for (e; e; e)" should be indented an extra
                                 * tab stop so that they don't conflict with
                                 * the code that follows */
-    bool else_if;              /* whether else-if pairs should be handled
-                                * specially */
+    bool else_if;              /* whether else-if pairs use the same line */
     bool function_brace_split; /* split function declaration and brace onto
                                 * separate lines */
     bool format_col1_comments; /* If comments which start in column 1 are to
-                                * be magically reformatted (just like
-                                * comments that begin in later columns) */
+                                * be reformatted (just like comments that
+                                * begin in later columns) */
     bool format_block_comments;        /* whether comments beginning with '/ * \n'
                                 * are to be reformatted */
     bool indent_parameters;
@@ -247,11 +246,26 @@ typedef struct paren_level_props {
                                 * form a type cast */
 } paren_level_props;
 
+/*
+ * The parser state determines the layout of the formatted text.
+ *
+ * In a function body, the number of block braces determines the indentation
+ * of statements and declarations.
+ *
+ * In a statement, the number of parentheses or brackets determines the
+ * indentation of follow-up lines.
+ *
+ * In an expression, the token type determine whether to put spaces around.
+ *
+ * In a source file, the types of line determine the vertical spacing, such as
+ * around preprocessing directives or function bodies, or above block
+ * comments.
+ */
 extern struct parser_state {
     lexer_symbol prev_token;   /* the previous token, but never comment,
                                 * newline or preprocessing line */
     bool curr_col_1;           /* whether the current token started in column
-                                * 1 of the unformatted input */
+                                * 1 of the original input */
     bool next_col_1;
     bool next_unary;           /* whether the following operator should be
                                 * unary; is used in declarations for '*', as
@@ -263,9 +277,8 @@ extern struct parser_state {
                                 * prefixed by a blank. (Said prefixing is
                                 * ignored in some cases.) */
 
-    bool force_nl;             /* when true, the following token goes to the
-                                * next line, unless it is a '{' and
-                                * opt.brace_same_line is set. */



Home | Main Index | Thread Index | Old Index