Module Name:    src
Committed By:   rillig
Date:           Sat May 13 15:34:22 UTC 2023

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

Log Message:
indent: implement 'blank after declarations'


To generate a diff of this commit:
cvs rdiff -u -r1.8 -r1.9 src/tests/usr.bin/indent/opt_bad.c
cvs rdiff -u -r1.3 -r1.4 src/usr.bin/indent/debug.c
cvs rdiff -u -r1.127 -r1.128 src/usr.bin/indent/indent.h
cvs rdiff -u -r1.159 -r1.160 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/tests/usr.bin/indent/opt_bad.c
diff -u src/tests/usr.bin/indent/opt_bad.c:1.8 src/tests/usr.bin/indent/opt_bad.c:1.9
--- src/tests/usr.bin/indent/opt_bad.c:1.8	Sat May 13 14:19:14 2023
+++ src/tests/usr.bin/indent/opt_bad.c	Sat May 13 15:34:22 2023
@@ -1,4 +1,4 @@
-/* $NetBSD: opt_bad.c,v 1.8 2023/05/13 14:19:14 rillig Exp $ */
+/* $NetBSD: opt_bad.c,v 1.9 2023/05/13 15:34:22 rillig Exp $ */
 
 /*
  * Tests for the options '-bad' and '-nbad'.
@@ -67,10 +67,10 @@ void
 function_definition(void)
 {
 	int		local_variable;
-/* $ TODO: add empty line */
+
 	function_call();
 	int		local_variable_after_statement;
-/* $ TODO: add empty line */
+
 	function_call();
 }
 //indent end
@@ -127,12 +127,18 @@ comments(void)
 void
 initializer(void)
 {
-	int local_var_init_1[] = {
-		1
-	};
-	int local_var_init_2[] = {
-		1
-	};
+	int local_var_init_1[] = {1};
+	int local_var_init_2[] = {1};
+	function_call();
+}
+
+void
+initializer_with_blank(void)
+{
+	int local_var_init_1[] = {1};
+
+	int local_var_init_2[] = {1};
+
 	function_call();
 }
 //indent end
@@ -141,13 +147,19 @@ initializer(void)
 void
 initializer(void)
 {
-	int local_var_init_1[] = {
-		1
-	};
-	int local_var_init_2[] = {
-		1
-	};
-	/* $ TODO: Add blank line here. */
+	int local_var_init_1[] = {1};
+	int local_var_init_2[] = {1};
+
+	function_call();
+}
+
+void
+initializer_with_blank(void)
+{
+	int local_var_init_1[] = {1};
+
+	int local_var_init_2[] = {1};
+
 	function_call();
 }
 //indent end

Index: src/usr.bin/indent/debug.c
diff -u src/usr.bin/indent/debug.c:1.3 src/usr.bin/indent/debug.c:1.4
--- src/usr.bin/indent/debug.c:1.3	Sat May 13 14:30:48 2023
+++ src/usr.bin/indent/debug.c	Sat May 13 15:34:22 2023
@@ -1,4 +1,4 @@
-/*	$NetBSD: debug.c,v 1.3 2023/05/13 14:30:48 rillig Exp $	*/
+/*	$NetBSD: debug.c,v 1.4 2023/05/13 15:34:22 rillig Exp $	*/
 
 /*-
  * Copyright (c) 2023 The NetBSD Foundation, Inc.
@@ -30,7 +30,7 @@
  */
 
 #include <sys/cdefs.h>
-__RCSID("$NetBSD: debug.c,v 1.3 2023/05/13 14:30:48 rillig Exp $");
+__RCSID("$NetBSD: debug.c,v 1.4 2023/05/13 15:34:22 rillig Exp $");
 
 #include "indent.h"
 
@@ -224,6 +224,7 @@ debug_parser_state(lexer_symbol lsym)
     debug_ps_bool(decl_on_line);
     debug_ps_bool(in_decl);
     debug_ps_enum(declaration, declaration_name);
+    debug_ps_bool(blank_line_after_decl);
     debug_ps_bool(in_func_def_params);
     debug_ps_enum(in_enum, in_enum_name);
     debug_ps_bool(decl_indent_done);

Index: src/usr.bin/indent/indent.h
diff -u src/usr.bin/indent/indent.h:1.127 src/usr.bin/indent/indent.h:1.128
--- src/usr.bin/indent/indent.h:1.127	Sat May 13 14:30:48 2023
+++ src/usr.bin/indent/indent.h	Sat May 13 15:34:22 2023
@@ -1,4 +1,4 @@
-/*	$NetBSD: indent.h,v 1.127 2023/05/13 14:30:48 rillig Exp $	*/
+/*	$NetBSD: indent.h,v 1.128 2023/05/13 15:34:22 rillig Exp $	*/
 
 /*-
  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
@@ -306,11 +306,14 @@ extern struct parser_state {
     bool in_decl;		/* whether we are in a declaration. The
 				 * processing of braces is then slightly
 				 * different */
+
     enum declaration {
 	decl_no,		/* no declaration anywhere nearby */
 	decl_begin,		/* collecting tokens of a declaration */
 	decl_end,		/* finished a declaration */
     } declaration;
+    bool blank_line_after_decl;
+
     bool in_func_def_params;
     enum {
 	in_enum_no,		/* outside any 'enum { ... }' */

Index: src/usr.bin/indent/io.c
diff -u src/usr.bin/indent/io.c:1.159 src/usr.bin/indent/io.c:1.160
--- src/usr.bin/indent/io.c:1.159	Sat May 13 14:30:48 2023
+++ src/usr.bin/indent/io.c	Sat May 13 15:34:22 2023
@@ -1,4 +1,4 @@
-/*	$NetBSD: io.c,v 1.159 2023/05/13 14:30:48 rillig Exp $	*/
+/*	$NetBSD: io.c,v 1.160 2023/05/13 15:34:22 rillig Exp $	*/
 
 /*-
  * SPDX-License-Identifier: BSD-4-Clause
@@ -43,7 +43,7 @@ static char sccsid[] = "@(#)io.c	8.1 (Be
 
 #include <sys/cdefs.h>
 #if defined(__NetBSD__)
-__RCSID("$NetBSD: io.c,v 1.159 2023/05/13 14:30:48 rillig Exp $");
+__RCSID("$NetBSD: io.c,v 1.160 2023/05/13 15:34:22 rillig Exp $");
 #elif defined(__FreeBSD__)
 __FBSDID("$FreeBSD: head/usr.bin/indent/io.c 334927 2018-06-10 16:44:18Z pstef $");
 #endif
@@ -288,10 +288,22 @@ output_complete_line(char line_terminato
 
     ps.is_function_definition = false;
 
+    if (ps.blank_line_after_decl && ps.declaration == decl_no) {
+	ps.blank_line_after_decl = false;
+	if (lab.e != lab.s || code.e != code.s || com.e != com.s)
+	    output_char('\n');
+    }
+
     if (!inhibit_formatting) {
 	if (ps.ind_level == 0)
 	    ps.in_stmt_cont = false;	/* this is a class A kludge */
 
+	if (opt.blank_line_after_decl && ps.declaration == decl_end
+	    && ps.tos > 1) {
+	    ps.declaration = decl_no;
+	    ps.blank_line_after_decl = true;
+	}
+
 	int ind = 0;
 	if (lab.e != lab.s)
 	    ind = output_line_label();
@@ -301,9 +313,6 @@ output_complete_line(char line_terminato
 	    output_line_comment(ind);
 
 	output_char(line_terminator);
-
-	if (ps.declaration == decl_end && opt.blank_line_after_decl)
-	    ps.declaration = decl_no;
     }
 
     ps.decl_on_line = ps.in_decl;	/* for proper comment indentation */

Reply via email to