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 the code, add a few tests



details:   https://anonhg.NetBSD.org/src/rev/841a1e0e9d6b
branches:  trunk
changeset: 376380:841a1e0e9d6b
user:      rillig <rillig%NetBSD.org@localhost>
date:      Wed Jun 14 20:46:08 2023 +0000

description:
indent: clean up the code, add a few tests

diffstat:

 tests/usr.bin/indent/fmt_block.c          |    3 +-
 tests/usr.bin/indent/fmt_decl.c           |  103 +++++++++++++++++++++++++++++-
 tests/usr.bin/indent/lsym_preprocessing.c |   37 ++++++++++-
 tests/usr.bin/indent/opt_bc.c             |   13 +++-
 tests/usr.bin/indent/ps_ind_level.c       |   17 +++-
 tests/usr.bin/indent/psym_rbrace.c        |   79 ++++++++++++++++++++++-
 usr.bin/indent/debug.c                    |    6 +-
 usr.bin/indent/indent.c                   |   99 +++++++++++++---------------
 usr.bin/indent/indent.h                   |   16 ++--
 usr.bin/indent/parse.c                    |   23 +++---
 10 files changed, 310 insertions(+), 86 deletions(-)

diffs (truncated from 753 to 300 lines):

diff -r 575816d554a2 -r 841a1e0e9d6b tests/usr.bin/indent/fmt_block.c
--- a/tests/usr.bin/indent/fmt_block.c  Wed Jun 14 19:05:40 2023 +0000
+++ b/tests/usr.bin/indent/fmt_block.c  Wed Jun 14 20:46:08 2023 +0000
@@ -1,4 +1,4 @@
-/* $NetBSD: fmt_block.c,v 1.7 2023/05/22 23:03:16 rillig Exp $ */
+/* $NetBSD: fmt_block.c,v 1.8 2023/06/14 20:46:08 rillig Exp $ */
 
 /*
  * Tests for formatting blocks of statements and declarations.
@@ -6,7 +6,6 @@
  * See also:
  *     lsym_lbrace.c
  *     psym_stmt.c
- *     psym_stmt_list.c
  */
 
 //indent input
diff -r 575816d554a2 -r 841a1e0e9d6b tests/usr.bin/indent/fmt_decl.c
--- a/tests/usr.bin/indent/fmt_decl.c   Wed Jun 14 19:05:40 2023 +0000
+++ b/tests/usr.bin/indent/fmt_decl.c   Wed Jun 14 20:46:08 2023 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: fmt_decl.c,v 1.54 2023/06/10 17:56:29 rillig Exp $     */
+/*     $NetBSD: fmt_decl.c,v 1.55 2023/06/14 20:46:08 rillig Exp $     */
 
 /*
  * Tests for declarations of global variables, external functions, and local
@@ -1085,3 +1085,104 @@ die(void)
 {
 }
 //indent end
+
+
+/*
+ * In very rare cases, the type of a declarator might include literal tab
+ * characters. This tab might affect the indentation of the declarator, but
+ * only if it occurs before the declarator, and that is hard to achieve.
+ */
+//indent input
+int            arr[sizeof "    "];
+//indent end
+
+//indent run-equals-input
+
+
+/*
+ * The '}' of an initializer is not supposed to end the statement, it only ends
+ * the brace level of the initializer expression.
+ */
+//indent input
+int multi_line[1][1][1] = {
+{
+{
+1
+},
+},
+};
+int single_line[2][1][1] = {{{1},},{{2}}};
+//indent end
+
+//indent run -di0
+int multi_line[1][1][1] = {
+       {
+               {
+                       1
+               },
+       },
+};
+int single_line[2][1][1] = {{{1},}, {{2}}};
+//indent end
+
+
+/*
+ * The '}' of an initializer is not supposed to end the statement, it only ends
+ * the brace level of the initializer expression.
+ */
+//indent input
+{
+int multi_line = {
+{
+{
+b
+},
+},
+};
+int single_line = {{{b},},{}};
+}
+//indent end
+
+//indent run -di0
+{
+       int multi_line = {
+               {
+                       {
+                               b
+                       },
+               },
+       };
+       int single_line = {{{b},}, {}};
+}
+//indent end
+
+
+/*
+ * In initializers, multi-line expressions don't have their second line
+ * indented, even though they should.
+ */
+//indent input
+{
+multi_line = (int[]){
+{1
++1},
+{1
++1},
+{1
++1},
+};
+}
+//indent end
+
+//indent run
+{
+       multi_line = (int[]){
+               {1
+               + 1},
+               {1
+               + 1},
+               {1
+               + 1},
+       };
+}
+//indent end
diff -r 575816d554a2 -r 841a1e0e9d6b tests/usr.bin/indent/lsym_preprocessing.c
--- a/tests/usr.bin/indent/lsym_preprocessing.c Wed Jun 14 19:05:40 2023 +0000
+++ b/tests/usr.bin/indent/lsym_preprocessing.c Wed Jun 14 20:46:08 2023 +0000
@@ -1,4 +1,4 @@
-/* $NetBSD: lsym_preprocessing.c,v 1.13 2023/06/14 17:07:32 rillig Exp $ */
+/* $NetBSD: lsym_preprocessing.c,v 1.14 2023/06/14 20:46:08 rillig Exp $ */
 
 /*
  * Tests for the token lsym_preprocessing, which represents a '#' that starts
@@ -307,3 +307,38 @@ error: Standard Input:2: Unmatched #elif
 error: Standard Input:3: Unmatched #elifdef
 error: Standard Input:4: Unmatched #endif
 //indent end
+
+
+/*
+ * The '#' can only occur at the beginning of a line, therefore indent does not
+ * care when it occurs in the middle of a line.
+ */
+//indent input
+int no = #;
+//indent end
+
+//indent run -di0
+int no =
+#;
+//indent end
+
+
+/*
+ * Preprocessing directives may be indented; indent moves them to the beginning
+ * of a line.
+ */
+//indent input
+#if 0
+       #if 1 \
+        || 2
+       #endif
+#endif
+//indent end
+
+//indent run
+#if 0
+#if 1 \
+        || 2
+#endif
+#endif
+//indent end
diff -r 575816d554a2 -r 841a1e0e9d6b tests/usr.bin/indent/opt_bc.c
--- a/tests/usr.bin/indent/opt_bc.c     Wed Jun 14 19:05:40 2023 +0000
+++ b/tests/usr.bin/indent/opt_bc.c     Wed Jun 14 20:46:08 2023 +0000
@@ -1,4 +1,4 @@
-/* $NetBSD: opt_bc.c,v 1.11 2023/06/14 14:11:28 rillig Exp $ */
+/* $NetBSD: opt_bc.c,v 1.12 2023/06/14 20:46:08 rillig Exp $ */
 
 /*
  * Tests for the options '-bc' and '-nbc'.
@@ -121,6 +121,17 @@ int                a = (1),
 //indent end
 
 
+//indent input
+int a,
+b,
+c;
+//indent end
+
+//indent run -nbc -di0
+int a, b, c;
+//indent end
+
+
 /*
  * When declarations are too long to fit in a single line, they should not be
  * joined.
diff -r 575816d554a2 -r 841a1e0e9d6b tests/usr.bin/indent/ps_ind_level.c
--- a/tests/usr.bin/indent/ps_ind_level.c       Wed Jun 14 19:05:40 2023 +0000
+++ b/tests/usr.bin/indent/ps_ind_level.c       Wed Jun 14 20:46:08 2023 +0000
@@ -1,10 +1,13 @@
-/* $NetBSD: ps_ind_level.c,v 1.7 2023/05/15 14:55:47 rillig Exp $ */
+/* $NetBSD: ps_ind_level.c,v 1.8 2023/06/14 20:46:08 rillig Exp $ */
 
 /*
  * The indentation of the very first line of a file determines the
- * indentation of the remaining code. Even if later code has a smaller
- * indentation, it is nevertheless indented to the level given by the first
- * line of code.
+ * indentation of the remaining code. This mode is meant for code snippets from
+ * function bodies. At this level, function definitions are not recognized
+ * properly.
+ *
+ * Even if later code has a smaller indentation, it is nevertheless indented to
+ * the level given by the first line of code.
  *
  * In this particular test, the indentation is set to 5 and the tabulator
  * width is set to 8, to demonstrate an off-by-one error in
@@ -18,6 +21,9 @@
                        int indented_by_24;
 
 void function_in_column_1(void){}
+
+                       #if indented
+#endif
 //indent end
 
 /* 5 spaces indentation, 8 spaces per tabulator */
@@ -26,6 +32,9 @@ void function_in_column_1(void){}
 
                    void            function_in_column_1(void) {
                    }
+
+#if indented
+#endif
 //indent end
 
 
diff -r 575816d554a2 -r 841a1e0e9d6b tests/usr.bin/indent/psym_rbrace.c
--- a/tests/usr.bin/indent/psym_rbrace.c        Wed Jun 14 19:05:40 2023 +0000
+++ b/tests/usr.bin/indent/psym_rbrace.c        Wed Jun 14 20:46:08 2023 +0000
@@ -1,4 +1,4 @@
-/* $NetBSD: psym_rbrace.c,v 1.3 2022/04/24 09:04:12 rillig Exp $ */
+/* $NetBSD: psym_rbrace.c,v 1.4 2023/06/14 20:46:08 rillig Exp $ */
 
 /*
  * Tests for the parser symbol psym_rbrace, which represents '}' and finishes
@@ -8,8 +8,81 @@
  *     psym_lbrace.c
  */
 
+
+/*
+ * While it is a syntax error to have an unfinished declaration between braces,
+ * indent is forgiving enough to accept this input.
+ */
 //indent input
-// TODO: add input
+{
+       int
+}
+//indent end
+
+//indent run
+{
+       int
+       }
+exit 1
+error: Standard Input:3: Statement nesting error
+error: Standard Input:3: Stuff missing from end of file
+//indent end
+
+
+//indent input
+{
+       do {
+       } while (cond)
+}
+//indent end
+
+// XXX: Why doesn't indent complain about the missing semicolon?
+//indent run-equals-input
+
+
+//indent input
+{
+       if (cond)
+}
 //indent end
 
-//indent run-equals-input



Home | Main Index | Thread Index | Old Index