Module Name:    src
Committed By:   rillig
Date:           Mon Jun 26 14:54:40 UTC 2023

Modified Files:
        src/tests/usr.bin/indent: opt_bad.c
        src/usr.bin/indent: indent.c io.c

Log Message:
indent: in -bad mode, don't add a blank line above a comment or '}'


To generate a diff of this commit:
cvs rdiff -u -r1.11 -r1.12 src/tests/usr.bin/indent/opt_bad.c
cvs rdiff -u -r1.384 -r1.385 src/usr.bin/indent/indent.c
cvs rdiff -u -r1.229 -r1.230 src/usr.bin/indent/io.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/tests/usr.bin/indent/opt_bad.c
diff -u src/tests/usr.bin/indent/opt_bad.c:1.11 src/tests/usr.bin/indent/opt_bad.c:1.12
--- src/tests/usr.bin/indent/opt_bad.c:1.11	Sat Jun 17 22:09:24 2023
+++ src/tests/usr.bin/indent/opt_bad.c	Mon Jun 26 14:54:40 2023
@@ -1,4 +1,4 @@
-/* $NetBSD: opt_bad.c,v 1.11 2023/06/17 22:09:24 rillig Exp $ */
+/* $NetBSD: opt_bad.c,v 1.12 2023/06/26 14:54:40 rillig Exp $ */
 
 /*
  * Tests for the options '-bad' and '-nbad'.
@@ -12,14 +12,6 @@
 
 /* Test global declarations. */
 //indent input
-int global_variable;
-void function_declaration(void);
-#if 0
-#endif
-/* comment */
-//indent end
-
-//indent run -bad
 int		global_variable;
 void		function_declaration(void);
 #if 0
@@ -27,7 +19,9 @@ void		function_declaration(void);
 /* comment */
 //indent end
 
-//indent run-equals-prev-output -nbad
+//indent run-equals-input -bad
+
+//indent run-equals-input -nbad
 
 
 /* See FreeBSD r303599. */
@@ -110,8 +104,10 @@ comments(void)
 {
 	int local_var_1;	/* comment */
 	int local_var_2;	/* comment */
-
+// $ Indent does not look ahead much, so it doesn't know whether this comment
+// $ will be followed by a declaration or a statement.
 	/* comment line */
+
 	function_call();
 }
 //indent end
@@ -169,20 +165,17 @@ initializer_with_blank(void)
 
 //indent input
 {
-	int decl;
+	// $ The '}' in an initializer does not finish a declaration,
+	// $ only a semicolon does.
+	int decl1[2][2] = {
+		{1, 2},
+		{3, 4},
+	};
 	/* comment */
-	int decl;
+	int decl2;
+	// $ If the declaration is followed by a '}' that terminates the block
+	// $ statement, * there is no need for a blank line before the '}'.
 }
 //indent end
 
-//indent run -bad -di0
-{
-	int decl;
-// $ FIXME: This blank line is _between_ the declarations, not _after_ them.
-
-	/* comment */
-	int decl;
-// $ XXX: This blank line is unnecessary, it doesn't occur in practice, though.
-
-}
-//indent end
+//indent run-equals-input -bad -di0

Index: src/usr.bin/indent/indent.c
diff -u src/usr.bin/indent/indent.c:1.384 src/usr.bin/indent/indent.c:1.385
--- src/usr.bin/indent/indent.c:1.384	Sun Jun 25 19:35:45 2023
+++ src/usr.bin/indent/indent.c	Mon Jun 26 14:54:40 2023
@@ -1,4 +1,4 @@
-/*	$NetBSD: indent.c,v 1.384 2023/06/25 19:35:45 rillig Exp $	*/
+/*	$NetBSD: indent.c,v 1.385 2023/06/26 14:54:40 rillig Exp $	*/
 
 /*-
  * SPDX-License-Identifier: BSD-4-Clause
@@ -38,7 +38,7 @@
  */
 
 #include <sys/cdefs.h>
-__RCSID("$NetBSD: indent.c,v 1.384 2023/06/25 19:35:45 rillig Exp $");
+__RCSID("$NetBSD: indent.c,v 1.385 2023/06/26 14:54:40 rillig Exp $");
 
 #include <sys/param.h>
 #include <err.h>
@@ -767,6 +767,8 @@ process_rbrace(void)
 	}
 
 	ps.declaration = decl_no;
+	if (ps.decl_level == 0)
+		ps.blank_line_after_decl = false;
 	if (ps.init_level > 0)
 		ps.init_level--;
 

Index: src/usr.bin/indent/io.c
diff -u src/usr.bin/indent/io.c:1.229 src/usr.bin/indent/io.c:1.230
--- src/usr.bin/indent/io.c:1.229	Sat Jun 17 23:03:20 2023
+++ src/usr.bin/indent/io.c	Mon Jun 26 14:54:40 2023
@@ -1,4 +1,4 @@
-/*	$NetBSD: io.c,v 1.229 2023/06/17 23:03:20 rillig Exp $	*/
+/*	$NetBSD: io.c,v 1.230 2023/06/26 14:54:40 rillig Exp $	*/
 
 /*-
  * SPDX-License-Identifier: BSD-4-Clause
@@ -38,7 +38,7 @@
  */
 
 #include <sys/cdefs.h>
-__RCSID("$NetBSD: io.c,v 1.229 2023/06/17 23:03:20 rillig Exp $");
+__RCSID("$NetBSD: io.c,v 1.230 2023/06/26 14:54:40 rillig Exp $");
 
 #include <stdio.h>
 
@@ -171,7 +171,8 @@ want_blank_line(void)
 	debug_println("%s: %s -> %s", __func__,
 	    line_kind_name[out.prev_line_kind], line_kind_name[out.line_kind]);
 
-	if (ps.blank_line_after_decl && ps.declaration == decl_no) {
+	if (ps.blank_line_after_decl && ps.declaration == decl_no
+	    && (lab.len > 0 || code.len > 0)) {
 		ps.blank_line_after_decl = false;
 		return true;
 	}

Reply via email to