Module Name:    src
Committed By:   rillig
Date:           Sat Jan  4 21:54:26 UTC 2025

Modified Files:
        src/tests/usr.bin/indent: lsym_rbrace.c
        src/usr.bin/indent: indent.c parse.c

Log Message:
indent: fix indentation of adjacent multi-line initializers

The main topic of this change is parse.c:66, which makes the indentation
of statements uniform with the indentation of other parser symbols.

That change had the side effect of messing up the indentation of files
whose first line does not start in column 1, such as in ps_ind_level.c.
To fix this side effect, the initial indentation must be determined
before pushing the placeholder token psym_stmt during initialization.


To generate a diff of this commit:
cvs rdiff -u -r1.7 -r1.8 src/tests/usr.bin/indent/lsym_rbrace.c
cvs rdiff -u -r1.394 -r1.395 src/usr.bin/indent/indent.c
cvs rdiff -u -r1.81 -r1.82 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/lsym_rbrace.c
diff -u src/tests/usr.bin/indent/lsym_rbrace.c:1.7 src/tests/usr.bin/indent/lsym_rbrace.c:1.8
--- src/tests/usr.bin/indent/lsym_rbrace.c:1.7	Thu Jun  8 06:47:14 2023
+++ src/tests/usr.bin/indent/lsym_rbrace.c	Sat Jan  4 21:54:26 2025
@@ -1,4 +1,4 @@
-/* $NetBSD: lsym_rbrace.c,v 1.7 2023/06/08 06:47:14 rillig Exp $ */
+/* $NetBSD: lsym_rbrace.c,v 1.8 2025/01/04 21:54:26 rillig Exp $ */
 
 /*
  * Tests for the token lsym_rbrace, which represents a '}' in these contexts:
@@ -90,9 +90,8 @@ int numbers[][] = {{11},
 		{31},
 	};
 	int numbers[][] = {{11},
-	// $ FIXME: Must be indented.
-	{21},
-	{31},
+		{21},
+		{31},
 	};
 }
 //indent end

Index: src/usr.bin/indent/indent.c
diff -u src/usr.bin/indent/indent.c:1.394 src/usr.bin/indent/indent.c:1.395
--- src/usr.bin/indent/indent.c:1.394	Sat Jan  4 21:20:59 2025
+++ src/usr.bin/indent/indent.c	Sat Jan  4 21:54:26 2025
@@ -1,4 +1,4 @@
-/*	$NetBSD: indent.c,v 1.394 2025/01/04 21:20:59 rillig Exp $	*/
+/*	$NetBSD: indent.c,v 1.395 2025/01/04 21:54:26 rillig Exp $	*/
 
 /*-
  * SPDX-License-Identifier: BSD-4-Clause
@@ -38,7 +38,7 @@
  */
 
 #include <sys/cdefs.h>
-__RCSID("$NetBSD: indent.c,v 1.394 2025/01/04 21:20:59 rillig Exp $");
+__RCSID("$NetBSD: indent.c,v 1.395 2025/01/04 21:54:26 rillig Exp $");
 
 #include <sys/param.h>
 #include <err.h>
@@ -181,18 +181,6 @@ ind_add(int ind, const char *s, size_t l
 }
 
 static void
-init_globals(void)
-{
-	ps_push(psym_stmt, false);	/* as a stop symbol */
-	ps.prev_lsym = lsym_semicolon;
-	ps.lbrace_kind = psym_lbrace_block;
-
-	const char *suffix = getenv("SIMPLE_BACKUP_SUFFIX");
-	if (suffix != NULL)
-		backup_suffix = suffix;
-}
-
-static void
 load_profiles(int argc, char **argv)
 {
 	const char *profile_name = NULL;
@@ -295,7 +283,7 @@ parse_command_line(int argc, char **argv
 }
 
 static void
-set_initial_indentation(void)
+initialize_parser(void)
 {
 	inp_read_line();
 
@@ -310,6 +298,9 @@ set_initial_indentation(void)
 	}
 
 	ps.ind_level = ps.ind_level_follow = ind / opt.indent_size;
+	ps_push(psym_stmt, false);	/* as a stop symbol */
+	ps.prev_lsym = lsym_semicolon;
+	ps.lbrace_kind = psym_lbrace_block;
 }
 
 static bool
@@ -1168,9 +1159,12 @@ indent(void)
 int
 main(int argc, char **argv)
 {
-	init_globals();
+	const char *suffix = getenv("SIMPLE_BACKUP_SUFFIX");
+	if (suffix != NULL)
+		backup_suffix = suffix;
+
 	load_profiles(argc, argv);
 	parse_command_line(argc, argv);
-	set_initial_indentation();
+	initialize_parser();
 	return indent();
 }

Index: src/usr.bin/indent/parse.c
diff -u src/usr.bin/indent/parse.c:1.81 src/usr.bin/indent/parse.c:1.82
--- src/usr.bin/indent/parse.c:1.81	Sat Jan  4 21:20:59 2025
+++ src/usr.bin/indent/parse.c	Sat Jan  4 21:54:26 2025
@@ -1,4 +1,4 @@
-/*	$NetBSD: parse.c,v 1.81 2025/01/04 21:20:59 rillig Exp $	*/
+/*	$NetBSD: parse.c,v 1.82 2025/01/04 21:54:26 rillig Exp $	*/
 
 /*-
  * SPDX-License-Identifier: BSD-4-Clause
@@ -38,12 +38,20 @@
  */
 
 #include <sys/cdefs.h>
-__RCSID("$NetBSD: parse.c,v 1.81 2025/01/04 21:20:59 rillig Exp $");
+__RCSID("$NetBSD: parse.c,v 1.82 2025/01/04 21:54:26 rillig Exp $");
 
 #include <stdlib.h>
 
 #include "indent.h"
 
+/* Replace the top 2 symbols with the given symbol. */
+static void
+psyms_replace2(parser_symbol psym)
+{
+	ps.psyms.len--;
+	ps.psyms.sym[ps.psyms.len - 1] = psym;
+}
+
 /*
  * Try to combine the statement on the top of the parse stack with the symbol
  * directly below it, replacing these two symbols with a single symbol.
@@ -54,19 +62,17 @@ psyms_reduce_stmt(void)
 	switch (ps.psyms.sym[ps.psyms.len - 2]) {
 
 	case psym_stmt:
-		ps.psyms.len--;
-		ps.psyms.sym[ps.psyms.len - 1] = psym_stmt;
+		psyms_replace2(psym_stmt);
+		ps.ind_level_follow = ps.psyms.ind_level[ps.psyms.len - 1];
 		return true;
 
 	case psym_do:
-		ps.psyms.len--;
-		ps.psyms.sym[ps.psyms.len - 1] = psym_do_stmt;
+		psyms_replace2(psym_do_stmt);
 		ps.ind_level_follow = ps.psyms.ind_level[ps.psyms.len - 1];
 		return true;
 
 	case psym_if_expr:
-		ps.psyms.len--;
-		ps.psyms.sym[ps.psyms.len - 1] = psym_if_expr_stmt;
+		psyms_replace2(psym_if_expr_stmt);
 		/* For the time being, assume that there is no 'else' on this
 		 * 'if', and set the indentation level accordingly. If an
 		 * 'else' is scanned, it will be fixed up later. */
@@ -82,8 +88,7 @@ psyms_reduce_stmt(void)
 	case psym_if_expr_stmt_else:
 	case psym_for_exprs:
 	case psym_while_expr:
-		ps.psyms.len--;
-		ps.psyms.sym[ps.psyms.len - 1] = psym_stmt;
+		psyms_replace2(psym_stmt);
 		ps.ind_level_follow = ps.psyms.ind_level[ps.psyms.len - 1];
 		return true;
 

Reply via email to