Module Name:    src
Committed By:   rillig
Date:           Thu May 11 09:28:53 UTC 2023

Modified Files:
        src/tests/usr.bin/indent: fmt_block.c fmt_decl.c fmt_else_comment.c
            label.c lsym_comment.c lsym_form_feed.c lsym_period.c opt_bl_br.c
            opt_ce.c opt_d.c opt_sob.c opt_v.c psym_if_expr_stmt_else.c
            t_errors.sh t_misc.sh t_options.awk
        src/usr.bin/indent: indent.c indent.h lexi.c parse.c

Log Message:
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.


To generate a diff of this commit:
cvs rdiff -u -r1.4 -r1.5 src/tests/usr.bin/indent/fmt_block.c \
    src/tests/usr.bin/indent/fmt_else_comment.c \
    src/tests/usr.bin/indent/label.c src/tests/usr.bin/indent/lsym_comment.c \
    src/tests/usr.bin/indent/lsym_form_feed.c \
    src/tests/usr.bin/indent/lsym_period.c src/tests/usr.bin/indent/opt_d.c \
    src/tests/usr.bin/indent/psym_if_expr_stmt_else.c
cvs rdiff -u -r1.36 -r1.37 src/tests/usr.bin/indent/fmt_decl.c
cvs rdiff -u -r1.6 -r1.7 src/tests/usr.bin/indent/opt_bl_br.c \
    src/tests/usr.bin/indent/opt_ce.c
cvs rdiff -u -r1.5 -r1.6 src/tests/usr.bin/indent/opt_sob.c
cvs rdiff -u -r1.8 -r1.9 src/tests/usr.bin/indent/opt_v.c
cvs rdiff -u -r1.24 -r1.25 src/tests/usr.bin/indent/t_errors.sh
cvs rdiff -u -r1.20 -r1.21 src/tests/usr.bin/indent/t_misc.sh
cvs rdiff -u -r1.10 -r1.11 src/tests/usr.bin/indent/t_options.awk
cvs rdiff -u -r1.245 -r1.246 src/usr.bin/indent/indent.c
cvs rdiff -u -r1.112 -r1.113 src/usr.bin/indent/indent.h
cvs rdiff -u -r1.172 -r1.173 src/usr.bin/indent/lexi.c
cvs rdiff -u -r1.49 -r1.50 src/usr.bin/indent/parse.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/fmt_block.c
diff -u src/tests/usr.bin/indent/fmt_block.c:1.4 src/tests/usr.bin/indent/fmt_block.c:1.5
--- src/tests/usr.bin/indent/fmt_block.c:1.4	Sun Apr 24 09:04:12 2022
+++ src/tests/usr.bin/indent/fmt_block.c	Thu May 11 09:28:53 2023
@@ -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
 
 
 /*
Index: src/tests/usr.bin/indent/fmt_else_comment.c
diff -u src/tests/usr.bin/indent/fmt_else_comment.c:1.4 src/tests/usr.bin/indent/fmt_else_comment.c:1.5
--- src/tests/usr.bin/indent/fmt_else_comment.c:1.4	Sun Apr 24 09:04:12 2022
+++ src/tests/usr.bin/indent/fmt_else_comment.c	Thu May 11 09:28:53 2023
@@ -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;
 	}
Index: src/tests/usr.bin/indent/label.c
diff -u src/tests/usr.bin/indent/label.c:1.4 src/tests/usr.bin/indent/label.c:1.5
--- src/tests/usr.bin/indent/label.c:1.4	Sun Apr 24 09:04:12 2022
+++ src/tests/usr.bin/indent/label.c	Thu May 11 09:28:53 2023
@@ -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 */
 	}
Index: src/tests/usr.bin/indent/lsym_comment.c
diff -u src/tests/usr.bin/indent/lsym_comment.c:1.4 src/tests/usr.bin/indent/lsym_comment.c:1.5
--- src/tests/usr.bin/indent/lsym_comment.c:1.4	Sun Apr 24 10:36:37 2022
+++ src/tests/usr.bin/indent/lsym_comment.c	Thu May 11 09:28:53 2023
@@ -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. */
Index: src/tests/usr.bin/indent/lsym_form_feed.c
diff -u src/tests/usr.bin/indent/lsym_form_feed.c:1.4 src/tests/usr.bin/indent/lsym_form_feed.c:1.5
--- src/tests/usr.bin/indent/lsym_form_feed.c:1.4	Sun Apr 24 10:36:37 2022
+++ src/tests/usr.bin/indent/lsym_form_feed.c	Thu May 11 09:28:53 2023
@@ -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
Index: src/tests/usr.bin/indent/lsym_period.c
diff -u src/tests/usr.bin/indent/lsym_period.c:1.4 src/tests/usr.bin/indent/lsym_period.c:1.5
--- src/tests/usr.bin/indent/lsym_period.c:1.4	Sun Apr 24 09:04:12 2022
+++ src/tests/usr.bin/indent/lsym_period.c	Thu May 11 09:28:53 2023
@@ -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
Index: src/tests/usr.bin/indent/opt_d.c
diff -u src/tests/usr.bin/indent/opt_d.c:1.4 src/tests/usr.bin/indent/opt_d.c:1.5
--- src/tests/usr.bin/indent/opt_d.c:1.4	Sun Apr 24 09:04:12 2022
+++ src/tests/usr.bin/indent/opt_d.c	Thu May 11 09:28:53 2023
@@ -1,4 +1,4 @@
-/* $NetBSD: opt_d.c,v 1.4 2022/04/24 09:04:12 rillig Exp $ */
+/* $NetBSD: opt_d.c,v 1.5 2023/05/11 09:28:53 rillig Exp $ */
 
 /*
  * Tests for the option '-d', which moves comments to the left of the
@@ -27,9 +27,9 @@ example(void)
 //indent run -d1
 void
 example(void)
-{
 /* comment at level 0 */
 /* comment at level 0 */
+{
 /* comment at level 1 */
 /* comment at level 1 */
 /* comment at level 1 */
Index: src/tests/usr.bin/indent/psym_if_expr_stmt_else.c
diff -u src/tests/usr.bin/indent/psym_if_expr_stmt_else.c:1.4 src/tests/usr.bin/indent/psym_if_expr_stmt_else.c:1.5
--- src/tests/usr.bin/indent/psym_if_expr_stmt_else.c:1.4	Sun Apr 24 09:04:12 2022
+++ src/tests/usr.bin/indent/psym_if_expr_stmt_else.c	Thu May 11 09:28:53 2023
@@ -1,4 +1,4 @@
-/* $NetBSD: psym_if_expr_stmt_else.c,v 1.4 2022/04/24 09:04:12 rillig Exp $ */
+/* $NetBSD: psym_if_expr_stmt_else.c,v 1.5 2023/05/11 09:28:53 rillig Exp $ */
 
 /*
  * Tests for the parser symbol psym_if_expr_stmt_else, which represents the
@@ -25,8 +25,10 @@ void
 example(_Bool cond)
 {
 	if (cond) {
-	} else if (cond) {
-	} else if (cond)
+	}
+	else if (cond) {
+	}
+	else if (cond)
 		i++;
 	else {
 	}
@@ -43,22 +45,6 @@ example(_Bool cond)
 {
 	if (cond)
 	{
-	} else if (cond)
-	{
-	} else if (cond)
-		i++;
-	else
-	{
-	}
-}
-//indent end
-
-//indent run -bl -nce
-void
-example(_Bool cond)
-{
-	if (cond)
-	{
 	}
 	else if (cond)
 	{
@@ -71,6 +57,8 @@ example(_Bool cond)
 }
 //indent end
 
+//indent run-equals-prev-output -bl -nce
+
 /*
  * Adding the option '-nei' (do not join 'else if') expands the code even
  * more.

Index: src/tests/usr.bin/indent/fmt_decl.c
diff -u src/tests/usr.bin/indent/fmt_decl.c:1.36 src/tests/usr.bin/indent/fmt_decl.c:1.37
--- src/tests/usr.bin/indent/fmt_decl.c:1.36	Sun Apr 24 10:36:37 2022
+++ src/tests/usr.bin/indent/fmt_decl.c	Thu May 11 09:28:53 2023
@@ -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
 

Index: src/tests/usr.bin/indent/opt_bl_br.c
diff -u src/tests/usr.bin/indent/opt_bl_br.c:1.6 src/tests/usr.bin/indent/opt_bl_br.c:1.7
--- src/tests/usr.bin/indent/opt_bl_br.c:1.6	Sun Apr 24 09:04:12 2022
+++ src/tests/usr.bin/indent/opt_bl_br.c	Thu May 11 09:28:53 2023
@@ -1,7 +1,49 @@
-/* $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
+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");
+	}
+}
+//indent end
+
+//indent run-equals-input -br
+
+//indent run -bl
+void
+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");
+	}
+}
+//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"); }
@@ -11,10 +53,6 @@ example(int n)
 }
 //indent end
 
-/*
- * XXX: The '} else' looks strange in this style since the '}' is not on a
- * line of its own.
- */
 //indent run -bl
 void
 example(int n)
@@ -22,10 +60,12 @@ 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
 	{
@@ -40,9 +80,11 @@ 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");
@@ -52,7 +94,8 @@ example(int n)
 
 
 /*
- * Test C99 comments after 'if (expr)', which are handled by search_stmt.
+ * An end-of-line comment after 'if (expr)' forces the '{' to go to the next
+ * line.
  */
 //indent input
 void function(void)
@@ -74,7 +117,8 @@ function(void)
 	if (expr)		// C99 comment
 		stmt();
 
-	if (expr) {		// C99 comment
+	if (expr)		// C99 comment
+	{
 		stmt();
 	}
 }
@@ -127,14 +171,5 @@ function(void)
 }
 //indent end
 
-//indent run -br
-void
-function(void)
-{
-	if (cond) {
-		stmt();
-	} else if (cond) {
-		stmt();
-	}
-}
-//indent end
+/* TODO: Remove the newline between ')' and '{'. */
+//indent run-equals-input -br
Index: src/tests/usr.bin/indent/opt_ce.c
diff -u src/tests/usr.bin/indent/opt_ce.c:1.6 src/tests/usr.bin/indent/opt_ce.c:1.7
--- src/tests/usr.bin/indent/opt_ce.c:1.6	Sun Apr 24 09:04:12 2022
+++ src/tests/usr.bin/indent/opt_ce.c	Thu May 11 09:28:53 2023
@@ -1,4 +1,4 @@
-/* $NetBSD: opt_ce.c,v 1.6 2022/04/24 09:04:12 rillig Exp $ */
+/* $NetBSD: opt_ce.c,v 1.7 2023/05/11 09:28:53 rillig Exp $ */
 
 /*
  * Tests for the options '-ce' and '-nce'.
@@ -15,22 +15,31 @@
 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"); }
+	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 -ce
+//indent run-equals-input -ce
+
+//indent run -nce
 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");
@@ -38,7 +47,22 @@ example(int n)
 }
 //indent end
 
-//indent run -nce
+
+//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
+
+/*
+ * TODO: Remove the newline between '}' and 'else'.
+ */
+//indent run -ce
 void
 example(int n)
 {
@@ -55,3 +79,5 @@ example(int n)
 	}
 }
 //indent end
+
+//indent run-equals-prev-output -nce

Index: src/tests/usr.bin/indent/opt_sob.c
diff -u src/tests/usr.bin/indent/opt_sob.c:1.5 src/tests/usr.bin/indent/opt_sob.c:1.6
--- src/tests/usr.bin/indent/opt_sob.c:1.5	Sun Apr 24 09:04:12 2022
+++ src/tests/usr.bin/indent/opt_sob.c	Thu May 11 09:28:53 2023
@@ -1,4 +1,4 @@
-/* $NetBSD: opt_sob.c,v 1.5 2022/04/24 09:04:12 rillig Exp $ */
+/* $NetBSD: opt_sob.c,v 1.6 2023/05/11 09:28:53 rillig Exp $ */
 
 /*
  * Tests for the options '-sob' and '-nsob'.
@@ -94,57 +94,6 @@ function_with_1_blank_line(void)
 	var = value;
 
 	if (var > 0)
-		var--;
-
-	return var;
-
-}
-
-
-int
-function_with_2_blank_lines(void)
-{
-
-
-	int		var;
-
-
-	var = value;
-
-
-	if (var > 0)
-		var--;
-
-
-	return var;
-
-
-}
-//indent end
-
-//indent run -nsob
-void		function_declaration(void);
-
-
-int
-function_with_0_blank_lines(void)
-{
-	int		var;
-	var = value;
-	if (var > 0)
-		var--;
-	return var;
-}
-
-int
-function_with_1_blank_line(void)
-{
-
-	int		var;
-
-	var = value;
-
-	if (var > 0)
 
 		var--;
 
@@ -175,3 +124,5 @@ function_with_2_blank_lines(void)
 
 }
 //indent end
+
+//indent run-equals-prev-output -nsob

Index: src/tests/usr.bin/indent/opt_v.c
diff -u src/tests/usr.bin/indent/opt_v.c:1.8 src/tests/usr.bin/indent/opt_v.c:1.9
--- src/tests/usr.bin/indent/opt_v.c:1.8	Sun Apr 24 09:04:12 2022
+++ src/tests/usr.bin/indent/opt_v.c	Thu May 11 09:28:53 2023
@@ -1,4 +1,4 @@
-/* $NetBSD: opt_v.c,v 1.8 2022/04/24 09:04:12 rillig Exp $ */
+/* $NetBSD: opt_v.c,v 1.9 2023/05/11 09:28:53 rillig Exp $ */
 
 /*
  * Tests for the options '-v' and '-nv'.
@@ -71,7 +71,8 @@ example(void)
 /* Demonstrates line number counting in verbose mode. */
 
 int *function(void)
-{}
+{
+}
 //indent end
 
 //indent run -v

Index: src/tests/usr.bin/indent/t_errors.sh
diff -u src/tests/usr.bin/indent/t_errors.sh:1.24 src/tests/usr.bin/indent/t_errors.sh:1.25
--- src/tests/usr.bin/indent/t_errors.sh:1.24	Fri Apr 22 21:21:20 2022
+++ src/tests/usr.bin/indent/t_errors.sh	Thu May 11 09:28:53 2023
@@ -1,5 +1,5 @@
 #! /bin/sh
-# $NetBSD: t_errors.sh,v 1.24 2022/04/22 21:21:20 rillig Exp $
+# $NetBSD: t_errors.sh,v 1.25 2023/05/11 09:28:53 rillig Exp $
 #
 # Copyright (c) 2021 The NetBSD Foundation, Inc.
 # All rights reserved.
@@ -441,7 +441,7 @@ unbalanced_parentheses_3_body()
 atf_test_case 'search_stmt_comment_segv'
 search_stmt_comment_segv_body()
 {
-	# As of NetBSD indent.c 1.188 from 2021-10-30, indent crashes while
+	# Before 2023-05-11, indent crashed while
 	# trying to format the following artificial code.
 
 	printf '{if(expr\n)/*c*/;}\n' > code.c
@@ -449,13 +449,11 @@ search_stmt_comment_segv_body()
 	cat <<\EOF > code.exp
 {
 	if (expr
-		)		/* c */
-		;
+		) /* c */ ;
 }
 EOF
 
-	# TODO: actually produce code.exp instead of an assertion failure.
-	atf_check -s 'signal' -o 'ignore' -e 'match:assert' \
+	atf_check -o 'file:code.exp' \
 	    "$indent" code.c -st
 }
 
@@ -530,10 +528,9 @@ function(void)
 EOF
 	sed '/^#/d' <<EOF > expected.err
 # FIXME: The parentheses _are_ balanced, the '}' does not end the block.
-error: code.c:9: Unbalanced parentheses
-warning: code.c:10: Extra ')'
-# FIXME: There is no line 12 in the input file.
-warning: code.c:12: Extra ')'
+error: code.c:7: Unbalanced parentheses
+warning: code.c:8: Extra ')'
+warning: code.c:9: Extra ')'
 EOF
 
 	atf_check -s 'exit:1' -o 'file:expected.out' -e 'file:expected.err' \

Index: src/tests/usr.bin/indent/t_misc.sh
diff -u src/tests/usr.bin/indent/t_misc.sh:1.20 src/tests/usr.bin/indent/t_misc.sh:1.21
--- src/tests/usr.bin/indent/t_misc.sh:1.20	Fri Apr 22 21:21:20 2022
+++ src/tests/usr.bin/indent/t_misc.sh	Thu May 11 09:28:53 2023
@@ -1,5 +1,5 @@
 #! /bin/sh
-# $NetBSD: t_misc.sh,v 1.20 2022/04/22 21:21:20 rillig Exp $
+# $NetBSD: t_misc.sh,v 1.21 2023/05/11 09:28:53 rillig Exp $
 #
 # Copyright (c) 2021 The NetBSD Foundation, Inc.
 # All rights reserved.
@@ -397,6 +397,16 @@ command_line_vs_profile_body()
 	    "$indent" -Pcustom.pro code.c -st -di0
 }
 
+
+atf_test_case 'opt_v_break_line'
+opt_v_break_line_body()
+{
+	printf '%s\n' 'int *function(void)' '{}' > code.c
+
+	atf_check -o 'ignore' -e 'inline:warning: code.c:2: Line broken\n' \
+	    "$indent" -v code.c -st
+}
+
 atf_init_test_cases()
 {
 	atf_add_test_case 'in_place'
@@ -407,6 +417,7 @@ atf_init_test_cases()
 	atf_add_test_case 'opt'
 	atf_add_test_case 'opt_npro'
 	atf_add_test_case 'opt_U'
+	atf_add_test_case 'opt_v_break_line'
 	atf_add_test_case 'line_no_counting'
 	atf_add_test_case 'default_backup_extension'
 	atf_add_test_case 'several_profiles'

Index: src/tests/usr.bin/indent/t_options.awk
diff -u src/tests/usr.bin/indent/t_options.awk:1.10 src/tests/usr.bin/indent/t_options.awk:1.11
--- src/tests/usr.bin/indent/t_options.awk:1.10	Sun Apr 24 09:04:12 2022
+++ src/tests/usr.bin/indent/t_options.awk	Thu May 11 09:28:53 2023
@@ -1,4 +1,4 @@
-# $NetBSD: t_options.awk,v 1.10 2022/04/24 09:04:12 rillig Exp $
+# $NetBSD: t_options.awk,v 1.11 2023/05/11 09:28:53 rillig Exp $
 #
 # Copyright (c) 2021 The NetBSD Foundation, Inc.
 # All rights reserved.
@@ -154,6 +154,9 @@ section == "" {
 		section_incl_comm = ""
 		unused_input_lineno = NR
 		seen_input_section = 1
+		output_excl_comm = ""
+		output_incl_comm = ""
+		output_lineno = 0
 
 	} else if ($2 == "run") {
 		if (section != "")

Index: src/usr.bin/indent/indent.c
diff -u src/usr.bin/indent/indent.c:1.245 src/usr.bin/indent/indent.c:1.246
--- src/usr.bin/indent/indent.c:1.245	Mon May  9 21:41:49 2022
+++ src/usr.bin/indent/indent.c	Thu May 11 09:28:53 2023
@@ -1,4 +1,4 @@
-/*	$NetBSD: indent.c,v 1.245 2022/05/09 21:41:49 rillig Exp $	*/
+/*	$NetBSD: indent.c,v 1.246 2023/05/11 09:28:53 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.245 2022/05/09 21:41:49 rillig Exp $");
+__RCSID("$NetBSD: indent.c,v 1.246 2023/05/11 09:28:53 rillig Exp $");
 #elif defined(__FreeBSD__)
 __FBSDID("$FreeBSD: head/usr.bin/indent/indent.c 340138 2018-11-04 19:24:49Z oshogbo $");
 #endif
@@ -219,193 +219,6 @@ ind_add(int ind, const char *start, cons
 }
 
 static void
-search_stmt_newline(bool *force_nl)
-{
-    inp_comment_init_newline();
-    inp_comment_add_char('\n');
-    debug_inp(__func__);
-
-    line_no++;
-
-    /*
-     * We may have inherited a force_nl == true from the previous token (like
-     * a semicolon). But once we know that a newline has been scanned in this
-     * loop, force_nl should be false.
-     *
-     * However, the force_nl == true must be preserved if newline is never
-     * scanned in this loop, so this assignment cannot be done earlier.
-     */
-    *force_nl = false;
-}
-
-static void
-search_stmt_comment(void)
-{
-    inp_comment_init_comment();
-    inp_comment_add_range(token.s, token.e);
-    if (token.e[-1] == '/') {
-	while (inp_peek() != '\n')
-	    inp_comment_add_char(inp_next());
-	debug_inp("search_stmt_comment: end of C99 comment");
-    } else {
-	while (!inp_comment_complete_block())
-	    inp_comment_add_char(inp_next());
-	debug_inp("search_stmt_comment: end of block comment");
-    }
-}
-
-static bool
-search_stmt_lbrace(void)
-{
-    /*
-     * Put KNF-style lbraces before the buffered up tokens and jump out of
-     * this loop in order to avoid copying the token again.
-     */
-    if (inp_comment_seen() && opt.brace_same_line) {
-	inp_comment_insert_lbrace();
-	/*
-	 * Originally the lbrace may have been alone on its own line, but it
-	 * will be moved into "the else's line", so if there was a newline
-	 * resulting from the "{" before, it must be scanned now and ignored.
-	 */
-	while (ch_isspace(inp_peek())) {
-	    inp_skip();
-	    if (inp_peek() == '\n')
-		break;
-	}
-	debug_inp(__func__);
-	return true;
-    }
-    return false;
-}
-
-static bool
-search_stmt_other(lexer_symbol lsym, bool *force_nl,
-    bool comment_buffered, bool last_else)
-{
-    bool remove_newlines;
-
-    remove_newlines =
-	/* "} else" */
-	(lsym == lsym_else && code.e != code.s && code.e[-1] == '}')
-	/* "else if" */
-	|| (lsym == lsym_if && last_else && opt.else_if);
-    if (remove_newlines)
-	*force_nl = false;
-
-    if (!inp_comment_seen()) {
-	ps.search_stmt = false;
-	return false;
-    }
-
-    debug_inp(__func__);
-    inp_comment_rtrim_blank();
-
-    if (opt.swallow_optional_blanklines ||
-	(!comment_buffered && remove_newlines)) {
-	*force_nl = !remove_newlines;
-	inp_comment_rtrim_newline();
-    }
-
-    if (*force_nl) {		/* if we should insert a newline here, put it
-				 * into the buffer */
-	*force_nl = false;
-	--line_no;		/* this will be re-increased when the newline
-				 * is read from the buffer */
-	inp_comment_add_char('\n');
-	inp_comment_add_char(' ');
-	if (opt.verbose)	/* warn if the line was not already broken */
-	    diag(0, "Line broken");
-    }
-
-    inp_comment_add_range(token.s, token.e);
-
-    debug_inp("search_stmt_other end");
-    return true;
-}
-
-static void
-search_stmt_lookahead(lexer_symbol *lsym)
-{
-    if (*lsym == lsym_eof)
-	return;
-
-    /*
-     * The only intended purpose of calling lexi() below is to categorize the
-     * next token in order to decide whether to continue buffering forthcoming
-     * tokens. Once the buffering is over, lexi() will be called again
-     * elsewhere on all of the tokens - this time for normal processing.
-     *
-     * Calling it for this purpose is a bug, because lexi() also changes the
-     * parser state and discards leading whitespace, which is needed mostly
-     * for comment-related considerations.
-     *
-     * Work around the former problem by giving lexi() a copy of the current
-     * parser state and discard it if the call turned out to be just a
-     * lookahead.
-     *
-     * Work around the latter problem by copying all whitespace characters
-     * into the buffer so that the later lexi() call will read them.
-     */
-    if (inp_comment_seen()) {
-	while (ch_isblank(inp_peek()))
-	    inp_comment_add_char(inp_next());
-	debug_inp(__func__);
-    }
-
-    struct parser_state backup_ps = ps;
-    debug_println("backed up parser state");
-    *lsym = lexi();
-    if (*lsym == lsym_newline || *lsym == lsym_form_feed ||
-	*lsym == lsym_comment || ps.search_stmt) {
-	ps = backup_ps;
-	debug_println("restored parser state");
-    }
-}
-
-/*
- * Move newlines and comments following an 'if (expr)', 'while (expr)',
- * 'else', etc. up to the start of the following statement to a buffer. This
- * allows proper handling of both kinds of brace placement (-br, -bl) and
- * "cuddling else" (-ce).
- */
-static void
-search_stmt(lexer_symbol *lsym, bool *force_nl, bool *last_else)
-{
-    bool comment_buffered = false;
-
-    while (ps.search_stmt) {
-	switch (*lsym) {
-	case lsym_newline:
-	    search_stmt_newline(force_nl);
-	    break;
-	case lsym_form_feed:
-	    /* XXX: Is simply removed from the source code. */
-	    break;
-	case lsym_comment:
-	    search_stmt_comment();
-	    comment_buffered = true;
-	    break;
-	case lsym_lbrace:
-	    if (search_stmt_lbrace())
-		goto switch_buffer;
-	    /* FALLTHROUGH */
-	default:
-	    if (!search_stmt_other(*lsym, force_nl, comment_buffered,
-		    *last_else))
-		return;
-    switch_buffer:
-	    ps.search_stmt = false;
-	    inp_comment_add_char(' ');	/* add trailing blank, just in case */
-	    inp_from_comment();
-	}
-	search_stmt_lookahead(lsym);
-    }
-
-    *last_else = false;
-}
-
-static void
 main_init_globals(void)
 {
     inp_init();
@@ -733,12 +546,6 @@ process_rparen_or_rbracket(bool *spaced_
 
 	parse_stmt_head(hd);
     }
-
-    /*
-     * This should ensure that constructs such as main(){...} and int[]{...}
-     * have their braces put in the right place.
-     */
-    ps.search_stmt = opt.brace_same_line;
 }
 
 static bool
@@ -981,9 +788,6 @@ process_rbrace(bool *spaced_expr, int *d
 
     out.blank_line_before = false;
     parse(psym_rbrace);
-    ps.search_stmt = opt.cuddle_else
-	&& ps.s_sym[ps.tos] == psym_if_expr_stmt
-	&& ps.s_ind_level[ps.tos] >= ps.ind_level;
 
     if (ps.tos <= 1 && opt.blanklines_after_procs && ps.decl_level <= 0)
 	out.blank_line_after = true;
@@ -1276,7 +1080,9 @@ main_loop(void)
     for (;;) {			/* loop until we reach eof */
 	lexer_symbol lsym = lexi();
 
-	search_stmt(&lsym, &force_nl, &last_else);
+	if (lsym == lsym_if && last_else && opt.else_if)
+	    force_nl = false;
+	last_else = false;
 
 	if (lsym == lsym_eof) {
 	    process_eof();

Index: src/usr.bin/indent/indent.h
diff -u src/usr.bin/indent/indent.h:1.112 src/usr.bin/indent/indent.h:1.113
--- src/usr.bin/indent/indent.h:1.112	Sat Apr 23 06:43:22 2022
+++ src/usr.bin/indent/indent.h	Thu May 11 09:28:53 2023
@@ -1,4 +1,4 @@
-/*	$NetBSD: indent.h,v 1.112 2022/04/23 06:43:22 rillig Exp $	*/
+/*	$NetBSD: indent.h,v 1.113 2023/05/11 09:28:53 rillig Exp $	*/
 
 /*-
  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
@@ -335,12 +335,6 @@ extern struct parser_state {
     bool is_case_label;		/* 'case' and 'default' labels are indented
 				 * differently from regular labels */
 
-    bool search_stmt;		/* whether it is necessary to buffer up all
-				 * text up to the start of a statement after
-				 * an 'if (expr)', 'while (expr)', etc., to
-				 * move the comments after the opening brace
-				 * of the following statement */
-
     int tos;			/* pointer to top of stack */
     parser_symbol s_sym[STACKSIZE];
     int s_ind_level[STACKSIZE];

Index: src/usr.bin/indent/lexi.c
diff -u src/usr.bin/indent/lexi.c:1.172 src/usr.bin/indent/lexi.c:1.173
--- src/usr.bin/indent/lexi.c:1.172	Sun Feb 13 12:43:26 2022
+++ src/usr.bin/indent/lexi.c	Thu May 11 09:28:53 2023
@@ -1,4 +1,4 @@
-/*	$NetBSD: lexi.c,v 1.172 2022/02/13 12:43:26 rillig Exp $	*/
+/*	$NetBSD: lexi.c,v 1.173 2023/05/11 09:28:53 rillig Exp $	*/
 
 /*-
  * SPDX-License-Identifier: BSD-4-Clause
@@ -43,7 +43,7 @@ static char sccsid[] = "@(#)lexi.c	8.1 (
 
 #include <sys/cdefs.h>
 #if defined(__NetBSD__)
-__RCSID("$NetBSD: lexi.c,v 1.172 2022/02/13 12:43:26 rillig Exp $");
+__RCSID("$NetBSD: lexi.c,v 1.173 2023/05/11 09:28:53 rillig Exp $");
 #elif defined(__FreeBSD__)
 __FBSDID("$FreeBSD: head/usr.bin/indent/lexi.c 337862 2018-08-15 18:19:45Z pstef $");
 #endif
@@ -347,8 +347,6 @@ debug_lexi(lexer_symbol lsym)
     debug_ps_bool(in_stmt_cont);
     debug_ps_bool(is_case_label);
 
-    debug_ps_bool(search_stmt);
-
     prev_ps = ps;
 }
 #endif

Index: src/usr.bin/indent/parse.c
diff -u src/usr.bin/indent/parse.c:1.49 src/usr.bin/indent/parse.c:1.50
--- src/usr.bin/indent/parse.c:1.49	Fri Apr 22 21:21:20 2022
+++ src/usr.bin/indent/parse.c	Thu May 11 09:28:53 2023
@@ -1,4 +1,4 @@
-/*	$NetBSD: parse.c,v 1.49 2022/04/22 21:21:20 rillig Exp $	*/
+/*	$NetBSD: parse.c,v 1.50 2023/05/11 09:28:53 rillig Exp $	*/
 
 /*-
  * SPDX-License-Identifier: BSD-4-Clause
@@ -43,7 +43,7 @@ static char sccsid[] = "@(#)parse.c	8.1 
 
 #include <sys/cdefs.h>
 #if defined(__NetBSD__)
-__RCSID("$NetBSD: parse.c,v 1.49 2022/04/22 21:21:20 rillig Exp $");
+__RCSID("$NetBSD: parse.c,v 1.50 2023/05/11 09:28:53 rillig Exp $");
 #else
 __FBSDID("$FreeBSD: head/usr.bin/indent/parse.c 337651 2018-08-11 19:20:06Z pstef $");
 #endif
@@ -113,9 +113,6 @@ parse(parser_symbol psym)
     switch (psym) {
 
     case psym_decl:
-	ps.search_stmt = opt.brace_same_line;
-	/* indicate that following brace should be on same line */
-
 	if (ps.s_sym[ps.tos] == psym_decl)
 	    break;		/* only put one declaration onto stack */
 
@@ -142,7 +139,6 @@ parse(parser_symbol psym)
 	ps.s_sym[++ps.tos] = psym;
 	ps.s_ind_level[ps.tos] = ps.ind_level = ps.ind_level_follow;
 	++ps.ind_level_follow;	/* subsequent statements should be indented 1 */
-	ps.search_stmt = opt.brace_same_line;
 	break;
 
     case psym_lbrace:
@@ -182,7 +178,6 @@ parse(parser_symbol psym)
 	    ps.s_sym[++ps.tos] = psym_while_expr;
 	    ps.s_ind_level[ps.tos] = ps.ind_level_follow;
 	    ++ps.ind_level_follow;
-	    ps.search_stmt = opt.brace_same_line;
 	}
 
 	break;
@@ -194,8 +189,6 @@ parse(parser_symbol psym)
 	    ps.ind_level = ps.s_ind_level[ps.tos];
 	    ps.ind_level_follow = ps.ind_level + 1;
 	    ps.s_sym[ps.tos] = psym_if_expr_stmt_else;
-
-	    ps.search_stmt = opt.brace_same_line || opt.else_if;
 	}
 	break;
 
@@ -214,7 +207,6 @@ parse(parser_symbol psym)
 	ps.s_ind_level[ps.tos] = ps.ind_level_follow;
 	case_ind = (float)ps.ind_level_follow + opt.case_indent;
 	ps.ind_level_follow += (int)opt.case_indent + 1;
-	ps.search_stmt = opt.brace_same_line;
 	break;
 
     case psym_semicolon:	/* a simple statement */

Reply via email to