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 lines around conditio...



details:   https://anonhg.NetBSD.org/src/rev/f19dcc612ab7
branches:  trunk
changeset: 375899:f19dcc612ab7
user:      rillig <rillig%NetBSD.org@localhost>
date:      Sat May 20 10:09:02 2023 +0000

description:
indent: implement blank lines around conditional compilation

diffstat:

 tests/usr.bin/indent/opt_bacc.c |  33 ++++++++++++----------
 usr.bin/indent/debug.c          |  13 +++++++-
 usr.bin/indent/indent.c         |   6 ++-
 usr.bin/indent/indent.h         |  11 ++++++-
 usr.bin/indent/io.c             |  60 ++++++++++++++++++++++++++++++----------
 5 files changed, 86 insertions(+), 37 deletions(-)

diffs (truncated from 326 to 300 lines):

diff -r 3c1127c180ab -r f19dcc612ab7 tests/usr.bin/indent/opt_bacc.c
--- a/tests/usr.bin/indent/opt_bacc.c   Sat May 20 09:41:34 2023 +0000
+++ b/tests/usr.bin/indent/opt_bacc.c   Sat May 20 10:09:02 2023 +0000
@@ -1,4 +1,4 @@
-/* $NetBSD: opt_bacc.c,v 1.11 2023/05/11 18:13:55 rillig Exp $ */
+/* $NetBSD: opt_bacc.c,v 1.12 2023/05/20 10:09:03 rillig Exp $ */
 
 /*
  * Tests for the options '-bacc' and '-nbacc' ("blank line around conditional
@@ -8,7 +8,7 @@
  * block.  For example, in front of every #ifdef and after every #endif.
  * Other blank lines surrounding such blocks are swallowed.
  *
- * The option '-nbacc' TODO.
+ * The option '-nbacc' leaves the vertical spacing as-is.
  */
 
 
@@ -21,24 +21,17 @@ int         b;
 int            c;
 //indent end
 
-/*
- * XXX: As of 2021-11-19, the option -bacc has no effect on declarations since
- * process_type resets out.blank_line_before unconditionally.
- */
 //indent run -bacc
 int            a;
-/* $ FIXME: expecting a blank line here */
+
 #if 0
 int            b;
 #endif
-/* $ FIXME: expecting a blank line here */
+
 int            c;
 //indent end
 
-/*
- * With '-nbacc' the code is unchanged since there are no blank lines to
- * remove.
- */
+/* The option '-nbacc' does not remove anything. */
 //indent run-equals-input -nbacc
 
 
@@ -80,13 +73,13 @@ os_name(void)
 const char *
 os_name(void)
 {
-/* $ FIXME: expecting a blank line here. */
+
 #if defined(__NetBSD__) || defined(__FreeBSD__)
        return "BSD";
 #else
        return "unknown";
 #endif
-/* $ FIXME: expecting a blank line here. */
+
 }
 //indent end
 
@@ -122,6 +115,16 @@ int outer_below;
 #endif
 //indent end
 
-//indent run-equals-input -di0 -bacc
+//indent run -di0 -bacc
+#ifdef outer
+int outer_above;
+
+#ifdef inner
+int inner;
+#endif
+
+int outer_below;
+#endif
+//indent end
 
 //indent run-equals-input -di0 -nbacc
diff -r 3c1127c180ab -r f19dcc612ab7 usr.bin/indent/debug.c
--- a/usr.bin/indent/debug.c    Sat May 20 09:41:34 2023 +0000
+++ b/usr.bin/indent/debug.c    Sat May 20 10:09:02 2023 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: debug.c,v 1.15 2023/05/20 02:47:35 rillig Exp $        */
+/*     $NetBSD: debug.c,v 1.16 2023/05/20 10:09:02 rillig Exp $        */
 
 /*-
  * Copyright (c) 2023 The NetBSD Foundation, Inc.
@@ -30,7 +30,7 @@
  */
 
 #include <sys/cdefs.h>
-__RCSID("$NetBSD: debug.c,v 1.15 2023/05/20 02:47:35 rillig Exp $");
+__RCSID("$NetBSD: debug.c,v 1.16 2023/05/20 10:09:02 rillig Exp $");
 
 #include <stdarg.h>
 
@@ -117,6 +117,12 @@ const char *const paren_level_cast_name[
        "(no cast)",
 };
 
+static const char *const line_kind_name[] = {
+       "other",
+       "#if",
+       "#endif",
+};
+
 void
 debug_printf(const char *fmt, ...)
 {
@@ -320,6 +326,9 @@ debug_parser_state(lexer_symbol lsym)
        debug_ps_enum(spaced_expr_psym, psym_name);
        debug_ps_int(quest_level);
 
+       debug_ps_enum(line_kind, line_kind_name);
+       debug_ps_enum(prev_line_kind, line_kind_name);
+
        prev_ps = ps;
 }
 
diff -r 3c1127c180ab -r f19dcc612ab7 usr.bin/indent/indent.c
--- a/usr.bin/indent/indent.c   Sat May 20 09:41:34 2023 +0000
+++ b/usr.bin/indent/indent.c   Sat May 20 10:09:02 2023 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: indent.c,v 1.296 2023/05/20 02:47:35 rillig Exp $      */
+/*     $NetBSD: indent.c,v 1.297 2023/05/20 10:09:02 rillig Exp $      */
 
 /*-
  * SPDX-License-Identifier: BSD-4-Clause
@@ -38,7 +38,7 @@
  */
 
 #include <sys/cdefs.h>
-__RCSID("$NetBSD: indent.c,v 1.296 2023/05/20 02:47:35 rillig Exp $");
+__RCSID("$NetBSD: indent.c,v 1.297 2023/05/20 10:09:02 rillig Exp $");
 
 #include <sys/param.h>
 #include <err.h>
@@ -975,6 +975,7 @@ process_preprocessing(void)
                        state_stack[ifdef_level++] = ps;
                else
                        diag(1, "#if stack overflow");
+               ps.line_kind = lk_if;
 
        } else if (substring_starts_with(dir, "el")) {  /* else, elif */
                if (ifdef_level <= 0)
@@ -988,6 +989,7 @@ process_preprocessing(void)
                        diag(1, "Unmatched #endif");
                else
                        ifdef_level--;
+               ps.line_kind = lk_endif;
 
        } else {
                if (!substring_equals(dir, "pragma") &&
diff -r 3c1127c180ab -r f19dcc612ab7 usr.bin/indent/indent.h
--- a/usr.bin/indent/indent.h   Sat May 20 09:41:34 2023 +0000
+++ b/usr.bin/indent/indent.h   Sat May 20 10:09:02 2023 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: indent.h,v 1.152 2023/05/20 02:47:35 rillig Exp $      */
+/*     $NetBSD: indent.h,v 1.153 2023/05/20 10:09:02 rillig Exp $      */
 
 /*-
  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
@@ -394,6 +394,14 @@ extern struct parser_state {
        } declaration;
        bool blank_line_after_decl;
 
+       enum line_kind {
+               lk_other,
+               lk_if,          /* #if, #ifdef, #ifndef */
+               lk_endif,       /* #endif */
+       } line_kind;            /* kind of the current line, is reset to
+                                * lk_other at the beginning of each line */
+       enum line_kind prev_line_kind;
+
        /* Comments */
 
        bool curr_col_1;        /* whether the current token started in column
@@ -436,7 +444,6 @@ void clear_indent_off_text(void);
 lexer_symbol lexi(void);
 void diag(int, const char *, ...) __printflike(2, 3);
 void output_line(void);
-void output_line_ff(void);
 void inp_read_line(void);
 void parse(parser_symbol);
 void process_comment(void);
diff -r 3c1127c180ab -r f19dcc612ab7 usr.bin/indent/io.c
--- a/usr.bin/indent/io.c       Sat May 20 09:41:34 2023 +0000
+++ b/usr.bin/indent/io.c       Sat May 20 10:09:02 2023 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: io.c,v 1.178 2023/05/18 05:33:27 rillig Exp $  */
+/*     $NetBSD: io.c,v 1.179 2023/05/20 10:09:02 rillig Exp $  */
 
 /*-
  * SPDX-License-Identifier: BSD-4-Clause
@@ -38,16 +38,18 @@
  */
 
 #include <sys/cdefs.h>
-__RCSID("$NetBSD: io.c,v 1.178 2023/05/18 05:33:27 rillig Exp $");
+__RCSID("$NetBSD: io.c,v 1.179 2023/05/20 10:09:02 rillig Exp $");
 
 #include <stdio.h>
-#include <string.h>
 
 #include "indent.h"
 
 struct buffer inp;
-static struct buffer indent_off_text;
-
+static struct buffer indent_off_text;  /* text from between 'INDENT OFF' and
+                                        * 'INDENT ON', both inclusive */
+static unsigned wrote_newlines = 2;    /* 0 in the middle of a line, 1 after
+                                        * a single '\n', > 1 means there were
+                                        * (n - 1) blank lines above */
 static int paren_indent;
 
 
@@ -92,10 +94,11 @@ inp_read_next_line(FILE *f)
 }
 
 static void
-output_char(char ch)
+output_newline(void)
 {
-       fputc(ch, output);
-       debug_vis_range("output_char '", &ch, 1, "'\n");
+       fputc('\n', output);
+       debug_println("output_newline");
+       wrote_newlines++;
 }
 
 static void
@@ -103,6 +106,8 @@ output_range(const char *s, size_t len)
 {
        fwrite(s, 1, len, output);
        debug_vis_range("output_range \"", s, len, "\"\n");
+       for (size_t i = 0; i < len; i++)
+               wrote_newlines = s[i] == '\n' ? wrote_newlines + 1 : 0;
 }
 
 static int
@@ -118,16 +123,41 @@ output_indent(int old_ind, int new_ind)
                for (int i = 0; i < n; i++) {
                        fputc('\t', output);
                        ind += tabsize;
+                       wrote_newlines = 0;
                }
        }
 
-       for (; ind < new_ind; ind++)
+       for (; ind < new_ind; ind++) {
                fputc(' ', output);
+               wrote_newlines = 0;
+       }
 
        debug_println("output_indent %d", ind);
        return ind;
 }
 
+static void
+maybe_output_blank_line(void)
+{
+       bool want_blank_line = false;
+
+       if (ps.blank_line_after_decl && ps.declaration == decl_no) {
+               ps.blank_line_after_decl = false;
+               want_blank_line = true;
+       }
+
+       if (opt.blanklines_around_conditional_compilation) {
+               if (ps.prev_line_kind != lk_if && ps.line_kind == lk_if)
+                       want_blank_line = true;
+               if (ps.prev_line_kind == lk_endif && ps.line_kind != lk_endif)
+                       want_blank_line = true;
+       }
+
+       if (want_blank_line && wrote_newlines < 2
+           && (lab.len > 0 || code.len > 0 || com.len > 0))
+               output_newline();
+}
+
 static int
 output_line_label(void)
 {
@@ -190,7 +220,7 @@ output_line_comment(int ind)
 
        /* if comment can't fit on this line, put it on the next line */
        if (ind > target_ind) {
-               output_char('\n');
+               output_newline();
                ind = 0;
        }
 
@@ -218,11 +248,7 @@ output_line(void)
 
        ps.is_function_definition = false;
 
-       if (ps.blank_line_after_decl && ps.declaration == decl_no) {



Home | Main Index | Thread Index | Old Index