Module Name:    src
Committed By:   rillig
Date:           Fri Jun  2 11:43:07 UTC 2023

Modified Files:
        src/tests/usr.bin/indent: opt_bc.c
        src/usr.bin/indent: debug.c indent.c indent.h parse.c

Log Message:
indent: fix formatting of declarations with preprocessing lines


To generate a diff of this commit:
cvs rdiff -u -r1.7 -r1.8 src/tests/usr.bin/indent/opt_bc.c
cvs rdiff -u -r1.23 -r1.24 src/usr.bin/indent/debug.c
cvs rdiff -u -r1.310 -r1.311 src/usr.bin/indent/indent.c
cvs rdiff -u -r1.159 -r1.160 src/usr.bin/indent/indent.h
cvs rdiff -u -r1.62 -r1.63 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/opt_bc.c
diff -u src/tests/usr.bin/indent/opt_bc.c:1.7 src/tests/usr.bin/indent/opt_bc.c:1.8
--- src/tests/usr.bin/indent/opt_bc.c:1.7	Fri Jun  2 11:26:21 2023
+++ src/tests/usr.bin/indent/opt_bc.c	Fri Jun  2 11:43:07 2023
@@ -1,4 +1,4 @@
-/* $NetBSD: opt_bc.c,v 1.7 2023/06/02 11:26:21 rillig Exp $ */
+/* $NetBSD: opt_bc.c,v 1.8 2023/06/02 11:43:07 rillig Exp $ */
 
 /*
  * Tests for the options '-bc' and '-nbc'.
@@ -65,6 +65,11 @@ double		a, b, c;
 //indent end
 
 
+/*
+ * Before indent.c 1.311 from 2023-06-02, indent formatted the two '#if'
+ * branches differently and merged the 'b, c' with the preceding preprocessing
+ * line.
+ */
 //indent input
 int a,
 #if 0
@@ -81,16 +86,16 @@ int		a,
 		c;
 int		d;
 #else
-// $ FIXME: The '#else' branch must be indented like the '#if' branch.
-		b, c;
+		b,
+		c;
 int		d;
 #endif
 //indent end
 
 //indent run -nbc
 int		a,
-// $ FIXME: 'b, c' must not be merged into the preprocessing line.
-#if 0		b, c;
+#if 0
+		b, c;
 int		d;
 #else
 		b, c;

Index: src/usr.bin/indent/debug.c
diff -u src/usr.bin/indent/debug.c:1.23 src/usr.bin/indent/debug.c:1.24
--- src/usr.bin/indent/debug.c:1.23	Tue May 23 16:53:57 2023
+++ src/usr.bin/indent/debug.c	Fri Jun  2 11:43:07 2023
@@ -1,4 +1,4 @@
-/*	$NetBSD: debug.c,v 1.23 2023/05/23 16:53:57 rillig Exp $	*/
+/*	$NetBSD: debug.c,v 1.24 2023/06/02 11:43:07 rillig Exp $	*/
 
 /*-
  * Copyright (c) 2023 The NetBSD Foundation, Inc.
@@ -30,7 +30,7 @@
  */
 
 #include <sys/cdefs.h>
-__RCSID("$NetBSD: debug.c,v 1.23 2023/05/23 16:53:57 rillig Exp $");
+__RCSID("$NetBSD: debug.c,v 1.24 2023/06/02 11:43:07 rillig Exp $");
 
 #include <stdarg.h>
 
@@ -296,6 +296,7 @@ debug_parser_state(void)
 	debug_ps_bool(next_unary);
 	debug_ps_bool(is_function_definition);
 	debug_ps_bool(want_blank);
+	debug_ps_bool(break_after_comma);
 	debug_ps_bool(force_nl);
 	debug_ps_int(line_start_nparen);
 	debug_ps_int(nparen);

Index: src/usr.bin/indent/indent.c
diff -u src/usr.bin/indent/indent.c:1.310 src/usr.bin/indent/indent.c:1.311
--- src/usr.bin/indent/indent.c:1.310	Tue May 23 18:16:28 2023
+++ src/usr.bin/indent/indent.c	Fri Jun  2 11:43:07 2023
@@ -1,4 +1,4 @@
-/*	$NetBSD: indent.c,v 1.310 2023/05/23 18:16:28 rillig Exp $	*/
+/*	$NetBSD: indent.c,v 1.311 2023/06/02 11:43:07 rillig Exp $	*/
 
 /*-
  * SPDX-License-Identifier: BSD-4-Clause
@@ -38,7 +38,7 @@
  */
 
 #include <sys/cdefs.h>
-__RCSID("$NetBSD: indent.c,v 1.310 2023/05/23 18:16:28 rillig Exp $");
+__RCSID("$NetBSD: indent.c,v 1.311 2023/06/02 11:43:07 rillig Exp $");
 
 #include <sys/param.h>
 #include <err.h>
@@ -81,7 +81,6 @@ struct buffer code;
 struct buffer com;
 
 bool found_err;
-bool break_comma;
 float case_ind;
 bool had_eof;
 int line_no = 1;
@@ -450,9 +449,11 @@ move_com_to_code(lexer_symbol lsym)
 static void
 process_newline(void)
 {
-	if (ps.prev_token == lsym_comma && ps.nparen == 0 && !ps.block_init &&
-	    !opt.break_after_comma && break_comma &&
-	    com.len == 0)
+	if (ps.prev_token == lsym_comma
+	    && ps.nparen == 0 && !ps.block_init
+	    && !opt.break_after_comma && ps.break_after_comma
+	    && lab.len == 0 /* for preprocessing lines */
+	    && com.len == 0)
 		goto stay_in_line;
 
 	output_line();
@@ -947,7 +948,7 @@ process_comma(void)
 		if (ps.block_init_level <= 0)
 			ps.block_init = false;
 		int typical_varname_length = 8;
-		if (break_comma && (opt.break_after_comma ||
+		if (ps.break_after_comma && (opt.break_after_comma ||
 		    ind_add(compute_code_indent(), code.st, code.len)
 		    >= opt.max_line_length - typical_varname_length))
 			ps.force_nl = true;

Index: src/usr.bin/indent/indent.h
diff -u src/usr.bin/indent/indent.h:1.159 src/usr.bin/indent/indent.h:1.160
--- src/usr.bin/indent/indent.h:1.159	Tue May 23 12:12:29 2023
+++ src/usr.bin/indent/indent.h	Fri Jun  2 11:43:07 2023
@@ -1,4 +1,4 @@
-/*	$NetBSD: indent.h,v 1.159 2023/05/23 12:12:29 rillig Exp $	*/
+/*	$NetBSD: indent.h,v 1.160 2023/06/02 11:43:07 rillig Exp $	*/
 
 /*-
  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
@@ -235,8 +235,6 @@ extern struct options {
 } opt;
 
 extern bool found_err;
-extern bool break_comma;	/* when true and not in parentheses, break
-				 * after a comma */
 extern float case_ind;		/* indentation level to be used for a "case n:"
 				 */
 extern bool had_eof;		/* whether input is exhausted */
@@ -393,6 +391,9 @@ extern struct parser_state {
 
 	/* Vertical spacing */
 
+	bool break_after_comma;	/* whether to add a newline after the next
+				 * comma; used in declarations but not in
+				 * initializer lists */
 	bool force_nl;		/* whether the next token is forced to go to a
 				 * new line; used after 'if (expr)' and in
 				 * similar situations; tokens like '{' may

Index: src/usr.bin/indent/parse.c
diff -u src/usr.bin/indent/parse.c:1.62 src/usr.bin/indent/parse.c:1.63
--- src/usr.bin/indent/parse.c:1.62	Tue May 23 12:12:29 2023
+++ src/usr.bin/indent/parse.c	Fri Jun  2 11:43:07 2023
@@ -1,4 +1,4 @@
-/*	$NetBSD: parse.c,v 1.62 2023/05/23 12:12:29 rillig Exp $	*/
+/*	$NetBSD: parse.c,v 1.63 2023/06/02 11:43:07 rillig Exp $	*/
 
 /*-
  * SPDX-License-Identifier: BSD-4-Clause
@@ -38,7 +38,7 @@
  */
 
 #include <sys/cdefs.h>
-__RCSID("$NetBSD: parse.c,v 1.62 2023/05/23 12:12:29 rillig Exp $");
+__RCSID("$NetBSD: parse.c,v 1.63 2023/06/02 11:43:07 rillig Exp $");
 
 #include <err.h>
 
@@ -79,8 +79,7 @@ parse(parser_symbol psym)
 		if (ps.s_sym[ps.tos] == psym_decl)
 			break;	/* only put one declaration onto stack */
 
-		break_comma = true;	/* while in a declaration, force a
-					 * newline after comma */
+		ps.break_after_comma = true;
 		ps.s_sym[++ps.tos] = psym_decl;
 		ps.s_ind_level[ps.tos] = ps.ind_level_follow;
 
@@ -105,8 +104,7 @@ parse(parser_symbol psym)
 		break;
 
 	case psym_lbrace:
-		break_comma = false;	/* don't break comma in an initializer
-					 * list */
+		ps.break_after_comma = false;
 		if (ps.s_sym[ps.tos] == psym_stmt
 		    || ps.s_sym[ps.tos] == psym_decl
 		    || ps.s_sym[ps.tos] == psym_stmt_list)
@@ -178,8 +176,7 @@ parse(parser_symbol psym)
 		break;
 
 	case psym_0:		/* a simple statement */
-		break_comma = false;	/* don't break after comma in a
-					 * declaration */
+		ps.break_after_comma = false;
 		ps.s_sym[++ps.tos] = psym_stmt;
 		ps.s_ind_level[ps.tos] = ps.ind_level;
 		break;

Reply via email to