Module Name:    src
Committed By:   rillig
Date:           Sun Oct 24 20:43:28 UTC 2021

Modified Files:
        src/tests/usr.bin/indent: opt_di.c opt_ldi.c token_semicolon.c
        src/usr.bin/indent: indent.c

Log Message:
indent: fix indentation of ad-hoc tagged variables

Seen among others in usr.bin/indent/lexi.c, variable 'keywords'.


To generate a diff of this commit:
cvs rdiff -u -r1.3 -r1.4 src/tests/usr.bin/indent/opt_di.c
cvs rdiff -u -r1.1 -r1.2 src/tests/usr.bin/indent/opt_ldi.c \
    src/tests/usr.bin/indent/token_semicolon.c
cvs rdiff -u -r1.150 -r1.151 src/usr.bin/indent/indent.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_di.c
diff -u src/tests/usr.bin/indent/opt_di.c:1.3 src/tests/usr.bin/indent/opt_di.c:1.4
--- src/tests/usr.bin/indent/opt_di.c:1.3	Sun Oct 24 19:24:22 2021
+++ src/tests/usr.bin/indent/opt_di.c	Sun Oct 24 20:43:28 2021
@@ -1,4 +1,4 @@
-/* $NetBSD: opt_di.c,v 1.3 2021/10/24 19:24:22 rillig Exp $ */
+/* $NetBSD: opt_di.c,v 1.4 2021/10/24 20:43:28 rillig Exp $ */
 /* $FreeBSD$ */
 
 /*
@@ -101,6 +101,15 @@ struct long_name long_name;
 #indent end
 
 
+/*
+ * A variable that has an ad-hoc struct/union/enum type does not need to be
+ * indented to the right of the keyword 'struct', it only needs a single space
+ * of indentation.
+ *
+ * Before NetBSD indent.c 1.151 from 2021-10-24, the indentation depended on
+ * the length of the keyword 'struct', 'union' or 'enum', together with type
+ * qualifiers like 'const' or the storage class like 'static'.
+ */
 #indent input
 struct {
 	int member;
@@ -109,14 +118,7 @@ struct {
 };
 #indent end
 
-/* FIXME: The variable name is indented by 6 spaces, should be 1. */
-#indent run -di0
-struct {
-	int member;
-}      var = {
-	3,
-};
-#indent end
+#indent run-equals-input -di0
 
 #indent run
 struct {

Index: src/tests/usr.bin/indent/opt_ldi.c
diff -u src/tests/usr.bin/indent/opt_ldi.c:1.1 src/tests/usr.bin/indent/opt_ldi.c:1.2
--- src/tests/usr.bin/indent/opt_ldi.c:1.1	Fri Oct 22 20:54:36 2021
+++ src/tests/usr.bin/indent/opt_ldi.c	Sun Oct 24 20:43:28 2021
@@ -1,4 +1,4 @@
-/* $NetBSD: opt_ldi.c,v 1.1 2021/10/22 20:54:36 rillig Exp $ */
+/* $NetBSD: opt_ldi.c,v 1.2 2021/10/24 20:43:28 rillig Exp $ */
 /* $FreeBSD$ */
 
 /*
@@ -35,3 +35,47 @@ function(void)
 	int			local;
 }
 #indent end
+
+
+/*
+ * A variable that has an ad-hoc struct/union/enum type does not need to be
+ * indented to the right of the keyword 'struct', it only needs a single space
+ * of indentation.
+ *
+ * Before NetBSD indent.c 1.151 from 2021-10-24, the indentation depended on
+ * the length of the keyword 'struct', 'union' or 'enum', together with type
+ * qualifiers like 'const' or the storage class like 'static'.
+ */
+#indent input
+{
+	struct {
+		int member;
+	} var = {
+		3,
+	};
+}
+#indent end
+
+/*
+ * Struct members use -di for indentation, no matter whether they are
+ * declared globally or locally.
+ */
+#indent run -ldi0
+{
+	struct {
+		int		member;
+	} var = {
+		3,
+	};
+}
+#indent end
+
+#indent run -ldi16
+{
+	struct {
+		int		member;
+	}		var = {
+		3,
+	};
+}
+#indent end
Index: src/tests/usr.bin/indent/token_semicolon.c
diff -u src/tests/usr.bin/indent/token_semicolon.c:1.1 src/tests/usr.bin/indent/token_semicolon.c:1.2
--- src/tests/usr.bin/indent/token_semicolon.c:1.1	Mon Oct 18 22:30:34 2021
+++ src/tests/usr.bin/indent/token_semicolon.c	Sun Oct 24 20:43:28 2021
@@ -1,4 +1,4 @@
-/* $NetBSD: token_semicolon.c,v 1.1 2021/10/18 22:30:34 rillig Exp $ */
+/* $NetBSD: token_semicolon.c,v 1.2 2021/10/24 20:43:28 rillig Exp $ */
 /* $FreeBSD$ */
 
 /*
@@ -14,7 +14,7 @@
 #indent input
 struct {
 	int member;
-}      global_var;
+} global_var;
 #indent end
 
 #indent run-equals-input -di0

Index: src/usr.bin/indent/indent.c
diff -u src/usr.bin/indent/indent.c:1.150 src/usr.bin/indent/indent.c:1.151
--- src/usr.bin/indent/indent.c:1.150	Sun Oct 24 19:33:26 2021
+++ src/usr.bin/indent/indent.c	Sun Oct 24 20:43:27 2021
@@ -1,4 +1,4 @@
-/*	$NetBSD: indent.c,v 1.150 2021/10/24 19:33:26 rillig Exp $	*/
+/*	$NetBSD: indent.c,v 1.151 2021/10/24 20:43:27 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.150 2021/10/24 19:33:26 rillig Exp $");
+__RCSID("$NetBSD: indent.c,v 1.151 2021/10/24 20:43:27 rillig Exp $");
 #elif defined(__FreeBSD__)
 __FBSDID("$FreeBSD: head/usr.bin/indent/indent.c 340138 2018-11-04 19:24:49Z oshogbo $");
 #endif
@@ -1023,8 +1023,11 @@ process_rbrace(bool *sp_sw, int *decl_in
 
     if (ps.decl_nest > 0) { /* we are in multi-level structure declaration */
 	*decl_ind = di_stack[--ps.decl_nest];
-	if (ps.decl_nest == 0 && !ps.in_parameter_declaration)
+	if (ps.decl_nest == 0 && !ps.in_parameter_declaration) {
 	    ps.just_saw_decl = 2;
+	    *decl_ind = ps.ind_level == 0
+		? opt.decl_indent : opt.local_decl_indent;
+	}
 	ps.in_decl = true;
     }
 

Reply via email to