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: remove fragile heuristic for detectin...



details:   https://anonhg.NetBSD.org/src/rev/a710eded1bf6
branches:  trunk
changeset: 376276:a710eded1bf6
user:      rillig <rillig%NetBSD.org@localhost>
date:      Thu Jun 08 21:18:54 2023 +0000

description:
indent: remove fragile heuristic for detecting cast expressions

The assumption that in an expression of the form '(a * anything)', the
'*' marks a pointer type was too simple-minded.

For now, fix the obvious cases and leave the others for later.  If
needed, they can be worked around using the '-T' option.

diffstat:

 tests/usr.bin/indent/fmt_decl.c      |  20 +++++++++++++++-----
 tests/usr.bin/indent/lsym_for.c      |  18 ++++++++++++++++--
 tests/usr.bin/indent/lsym_unary_op.c |  12 +++++++-----
 usr.bin/indent/debug.c               |  12 ++----------
 usr.bin/indent/indent.c              |  24 ++----------------------
 usr.bin/indent/indent.h              |  10 +---------
 usr.bin/indent/lexi.c                |   6 +++---
 7 files changed, 46 insertions(+), 56 deletions(-)

diffs (252 lines):

diff -r 5f4e95353c9a -r a710eded1bf6 tests/usr.bin/indent/fmt_decl.c
--- a/tests/usr.bin/indent/fmt_decl.c   Thu Jun 08 21:14:09 2023 +0000
+++ b/tests/usr.bin/indent/fmt_decl.c   Thu Jun 08 21:18:54 2023 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: fmt_decl.c,v 1.49 2023/06/05 15:02:54 rillig Exp $     */
+/*     $NetBSD: fmt_decl.c,v 1.50 2023/06/08 21:18:54 rillig Exp $     */
 
 /*
  * Tests for declarations of global variables, external functions, and local
@@ -570,7 +570,17 @@ buffer_add(buffer *buf, char ch)
 }
 //indent end
 
-//indent run-equals-input
+//indent run
+void           buffer_add(buffer *, char);
+// $ FIXME: There should be no space after the '*'.
+void           buffer_add(buffer * buf, char ch);
+
+void
+buffer_add(buffer *buf, char ch)
+{
+       *buf->e++ = ch;
+}
+//indent end
 
 
 /*
@@ -788,8 +798,7 @@ char str[*ptr * *ptr];
 char str[sizeof(expr * expr)];
 char str[sizeof(int) * expr];
 char str[sizeof(*ptr)];
-/* $ FIXME: should be 'type **' */
-char str[sizeof(type * *)];
+char str[sizeof(type **)];
 char str[sizeof(**ptr)];
 //indent end
 
@@ -970,7 +979,8 @@ void
 //indent run -ci4 -di0 -ndj -nlp
 void
 // $ FIXME: Wrong indentation, should be 0 instead.
-     (error_at)(int msgid, const pos_t *pos, ...)
+// $ FIXME: There should be no space after the '*'.
+     (error_at)(int msgid, const pos_t * pos, ...)
 {
 }
 //indent end
diff -r 5f4e95353c9a -r a710eded1bf6 tests/usr.bin/indent/lsym_for.c
--- a/tests/usr.bin/indent/lsym_for.c   Thu Jun 08 21:14:09 2023 +0000
+++ b/tests/usr.bin/indent/lsym_for.c   Thu Jun 08 21:18:54 2023 +0000
@@ -1,4 +1,4 @@
-/* $NetBSD: lsym_for.c,v 1.6 2023/05/23 06:35:01 rillig Exp $ */
+/* $NetBSD: lsym_for.c,v 1.7 2023/06/08 21:18:54 rillig Exp $ */
 
 /*
  * Tests for the token lsym_for, which represents the keyword 'for' that
@@ -103,4 +103,18 @@ function(void)
 }
 //indent end
 
-//indent run-equals-input
+//indent run
+{
+// $ FIXME: There should be no space after the '*'.
+       for (const list_item * i = first; i != NULL; i = i->next) {
+       }
+// $ FIXME: There should be no space after the '*'.
+       for (list_item * *i = first; i != NULL; i = i->next) {
+       }
+// $ FIXME: There should be no space after the '*'.
+       for (list_item * const *i = first; i != NULL; i = i->next) {
+       }
+       for (const char *const *i = first; i != NULL; i = i->next) {
+       }
+}
+//indent end
diff -r 5f4e95353c9a -r a710eded1bf6 tests/usr.bin/indent/lsym_unary_op.c
--- a/tests/usr.bin/indent/lsym_unary_op.c      Thu Jun 08 21:14:09 2023 +0000
+++ b/tests/usr.bin/indent/lsym_unary_op.c      Thu Jun 08 21:18:54 2023 +0000
@@ -1,4 +1,4 @@
-/* $NetBSD: lsym_unary_op.c,v 1.9 2023/06/08 20:49:04 rillig Exp $ */
+/* $NetBSD: lsym_unary_op.c,v 1.10 2023/06/08 21:18:54 rillig Exp $ */
 
 /*
  * Tests for the token lsym_unary_op, which represents a unary operator.
@@ -79,6 +79,10 @@ unary_operators(void)
 //indent run-equals-input -di0
 
 
+/*
+ * Ensure that a '*' is not interpreted as unary operator in situations that
+ * may look like a cast expression.
+ */
 //indent input
 {
 sbuf_t *sb = *(sbuf_t **)sp;
@@ -91,9 +95,7 @@ a = (2 * b == c);
 {
 // $ FIXME: Wrong spacing after the cast.
        sbuf_t *sb = *(sbuf_t **) sp;
-// $ FIXME: Wrong spacing after the '*'.
-       return (int)(a *(float)b);
-// $ FIXME: Wrong spacing after the '*'.
-       a = (2 *b == c);
+       return (int)(a * (float)b);
+       a = (2 * b == c);
 }
 //indent end
diff -r 5f4e95353c9a -r a710eded1bf6 usr.bin/indent/debug.c
--- a/usr.bin/indent/debug.c    Thu Jun 08 21:14:09 2023 +0000
+++ b/usr.bin/indent/debug.c    Thu Jun 08 21:18:54 2023 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: debug.c,v 1.39 2023/06/07 15:46:11 rillig Exp $        */
+/*     $NetBSD: debug.c,v 1.40 2023/06/08 21:18:54 rillig Exp $        */
 
 /*-
  * Copyright (c) 2023 The NetBSD Foundation, Inc.
@@ -30,7 +30,7 @@
  */
 
 #include <sys/cdefs.h>
-__RCSID("$NetBSD: debug.c,v 1.39 2023/06/07 15:46:11 rillig Exp $");
+__RCSID("$NetBSD: debug.c,v 1.40 2023/06/08 21:18:54 rillig Exp $");
 
 #include <stdarg.h>
 
@@ -135,13 +135,6 @@ static const char *const extra_expr_inde
        "last",
 };
 
-static const char *const decl_ptr_name[] = {
-       "start",
-       "word",
-       "word *",
-       "other",
-};
-
 static unsigned wrote_newlines = 1;
 
 
@@ -333,7 +326,6 @@ debug_parser_state(void)
        debug_ps_int(line_start_nparen);
        debug_ps_int(nparen);
        debug_ps_paren(&prev_ps);
-       debug_ps_enum(decl_ptr, decl_ptr_name);
 
        debug_println("horizontal spacing for comments");
        debug_ps_int(comment_delta);
diff -r 5f4e95353c9a -r a710eded1bf6 usr.bin/indent/indent.c
--- a/usr.bin/indent/indent.c   Thu Jun 08 21:14:09 2023 +0000
+++ b/usr.bin/indent/indent.c   Thu Jun 08 21:18:54 2023 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: indent.c,v 1.340 2023/06/08 20:55:22 rillig Exp $      */
+/*     $NetBSD: indent.c,v 1.341 2023/06/08 21:18:54 rillig Exp $      */
 
 /*-
  * SPDX-License-Identifier: BSD-4-Clause
@@ -38,7 +38,7 @@
  */
 
 #include <sys/cdefs.h>
-__RCSID("$NetBSD: indent.c,v 1.340 2023/06/08 20:55:22 rillig Exp $");
+__RCSID("$NetBSD: indent.c,v 1.341 2023/06/08 21:18:54 rillig Exp $");
 
 #include <sys/param.h>
 #include <err.h>
@@ -334,25 +334,6 @@ indent_declarator(int decl_ind, bool tab
 }
 
 static void
-update_ps_decl_ptr(lexer_symbol lsym)
-{
-       if (lsym == lsym_semicolon
-           || lsym == lsym_lbrace
-           || lsym == lsym_rbrace
-           || (lsym == lsym_lparen && ps.prev_lsym != lsym_sizeof)
-           || (lsym == lsym_comma && ps.in_decl)
-           || lsym == lsym_modifier)
-               ps.decl_ptr = dp_start;
-       else if (ps.decl_ptr == dp_start && lsym == lsym_word)
-               ps.decl_ptr = dp_word;
-       else if ((ps.decl_ptr == dp_word || ps.decl_ptr == dp_word_asterisk)
-           && (lsym == lsym_unary_op && token.s[0] == '*'))
-               ps.decl_ptr = dp_word_asterisk;
-       else
-               ps.decl_ptr = dp_other;
-}
-
-static void
 update_ps_lbrace_kind(lexer_symbol lsym)
 {
        if (lsym == lsym_tag) {
@@ -1106,7 +1087,6 @@ indent(void)
                        ps.in_stmt_or_decl = true;
                        if (com.len > 0)
                                move_com_to_code(lsym);
-                       update_ps_decl_ptr(lsym);
                        update_ps_lbrace_kind(lsym);
                }
 
diff -r 5f4e95353c9a -r a710eded1bf6 usr.bin/indent/indent.h
--- a/usr.bin/indent/indent.h   Thu Jun 08 21:14:09 2023 +0000
+++ b/usr.bin/indent/indent.h   Thu Jun 08 21:18:54 2023 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: indent.h,v 1.178 2023/06/08 06:47:13 rillig Exp $      */
+/*     $NetBSD: indent.h,v 1.179 2023/06/08 21:18:54 rillig Exp $      */
 
 /*-
  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
@@ -374,14 +374,6 @@ extern struct parser_state {
                                 * remaining lines of the statement,
                                 * initializer or declaration */
        paren_level_props paren[20];
-       enum {
-               dp_start,       /* the beginning of a declaration */
-               dp_word,        /* seen a type name */
-               dp_word_asterisk,       /* seen a type name and some '*' */
-               dp_other,
-       } decl_ptr;             /* detects declarations like 'typename *x', to
-                                * prevent the '*' from being interpreted as a
-                                * binary operator */
 
        /* Horizontal spacing for comments */
 
diff -r 5f4e95353c9a -r a710eded1bf6 usr.bin/indent/lexi.c
--- a/usr.bin/indent/lexi.c     Thu Jun 08 21:14:09 2023 +0000
+++ b/usr.bin/indent/lexi.c     Thu Jun 08 21:18:54 2023 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: lexi.c,v 1.216 2023/06/07 15:46:12 rillig Exp $        */
+/*     $NetBSD: lexi.c,v 1.217 2023/06/08 21:18:54 rillig Exp $        */
 
 /*-
  * SPDX-License-Identifier: BSD-4-Clause
@@ -38,7 +38,7 @@
  */
 
 #include <sys/cdefs.h>
-__RCSID("$NetBSD: lexi.c,v 1.216 2023/06/07 15:46:12 rillig Exp $");
+__RCSID("$NetBSD: lexi.c,v 1.217 2023/06/08 21:18:54 rillig Exp $");
 
 #include <stdlib.h>
 #include <string.h>
@@ -433,7 +433,7 @@ found_typename:
 static bool
 is_asterisk_unary(void)
 {
-       if (ps.decl_ptr == dp_word)
+       if (inp_p[strspn(inp_p, "* \t")] == ')')
                return true;
        if (ps.next_unary || ps.in_func_def_params)
                return true;



Home | Main Index | Thread Index | Old Index