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: don't force a blank line between '}' ...



details:   https://anonhg.NetBSD.org/src/rev/75eeea48d7e6
branches:  trunk
changeset: 376403:75eeea48d7e6
user:      rillig <rillig%NetBSD.org@localhost>
date:      Fri Jun 16 11:48:32 2023 +0000

description:
indent: don't force a blank line between '}' and preprocessing line

diffstat:

 tests/usr.bin/indent/opt_bap.c |  31 +++++++++++++++++++------------
 usr.bin/indent/debug.c         |   5 +++--
 usr.bin/indent/indent.c        |  12 +++++++-----
 usr.bin/indent/indent.h        |   7 ++++---
 usr.bin/indent/io.c            |  13 +++++++------
 5 files changed, 40 insertions(+), 28 deletions(-)

diffs (182 lines):

diff -r 2ac8c7861d25 -r 75eeea48d7e6 tests/usr.bin/indent/opt_bap.c
--- a/tests/usr.bin/indent/opt_bap.c    Fri Jun 16 11:27:49 2023 +0000
+++ b/tests/usr.bin/indent/opt_bap.c    Fri Jun 16 11:48:32 2023 +0000
@@ -1,4 +1,4 @@
-/* $NetBSD: opt_bap.c,v 1.9 2023/05/23 06:18:00 rillig Exp $ */
+/* $NetBSD: opt_bap.c,v 1.10 2023/06/16 11:48:32 rillig Exp $ */
 
 /*
  * Tests for the options '-bap' and '-nbap' ("blank line after procedure
@@ -113,6 +113,10 @@ example(void)
 //indent run-equals-input -bap
 
 
+/*
+ * A preprocessing line after the end of a function body does not force a blank
+ * line, as these lines are not a different syntactic layer.
+ */
 //indent input
 #if 0
 void
@@ -123,17 +127,20 @@ f(void)
 #endif
 //indent end
 
-//indent run -bacc -bap
-#if 0
-void
-f(void)
+//indent run-equals-input -bacc -bap
+
+
+/*
+ * Do not add a blank line between the end of a function body and an '#undef',
+ * as this is a common way to undefine a function-local macro.
+ */
+//indent input
+#define replace
 {
 }
-// $ The following blank line may be considered optional, as it precedes a
-// $ preprocessing line.  In that case, the -bap option would only apply to
-// $ elements on the same syntactic level, such as function definitions and
-// $ other declarations.
+#undef replace
+//indent end
 
-#else
-#endif
-//indent end
+//indent run-equals-input -bap
+
+//indent run-equals-input -bap -bacc
diff -r 2ac8c7861d25 -r 75eeea48d7e6 usr.bin/indent/debug.c
--- a/usr.bin/indent/debug.c    Fri Jun 16 11:27:49 2023 +0000
+++ b/usr.bin/indent/debug.c    Fri Jun 16 11:48:32 2023 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: debug.c,v 1.63 2023/06/16 11:27:49 rillig Exp $        */
+/*     $NetBSD: debug.c,v 1.64 2023/06/16 11:48:32 rillig Exp $        */
 
 /*-
  * Copyright (c) 2023 The NetBSD Foundation, Inc.
@@ -30,7 +30,7 @@
  */
 
 #include <sys/cdefs.h>
-__RCSID("$NetBSD: debug.c,v 1.63 2023/06/16 11:27:49 rillig Exp $");
+__RCSID("$NetBSD: debug.c,v 1.64 2023/06/16 11:48:32 rillig Exp $");
 
 #include <stdarg.h>
 #include <string.h>
@@ -125,6 +125,7 @@ const char *const line_kind_name[] = {
        "blank",
        "#if",
        "#endif",
+       "#other",
        "stmt head",
        "}",
        "block comment",
diff -r 2ac8c7861d25 -r 75eeea48d7e6 usr.bin/indent/indent.c
--- a/usr.bin/indent/indent.c   Fri Jun 16 11:27:49 2023 +0000
+++ b/usr.bin/indent/indent.c   Fri Jun 16 11:48:32 2023 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: indent.c,v 1.372 2023/06/15 11:27:36 rillig Exp $      */
+/*     $NetBSD: indent.c,v 1.373 2023/06/16 11:48:32 rillig Exp $      */
 
 /*-
  * SPDX-License-Identifier: BSD-4-Clause
@@ -38,7 +38,7 @@
  */
 
 #include <sys/cdefs.h>
-__RCSID("$NetBSD: indent.c,v 1.372 2023/06/15 11:27:36 rillig Exp $");
+__RCSID("$NetBSD: indent.c,v 1.373 2023/06/16 11:48:32 rillig Exp $");
 
 #include <sys/param.h>
 #include <err.h>
@@ -527,21 +527,23 @@ process_preprocessing(void)
                                sizeof(ifdef.item[0]) * ifdef.cap));
                }
                parser_state_back_up(ifdef.item + ifdef.len++);
-               out.line_kind = lk_if;
+               out.line_kind = lk_pre_if;
 
        } else if (dir_len >= 2 && memcmp(dir, "el", 2) == 0) {
                if (ifdef.len == 0)
                        diag(1, "Unmatched #%.*s", (int)dir_len, dir);
                else
                        parser_state_restore(ifdef.item + ifdef.len - 1);
+               out.line_kind = lk_pre_other;
 
        } else if (dir_len == 5 && memcmp(dir, "endif", 5) == 0) {
                if (ifdef.len == 0)
                        diag(1, "Unmatched #endif");
                else
                        parser_state_free(ifdef.item + --ifdef.len);
-               out.line_kind = lk_endif;
-       }
+               out.line_kind = lk_pre_endif;
+       } else
+               out.line_kind = lk_pre_other;
 }
 
 static void
diff -r 2ac8c7861d25 -r 75eeea48d7e6 usr.bin/indent/indent.h
--- a/usr.bin/indent/indent.h   Fri Jun 16 11:27:49 2023 +0000
+++ b/usr.bin/indent/indent.h   Fri Jun 16 11:48:32 2023 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: indent.h,v 1.199 2023/06/16 11:27:49 rillig Exp $      */
+/*     $NetBSD: indent.h,v 1.200 2023/06/16 11:48:32 rillig Exp $      */
 
 /*-
  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
@@ -410,8 +410,9 @@ extern struct output_state {
        enum line_kind {
                lk_other,
                lk_blank,
-               lk_if,          /* #if, #ifdef, #ifndef */
-               lk_endif,       /* #endif */
+               lk_pre_if,      /* #if, #ifdef, #ifndef */
+               lk_pre_endif,   /* #endif */
+               lk_pre_other,   /* #else, #elif, #define, #undef */
                lk_stmt_head,   /* the ')' of an incomplete statement such as
                                 * 'if (expr)' or 'for (expr; expr; expr)' */
                lk_func_end,    /* the last '}' of a function body */
diff -r 2ac8c7861d25 -r 75eeea48d7e6 usr.bin/indent/io.c
--- a/usr.bin/indent/io.c       Fri Jun 16 11:27:49 2023 +0000
+++ b/usr.bin/indent/io.c       Fri Jun 16 11:48:32 2023 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: io.c,v 1.226 2023/06/16 11:27:49 rillig Exp $  */
+/*     $NetBSD: io.c,v 1.227 2023/06/16 11:48:32 rillig Exp $  */
 
 /*-
  * SPDX-License-Identifier: BSD-4-Clause
@@ -38,7 +38,7 @@
  */
 
 #include <sys/cdefs.h>
-__RCSID("$NetBSD: io.c,v 1.226 2023/06/16 11:27:49 rillig Exp $");
+__RCSID("$NetBSD: io.c,v 1.227 2023/06/16 11:48:32 rillig Exp $");
 
 #include <stdio.h>
 
@@ -176,14 +176,15 @@ want_blank_line(void)
                return true;
        }
        if (opt.blank_line_around_conditional_compilation) {
-               if (out.prev_line_kind != lk_if && out.line_kind == lk_if)
+               if (out.prev_line_kind != lk_pre_if
+                   && out.line_kind == lk_pre_if)
                        return true;
-               if (out.prev_line_kind == lk_endif
-                   && out.line_kind != lk_endif)
+               if (out.prev_line_kind == lk_pre_endif
+                   && out.line_kind != lk_pre_endif)
                        return true;
        }
        if (opt.blank_line_after_proc && out.prev_line_kind == lk_func_end
-           && out.line_kind != lk_endif)
+           && out.line_kind != lk_pre_endif && out.line_kind != lk_pre_other)
                return true;
        if (opt.blank_line_before_block_comment
            && out.line_kind == lk_block_comment)



Home | Main Index | Thread Index | Old Index