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 spacing in declarations in for loops



details:   https://anonhg.NetBSD.org/src/rev/09a1963020c1
branches:  trunk
changeset: 375959:09a1963020c1
user:      rillig <rillig%NetBSD.org@localhost>
date:      Tue May 23 06:35:01 2023 +0000

description:
indent: fix spacing in declarations in for loops

diffstat:

 tests/usr.bin/indent/lsym_for.c |  17 ++++++++-------
 usr.bin/indent/debug.c          |  12 +++++++++-
 usr.bin/indent/indent.c         |  42 +++++++++++++++++++++++++++++++++++++++-
 usr.bin/indent/indent.h         |  10 ++++++++-
 usr.bin/indent/lexi.c           |   6 +++-
 5 files changed, 72 insertions(+), 15 deletions(-)

diffs (196 lines):

diff -r a81a0fa6624e -r 09a1963020c1 tests/usr.bin/indent/lsym_for.c
--- a/tests/usr.bin/indent/lsym_for.c   Tue May 23 06:18:00 2023 +0000
+++ b/tests/usr.bin/indent/lsym_for.c   Tue May 23 06:35:01 2023 +0000
@@ -1,4 +1,4 @@
-/* $NetBSD: lsym_for.c,v 1.5 2023/05/23 06:18:00 rillig Exp $ */
+/* $NetBSD: lsym_for.c,v 1.6 2023/05/23 06:35:01 rillig Exp $ */
 
 /*
  * Tests for the token lsym_for, which represents the keyword 'for' that
@@ -89,17 +89,18 @@ function(void)
 //indent run-equals-input
 
 
+/* Ensure that the '*' after 'list_item' is a unary operator. */
 //indent input
 {
        for (const list_item *i = first; i != NULL; i = i->next) {
        }
+       for (list_item **i = first; i != NULL; i = i->next) {
+       }
+       for (list_item *const *i = first; i != NULL; i = i->next) {
+       }
+       for (const char *const *i = first; i != NULL; i = i->next) {
+       }
 }
 //indent end
 
-//indent run
-{
-// $ FIXME: Wrong spacing after '*'.
-       for (const list_item * i = first; i != NULL; i = i->next) {
-       }
-}
-//indent end
+//indent run-equals-input
diff -r a81a0fa6624e -r 09a1963020c1 usr.bin/indent/debug.c
--- a/usr.bin/indent/debug.c    Tue May 23 06:18:00 2023 +0000
+++ b/usr.bin/indent/debug.c    Tue May 23 06:35:01 2023 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: debug.c,v 1.20 2023/05/22 10:28:59 rillig Exp $        */
+/*     $NetBSD: debug.c,v 1.21 2023/05/23 06:35:01 rillig Exp $        */
 
 /*-
  * Copyright (c) 2023 The NetBSD Foundation, Inc.
@@ -30,7 +30,7 @@
  */
 
 #include <sys/cdefs.h>
-__RCSID("$NetBSD: debug.c,v 1.20 2023/05/22 10:28:59 rillig Exp $");
+__RCSID("$NetBSD: debug.c,v 1.21 2023/05/23 06:35:01 rillig Exp $");
 
 #include <stdarg.h>
 
@@ -127,6 +127,13 @@ const char *const line_kind_name[] = {
        "block comment",
 };
 
+static const char *const decl_ptr_name[] = {
+       "start",
+       "word",
+       "word *",
+       "other",
+};
+
 void
 debug_printf(const char *fmt, ...)
 {
@@ -316,6 +323,7 @@ debug_parser_state(lexer_symbol lsym)
        debug_ps_bool(blank_line_after_decl);
        debug_ps_bool(in_func_def_params);
        debug_ps_enum(in_enum, in_enum_name);
+       debug_ps_enum(decl_ptr, decl_ptr_name);
        debug_ps_bool(decl_indent_done);
        debug_ps_int(decl_ind);
        debug_ps_bool(tabs_to_var);
diff -r a81a0fa6624e -r 09a1963020c1 usr.bin/indent/indent.c
--- a/usr.bin/indent/indent.c   Tue May 23 06:18:00 2023 +0000
+++ b/usr.bin/indent/indent.c   Tue May 23 06:35:01 2023 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: indent.c,v 1.304 2023/05/22 23:03:16 rillig Exp $      */
+/*     $NetBSD: indent.c,v 1.305 2023/05/23 06:35:01 rillig Exp $      */
 
 /*-
  * SPDX-License-Identifier: BSD-4-Clause
@@ -38,7 +38,7 @@
  */
 
 #include <sys/cdefs.h>
-__RCSID("$NetBSD: indent.c,v 1.304 2023/05/22 23:03:16 rillig Exp $");
+__RCSID("$NetBSD: indent.c,v 1.305 2023/05/23 06:35:01 rillig Exp $");
 
 #include <sys/param.h>
 #include <err.h>
@@ -334,6 +334,42 @@ code_add_decl_indent(int decl_ind, bool 
        }
 }
 
+static void
+update_ps_decl_ptr(lexer_symbol lsym)
+{
+       switch (ps.decl_ptr) {
+       case dp_start:
+               if (lsym == lsym_storage_class)
+                       ps.decl_ptr = dp_start;
+               else if (lsym == lsym_type_outside_parentheses)
+                       ps.decl_ptr = dp_word;
+               else if (lsym == lsym_word)
+                       ps.decl_ptr = dp_word;
+               else
+                       ps.decl_ptr = dp_other;
+               break;
+       case dp_word:
+               if (lsym == lsym_unary_op && token.st[0] == '*')
+                       ps.decl_ptr = dp_word_asterisk;
+               else
+                       ps.decl_ptr = dp_other;
+               break;
+       case dp_word_asterisk:
+               if (lsym == lsym_unary_op && token.st[0] == '*')
+                       ps.decl_ptr = dp_word_asterisk;
+               else
+                       ps.decl_ptr = dp_other;
+               break;
+       case dp_other:
+               if (lsym == lsym_semicolon || lsym == lsym_rbrace)
+                       ps.decl_ptr = dp_start;
+               if (lsym == lsym_lparen_or_lbracket
+                   && ps.prev_token == lsym_for)
+                       ps.decl_ptr = dp_start;
+               break;
+       }
+}
+
 static int
 process_eof(void)
 {
@@ -1006,6 +1042,8 @@ indent(void)
                                move_com_to_code(lsym);
                }
 
+               update_ps_decl_ptr(lsym);
+
                switch (lsym) {
 
                case lsym_newline:
diff -r a81a0fa6624e -r 09a1963020c1 usr.bin/indent/indent.h
--- a/usr.bin/indent/indent.h   Tue May 23 06:18:00 2023 +0000
+++ b/usr.bin/indent/indent.h   Tue May 23 06:35:01 2023 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: indent.h,v 1.157 2023/05/22 10:28:59 rillig Exp $      */
+/*     $NetBSD: indent.h,v 1.158 2023/05/23 06:35:01 rillig Exp $      */
 
 /*-
  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
@@ -371,6 +371,14 @@ extern struct parser_state {
                                 * are currently open; used to indent the
                                 * remaining lines of the statement,
                                 * initializer or declaration */
+       enum {
+           dp_start,
+           dp_word,
+           dp_word_asterisk,
+           dp_other,
+       } decl_ptr;             /* detects declarations like 'typename *x',
+                                * to prevent the '*' from being interpreted as
+                                * a binary operator */
        paren_level_props paren[20];
 
        /* Horizontal spacing for comments */
diff -r a81a0fa6624e -r 09a1963020c1 usr.bin/indent/lexi.c
--- a/usr.bin/indent/lexi.c     Tue May 23 06:18:00 2023 +0000
+++ b/usr.bin/indent/lexi.c     Tue May 23 06:35:01 2023 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: lexi.c,v 1.203 2023/05/22 22:09:45 rillig Exp $        */
+/*     $NetBSD: lexi.c,v 1.204 2023/05/23 06:35:01 rillig Exp $        */
 
 /*-
  * SPDX-License-Identifier: BSD-4-Clause
@@ -38,7 +38,7 @@
  */
 
 #include <sys/cdefs.h>
-__RCSID("$NetBSD: lexi.c,v 1.203 2023/05/22 22:09:45 rillig Exp $");
+__RCSID("$NetBSD: lexi.c,v 1.204 2023/05/23 06:35:01 rillig Exp $");
 
 #include <stdlib.h>
 #include <string.h>
@@ -438,6 +438,8 @@ found_typename:
 static bool
 is_asterisk_unary(void)
 {
+       if (ps.decl_ptr == dp_word)
+               return true;
        if (ps.next_unary || ps.in_func_def_params)
                return true;
        if (ps.prev_token == lsym_word ||



Home | Main Index | Thread Index | Old Index