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: remove buggy code for swapping tokens



details:   https://anonhg.NetBSD.org/src/rev/079efbdf5047
branches:  trunk
changeset: 374751:079efbdf5047
user:      rillig <rillig%NetBSD.org@localhost>
date:      Thu May 11 09:28:53 2023 +0000

description:
indent: remove buggy code for swapping tokens

It is not the job of an indenter to swap tokens, even if it's only about
placing comments elsewhere.  The code that swapped the tokens was
complicated, buggy and impossible to understand.

In -br (brace right) mode, indent no longer moves a '{' from the
beginning of a line to the end of the previous line, as that was handled
by the token swapping code as well.  This change is unintended, but it
will be easier to re-add that now that the code is simpler.

diffstat:

 tests/usr.bin/indent/fmt_block.c              |   17 +-
 tests/usr.bin/indent/fmt_decl.c               |   14 +-
 tests/usr.bin/indent/fmt_else_comment.c       |    7 +-
 tests/usr.bin/indent/label.c                  |    5 +-
 tests/usr.bin/indent/lsym_comment.c           |    7 +-
 tests/usr.bin/indent/lsym_form_feed.c         |    6 +-
 tests/usr.bin/indent/lsym_period.c            |   12 +-
 tests/usr.bin/indent/opt_bl_br.c              |   87 +++++++---
 tests/usr.bin/indent/opt_ce.c                 |   50 ++++-
 tests/usr.bin/indent/opt_d.c                  |    4 +-
 tests/usr.bin/indent/opt_sob.c                |   55 +------
 tests/usr.bin/indent/opt_v.c                  |    5 +-
 tests/usr.bin/indent/psym_if_expr_stmt_else.c |   26 +--
 tests/usr.bin/indent/t_errors.sh              |   17 +-
 tests/usr.bin/indent/t_misc.sh                |   13 +-
 tests/usr.bin/indent/t_options.awk            |    5 +-
 usr.bin/indent/indent.c                       |  204 +-------------------------
 usr.bin/indent/indent.h                       |    8 +-
 usr.bin/indent/lexi.c                         |    6 +-
 usr.bin/indent/parse.c                        |   12 +-
 20 files changed, 176 insertions(+), 384 deletions(-)

diffs (truncated from 1037 to 300 lines):

diff -r 88e2d6a58d02 -r 079efbdf5047 tests/usr.bin/indent/fmt_block.c
--- a/tests/usr.bin/indent/fmt_block.c  Thu May 11 08:49:41 2023 +0000
+++ b/tests/usr.bin/indent/fmt_block.c  Thu May 11 09:28:53 2023 +0000
@@ -1,4 +1,4 @@
-/* $NetBSD: fmt_block.c,v 1.4 2022/04/24 09:04:12 rillig Exp $ */
+/* $NetBSD: fmt_block.c,v 1.5 2023/05/11 09:28:53 rillig Exp $ */
 
 /*
  * Tests for formatting blocks of statements and declarations.
@@ -23,20 +23,7 @@ function(void)
 }
 //indent end
 
-//indent run
-void
-function(void)
-{
-       if (true) {
-
-/* $ FIXME: indent must not merge these braces. */
-       } {
-/* $ FIXME: the following empty line was not in the input. */
-
-               print("block");
-       }
-}
-//indent end
+//indent run-equals-input
 
 
 /*
diff -r 88e2d6a58d02 -r 079efbdf5047 tests/usr.bin/indent/fmt_decl.c
--- a/tests/usr.bin/indent/fmt_decl.c   Thu May 11 08:49:41 2023 +0000
+++ b/tests/usr.bin/indent/fmt_decl.c   Thu May 11 09:28:53 2023 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: fmt_decl.c,v 1.36 2022/04/24 10:36:37 rillig Exp $     */
+/*     $NetBSD: fmt_decl.c,v 1.37 2023/05/11 09:28:53 rillig Exp $     */
 
 /*
  * Tests for declarations of global variables, external functions, and local
@@ -55,7 +55,8 @@ static const struct
 //indent end
 
 //indent run
-static const struct {
+static const struct
+{
        double          x;
        double          y, z;
 }              n[m + 1] =
@@ -78,7 +79,8 @@ typedef struct Complex
 //indent end
 
 //indent run
-typedef struct Complex {
+typedef struct Complex
+{
        double          x;
        double          y;
 }              Complex;
@@ -528,7 +530,8 @@ function_name_____20________30________40
 /* FIXME: The space between '){' is missing. */
 //indent run
 int           *function_name_____20________30________40________50
-               (void){
+               (void)
+{
 }
 //indent end
 
@@ -546,7 +549,8 @@ aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
 
 //indent run
 int           *aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
-               (void){
+               (void)
+{
 }
 //indent end
 
diff -r 88e2d6a58d02 -r 079efbdf5047 tests/usr.bin/indent/fmt_else_comment.c
--- a/tests/usr.bin/indent/fmt_else_comment.c   Thu May 11 08:49:41 2023 +0000
+++ b/tests/usr.bin/indent/fmt_else_comment.c   Thu May 11 09:28:53 2023 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: fmt_else_comment.c,v 1.4 2022/04/24 09:04:12 rillig Exp $      */
+/*     $NetBSD: fmt_else_comment.c,v 1.5 2023/05/11 09:28:53 rillig Exp $      */
 
 /*
  * Tests for comments after 'if (expr)' and 'else'. If the option '-br' is
@@ -95,7 +95,10 @@ t(void)
        if (1)
        {
                int             a;
-       } else if (0)
+       }
+
+
+       else if (0)
        {
                int             b;
        }
diff -r 88e2d6a58d02 -r 079efbdf5047 tests/usr.bin/indent/label.c
--- a/tests/usr.bin/indent/label.c      Thu May 11 08:49:41 2023 +0000
+++ b/tests/usr.bin/indent/label.c      Thu May 11 09:28:53 2023 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: label.c,v 1.4 2022/04/24 09:04:12 rillig Exp $ */
+/*     $NetBSD: label.c,v 1.5 2023/05/11 09:28:53 rillig Exp $ */
 
 /* See FreeBSD r303489 */
 
@@ -20,7 +20,8 @@ U: ;
 void
 t(void)
 {
-       switch (1) {
+       switch (1)
+       {
        case 1:                 /* test */
        case 2:                 /* test */
        }
diff -r 88e2d6a58d02 -r 079efbdf5047 tests/usr.bin/indent/lsym_comment.c
--- a/tests/usr.bin/indent/lsym_comment.c       Thu May 11 08:49:41 2023 +0000
+++ b/tests/usr.bin/indent/lsym_comment.c       Thu May 11 09:28:53 2023 +0000
@@ -1,4 +1,4 @@
-/* $NetBSD: lsym_comment.c,v 1.4 2022/04/24 10:36:37 rillig Exp $ */
+/* $NetBSD: lsym_comment.c,v 1.5 2023/05/11 09:28:53 rillig Exp $ */
 
 /*
  * Tests for the token lsym_comment, which starts a comment.
@@ -340,7 +340,7 @@ tab1+++     tab2--- tab3+++ tab4--- tab5+++ 
 /* The comment in the output has moved to the right of the '{'. */
 //indent run
 {
-       if (0) {                /* comment */
+       if (0) /* comment */ {
        }
 }
 //indent end
@@ -729,8 +729,7 @@ while(cond)/*comment*/;
 void
 loop(void)
 {
-       while (cond)            /* comment */
-               ;
+       while (cond) /* comment */ ;
 
        while (cond)
 /* $ XXX: The spaces around the comment look unintentional. */
diff -r 88e2d6a58d02 -r 079efbdf5047 tests/usr.bin/indent/lsym_form_feed.c
--- a/tests/usr.bin/indent/lsym_form_feed.c     Thu May 11 08:49:41 2023 +0000
+++ b/tests/usr.bin/indent/lsym_form_feed.c     Thu May 11 09:28:53 2023 +0000
@@ -1,4 +1,4 @@
-/* $NetBSD: lsym_form_feed.c,v 1.4 2022/04/24 10:36:37 rillig Exp $ */
+/* $NetBSD: lsym_form_feed.c,v 1.5 2023/05/11 09:28:53 rillig Exp $ */
 
 /*
  * Tests for the token lsym_form_feed, which represents a form feed, a special
@@ -40,9 +40,11 @@ void function(void)
 void
 function(void)
 {
-       if (expr) {
+       if (expr)
                /* $ XXX: The form feed has disappeared. */
+
                /* <-- form feed */
+       {
        }
 }
 //indent end
diff -r 88e2d6a58d02 -r 079efbdf5047 tests/usr.bin/indent/lsym_period.c
--- a/tests/usr.bin/indent/lsym_period.c        Thu May 11 08:49:41 2023 +0000
+++ b/tests/usr.bin/indent/lsym_period.c        Thu May 11 09:28:53 2023 +0000
@@ -1,4 +1,4 @@
-/* $NetBSD: lsym_period.c,v 1.4 2022/04/24 09:04:12 rillig Exp $ */
+/* $NetBSD: lsym_period.c,v 1.5 2023/05/11 09:28:53 rillig Exp $ */
 
 /*
  * Tests for the token lsym_period, which represents '.' in these contexts:
@@ -42,20 +42,14 @@ get_time(struct stat st)
 
 //indent run
 time_t
-/* $ FIXME: The '{' must be in the next line. */
-get_time(struct stat st){
-       return st.st_mtime > 0 ? st.st_atime : st.st_ctime;
-}
-//indent end
-
-//indent run -Ttime_t
-time_t
 get_time(struct stat st)
 {
        return st.st_mtime > 0 ? st.st_atime : st.st_ctime;
 }
 //indent end
 
+//indent run-equals-prev-output -Ttime_t
+
 
 /* Varargs in a function declaration */
 //indent input
diff -r 88e2d6a58d02 -r 079efbdf5047 tests/usr.bin/indent/opt_bl_br.c
--- a/tests/usr.bin/indent/opt_bl_br.c  Thu May 11 08:49:41 2023 +0000
+++ b/tests/usr.bin/indent/opt_bl_br.c  Thu May 11 09:28:53 2023 +0000
@@ -1,23 +1,26 @@
-/* $NetBSD: opt_bl_br.c,v 1.6 2022/04/24 09:04:12 rillig Exp $ */
+/* $NetBSD: opt_bl_br.c,v 1.7 2023/05/11 09:28:53 rillig Exp $ */
 
 //indent input
 void
-example(int n)
+standard_style(int n)
 {
-       if (n > 99) { print("large"); }
-       else if (n > 9) { print("double-digit"); }
-       else if (n > 0) print("positive");
-       else { print("negative"); }
+       if (n > 99) {
+               print("large");
+       } else if (n > 9) {
+               print("double-digit");
+       } else if (n > 0)
+               print("positive");
+       else {
+               print("negative");
+       }
 }
 //indent end
 
-/*
- * XXX: The '} else' looks strange in this style since the '}' is not on a
- * line of its own.
- */
+//indent run-equals-input -br
+
 //indent run -bl
 void
-example(int n)
+standard_style(int n)
 {
        if (n > 99)
        {
@@ -34,15 +37,54 @@ example(int n)
 }
 //indent end
 
+
+/*
+ * In this very condensed style, the additional newline between '}' and 'else'
+ * is kept.
+ */
+//indent input
+void
+example(int n)
+{
+       if (n > 99) { print("large"); }
+       else if (n > 9) { print("double-digit"); }
+       else if (n > 0) print("positive");
+       else { print("negative"); }
+}
+//indent end
+
+//indent run -bl
+void
+example(int n)
+{
+       if (n > 99)
+       {
+               print("large");
+       }
+       else if (n > 9)
+       {
+               print("double-digit");
+       }
+       else if (n > 0)
+               print("positive");
+       else
+       {
+               print("negative");
+       }
+}
+//indent end
+
 //indent run -br
 void
 example(int n)
 {
        if (n > 99) {
                print("large");
-       } else if (n > 9) {
+       }
+       else if (n > 9) {
                print("double-digit");
-       } else if (n > 0)
+       }
+       else if (n > 0)
                print("positive");
        else {
                print("negative");



Home | Main Index | Thread Index | Old Index