summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorrillig <rillig@NetBSD.org>2023-06-26 14:54:40 +0000
committerrillig <rillig@NetBSD.org>2023-06-26 14:54:40 +0000
commita8f0f20434af349eca3c46f9e2b1e6be86012f98 (patch)
tree2891dadd04b087aa62948282b0371fc8787badea
parentf0dd025e4295159d0e9c2b3449e94d3ae1d01306 (diff)
indent: in -bad mode, don't add a blank line above a comment or '}'
-rw-r--r--tests/usr.bin/indent/opt_bad.c41
-rw-r--r--usr.bin/indent/indent.c6
-rw-r--r--usr.bin/indent/io.c7
3 files changed, 25 insertions, 29 deletions
diff --git a/tests/usr.bin/indent/opt_bad.c b/tests/usr.bin/indent/opt_bad.c
index 896d10c8712..51692f37ad4 100644
--- a/tests/usr.bin/indent/opt_bad.c
+++ b/tests/usr.bin/indent/opt_bad.c
@@ -1,4 +1,4 @@
-/* $NetBSD: opt_bad.c,v 1.11 2023/06/17 22:09:24 rillig Exp $ */
+/* $NetBSD: opt_bad.c,v 1.12 2023/06/26 14:54:40 rillig Exp $ */
/*
* Tests for the options '-bad' and '-nbad'.
@@ -12,14 +12,6 @@
/* Test global declarations. */
//indent input
-int global_variable;
-void function_declaration(void);
-#if 0
-#endif
-/* comment */
-//indent end
-
-//indent run -bad
int global_variable;
void function_declaration(void);
#if 0
@@ -27,7 +19,9 @@ void function_declaration(void);
/* comment */
//indent end
-//indent run-equals-prev-output -nbad
+//indent run-equals-input -bad
+
+//indent run-equals-input -nbad
/* See FreeBSD r303599. */
@@ -110,8 +104,10 @@ comments(void)
{
int local_var_1; /* comment */
int local_var_2; /* comment */
-
+// $ Indent does not look ahead much, so it doesn't know whether this comment
+// $ will be followed by a declaration or a statement.
/* comment line */
+
function_call();
}
//indent end
@@ -169,20 +165,17 @@ initializer_with_blank(void)
//indent input
{
- int decl;
+ // $ The '}' in an initializer does not finish a declaration,
+ // $ only a semicolon does.
+ int decl1[2][2] = {
+ {1, 2},
+ {3, 4},
+ };
/* comment */
- int decl;
+ int decl2;
+ // $ If the declaration is followed by a '}' that terminates the block
+ // $ statement, * there is no need for a blank line before the '}'.
}
//indent end
-//indent run -bad -di0
-{
- int decl;
-// $ FIXME: This blank line is _between_ the declarations, not _after_ them.
-
- /* comment */
- int decl;
-// $ XXX: This blank line is unnecessary, it doesn't occur in practice, though.
-
-}
-//indent end
+//indent run-equals-input -bad -di0
diff --git a/usr.bin/indent/indent.c b/usr.bin/indent/indent.c
index e1fed15fc9b..10304330a6d 100644
--- a/usr.bin/indent/indent.c
+++ b/usr.bin/indent/indent.c
@@ -1,4 +1,4 @@
-/* $NetBSD: indent.c,v 1.384 2023/06/25 19:35:45 rillig Exp $ */
+/* $NetBSD: indent.c,v 1.385 2023/06/26 14:54:40 rillig Exp $ */
/*-
* SPDX-License-Identifier: BSD-4-Clause
@@ -38,7 +38,7 @@
*/
#include <sys/cdefs.h>
-__RCSID("$NetBSD: indent.c,v 1.384 2023/06/25 19:35:45 rillig Exp $");
+__RCSID("$NetBSD: indent.c,v 1.385 2023/06/26 14:54:40 rillig Exp $");
#include <sys/param.h>
#include <err.h>
@@ -767,6 +767,8 @@ process_rbrace(void)
}
ps.declaration = decl_no;
+ if (ps.decl_level == 0)
+ ps.blank_line_after_decl = false;
if (ps.init_level > 0)
ps.init_level--;
diff --git a/usr.bin/indent/io.c b/usr.bin/indent/io.c
index d104bbeb2ee..f0ce5561f5b 100644
--- a/usr.bin/indent/io.c
+++ b/usr.bin/indent/io.c
@@ -1,4 +1,4 @@
-/* $NetBSD: io.c,v 1.229 2023/06/17 23:03:20 rillig Exp $ */
+/* $NetBSD: io.c,v 1.230 2023/06/26 14:54:40 rillig Exp $ */
/*-
* SPDX-License-Identifier: BSD-4-Clause
@@ -38,7 +38,7 @@
*/
#include <sys/cdefs.h>
-__RCSID("$NetBSD: io.c,v 1.229 2023/06/17 23:03:20 rillig Exp $");
+__RCSID("$NetBSD: io.c,v 1.230 2023/06/26 14:54:40 rillig Exp $");
#include <stdio.h>
@@ -171,7 +171,8 @@ want_blank_line(void)
debug_println("%s: %s -> %s", __func__,
line_kind_name[out.prev_line_kind], line_kind_name[out.line_kind]);
- if (ps.blank_line_after_decl && ps.declaration == decl_no) {
+ if (ps.blank_line_after_decl && ps.declaration == decl_no
+ && (lab.len > 0 || code.len > 0)) {
ps.blank_line_after_decl = false;
return true;
}