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 parsing of preprocessor lines wit...



details:   https://anonhg.NetBSD.org/src/rev/24cda80a6b0c
branches:  trunk
changeset: 988653:24cda80a6b0c
user:      rillig <rillig%NetBSD.org@localhost>
date:      Fri Oct 08 20:14:52 2021 +0000

description:
indent: fix parsing of preprocessor lines with comments and strings

diffstat:

 tests/usr.bin/indent/token-preprocessing.0.stdout |   6 +-
 usr.bin/indent/indent.c                           |  39 ++++++++++++----------
 2 files changed, 24 insertions(+), 21 deletions(-)

diffs (107 lines):

diff -r 49ee52d4c54c -r 24cda80a6b0c tests/usr.bin/indent/token-preprocessing.0.stdout
--- a/tests/usr.bin/indent/token-preprocessing.0.stdout Fri Oct 08 20:07:44 2021 +0000
+++ b/tests/usr.bin/indent/token-preprocessing.0.stdout Fri Oct 08 20:14:52 2021 +0000
@@ -1,4 +1,4 @@
-/* $NetBSD: token-preprocessing.0.stdout,v 1.5 2021/10/08 20:04:26 rillig Exp $ */
+/* $NetBSD: token-preprocessing.0.stdout,v 1.6 2021/10/08 20:14:52 rillig Exp $ */
 /* $FreeBSD$ */
 
 /*-
@@ -49,12 +49,12 @@
  */
 int            d1;
 #define confuse_d /*"*/ "/*"
-int d2 ;
+int            d2;
 #define resolve_d "*/"
 int            d3;
 
 int            s1;
 #define confuse_s /*'*/ '/*'
-int s2 ;
+int            s2;
 #define resolve_s '*/'
 int            s3;
diff -r 49ee52d4c54c -r 24cda80a6b0c usr.bin/indent/indent.c
--- a/usr.bin/indent/indent.c   Fri Oct 08 20:07:44 2021 +0000
+++ b/usr.bin/indent/indent.c   Fri Oct 08 20:14:52 2021 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: indent.c,v 1.128 2021/10/08 19:03:34 rillig Exp $      */
+/*     $NetBSD: indent.c,v 1.129 2021/10/08 20:14:52 rillig Exp $      */
 
 /*-
  * SPDX-License-Identifier: BSD-4-Clause
@@ -43,7 +43,7 @@
 
 #include <sys/cdefs.h>
 #if defined(__NetBSD__)
-__RCSID("$NetBSD: indent.c,v 1.128 2021/10/08 19:03:34 rillig Exp $");
+__RCSID("$NetBSD: indent.c,v 1.129 2021/10/08 20:14:52 rillig Exp $");
 #elif defined(__FreeBSD__)
 __FBSDID("$FreeBSD: head/usr.bin/indent/indent.c 340138 2018-11-04 19:24:49Z oshogbo $");
 #endif
@@ -1117,45 +1117,48 @@
 static void
 read_preprocessing_line(void)
 {
+    enum {
+       PLAIN, STR, CHR, COMM
+    } state;
+
     buf_add_char(&lab, '#');
 
-    bool in_comment = false;
+    state = PLAIN;
     int com_start = 0, com_end = 0;
-    char quote = '\0';
 
     while (is_hspace(*inp.s))
        inbuf_skip();
 
-    while (*inp.s != '\n' || (in_comment && !had_eof)) {
+    while (*inp.s != '\n' || (state == COMM && !had_eof)) {
        buf_reserve(&lab, 2);
        *lab.e++ = inbuf_next();
        switch (lab.e[-1]) {
        case '\\':
-           if (!in_comment)
+           if (state != COMM)
                *lab.e++ = inbuf_next();
            break;
        case '/':
-           if (*inp.s == '*' && !in_comment && quote == '\0') {
-               in_comment = true;
+           if (*inp.s == '*' && state == PLAIN) {
+               state = COMM;
                *lab.e++ = *inp.s++;
                com_start = (int)buf_len(&lab) - 2;
            }
            break;
        case '"':
-           if (quote == '"')
-               quote = '\0';
-           else if (quote == '\0')
-               quote = '"';
+           if (state == STR)
+               state = PLAIN;
+           else if (state == PLAIN)
+               state = STR;
            break;
        case '\'':
-           if (quote == '\'')
-               quote = '\0';
-           else if (quote == '\0')
-               quote = '\'';
+           if (state == CHR)
+               state = PLAIN;
+           else if (state == PLAIN)
+               state = CHR;
            break;
        case '*':
-           if (*inp.s == '/' && in_comment) {
-               in_comment = false;
+           if (*inp.s == '/' && state == COMM) {
+               state = PLAIN;
                *lab.e++ = *inp.s++;
                com_end = (int)buf_len(&lab);
            }



Home | Main Index | Thread Index | Old Index