Module Name:    src
Committed By:   rillig
Date:           Tue Jun  6 04:37:27 UTC 2023

Modified Files:
        src/tests/usr.bin/indent: opt_cli.c psym_switch_expr.c
        src/usr.bin/indent: indent.c indent.h io.c parse.c

Log Message:
indent: compute indentation of 'case' labels on-demand

One less moving part to keep track of.

No functional change.


To generate a diff of this commit:
cvs rdiff -u -r1.5 -r1.6 src/tests/usr.bin/indent/opt_cli.c
cvs rdiff -u -r1.4 -r1.5 src/tests/usr.bin/indent/psym_switch_expr.c
cvs rdiff -u -r1.336 -r1.337 src/usr.bin/indent/indent.c
cvs rdiff -u -r1.175 -r1.176 src/usr.bin/indent/indent.h
cvs rdiff -u -r1.198 -r1.199 src/usr.bin/indent/io.c
cvs rdiff -u -r1.66 -r1.67 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_cli.c
diff -u src/tests/usr.bin/indent/opt_cli.c:1.5 src/tests/usr.bin/indent/opt_cli.c:1.6
--- src/tests/usr.bin/indent/opt_cli.c:1.5	Sun Apr 24 09:04:12 2022
+++ src/tests/usr.bin/indent/opt_cli.c	Tue Jun  6 04:37:27 2023
@@ -1,4 +1,4 @@
-/* $NetBSD: opt_cli.c,v 1.5 2022/04/24 09:04:12 rillig Exp $ */
+/* $NetBSD: opt_cli.c,v 1.6 2023/06/06 04:37:27 rillig Exp $ */
 
 /*
  * Tests for the option '-cli' ("case label indentation"), which sets the
@@ -72,3 +72,28 @@ classify(int n)
 	}
 }
 //indent end
+
+//indent run -cli3.25
+void
+classify(int n)
+{
+	switch (n) {
+				  case 0:
+					print("zero");
+					break;
+				  case 1:
+					print("one");
+					break;
+				  case 2:
+				  case 3:
+					print("prime");
+					break;
+				  case 4:
+					print("square");
+					break;
+				  default:
+					print("large");
+					break;
+	}
+}
+//indent end

Index: src/tests/usr.bin/indent/psym_switch_expr.c
diff -u src/tests/usr.bin/indent/psym_switch_expr.c:1.4 src/tests/usr.bin/indent/psym_switch_expr.c:1.5
--- src/tests/usr.bin/indent/psym_switch_expr.c:1.4	Sun Apr 24 09:04:12 2022
+++ src/tests/usr.bin/indent/psym_switch_expr.c	Tue Jun  6 04:37:27 2023
@@ -1,4 +1,4 @@
-/* $NetBSD: psym_switch_expr.c,v 1.4 2022/04/24 09:04:12 rillig Exp $ */
+/* $NetBSD: psym_switch_expr.c,v 1.5 2023/06/06 04:37:27 rillig Exp $ */
 
 /*
  * Tests for the parser symbol psym_switch_expr, which represents the keyword
@@ -6,10 +6,6 @@
  * statement (usually a block) containing the 'case' labels.
  */
 
-//indent input
-// TODO: add input
-//indent end
-
 //indent run-equals-input
 
 
@@ -43,3 +39,17 @@ function(void)
 		}
 }
 //indent end
+
+//indent run -cli-0.375
+void
+function(void)
+{
+	switch (expr)
+		if (cond) {
+     case 1:
+			return;
+     case 2:
+			break;
+		}
+}
+//indent end

Index: src/usr.bin/indent/indent.c
diff -u src/usr.bin/indent/indent.c:1.336 src/usr.bin/indent/indent.c:1.337
--- src/usr.bin/indent/indent.c:1.336	Mon Jun  5 20:56:18 2023
+++ src/usr.bin/indent/indent.c	Tue Jun  6 04:37:26 2023
@@ -1,4 +1,4 @@
-/*	$NetBSD: indent.c,v 1.336 2023/06/05 20:56:18 rillig Exp $	*/
+/*	$NetBSD: indent.c,v 1.337 2023/06/06 04:37:26 rillig Exp $	*/
 
 /*-
  * SPDX-License-Identifier: BSD-4-Clause
@@ -38,7 +38,7 @@
  */
 
 #include <sys/cdefs.h>
-__RCSID("$NetBSD: indent.c,v 1.336 2023/06/05 20:56:18 rillig Exp $");
+__RCSID("$NetBSD: indent.c,v 1.337 2023/06/06 04:37:26 rillig Exp $");
 
 #include <sys/param.h>
 #include <err.h>
@@ -81,7 +81,6 @@ struct buffer code;
 struct buffer com;
 
 bool found_err;
-float case_ind;
 bool had_eof;
 int line_no = 1;
 enum indent_enabled indent_enabled;

Index: src/usr.bin/indent/indent.h
diff -u src/usr.bin/indent/indent.h:1.175 src/usr.bin/indent/indent.h:1.176
--- src/usr.bin/indent/indent.h:1.175	Mon Jun  5 14:22:26 2023
+++ src/usr.bin/indent/indent.h	Tue Jun  6 04:37:26 2023
@@ -1,4 +1,4 @@
-/*	$NetBSD: indent.h,v 1.175 2023/06/05 14:22:26 rillig Exp $	*/
+/*	$NetBSD: indent.h,v 1.176 2023/06/06 04:37:26 rillig Exp $	*/
 
 /*-
  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
@@ -247,8 +247,6 @@ extern struct options {
 } opt;
 
 extern bool found_err;
-extern float case_ind;		/* indentation level to be used for a "case n:"
-				 */
 extern bool had_eof;		/* whether input is exhausted */
 extern int line_no;		/* the current line number. */
 extern enum indent_enabled {
@@ -355,7 +353,6 @@ extern struct parser_state {
 	int tos;		/* pointer to top of stack */
 	parser_symbol s_sym[STACKSIZE];
 	int s_ind_level[STACKSIZE];
-	float s_case_ind_level[STACKSIZE];
 
 	/* Spacing inside a statement or declaration */
 

Index: src/usr.bin/indent/io.c
diff -u src/usr.bin/indent/io.c:1.198 src/usr.bin/indent/io.c:1.199
--- src/usr.bin/indent/io.c:1.198	Mon Jun  5 12:06:51 2023
+++ src/usr.bin/indent/io.c	Tue Jun  6 04:37:26 2023
@@ -1,4 +1,4 @@
-/*	$NetBSD: io.c,v 1.198 2023/06/05 12:06:51 rillig Exp $	*/
+/*	$NetBSD: io.c,v 1.199 2023/06/06 04:37:26 rillig Exp $	*/
 
 /*-
  * SPDX-License-Identifier: BSD-4-Clause
@@ -38,7 +38,7 @@
  */
 
 #include <sys/cdefs.h>
-__RCSID("$NetBSD: io.c,v 1.198 2023/06/05 12:06:51 rillig Exp $");
+__RCSID("$NetBSD: io.c,v 1.199 2023/06/06 04:37:26 rillig Exp $");
 
 #include <stdio.h>
 
@@ -362,11 +362,21 @@ compute_code_indent(void)
 		    opt.continuation_indent * ps.line_start_nparen;
 }
 
+static int
+compute_case_label_indent(void)
+{
+	int i = ps.tos;
+	while (i > 0 && ps.s_sym[i] != psym_switch_expr)
+		i--;
+	float case_ind = (float)ps.s_ind_level[i] + opt.case_indent;
+	return (int)(case_ind * (float)opt.indent_size);
+}
+
 int
 compute_label_indent(void)
 {
 	if (out.line_kind == lk_case_or_default)
-		return (int)(case_ind * (float)opt.indent_size);
+		return compute_case_label_indent();
 	if (lab.s[0] == '#')
 		return 0;
 	return opt.indent_size * (ps.ind_level - 2);

Index: src/usr.bin/indent/parse.c
diff -u src/usr.bin/indent/parse.c:1.66 src/usr.bin/indent/parse.c:1.67
--- src/usr.bin/indent/parse.c:1.66	Mon Jun  5 07:35:05 2023
+++ src/usr.bin/indent/parse.c	Tue Jun  6 04:37:26 2023
@@ -1,4 +1,4 @@
-/*	$NetBSD: parse.c,v 1.66 2023/06/05 07:35:05 rillig Exp $	*/
+/*	$NetBSD: parse.c,v 1.67 2023/06/06 04:37:26 rillig Exp $	*/
 
 /*-
  * SPDX-License-Identifier: BSD-4-Clause
@@ -38,7 +38,7 @@
  */
 
 #include <sys/cdefs.h>
-__RCSID("$NetBSD: parse.c,v 1.66 2023/06/05 07:35:05 rillig Exp $");
+__RCSID("$NetBSD: parse.c,v 1.67 2023/06/06 04:37:26 rillig Exp $");
 
 #include <err.h>
 
@@ -183,8 +183,6 @@ parse(parser_symbol psym)
 
 	case psym_switch_expr:
 		ps_push_follow(psym_switch_expr);
-		ps.s_case_ind_level[ps.tos] = case_ind;
-		case_ind = (float)ps.ind_level_follow + opt.case_indent;
 		ps.ind_level_follow += (int)opt.case_indent + 1;
 		break;
 
@@ -239,9 +237,7 @@ reduce_stmt(void)
 		return true;
 
 	case psym_switch_expr:
-		case_ind = ps.s_case_ind_level[ps.tos - 1];
-		/* FALLTHROUGH */
-	case psym_decl:		/* finish of a declaration */
+	case psym_decl:
 	case psym_if_expr_stmt_else:
 	case psym_for_exprs:
 	case psym_while_expr:

Reply via email to