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: fix handling of INDENT OFF/ON comments



details:   https://anonhg.NetBSD.org/src/rev/869ff6c1592b
branches:  trunk
changeset: 375383:869ff6c1592b
user:      rillig <rillig%NetBSD.org@localhost>
date:      Tue May 16 08:04:03 2023 +0000

description:
indent: fix handling of INDENT OFF/ON comments

Previously, the 'INDENT OFF' comments were interpreted when the newline
token from the line above the comment was processed, which was earlier
than could be reasonably expected.

The 'INDENT ON' comments were interpreted equally early, which led to
the situation that the 'INDENT OFF' comments were preserved literally
but the 'INDENT ON' comments weren't.

diffstat:

 tests/usr.bin/indent/indent_off_on.c |  65 ++++++-----------------------------
 usr.bin/indent/indent.c              |  12 ++++-
 usr.bin/indent/indent.h              |  10 +++-
 usr.bin/indent/io.c                  |  27 ++++++++++----
 usr.bin/indent/lexi.c                |  19 ++++++----
 5 files changed, 58 insertions(+), 75 deletions(-)

diffs (truncated from 377 to 300 lines):

diff -r 7548793d6847 -r 869ff6c1592b tests/usr.bin/indent/indent_off_on.c
--- a/tests/usr.bin/indent/indent_off_on.c      Tue May 16 07:48:50 2023 +0000
+++ b/tests/usr.bin/indent/indent_off_on.c      Tue May 16 08:04:03 2023 +0000
@@ -1,4 +1,4 @@
-/* $NetBSD: indent_off_on.c,v 1.10 2023/05/14 17:53:38 rillig Exp $ */
+/* $NetBSD: indent_off_on.c,v 1.11 2023/05/16 08:04:04 rillig Exp $ */
 
 /*
  * Tests for the comments 'INDENT OFF' and 'INDENT ON', which temporarily
@@ -15,18 +15,12 @@
 {}
 //indent end
 
-/*
- * XXX: It is asymmetric that 'INDENT OFF' is kept as is, while 'INDENT ON'
- * gets enclosed with spaces.
- */
 //indent run
 {
 }
-/* $ FIXME: This empty line must stay. */
+
 /*INDENT OFF*/
-/* $ FIXME: The empty line below must be above the 'OFF' comment. */
-
-/* INDENT ON */
+/*INDENT ON*/
 
 {
 }
@@ -45,10 +39,9 @@
 {
 }
 
+
 /*INDENT OFF*/
-/* $ FIXME: The empty line below must be above the 'OFF' comment. */
-
-/* INDENT ON */
+/*INDENT ON*/
 //indent end
 
 
@@ -59,17 +52,11 @@
 {}
 //indent end
 
-/*
- * XXX: It is asymmetric that 'INDENT OFF' is indented, while 'INDENT ON'
- * is aligned.
- */
 //indent run
 {
 }
  /* INDENT OFF */
-/* $ FIXME: The empty line below must be removed. */
-
-/* INDENT ON */
+ /* INDENT ON */
 {
 }
 //indent end
@@ -82,17 +69,11 @@
 {}
 //indent end
 
-/*
- * XXX: It is asymmetric that 'INDENT OFF' is indented, while 'INDENT ON'
- * is aligned.
- */
 //indent run
 {
 }
        /* INDENT OFF */
-/* $ FIXME: The empty line below must be removed. */
-
-/* INDENT ON */
+       /* INDENT ON */
 {
 }
 //indent end
@@ -114,9 +95,7 @@ int   decl   ;
 int decl;
 /*INDENTOFF*/
 int   decl   ;
-/* $ FIXME: The empty line below must be removed. */
-
-/* INDENTON */
+/*INDENTON*/
 int decl;
 //indent end
 
@@ -133,17 +112,11 @@ int   decl   ;
 int   decl   ;
 //indent end
 
-/*
- * XXX: It is asymmetric that 'INDENT OFF' is indented, while 'INDENT ON'
- * is pushed to the start of the line.
- */
 //indent run -di0
 int decl;
 /*             INDENT          OFF             */
 int   decl   ;
-/* $ FIXME: The empty line below must be removed. */
-
-/* INDENT              ON              */
+/*             INDENT          ON              */
 int decl;
 //indent end
 
@@ -160,11 +133,7 @@ int format( void ) {{{
 /*INDENT OFF*/
 /* No formatting takes place here. */
 int format( void ) {{{
-/* $ XXX: Why is the INDENT ON comment indented? */
-/* $ XXX: Why does the INDENT ON comment get spaces, but not the OFF comment? */
-/* $ FIXME: The empty line below must be removed. */
-
-                       /* INDENT ON */
+/*INDENT ON*/
 }
 }
 }
@@ -197,19 +166,11 @@ void indent_still_on ( void ) ;   /* due t
 //indent run
 /* INDENT OFF */
 void indent_off ( void ) ;
-/* $ FIXME: The empty line below must be removed. */
-
-/* $ XXX: The double space from the below comment got merged to a single */
-/* $ XXX: space even though the comment might be regarded to be still in */
-/* $ XXX: the OFF section. */
-/* INDENT */
+/*  INDENT */
 void           indent_on(void);
 /* INDENT OFF */
 void indent_off ( void ) ;
-/* $ FIXME: The empty line below must be removed. */
-
-/* $ XXX: The below comment got moved from column 9 to column 1. */
-/* INDENT ON */
+       /* INDENT ON */
 void           indent_on(void);        /* the comment may be indented */
 /* INDENT              OFF                                     */
 void indent_off ( void ) ;
@@ -217,8 +178,6 @@ void indent_off ( void ) ;
 void indent_still_off ( void ) ;       /* due to the word 'INDENTATION' */
 /* INDENT ON * */
 void indent_still_off ( void ) ;       /* due to the extra '*' at the end */
-/* $ FIXME: The empty line below must be removed. */
-
 /* INDENT ON */
 void           indent_on(void);
 /* INDENT: OFF */
diff -r 7548793d6847 -r 869ff6c1592b usr.bin/indent/indent.c
--- a/usr.bin/indent/indent.c   Tue May 16 07:48:50 2023 +0000
+++ b/usr.bin/indent/indent.c   Tue May 16 08:04:03 2023 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: indent.c,v 1.286 2023/05/15 22:52:21 rillig Exp $      */
+/*     $NetBSD: indent.c,v 1.287 2023/05/16 08:04:03 rillig Exp $      */
 
 /*-
  * SPDX-License-Identifier: BSD-4-Clause
@@ -38,7 +38,7 @@
  */
 
 #include <sys/cdefs.h>
-__RCSID("$NetBSD: indent.c,v 1.286 2023/05/15 22:52:21 rillig Exp $");
+__RCSID("$NetBSD: indent.c,v 1.287 2023/05/16 08:04:03 rillig Exp $");
 
 #include <sys/param.h>
 #include <err.h>
@@ -85,7 +85,7 @@ bool break_comma;
 float case_ind;
 bool had_eof;
 int line_no = 1;
-bool inhibit_formatting;
+enum indent_enabled indent_enabled;
 
 static int ifdef_level;
 static struct parser_state state_stack[5];
@@ -118,6 +118,8 @@ buf_add_char(struct buffer *buf, char ch
 void
 buf_add_chars(struct buffer *buf, const char *s, size_t len)
 {
+    if (len == 0)
+       return;
     if (len > buf->cap - buf->len)
        buf_expand(buf, len);
     memcpy(buf->mem + buf->len, s, len);
@@ -327,6 +329,10 @@ process_eof(void)
 {
     if (lab.len > 0 || code.len > 0 || com.len > 0)
        output_line();
+    if (indent_enabled != indent_on) {
+       indent_enabled = indent_last_off_line;
+       output_line();
+    }
 
     if (ps.tos > 1)            /* check for balanced braces */
        diag(1, "Stuff missing from end of file");
diff -r 7548793d6847 -r 869ff6c1592b usr.bin/indent/indent.h
--- a/usr.bin/indent/indent.h   Tue May 16 07:48:50 2023 +0000
+++ b/usr.bin/indent/indent.h   Tue May 16 08:04:03 2023 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: indent.h,v 1.143 2023/05/16 07:13:05 rillig Exp $      */
+/*     $NetBSD: indent.h,v 1.144 2023/05/16 08:04:03 rillig Exp $      */
 
 /*-
  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
@@ -229,7 +229,11 @@ extern float case_ind;             /* indentation l
                                 * n:" */
 extern bool had_eof;           /* whether input is exhausted */
 extern int line_no;            /* the current line number. */
-extern bool inhibit_formatting;        /* true if INDENT OFF is in effect */
+extern enum indent_enabled {
+    indent_on,
+    indent_off,
+    indent_last_off_line,
+} indent_enabled;
 
 #define        STACKSIZE 256
 
@@ -412,9 +416,9 @@ char inp_peek(void);
 char inp_lookahead(size_t);
 void inp_skip(void);
 char inp_next(void);
+void clear_indent_off_text(void);
 
 lexer_symbol lexi(void);
-void lex_indent_comment(void);
 void diag(int, const char *, ...) __printflike(2, 3);
 void output_line(void);
 void output_line_ff(void);
diff -r 7548793d6847 -r 869ff6c1592b usr.bin/indent/io.c
--- a/usr.bin/indent/io.c       Tue May 16 07:48:50 2023 +0000
+++ b/usr.bin/indent/io.c       Tue May 16 08:04:03 2023 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: io.c,v 1.172 2023/05/16 07:13:05 rillig Exp $  */
+/*     $NetBSD: io.c,v 1.173 2023/05/16 08:04:03 rillig Exp $  */
 
 /*-
  * SPDX-License-Identifier: BSD-4-Clause
@@ -38,7 +38,7 @@
  */
 
 #include <sys/cdefs.h>
-__RCSID("$NetBSD: io.c,v 1.172 2023/05/16 07:13:05 rillig Exp $");
+__RCSID("$NetBSD: io.c,v 1.173 2023/05/16 08:04:03 rillig Exp $");
 
 #include <stdio.h>
 #include <string.h>
@@ -50,6 +50,7 @@
  * current read position is inp.s, and the invariant inp.s < inp.e holds.
  */
 static struct buffer inp;
+static struct buffer indent_off_text;
 
 static int paren_indent;
 
@@ -103,7 +104,7 @@ inp_read_next_line(FILE *f)
     for (;;) {
        int ch = getc(f);
        if (ch == EOF) {
-           if (!inhibit_formatting) {
+           if (indent_enabled == indent_on) {
                buf_add_char(&inp, ' ');
                buf_add_char(&inp, '\n');
            }
@@ -250,7 +251,7 @@ output_complete_line(char line_terminato
            output_char('\n');
     }
 
-    if (!inhibit_formatting) {
+    if (indent_enabled == indent_on) {
        if (ps.ind_level == 0)
            ps.in_stmt_cont = false;    /* this is a class A kludge */
 
@@ -271,6 +272,12 @@ output_complete_line(char line_terminato
        output_char(line_terminator);
     }
 
+    if (indent_enabled == indent_last_off_line) {
+       indent_enabled = indent_on;
+       output_range(indent_off_text.st, indent_off_text.len);
+       indent_off_text.len = 0;
+    }
+
     ps.decl_on_line = ps.in_decl;      /* for proper comment indentation */
     ps.in_stmt_cont = ps.in_stmt_or_decl && !ps.in_decl;
     ps.decl_indent_done = false;
@@ -362,10 +369,14 @@ compute_label_indent(void)
 void
 inp_read_line(void)
 {
+    if (indent_enabled == indent_on)
+       indent_off_text.len = 0;
+    buf_add_chars(&indent_off_text, inp.mem, inp.len);
     inp_read_next_line(input);



Home | Main Index | Thread Index | Old Index