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: improve heuristic for spaces around '...



details:   https://anonhg.NetBSD.org/src/rev/a869300b0f13
branches:  trunk
changeset: 1026464:a869300b0f13
user:      rillig <rillig%NetBSD.org@localhost>
date:      Thu Nov 25 18:36:30 2021 +0000

description:
indent: improve heuristic for spaces around '*' in declarations

diffstat:

 tests/usr.bin/indent/fmt_decl.c    |  37 +++++++++++++++++++++----------------
 tests/usr.bin/indent/lsym_sizeof.c |   9 ++-------
 usr.bin/indent/lexi.c              |  18 ++++++++++++++----
 3 files changed, 37 insertions(+), 27 deletions(-)

diffs (136 lines):

diff -r 23e93681ca6f -r a869300b0f13 tests/usr.bin/indent/fmt_decl.c
--- a/tests/usr.bin/indent/fmt_decl.c   Thu Nov 25 18:20:21 2021 +0000
+++ b/tests/usr.bin/indent/fmt_decl.c   Thu Nov 25 18:36:30 2021 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: fmt_decl.c,v 1.28 2021/11/25 18:20:21 rillig Exp $     */
+/*     $NetBSD: fmt_decl.c,v 1.29 2021/11/25 18:36:30 rillig Exp $     */
 /* $FreeBSD: head/usr.bin/indent/tests/declarations.0 334478 2018-06-01 09:41:15Z pstef $ */
 
 /*
@@ -590,7 +590,17 @@
 #indent end
 
 /* Before lexi.c 1.156 from 2021-11-25, indent generated 'buffer * buf'. */
-#indent run-equals-input
+#indent run
+void           buffer_add(buffer *, char);
+/* $ FIXME: space after '*' */
+void           buffer_add(buffer * buf, char ch);
+
+void
+buffer_add(buffer *buf, char ch)
+{
+       *buf->e++ = ch;
+}
+#indent end
 
 
 /*
@@ -793,9 +803,8 @@
 
 /*
  * In declarations, most occurrences of '*' are pointer type derivations.
- * There are a few exceptions though.
- *
- * Broken since lexi.c 1.156 from 2021-11-25.
+ * There are a few exceptions though. Some of these are hard to detect
+ * without knowing which identifiers are type names.
  */
 #indent input
 char str[expr * expr];
@@ -809,17 +818,13 @@
 #indent end
 
 #indent run -di0
-/* $ FIXME: The '*' must be a binary operator. */
-char str[expr *expr];
-/* $ FIXME: The first '*' must be a binary operator. */
-char str[expr **ptr];
-/* $ FIXME: The second '*' must be a binary operator. */
-char str[*ptr **ptr];
-/* $ FIXME: The '*' must be a binary operator. */
-char str[sizeof(expr *expr)];
-/* $ FIXME: The '*' must be a binary operator. */
-char str[sizeof(int) *expr];
+char str[expr * expr];
+char str[expr * *ptr];
+char str[*ptr * *ptr];
+char str[sizeof(expr * expr)];
+char str[sizeof(int) * expr];
 char str[sizeof(*ptr)];
-char str[sizeof(type **)];
+/* $ FIXME: should be 'type **' */
+char str[sizeof(type * *)];
 char str[sizeof(**ptr)];
 #indent end
diff -r 23e93681ca6f -r a869300b0f13 tests/usr.bin/indent/lsym_sizeof.c
--- a/tests/usr.bin/indent/lsym_sizeof.c        Thu Nov 25 18:20:21 2021 +0000
+++ b/tests/usr.bin/indent/lsym_sizeof.c        Thu Nov 25 18:36:30 2021 +0000
@@ -1,4 +1,4 @@
-/* $NetBSD: lsym_sizeof.c,v 1.2 2021/11/25 18:10:23 rillig Exp $ */
+/* $NetBSD: lsym_sizeof.c,v 1.3 2021/11/25 18:36:30 rillig Exp $ */
 /* $FreeBSD$ */
 
 /*
@@ -20,14 +20,9 @@
 /*
  * After 'sizeof', a type name in parentheses does not start a cast
  * expression.
- *
- * Broken since lexi.c 1.156 from 2021-11-25.
  */
 #indent input
 char str[sizeof(int) * CHAR_BIT + 1];
 #indent end
 
-/* FIXME: The '*' must be a binary operator here. */
-#indent run -di0
-char str[sizeof(int) *CHAR_BIT + 1];
-#indent end
+#indent run-equals-input -di0
diff -r 23e93681ca6f -r a869300b0f13 usr.bin/indent/lexi.c
--- a/usr.bin/indent/lexi.c     Thu Nov 25 18:20:21 2021 +0000
+++ b/usr.bin/indent/lexi.c     Thu Nov 25 18:36:30 2021 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: lexi.c,v 1.162 2021/11/25 17:50:00 rillig Exp $        */
+/*     $NetBSD: lexi.c,v 1.163 2021/11/25 18:36:30 rillig Exp $        */
 
 /*-
  * SPDX-License-Identifier: BSD-4-Clause
@@ -43,7 +43,7 @@
 
 #include <sys/cdefs.h>
 #if defined(__NetBSD__)
-__RCSID("$NetBSD: lexi.c,v 1.162 2021/11/25 17:50:00 rillig Exp $");
+__RCSID("$NetBSD: lexi.c,v 1.163 2021/11/25 18:36:30 rillig Exp $");
 #elif defined(__FreeBSD__)
 __FBSDID("$FreeBSD: head/usr.bin/indent/lexi.c 337862 2018-08-15 18:19:45Z pstef $");
 #endif
@@ -552,6 +552,17 @@
     return is_type ? lsym_type_in_parentheses : lsym_word;
 }
 
+static bool
+is_asterisk_unary(void)
+{
+    if (ps.next_unary || ps.in_parameter_declaration)
+       return true;
+    if (ps.prev_token == lsym_word ||
+           ps.prev_token == lsym_rparen_or_rbracket)
+       return false;
+    return ps.in_decl && ps.p_l_follow > 0;
+}
+
 static void
 lex_asterisk_unary(void)
 {
@@ -694,8 +705,7 @@
        break;
 
     case '*':
-       if (ps.next_unary || ps.in_parameter_declaration ||
-               (ps.in_decl && ps.p_l_follow > 0)) {
+       if (is_asterisk_unary()) {
            lex_asterisk_unary();
            lsym = lsym_unary_op;
            next_unary = true;



Home | Main Index | Thread Index | Old Index