Module Name: src
Committed By: rillig
Date: Sat May 20 10:09:03 UTC 2023
Modified Files:
src/tests/usr.bin/indent: opt_bacc.c
src/usr.bin/indent: debug.c indent.c indent.h io.c
Log Message:
indent: implement blank lines around conditional compilation
To generate a diff of this commit:
cvs rdiff -u -r1.11 -r1.12 src/tests/usr.bin/indent/opt_bacc.c
cvs rdiff -u -r1.15 -r1.16 src/usr.bin/indent/debug.c
cvs rdiff -u -r1.296 -r1.297 src/usr.bin/indent/indent.c
cvs rdiff -u -r1.152 -r1.153 src/usr.bin/indent/indent.h
cvs rdiff -u -r1.178 -r1.179 src/usr.bin/indent/io.c
Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.
Modified files:
Index: src/tests/usr.bin/indent/opt_bacc.c
diff -u src/tests/usr.bin/indent/opt_bacc.c:1.11 src/tests/usr.bin/indent/opt_bacc.c:1.12
--- src/tests/usr.bin/indent/opt_bacc.c:1.11 Thu May 11 18:13:55 2023
+++ src/tests/usr.bin/indent/opt_bacc.c Sat May 20 10:09:03 2023
@@ -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
Index: src/usr.bin/indent/debug.c
diff -u src/usr.bin/indent/debug.c:1.15 src/usr.bin/indent/debug.c:1.16
--- src/usr.bin/indent/debug.c:1.15 Sat May 20 02:47:35 2023
+++ src/usr.bin/indent/debug.c Sat May 20 10:09:02 2023
@@ -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;
}
Index: src/usr.bin/indent/indent.c
diff -u src/usr.bin/indent/indent.c:1.296 src/usr.bin/indent/indent.c:1.297
--- src/usr.bin/indent/indent.c:1.296 Sat May 20 02:47:35 2023
+++ src/usr.bin/indent/indent.c Sat May 20 10:09:02 2023
@@ -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") &&
Index: src/usr.bin/indent/indent.h
diff -u src/usr.bin/indent/indent.h:1.152 src/usr.bin/indent/indent.h:1.153
--- src/usr.bin/indent/indent.h:1.152 Sat May 20 02:47:35 2023
+++ src/usr.bin/indent/indent.h Sat May 20 10:09:02 2023
@@ -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);
Index: src/usr.bin/indent/io.c
diff -u src/usr.bin/indent/io.c:1.178 src/usr.bin/indent/io.c:1.179
--- src/usr.bin/indent/io.c:1.178 Thu May 18 05:33:27 2023
+++ src/usr.bin/indent/io.c Sat May 20 10:09:02 2023
@@ -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) {
- ps.blank_line_after_decl = false;
- if (lab.len > 0 || code.len > 0 || com.len > 0)
- output_char('\n');
- }
+ maybe_output_blank_line();
if (indent_enabled == indent_on) {
if (ps.ind_level == 0)
@@ -243,7 +269,7 @@ output_line(void)
if (com.len > 0)
output_line_comment(ind);
- output_char('\n');
+ output_newline();
}
if (indent_enabled == indent_last_off_line) {
@@ -272,6 +298,8 @@ output_line(void)
}
ps.want_blank = false;
+ ps.prev_line_kind = ps.line_kind;
+ ps.line_kind = lk_other;
}
static int