Module Name: src
Committed By: rillig
Date: Tue May 23 06:35:01 UTC 2023
Modified Files:
src/tests/usr.bin/indent: lsym_for.c
src/usr.bin/indent: debug.c indent.c indent.h lexi.c
Log Message:
indent: fix spacing in declarations in for loops
To generate a diff of this commit:
cvs rdiff -u -r1.5 -r1.6 src/tests/usr.bin/indent/lsym_for.c
cvs rdiff -u -r1.20 -r1.21 src/usr.bin/indent/debug.c
cvs rdiff -u -r1.304 -r1.305 src/usr.bin/indent/indent.c
cvs rdiff -u -r1.157 -r1.158 src/usr.bin/indent/indent.h
cvs rdiff -u -r1.203 -r1.204 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/lsym_for.c
diff -u src/tests/usr.bin/indent/lsym_for.c:1.5 src/tests/usr.bin/indent/lsym_for.c:1.6
--- src/tests/usr.bin/indent/lsym_for.c:1.5 Tue May 23 06:18:00 2023
+++ src/tests/usr.bin/indent/lsym_for.c Tue May 23 06:35:01 2023
@@ -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) {
}
-}
-//indent end
-
-//indent run
-{
-// $ FIXME: Wrong spacing after '*'.
- 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-equals-input
Index: src/usr.bin/indent/debug.c
diff -u src/usr.bin/indent/debug.c:1.20 src/usr.bin/indent/debug.c:1.21
--- src/usr.bin/indent/debug.c:1.20 Mon May 22 10:28:59 2023
+++ src/usr.bin/indent/debug.c Tue May 23 06:35:01 2023
@@ -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);
Index: src/usr.bin/indent/indent.c
diff -u src/usr.bin/indent/indent.c:1.304 src/usr.bin/indent/indent.c:1.305
--- src/usr.bin/indent/indent.c:1.304 Mon May 22 23:03:16 2023
+++ src/usr.bin/indent/indent.c Tue May 23 06:35:01 2023
@@ -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:
Index: src/usr.bin/indent/indent.h
diff -u src/usr.bin/indent/indent.h:1.157 src/usr.bin/indent/indent.h:1.158
--- src/usr.bin/indent/indent.h:1.157 Mon May 22 10:28:59 2023
+++ src/usr.bin/indent/indent.h Tue May 23 06:35:01 2023
@@ -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 */
Index: src/usr.bin/indent/lexi.c
diff -u src/usr.bin/indent/lexi.c:1.203 src/usr.bin/indent/lexi.c:1.204
--- src/usr.bin/indent/lexi.c:1.203 Mon May 22 22:09:45 2023
+++ src/usr.bin/indent/lexi.c Tue May 23 06:35:01 2023
@@ -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 ||