Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/usr.bin/indent indent: implement 'blank after declarations'



details:   https://anonhg.NetBSD.org/src/rev/f6234f187cff
branches:  trunk
changeset: 374829:f6234f187cff
user:      rillig <rillig%NetBSD.org@localhost>
date:      Sat May 13 15:34:22 2023 +0000

description:
indent: implement 'blank after declarations'

diffstat:

 tests/usr.bin/indent/opt_bad.c |  44 ++++++++++++++++++++++++++---------------
 usr.bin/indent/debug.c         |   5 ++-
 usr.bin/indent/indent.h        |   5 +++-
 usr.bin/indent/io.c            |  19 +++++++++++++----
 4 files changed, 49 insertions(+), 24 deletions(-)

diffs (175 lines):

diff -r 59ae2846f5a1 -r f6234f187cff tests/usr.bin/indent/opt_bad.c
--- a/tests/usr.bin/indent/opt_bad.c    Sat May 13 15:12:13 2023 +0000
+++ b/tests/usr.bin/indent/opt_bad.c    Sat May 13 15:34:22 2023 +0000
@@ -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
diff -r 59ae2846f5a1 -r f6234f187cff usr.bin/indent/debug.c
--- a/usr.bin/indent/debug.c    Sat May 13 15:12:13 2023 +0000
+++ b/usr.bin/indent/debug.c    Sat May 13 15:34:22 2023 +0000
@@ -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);
diff -r 59ae2846f5a1 -r f6234f187cff usr.bin/indent/indent.h
--- a/usr.bin/indent/indent.h   Sat May 13 15:12:13 2023 +0000
+++ b/usr.bin/indent/indent.h   Sat May 13 15:34:22 2023 +0000
@@ -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 { ... }' */
diff -r 59ae2846f5a1 -r f6234f187cff usr.bin/indent/io.c
--- a/usr.bin/indent/io.c       Sat May 13 15:12:13 2023 +0000
+++ b/usr.bin/indent/io.c       Sat May 13 15:34:22 2023 +0000
@@ -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 */



Home | Main Index | Thread Index | Old Index