Module Name:    src
Committed By:   rillig
Date:           Sun Jun  4 11:09:18 UTC 2023

Modified Files:
        src/usr.bin/indent: debug.c indent.c indent.h io.c

Log Message:
indent: handle the indentation of 'case' in a simpler way


To generate a diff of this commit:
cvs rdiff -u -r1.26 -r1.27 src/usr.bin/indent/debug.c
cvs rdiff -u -r1.318 -r1.319 src/usr.bin/indent/indent.c
cvs rdiff -u -r1.163 -r1.164 src/usr.bin/indent/indent.h
cvs rdiff -u -r1.187 -r1.188 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/usr.bin/indent/debug.c
diff -u src/usr.bin/indent/debug.c:1.26 src/usr.bin/indent/debug.c:1.27
--- src/usr.bin/indent/debug.c:1.26	Sun Jun  4 10:23:36 2023
+++ src/usr.bin/indent/debug.c	Sun Jun  4 11:09:18 2023
@@ -1,4 +1,4 @@
-/*	$NetBSD: debug.c,v 1.26 2023/06/04 10:23:36 rillig Exp $	*/
+/*	$NetBSD: debug.c,v 1.27 2023/06/04 11:09:18 rillig Exp $	*/
 
 /*-
  * Copyright (c) 2023 The NetBSD Foundation, Inc.
@@ -30,7 +30,7 @@
  */
 
 #include <sys/cdefs.h>
-__RCSID("$NetBSD: debug.c,v 1.26 2023/06/04 10:23:36 rillig Exp $");
+__RCSID("$NetBSD: debug.c,v 1.27 2023/06/04 11:09:18 rillig Exp $");
 
 #include <stdarg.h>
 
@@ -127,6 +127,7 @@ const char *const line_kind_name[] = {
 	"stmt head",
 	"}",
 	"block comment",
+	"case/default",
 };
 
 static const char *const decl_ptr_name[] = {
@@ -329,7 +330,6 @@ debug_parser_state(void)
 
 	debug_ps_bool(in_stmt_or_decl);
 	debug_ps_bool(in_stmt_cont);
-	debug_ps_bool(is_case_label);
 	debug_ps_bool(seen_case);
 
 	// The debug output for the parser symbols is done in 'parse' instead.

Index: src/usr.bin/indent/indent.c
diff -u src/usr.bin/indent/indent.c:1.318 src/usr.bin/indent/indent.c:1.319
--- src/usr.bin/indent/indent.c:1.318	Sun Jun  4 10:23:36 2023
+++ src/usr.bin/indent/indent.c	Sun Jun  4 11:09:18 2023
@@ -1,4 +1,4 @@
-/*	$NetBSD: indent.c,v 1.318 2023/06/04 10:23:36 rillig Exp $	*/
+/*	$NetBSD: indent.c,v 1.319 2023/06/04 11:09:18 rillig Exp $	*/
 
 /*-
  * SPDX-License-Identifier: BSD-4-Clause
@@ -38,7 +38,7 @@
  */
 
 #include <sys/cdefs.h>
-__RCSID("$NetBSD: indent.c,v 1.318 2023/06/04 10:23:36 rillig Exp $");
+__RCSID("$NetBSD: indent.c,v 1.319 2023/06/04 11:09:18 rillig Exp $");
 
 #include <sys/param.h>
 #include <err.h>
@@ -712,8 +712,9 @@ process_colon(void)
 	buf_add_char(&lab, ':');
 	code.len = 0;
 
+	if (ps.seen_case)
+		out.line_kind = lk_case_or_default;
 	ps.in_stmt_or_decl = false;
-	ps.is_case_label = ps.seen_case;
 	ps.force_nl = ps.seen_case;
 	ps.seen_case = false;
 	ps.want_blank = false;
@@ -1058,8 +1059,6 @@ process_preprocessing(void)
 
 	read_preprocessing_line();
 
-	ps.is_case_label = false;
-
 	const char *end = lab.mem + lab.len;
 	const char *dir = lab.st + 1;
 	while (dir < end && ch_isblank(*dir))

Index: src/usr.bin/indent/indent.h
diff -u src/usr.bin/indent/indent.h:1.163 src/usr.bin/indent/indent.h:1.164
--- src/usr.bin/indent/indent.h:1.163	Sun Jun  4 10:23:36 2023
+++ src/usr.bin/indent/indent.h	Sun Jun  4 11:09:18 2023
@@ -1,4 +1,4 @@
-/*	$NetBSD: indent.h,v 1.163 2023/06/04 10:23:36 rillig Exp $	*/
+/*	$NetBSD: indent.h,v 1.164 2023/06/04 11:09:18 rillig Exp $	*/
 
 /*-
  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
@@ -313,8 +313,6 @@ extern struct parser_state {
 	bool in_func_def_params;
 	bool seen_case;		/* set to true when we see a 'case', so we know
 				 * what to do with the following colon */
-	bool is_case_label;	/* 'case' and 'default' labels are indented
-				 * differently from regular labels */
 	parser_symbol spaced_expr_psym;	/* the parser symbol to be shifted
 					 * after the parenthesized expression
 					 * from a 'for', 'if', 'switch' or
@@ -425,6 +423,7 @@ extern struct output_state {
 				 * 'if (expr)' or 'for (expr; expr; expr)' */
 		lk_func_end,	/* the last '}' of a function body */
 		lk_block_comment,
+		lk_case_or_default,
 	} line_kind;		/* kind of the line that is being prepared for
 				 * output; is reset to lk_other each time after
 				 * trying to send a line to the output, even if

Index: src/usr.bin/indent/io.c
diff -u src/usr.bin/indent/io.c:1.187 src/usr.bin/indent/io.c:1.188
--- src/usr.bin/indent/io.c:1.187	Tue May 23 18:16:28 2023
+++ src/usr.bin/indent/io.c	Sun Jun  4 11:09:18 2023
@@ -1,4 +1,4 @@
-/*	$NetBSD: io.c,v 1.187 2023/05/23 18:16:28 rillig Exp $	*/
+/*	$NetBSD: io.c,v 1.188 2023/06/04 11:09:18 rillig Exp $	*/
 
 /*-
  * SPDX-License-Identifier: BSD-4-Clause
@@ -38,7 +38,7 @@
  */
 
 #include <sys/cdefs.h>
-__RCSID("$NetBSD: io.c,v 1.187 2023/05/23 18:16:28 rillig Exp $");
+__RCSID("$NetBSD: io.c,v 1.188 2023/06/04 11:09:18 rillig Exp $");
 
 #include <stdio.h>
 
@@ -174,17 +174,13 @@ is_blank_line_optional(void)
 static int
 output_line_label(void)
 {
-	int ind;
 
 	while (lab.len > 0 && ch_isblank(lab.mem[lab.len - 1]))
 		lab.len--;
 
-	ind = output_indent(0, compute_label_indent());
+	int ind = output_indent(0, compute_label_indent());
 	output_range(lab.st, lab.len);
-	ind = ind_add(ind, lab.st, lab.len);
-
-	ps.is_case_label = false;
-	return ind;
+	return ind_add(ind, lab.st, lab.len);
 }
 
 static int
@@ -376,7 +372,7 @@ compute_code_indent(void)
 int
 compute_label_indent(void)
 {
-	if (ps.is_case_label)
+	if (out.line_kind == lk_case_or_default)
 		return (int)(case_ind * (float)opt.indent_size);
 	if (lab.st[0] == '#')
 		return 0;

Reply via email to