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/52303adeceb5
branches:  trunk
changeset: 376324:52303adeceb5
user:      rillig <rillig%NetBSD.org@localhost>
date:      Sat Jun 10 16:43:55 2023 +0000

description:
indent: miscellaneous cleanups

diffstat:

 tests/usr.bin/indent/lsym_comment.c |   24 +++++-
 tests/usr.bin/indent/lsym_if.c      |   24 +++++-
 tests/usr.bin/indent/lsym_switch.c  |   21 ++++-
 usr.bin/indent/args.c               |   14 +-
 usr.bin/indent/debug.c              |   10 +-
 usr.bin/indent/indent.c             |   83 ++++++++++----------
 usr.bin/indent/indent.h             |   51 ++++++------
 usr.bin/indent/io.c                 |   34 ++++----
 usr.bin/indent/lexi.c               |  141 +++++++++++++++--------------------
 usr.bin/indent/parse.c              |  130 +++++++++++++++-----------------
 usr.bin/indent/pr_comment.c         |    6 +-
 11 files changed, 283 insertions(+), 255 deletions(-)

diffs (truncated from 1157 to 300 lines):

diff -r 7369559e1063 -r 52303adeceb5 tests/usr.bin/indent/lsym_comment.c
--- a/tests/usr.bin/indent/lsym_comment.c       Sat Jun 10 16:08:41 2023 +0000
+++ b/tests/usr.bin/indent/lsym_comment.c       Sat Jun 10 16:43:55 2023 +0000
@@ -1,4 +1,4 @@
-/* $NetBSD: lsym_comment.c,v 1.19 2023/06/10 06:38:21 rillig Exp $ */
+/* $NetBSD: lsym_comment.c,v 1.20 2023/06/10 16:43:56 rillig Exp $ */
 
 /*
  * Tests for the token lsym_comment, which starts a comment.
@@ -32,7 +32,7 @@
  * - block/end-of-line comment to the right of code
  * - block/end-of-line comment to the right of label with code
  *
- * - with/without opt.comment_delimiter_on_blankline (-cdb)
+ * - with/without opt.comment_delimiter_on_blank_line (-cdb)
  * - with/without opt.star_comment_cont (-sc)
  * - with/without opt.format_block_comments (-fbc)
  * - with varying opt.max_line_length (32, 64, 80, 140)
@@ -1111,3 +1111,23 @@ a>b;
        a > b;
 }
 //indent end
+
+
+/*
+ * Line comments are only related to a code snippet if they are on the same
+ * line; they cannot be continued in the next lines.
+ */
+//indent input
+int line;      // comment line 1
+               // comment line 2
+int block;     /* comment line 1
+                * comment line 2
+                */
+//indent end
+
+//indent run -di0
+int line;                      // comment line 1
+// $ XXX: This comment was probably intended to continue 'comment line 1'.
+// comment line 2
+int block;                     /* comment line 1 comment line 2 */
+//indent end
diff -r 7369559e1063 -r 52303adeceb5 tests/usr.bin/indent/lsym_if.c
--- a/tests/usr.bin/indent/lsym_if.c    Sat Jun 10 16:08:41 2023 +0000
+++ b/tests/usr.bin/indent/lsym_if.c    Sat Jun 10 16:43:55 2023 +0000
@@ -1,4 +1,4 @@
-/* $NetBSD: lsym_if.c,v 1.4 2022/04/24 10:36:37 rillig Exp $ */
+/* $NetBSD: lsym_if.c,v 1.5 2023/06/10 16:43:56 rillig Exp $ */
 
 /*
  * Tests for the token lsym_if, which represents the keyword 'if' that starts
@@ -21,3 +21,25 @@ function(void)
                stmt();
 }
 //indent end
+
+
+/*
+ * After an 'if' statement without an 'else' branch, braces start a separate
+ * block.
+ */
+//indent input
+{
+       if(0)if(1)if(2)stmt();{}
+}
+//indent end
+
+//indent run
+{
+       if (0)
+               if (1)
+                       if (2)
+// $ FIXME: The '{' must be on a separate line, with indentation 8.
+                               stmt(); {
+                               }
+}
+//indent end
diff -r 7369559e1063 -r 52303adeceb5 tests/usr.bin/indent/lsym_switch.c
--- a/tests/usr.bin/indent/lsym_switch.c        Sat Jun 10 16:08:41 2023 +0000
+++ b/tests/usr.bin/indent/lsym_switch.c        Sat Jun 10 16:43:55 2023 +0000
@@ -1,4 +1,4 @@
-/* $NetBSD: lsym_switch.c,v 1.3 2022/04/24 09:04:12 rillig Exp $ */
+/* $NetBSD: lsym_switch.c,v 1.4 2023/06/10 16:43:56 rillig Exp $ */
 
 /*
  * Tests for the token lsym_switch, which represents the keyword 'switch' that
@@ -9,8 +9,23 @@
  *     C11 6.8.4.2             "The 'switch' statement"
  */
 
+// TODO: Add systematic tests.
+
+/*
+ * Ensure that an unfinished 'switch' statement does not eat comments.
+ */
 //indent input
-// TODO: add input
+{
+       switch (expr) // comment
+       {
+       }
+}
 //indent end
 
-//indent run-equals-input
+//indent run
+{
+// $ FIXME: The '{' has moved to the comment.
+       switch (expr) // comment {
+       }
+}
+//indent end
diff -r 7369559e1063 -r 52303adeceb5 usr.bin/indent/args.c
--- a/usr.bin/indent/args.c     Sat Jun 10 16:08:41 2023 +0000
+++ b/usr.bin/indent/args.c     Sat Jun 10 16:43:55 2023 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: args.c,v 1.82 2023/06/05 10:12:21 rillig Exp $ */
+/*     $NetBSD: args.c,v 1.83 2023/06/10 16:43:55 rillig Exp $ */
 
 /*-
  * SPDX-License-Identifier: BSD-4-Clause
@@ -38,7 +38,7 @@
  */
 
 #include <sys/cdefs.h>
-__RCSID("$NetBSD: args.c,v 1.82 2023/06/05 10:12:21 rillig Exp $");
+__RCSID("$NetBSD: args.c,v 1.83 2023/06/10 16:43:55 rillig Exp $");
 
 /* Read options from profile files and from the command line. */
 
@@ -73,18 +73,18 @@ static const struct pro {
        short i_max;
        void *p_var;            /* the associated variable */
 } pro[] = {
-       bool_options("bacc", blanklines_around_conditional_compilation),
+       bool_options("bacc", blank_line_around_conditional_compilation),
        bool_options("bad", blank_line_after_decl),
        bool_options("badp", blank_line_after_decl_at_top),
-       bool_options("bap", blanklines_after_procs),
-       bool_options("bbb", blanklines_before_block_comments),
+       bool_options("bap", blank_line_after_proc),
+       bool_options("bbb", blank_line_before_block_comment),
        bool_options("bc", break_after_comma),
        bool_option("bl", false, brace_same_line),
        bool_option("br", true, brace_same_line),
        bool_options("bs", blank_after_sizeof),
        int_option("c", comment_column, 1, 999),
        int_option("cd", decl_comment_column, 1, 999),
-       bool_options("cdb", comment_delimiter_on_blankline),
+       bool_options("cdb", comment_delimiter_on_blank_line),
        bool_options("ce", cuddle_else),
        int_option("ci", continuation_indent, 0, 999),
        /* "cli" is special */
@@ -109,7 +109,7 @@ static const struct pro {
        bool_options("pcs", proc_calls_space),
        bool_options("psl", procnames_start_line),
        bool_options("sc", star_comment_cont),
-       bool_options("sob", swallow_optional_blanklines),
+       bool_options("sob", swallow_optional_blank_lines),
        /* "st" is special */
        bool_option("ta", true, auto_typedefs),
        /* "T" is special */
diff -r 7369559e1063 -r 52303adeceb5 usr.bin/indent/debug.c
--- a/usr.bin/indent/debug.c    Sat Jun 10 16:08:41 2023 +0000
+++ b/usr.bin/indent/debug.c    Sat Jun 10 16:43:55 2023 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: debug.c,v 1.49 2023/06/10 13:03:17 rillig Exp $        */
+/*     $NetBSD: debug.c,v 1.50 2023/06/10 16:43:55 rillig Exp $        */
 
 /*-
  * Copyright (c) 2023 The NetBSD Foundation, Inc.
@@ -30,7 +30,7 @@
  */
 
 #include <sys/cdefs.h>
-__RCSID("$NetBSD: debug.c,v 1.49 2023/06/10 13:03:17 rillig Exp $");
+__RCSID("$NetBSD: debug.c,v 1.50 2023/06/10 16:43:55 rillig Exp $");
 
 #include <stdarg.h>
 #include <string.h>
@@ -264,7 +264,7 @@ ps_paren_has_changed(void)
        if (state.prev_ps.nparen != ps.nparen)
                return true;
 
-       const paren_level_props *prev = state.prev_ps.paren, *curr = ps.paren;
+       const struct paren_level *prev = state.prev_ps.paren, *curr = ps.paren;
        for (int i = 0; i < ps.nparen; i++)
                if (curr[i].indent != prev[i].indent
                    || curr[i].cast != prev[i].cast)
@@ -356,7 +356,7 @@ debug_parser_state(void)
        debug_ps_bool(tabs_to_var);
        debug_ps_enum(extra_expr_indent, extra_expr_indent_name);
 
-       // The parser symbol stack is printed in debug_parse_stack instead.
+       // The parser symbol stack is printed in debug_psyms_stack instead.
 
        state.heading = "spacing inside a statement or declaration";
        debug_ps_bool(next_unary);
@@ -388,7 +388,7 @@ debug_parser_state(void)
 }
 
 void
-debug_parse_stack(const char *situation)
+debug_psyms_stack(const char *situation)
 {
        debug_printf("parse stack %s:", situation);
        const struct psym_stack *psyms = &ps.psyms;
diff -r 7369559e1063 -r 52303adeceb5 usr.bin/indent/indent.c
--- a/usr.bin/indent/indent.c   Sat Jun 10 16:08:41 2023 +0000
+++ b/usr.bin/indent/indent.c   Sat Jun 10 16:43:55 2023 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: indent.c,v 1.353 2023/06/10 12:59:31 rillig Exp $      */
+/*     $NetBSD: indent.c,v 1.354 2023/06/10 16:43:55 rillig Exp $      */
 
 /*-
  * SPDX-License-Identifier: BSD-4-Clause
@@ -38,22 +38,20 @@
  */
 
 #include <sys/cdefs.h>
-__RCSID("$NetBSD: indent.c,v 1.353 2023/06/10 12:59:31 rillig Exp $");
+__RCSID("$NetBSD: indent.c,v 1.354 2023/06/10 16:43:55 rillig Exp $");
 
 #include <sys/param.h>
 #include <err.h>
-#include <fcntl.h>
 #include <stdarg.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
-#include <unistd.h>
 
 #include "indent.h"
 
 struct options opt = {
        .brace_same_line = true,
-       .comment_delimiter_on_blankline = true,
+       .comment_delimiter_on_blank_line = true,
        .cuddle_else = true,
        .comment_column = 33,
        .decl_indent = 16,
@@ -91,8 +89,8 @@ FILE *input;
 FILE *output;
 
 static const char *in_name = "Standard Input";
+static char backup_name[PATH_MAX];
 static const char *backup_suffix = ".BAK";
-static char bakfile[MAXPATHLEN] = "";
 
 
 void *
@@ -218,38 +216,36 @@ load_profiles(int argc, char **argv)
  * and the original input file the output.
  */
 static void
-bakcopy(void)
+copy_to_bak_file(void)
 {
-       ssize_t n;
-       int bak_fd;
-       char buff[8 * 1024];
+       size_t n;
+       char buff[BUFSIZ];
 
        const char *last_slash = strrchr(in_name, '/');
-       snprintf(bakfile, sizeof(bakfile), "%s%s",
-           last_slash != NULL ? last_slash + 1 : in_name, backup_suffix);
+       const char *base = last_slash != NULL ? last_slash + 1 : in_name;
+       snprintf(backup_name, sizeof(backup_name), "%s%s", base, backup_suffix);
 
-       /* copy in_name to backup file */
-       bak_fd = creat(bakfile, 0600);
-       if (bak_fd < 0)
-               err(1, "%s", bakfile);
+       /* copy the input file to the backup file */
+       FILE *bak = fopen(backup_name, "w");
+       if (bak == NULL)
+               err(1, "%s", backup_name);
 
-       while ((n = read(fileno(input), buff, sizeof(buff))) > 0)
-               if (write(bak_fd, buff, (size_t)n) != n)
-                       err(1, "%s", bakfile);
-       if (n < 0)
+       while ((n = fread(buff, 1, sizeof(buff), input)) > 0)
+               if (fwrite(buff, 1, n, bak) != n)
+                       err(1, "%s", backup_name);
+       if (fclose(input) != 0)
                err(1, "%s", in_name);
+       if (fclose(bak) != 0)
+               err(1, "%s", backup_name);
 
-       close(bak_fd);
-       (void)fclose(input);
-
-       /* re-open backup file as the input file */
-       input = fopen(bakfile, "r");
+       /* re-open the backup file as the input file */
+       input = fopen(backup_name, "r");
        if (input == NULL)
-               err(1, "%s", bakfile);



Home | Main Index | Thread Index | Old Index