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 line above first sta...
details: https://anonhg.NetBSD.org/src/rev/dbc1e20af584
branches: trunk
changeset: 376633:dbc1e20af584
user: rillig <rillig%NetBSD.org@localhost>
date: Mon Jun 26 20:03:09 2023 +0000
description:
indent: implement 'blank line above first statement in function body'
diffstat:
tests/usr.bin/indent/opt_badp.c | 51 ++++++++++++++++++++++++++++++++++++----
usr.bin/indent/debug.c | 13 ++++++++-
usr.bin/indent/indent.c | 22 ++++++++++++++++-
usr.bin/indent/indent.h | 16 ++++++++++++-
usr.bin/indent/io.c | 12 ++++++++-
5 files changed, 102 insertions(+), 12 deletions(-)
diffs (244 lines):
diff -r 564664ee02c2 -r dbc1e20af584 tests/usr.bin/indent/opt_badp.c
--- a/tests/usr.bin/indent/opt_badp.c Mon Jun 26 17:47:04 2023 +0000
+++ b/tests/usr.bin/indent/opt_badp.c Mon Jun 26 20:03:09 2023 +0000
@@ -1,4 +1,4 @@
-/* $NetBSD: opt_badp.c,v 1.13 2023/06/26 11:01:08 rillig Exp $ */
+/* $NetBSD: opt_badp.c,v 1.14 2023/06/26 20:03:09 rillig Exp $ */
/*
* Tests for the options '-badp' and '-nbadp'.
@@ -65,8 +65,14 @@ statement(void)
}
//indent end
-/* TODO: add blank line */
-//indent run-equals-input -badp
+//indent run -badp
+void
+statement(void)
+{
+
+ stmt();
+}
+//indent end
//indent run-equals-input -nbadp
@@ -89,7 +95,7 @@ void
declaration_statement(void)
{
int decl;
- /* $ FIXME: missing empty line */
+
stmt();
}
//indent end
@@ -146,7 +152,17 @@ nested(void)
}
//indent end
-//indent run-equals-input -badp
+//indent run -badp
+static void
+nested(void)
+{
+
+ {
+ int decl;
+ stmt();
+ }
+}
+//indent end
//indent run-equals-input -nbadp
@@ -169,3 +185,28 @@ struct {
//indent run-equals-input -di0 -badp
//indent run-equals-input -di0 -nbadp
+
+
+/* Single-line function definitions must be handled correctly as well. */
+//indent input
+void f(void) { int decl; stmt; }
+//indent end
+
+//indent run -badp
+void
+f(void)
+{
+ int decl;
+
+ stmt;
+}
+//indent end
+
+//indent run -nfbs -badp
+void
+f(void) {
+ int decl;
+
+ stmt;
+}
+//indent end
diff -r 564664ee02c2 -r dbc1e20af584 usr.bin/indent/debug.c
--- a/usr.bin/indent/debug.c Mon Jun 26 17:47:04 2023 +0000
+++ b/usr.bin/indent/debug.c Mon Jun 26 20:03:09 2023 +0000
@@ -1,4 +1,4 @@
-/* $NetBSD: debug.c,v 1.68 2023/06/23 20:43:21 rillig Exp $ */
+/* $NetBSD: debug.c,v 1.69 2023/06/26 20:03:09 rillig Exp $ */
/*-
* Copyright (c) 2023 The NetBSD Foundation, Inc.
@@ -30,7 +30,7 @@
*/
#include <sys/cdefs.h>
-__RCSID("$NetBSD: debug.c,v 1.68 2023/06/23 20:43:21 rillig Exp $");
+__RCSID("$NetBSD: debug.c,v 1.69 2023/06/26 20:03:09 rillig Exp $");
#include <stdarg.h>
#include <string.h>
@@ -113,6 +113,14 @@ static const char *const declaration_nam
"end",
};
+static const char *const badp_name[] = {
+ "none",
+ "seen{",
+ "decl",
+ "seen_decl",
+ "yes",
+};
+
const char *const paren_level_cast_name[] = {
"(unknown cast)",
"(maybe cast)",
@@ -370,6 +378,7 @@ debug_parser_state(void)
debug_ps_bool(break_after_comma);
debug_ps_bool(want_newline);
debug_ps_enum(declaration, declaration_name);
+ debug_ps_enum(badp, badp_name);
debug_ps_bool(blank_line_after_decl);
state.heading = NULL;
diff -r 564664ee02c2 -r dbc1e20af584 usr.bin/indent/indent.c
--- a/usr.bin/indent/indent.c Mon Jun 26 17:47:04 2023 +0000
+++ b/usr.bin/indent/indent.c Mon Jun 26 20:03:09 2023 +0000
@@ -1,4 +1,4 @@
-/* $NetBSD: indent.c,v 1.385 2023/06/26 14:54:40 rillig Exp $ */
+/* $NetBSD: indent.c,v 1.386 2023/06/26 20:03:09 rillig Exp $ */
/*-
* SPDX-License-Identifier: BSD-4-Clause
@@ -38,7 +38,7 @@
*/
#include <sys/cdefs.h>
-__RCSID("$NetBSD: indent.c,v 1.385 2023/06/26 14:54:40 rillig Exp $");
+__RCSID("$NetBSD: indent.c,v 1.386 2023/06/26 20:03:09 rillig Exp $");
#include <sys/param.h>
#include <err.h>
@@ -352,6 +352,22 @@ update_ps_lbrace_kind(lexer_symbol lsym)
}
static void
+update_ps_badp(lexer_symbol lsym)
+{
+ if (lsym == lsym_lbrace && ps.lbrace_kind == psym_lbrace_block
+ && ps.psyms.len == 3)
+ ps.badp = badp_seen_lbrace;
+ if (lsym == lsym_rbrace && ps.decl_level == 0)
+ ps.badp = badp_none;
+ if (lsym == lsym_type && ps.paren.len == 0
+ && (ps.badp == badp_seen_lbrace || ps.badp == badp_yes))
+ ps.badp = badp_decl;
+ if (lsym == lsym_semicolon && ps.badp == badp_decl
+ && ps.decl_level == 0)
+ ps.badp = badp_seen_decl;
+}
+
+static void
indent_declarator(int decl_ind, bool tabs_to_var)
{
int base = ps.ind_level * opt.indent_size;
@@ -1122,6 +1138,8 @@ indent(void)
process_lsym(lsym);
+ if (opt.blank_line_after_decl_at_top)
+ update_ps_badp(lsym);
if (lsym != lsym_preprocessing
&& lsym != lsym_newline
&& lsym != lsym_comment)
diff -r 564664ee02c2 -r dbc1e20af584 usr.bin/indent/indent.h
--- a/usr.bin/indent/indent.h Mon Jun 26 17:47:04 2023 +0000
+++ b/usr.bin/indent/indent.h Mon Jun 26 20:03:09 2023 +0000
@@ -1,4 +1,4 @@
-/* $NetBSD: indent.h,v 1.203 2023/06/23 20:43:21 rillig Exp $ */
+/* $NetBSD: indent.h,v 1.204 2023/06/26 20:03:09 rillig Exp $ */
/*-
* SPDX-License-Identifier: BSD-2-Clause-FreeBSD
@@ -404,6 +404,20 @@ extern struct parser_state {
decl_end, /* finished a declaration */
} declaration;
bool blank_line_after_decl;
+
+ enum {
+ badp_none,
+ badp_seen_lbrace,
+ badp_decl, /* in an unfinished declaration in the first
+ * block of declarations in a function body */
+ badp_seen_decl, /* seen the semicolon of a declaration; the
+ * next line is a candidate for inserting a
+ * blank line above */
+ badp_yes, /* this line is a candidate for inserting a
+ * blank line above, unless the line turns out
+ * to start a declaration */
+ } badp; /* insert a blank line before the first
+ * statement in a function body */
} ps;
extern struct output_state {
diff -r 564664ee02c2 -r dbc1e20af584 usr.bin/indent/io.c
--- a/usr.bin/indent/io.c Mon Jun 26 17:47:04 2023 +0000
+++ b/usr.bin/indent/io.c Mon Jun 26 20:03:09 2023 +0000
@@ -1,4 +1,4 @@
-/* $NetBSD: io.c,v 1.230 2023/06/26 14:54:40 rillig Exp $ */
+/* $NetBSD: io.c,v 1.231 2023/06/26 20:03:09 rillig Exp $ */
/*-
* SPDX-License-Identifier: BSD-4-Clause
@@ -38,7 +38,7 @@
*/
#include <sys/cdefs.h>
-__RCSID("$NetBSD: io.c,v 1.230 2023/06/26 14:54:40 rillig Exp $");
+__RCSID("$NetBSD: io.c,v 1.231 2023/06/26 20:03:09 rillig Exp $");
#include <stdio.h>
@@ -176,6 +176,11 @@ want_blank_line(void)
ps.blank_line_after_decl = false;
return true;
}
+ if (ps.badp == badp_yes) {
+ ps.badp = badp_none;
+ return true;
+ }
+
if (opt.blank_line_around_conditional_compilation) {
if (out.prev_line_kind != lk_pre_if
&& out.line_kind == lk_pre_if)
@@ -400,6 +405,9 @@ prepare_next_line(void)
ps.ind_level = ps.ind_level_follow;
ps.ind_paren_level = (int)ps.paren.len;
ps.want_blank = false;
+ if ((ps.badp == badp_seen_lbrace || ps.badp == badp_seen_decl)
+ && !ps.in_decl)
+ ps.badp = badp_yes;
if (ps.paren.len > 0) {
/* TODO: explain what negative indentation means */
Home |
Main Index |
Thread Index |
Old Index