Module Name:    src
Committed By:   rillig
Date:           Fri Jun 16 12:30:45 UTC 2023

Modified Files:
        src/tests/usr.bin/indent: fmt_decl.c lsym_typedef.c
        src/usr.bin/indent: indent.c indent.h

Log Message:
indent: fix indentation and linebreaks in typedef declarations


To generate a diff of this commit:
cvs rdiff -u -r1.57 -r1.58 src/tests/usr.bin/indent/fmt_decl.c
cvs rdiff -u -r1.7 -r1.8 src/tests/usr.bin/indent/lsym_typedef.c
cvs rdiff -u -r1.373 -r1.374 src/usr.bin/indent/indent.c
cvs rdiff -u -r1.200 -r1.201 src/usr.bin/indent/indent.h

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/fmt_decl.c
diff -u src/tests/usr.bin/indent/fmt_decl.c:1.57 src/tests/usr.bin/indent/fmt_decl.c:1.58
--- src/tests/usr.bin/indent/fmt_decl.c:1.57	Thu Jun 15 09:19:07 2023
+++ src/tests/usr.bin/indent/fmt_decl.c	Fri Jun 16 12:30:45 2023
@@ -1,4 +1,4 @@
-/*	$NetBSD: fmt_decl.c,v 1.57 2023/06/15 09:19:07 rillig Exp $	*/
+/*	$NetBSD: fmt_decl.c,v 1.58 2023/06/16 12:30:45 rillig Exp $	*/
 
 /*
  * Tests for declarations of global variables, external functions, and local
@@ -83,7 +83,7 @@ typedef struct Complex
 {
 	double		x;
 	double		y;
-}		Complex;
+} Complex;
 //indent end
 
 
@@ -606,7 +606,7 @@ ToToken(bool cond)
 typedef struct OpenDirs {
 	CachedDirList	list;
 	HashTable /* of CachedDirListNode */ table;
-}		OpenDirs;
+} OpenDirs;
 //indent end
 
 //indent run-equals-input -THashTable
@@ -1041,9 +1041,7 @@ ParseVErrorInternal(FILE *f, bool useVar
 
 typedef struct {
 	const char *m_name;
-}
-// $ FIXME: Remove this line break.
-mod_t;
+} mod_t;
 //indent end
 
 

Index: src/tests/usr.bin/indent/lsym_typedef.c
diff -u src/tests/usr.bin/indent/lsym_typedef.c:1.7 src/tests/usr.bin/indent/lsym_typedef.c:1.8
--- src/tests/usr.bin/indent/lsym_typedef.c:1.7	Fri Jun 16 11:58:33 2023
+++ src/tests/usr.bin/indent/lsym_typedef.c	Fri Jun 16 12:30:45 2023
@@ -1,4 +1,4 @@
-/* $NetBSD: lsym_typedef.c,v 1.7 2023/06/16 11:58:33 rillig Exp $ */
+/* $NetBSD: lsym_typedef.c,v 1.8 2023/06/16 12:30:45 rillig Exp $ */
 
 /*
  * Tests for the token lsym_typedef, which represents the keyword 'typedef'
@@ -70,14 +70,23 @@ typedef struct {
 	int member;
 	bool bit:1;
 } typedef_name;
+
+struct {
+	int member;
+	bool bit:1;
+} var_name;
 //indent end
 
-//indent run -di0
+//indent run
 typedef struct {
-	int member;
-// $ FIXME: No space after the ':' here.
-	bool bit: 1;
-}
-// $ FIXME: No linebreak here.
-typedef_name;
+	int		member;
+	bool		bit:1;
+} typedef_name;
+
+struct {
+	int		member;
+	bool		bit:1;
+}		var_name;
 //indent end
+
+//indent run-equals-input -di0

Index: src/usr.bin/indent/indent.c
diff -u src/usr.bin/indent/indent.c:1.373 src/usr.bin/indent/indent.c:1.374
--- src/usr.bin/indent/indent.c:1.373	Fri Jun 16 11:48:32 2023
+++ src/usr.bin/indent/indent.c	Fri Jun 16 12:30:45 2023
@@ -1,4 +1,4 @@
-/*	$NetBSD: indent.c,v 1.373 2023/06/16 11:48:32 rillig Exp $	*/
+/*	$NetBSD: indent.c,v 1.374 2023/06/16 12:30:45 rillig Exp $	*/
 
 /*-
  * SPDX-License-Identifier: BSD-4-Clause
@@ -38,7 +38,7 @@
  */
 
 #include <sys/cdefs.h>
-__RCSID("$NetBSD: indent.c,v 1.373 2023/06/16 11:48:32 rillig Exp $");
+__RCSID("$NetBSD: indent.c,v 1.374 2023/06/16 12:30:45 rillig Exp $");
 
 #include <sys/param.h>
 #include <err.h>
@@ -377,6 +377,7 @@ static bool
 is_function_pointer_declaration(void)
 {
 	return ps.in_decl
+	    && !ps.in_typedef_decl
 	    && !ps.in_init
 	    && !ps.decl_indent_done
 	    && !ps.line_has_func_def
@@ -870,8 +871,10 @@ process_semicolon(void)
 {
 	if (out.line_kind == lk_stmt_head)
 		out.line_kind = lk_other;
-	if (ps.decl_level == 0)
+	if (ps.decl_level == 0) {
 		ps.in_var_decl = false;
+		ps.in_typedef_decl = false;
+	}
 	ps.seen_case = false;	/* only needs to be reset on error */
 	ps.quest_level = 0;	/* only needs to be reset on error */
 	if (ps.prev_lsym == lsym_rparen)
@@ -924,7 +927,7 @@ process_type_outside_parentheses(void)
 	}
 
 	ps.in_var_decl = /* maybe */ true;
-	ps.in_decl = ps.prev_lsym != lsym_typedef;
+	ps.in_decl = true;
 	ps.line_has_decl = ps.in_decl;
 	if (ps.decl_level == 0)
 		ps.declaration = decl_begin;
@@ -951,7 +954,8 @@ process_word(lexer_symbol lsym)
 			else if (ps.want_blank)
 				buf_add_char(&code, ' ');
 			ps.want_blank = false;
-
+		} else if (ps.in_typedef_decl && ps.decl_level == 0) {
+			/* Do not indent typedef declarators. */
 		} else if (!ps.in_init && !ps.decl_indent_done &&
 		    ps.ind_paren_level == 0) {
 			if (opt.decl_indent == 0
@@ -1020,7 +1024,7 @@ process_lsym(lexer_symbol lsym)
 	case lsym_other_colon:	process_other_colon();	break;
 	case lsym_comma:	process_comma();	break;
 	case lsym_semicolon:	process_semicolon();	break;
-	case lsym_typedef:				goto copy_token;
+	case lsym_typedef:	ps.in_typedef_decl = true; goto copy_token;
 	case lsym_modifier:				goto copy_token;
 	case lsym_case:		ps.seen_case = true;	goto copy_token;
 	case lsym_default:	ps.seen_case = true;	goto copy_token;

Index: src/usr.bin/indent/indent.h
diff -u src/usr.bin/indent/indent.h:1.200 src/usr.bin/indent/indent.h:1.201
--- src/usr.bin/indent/indent.h:1.200	Fri Jun 16 11:48:32 2023
+++ src/usr.bin/indent/indent.h	Fri Jun 16 12:30:45 2023
@@ -1,4 +1,4 @@
-/*	$NetBSD: indent.h,v 1.200 2023/06/16 11:48:32 rillig Exp $	*/
+/*	$NetBSD: indent.h,v 1.201 2023/06/16 12:30:45 rillig Exp $	*/
 
 /*-
  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
@@ -306,6 +306,7 @@ extern struct parser_state {
 	bool in_stmt_or_decl;	/* whether in a statement or a struct
 				 * declaration or a plain declaration */
 	bool in_decl;		/* XXX: double-check the exact meaning */
+	bool in_typedef_decl;
 	bool in_var_decl;	/* starts at a type name or a '){' from a
 				 * compound literal; ends at the '(' from a
 				 * function definition or a ';' outside '{}';

Reply via email to