Module Name:    src
Committed By:   rillig
Date:           Thu Jun  8 21:18:55 UTC 2023

Modified Files:
        src/tests/usr.bin/indent: fmt_decl.c lsym_for.c lsym_unary_op.c
        src/usr.bin/indent: debug.c indent.c indent.h lexi.c

Log Message:
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.


To generate a diff of this commit:
cvs rdiff -u -r1.49 -r1.50 src/tests/usr.bin/indent/fmt_decl.c
cvs rdiff -u -r1.6 -r1.7 src/tests/usr.bin/indent/lsym_for.c
cvs rdiff -u -r1.9 -r1.10 src/tests/usr.bin/indent/lsym_unary_op.c
cvs rdiff -u -r1.39 -r1.40 src/usr.bin/indent/debug.c
cvs rdiff -u -r1.340 -r1.341 src/usr.bin/indent/indent.c
cvs rdiff -u -r1.178 -r1.179 src/usr.bin/indent/indent.h
cvs rdiff -u -r1.216 -r1.217 src/usr.bin/indent/lexi.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/tests/usr.bin/indent/fmt_decl.c
diff -u src/tests/usr.bin/indent/fmt_decl.c:1.49 src/tests/usr.bin/indent/fmt_decl.c:1.50
--- src/tests/usr.bin/indent/fmt_decl.c:1.49	Mon Jun  5 15:02:54 2023
+++ src/tests/usr.bin/indent/fmt_decl.c	Thu Jun  8 21:18:54 2023
@@ -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

Index: src/tests/usr.bin/indent/lsym_for.c
diff -u src/tests/usr.bin/indent/lsym_for.c:1.6 src/tests/usr.bin/indent/lsym_for.c:1.7
--- src/tests/usr.bin/indent/lsym_for.c:1.6	Tue May 23 06:35:01 2023
+++ src/tests/usr.bin/indent/lsym_for.c	Thu Jun  8 21:18:54 2023
@@ -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

Index: src/tests/usr.bin/indent/lsym_unary_op.c
diff -u src/tests/usr.bin/indent/lsym_unary_op.c:1.9 src/tests/usr.bin/indent/lsym_unary_op.c:1.10
--- src/tests/usr.bin/indent/lsym_unary_op.c:1.9	Thu Jun  8 20:49:04 2023
+++ src/tests/usr.bin/indent/lsym_unary_op.c	Thu Jun  8 21:18:54 2023
@@ -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

Index: src/usr.bin/indent/debug.c
diff -u src/usr.bin/indent/debug.c:1.39 src/usr.bin/indent/debug.c:1.40
--- src/usr.bin/indent/debug.c:1.39	Wed Jun  7 15:46:11 2023
+++ src/usr.bin/indent/debug.c	Thu Jun  8 21:18:54 2023
@@ -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);

Index: src/usr.bin/indent/indent.c
diff -u src/usr.bin/indent/indent.c:1.340 src/usr.bin/indent/indent.c:1.341
--- src/usr.bin/indent/indent.c:1.340	Thu Jun  8 20:55:22 2023
+++ src/usr.bin/indent/indent.c	Thu Jun  8 21:18:54 2023
@@ -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);
 		}
 

Index: src/usr.bin/indent/indent.h
diff -u src/usr.bin/indent/indent.h:1.178 src/usr.bin/indent/indent.h:1.179
--- src/usr.bin/indent/indent.h:1.178	Thu Jun  8 06:47:13 2023
+++ src/usr.bin/indent/indent.h	Thu Jun  8 21:18:54 2023
@@ -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 */
 

Index: src/usr.bin/indent/lexi.c
diff -u src/usr.bin/indent/lexi.c:1.216 src/usr.bin/indent/lexi.c:1.217
--- src/usr.bin/indent/lexi.c:1.216	Wed Jun  7 15:46:12 2023
+++ src/usr.bin/indent/lexi.c	Thu Jun  8 21:18:54 2023
@@ -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;

Reply via email to