Module Name:    src
Committed By:   rillig
Date:           Mon May 15 22:52:21 UTC 2023

Modified Files:
        src/usr.bin/indent: debug.c indent.c indent.h lexi.c

Log Message:
indent: clean up detection of whether parentheses form a cast

No functional change.


To generate a diff of this commit:
cvs rdiff -u -r1.8 -r1.9 src/usr.bin/indent/debug.c
cvs rdiff -u -r1.285 -r1.286 src/usr.bin/indent/indent.c
cvs rdiff -u -r1.141 -r1.142 src/usr.bin/indent/indent.h
cvs rdiff -u -r1.191 -r1.192 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/usr.bin/indent/debug.c
diff -u src/usr.bin/indent/debug.c:1.8 src/usr.bin/indent/debug.c:1.9
--- src/usr.bin/indent/debug.c:1.8	Mon May 15 13:37:16 2023
+++ src/usr.bin/indent/debug.c	Mon May 15 22:52:21 2023
@@ -1,4 +1,4 @@
-/*	$NetBSD: debug.c,v 1.8 2023/05/15 13:37:16 rillig Exp $	*/
+/*	$NetBSD: debug.c,v 1.9 2023/05/15 22:52:21 rillig Exp $	*/
 
 /*-
  * Copyright (c) 2023 The NetBSD Foundation, Inc.
@@ -30,7 +30,7 @@
  */
 
 #include <sys/cdefs.h>
-__RCSID("$NetBSD: debug.c,v 1.8 2023/05/15 13:37:16 rillig Exp $");
+__RCSID("$NetBSD: debug.c,v 1.9 2023/05/15 22:52:21 rillig Exp $");
 
 #include <stdarg.h>
 
@@ -99,19 +99,25 @@ const char *const psym_name[] = {
     "while_expr",
 };
 
-static const char *declaration_name[] = {
+static const char *const declaration_name[] = {
     "no",
     "begin",
     "end",
 };
 
-static const char *in_enum_name[] = {
+static const char *const in_enum_name[] = {
     "no",
     "enum",
     "type",
     "brace",
 };
 
+const char *const paren_level_cast_name[] = {
+    "(unknown cast)",
+    "(maybe cast)",
+    "(no cast)",
+};
+
 void
 debug_printf(const char *fmt, ...)
 {
@@ -208,12 +214,9 @@ ps_paren_has_changed(const struct parser
     if (prev_ps->nparen != ps.nparen)
 	return true;
 
-    for (int i = 0; i < ps.nparen; i++) {
-	if (curr[i].indent != prev[i].indent ||
-		curr[i].maybe_cast != prev[i].maybe_cast ||
-		curr[i].no_cast != prev[i].no_cast)
+    for (int i = 0; i < ps.nparen; i++)
+	if (curr[i].indent != prev[i].indent || curr[i].cast != prev[i].cast)
 	    return true;
-    }
     return false;
 }
 
@@ -225,11 +228,8 @@ debug_ps_paren(const struct parser_state
 
     debug_printf("           ps.paren:");
     for (int i = 0; i < ps.nparen; i++) {
-	const paren_level_props *props = ps.paren + i;
-	const char *cast = props->no_cast ? "(no cast)"
-	    : props->maybe_cast ? "(cast)"
-	    : "";
-	debug_printf(" %s%d", cast, props->indent);
+	debug_printf(" %s%d",
+	    paren_level_cast_name[ps.paren[i].cast], ps.paren[i].indent);
     }
     if (ps.nparen == 0)
 	debug_printf(" none");

Index: src/usr.bin/indent/indent.c
diff -u src/usr.bin/indent/indent.c:1.285 src/usr.bin/indent/indent.c:1.286
--- src/usr.bin/indent/indent.c:1.285	Mon May 15 22:35:41 2023
+++ src/usr.bin/indent/indent.c	Mon May 15 22:52:21 2023
@@ -1,4 +1,4 @@
-/*	$NetBSD: indent.c,v 1.285 2023/05/15 22:35:41 rillig Exp $	*/
+/*	$NetBSD: indent.c,v 1.286 2023/05/15 22:52:21 rillig Exp $	*/
 
 /*-
  * SPDX-License-Identifier: BSD-4-Clause
@@ -38,7 +38,7 @@
  */
 
 #include <sys/cdefs.h>
-__RCSID("$NetBSD: indent.c,v 1.285 2023/05/15 22:35:41 rillig Exp $");
+__RCSID("$NetBSD: indent.c,v 1.286 2023/05/15 22:52:21 rillig Exp $");
 
 #include <sys/param.h>
 #include <err.h>
@@ -443,7 +443,7 @@ process_lparen_or_lbracket(void)
     buf_add_char(&code, token.st[0]);
 
     int indent = ind_add(0, code.st, code.len);
-    bool no_cast = false;
+    enum paren_level_cast cast = cast_unknown;
 
     if (opt.extra_expr_indent && !opt.lineup_to_parens
 	    && ps.spaced_expr_psym != psym_0 && ps.nparen == 1
@@ -465,13 +465,12 @@ process_lparen_or_lbracket(void)
 
     if (ps.prev_token == lsym_offsetof || ps.prev_token == lsym_sizeof
 	    || ps.is_function_definition)
-	no_cast = true;
+	cast = cast_no;
 
     ps.paren[ps.nparen - 1].indent = (short)indent;
-    ps.paren[ps.nparen - 1].maybe_cast = false;
-    ps.paren[ps.nparen - 1].no_cast = no_cast;
+    ps.paren[ps.nparen - 1].cast = cast;
     debug_println("paren_indents[%d] is now %s%d",
-	ps.nparen - 1, no_cast ? "(no cast)" : "", indent);
+	ps.nparen - 1, paren_level_cast_name[cast], indent);
 }
 
 static void
@@ -482,13 +481,11 @@ process_rparen_or_rbracket(void)
 	goto unbalanced;
     }
 
-    bool maybe_cast = ps.paren[ps.nparen - 1].maybe_cast;
-    bool no_cast = ps.paren[ps.nparen - 1].no_cast;
+    enum paren_level_cast cast = ps.paren[--ps.nparen].cast;
     if (ps.decl_on_line && !ps.block_init)
-	no_cast = true;
-    ps.nparen--;
+	cast = cast_no;
 
-    if (maybe_cast && !no_cast) {
+    if (cast == cast_maybe) {
 	ps.next_unary = true;
 	ps.want_blank = opt.space_after_cast;
     } else

Index: src/usr.bin/indent/indent.h
diff -u src/usr.bin/indent/indent.h:1.141 src/usr.bin/indent/indent.h:1.142
--- src/usr.bin/indent/indent.h:1.141	Mon May 15 20:30:20 2023
+++ src/usr.bin/indent/indent.h	Mon May 15 22:52:21 2023
@@ -1,4 +1,4 @@
-/*	$NetBSD: indent.h,v 1.141 2023/05/15 20:30:20 rillig Exp $	*/
+/*	$NetBSD: indent.h,v 1.142 2023/05/15 22:52:21 rillig Exp $	*/
 
 /*-
  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
@@ -238,10 +238,11 @@ typedef struct paren_level_props {
     short indent;		/* indentation of the operand/argument,
 				 * relative to the enclosing statement; if
 				 * negative, reflected at -1 */
-    bool maybe_cast;		/* whether the parentheses may form a type
-				 * cast */
-    bool no_cast;		/* whether the parentheses definitely do not
-				 * form a type cast */
+    enum paren_level_cast {
+	cast_unknown,
+	cast_maybe,
+	cast_no,
+    } cast;			/* whether the parentheses form a type cast */
 } paren_level_props;
 
 /*
@@ -389,6 +390,7 @@ void debug_parse_stack(const char *);
 void debug_buffers(void);
 extern const char *const lsym_name[];
 extern const char *const psym_name[];
+extern const char *const paren_level_cast_name[];
 #else
 #define debug_noop() do { } while (false)
 #define	debug_printf(fmt, ...) debug_noop()

Index: src/usr.bin/indent/lexi.c
diff -u src/usr.bin/indent/lexi.c:1.191 src/usr.bin/indent/lexi.c:1.192
--- src/usr.bin/indent/lexi.c:1.191	Mon May 15 18:22:40 2023
+++ src/usr.bin/indent/lexi.c	Mon May 15 22:52:21 2023
@@ -1,4 +1,4 @@
-/*	$NetBSD: lexi.c,v 1.191 2023/05/15 18:22:40 rillig Exp $	*/
+/*	$NetBSD: lexi.c,v 1.192 2023/05/15 22:52:21 rillig Exp $	*/
 
 /*-
  * SPDX-License-Identifier: BSD-4-Clause
@@ -38,7 +38,7 @@
  */
 
 #include <sys/cdefs.h>
-__RCSID("$NetBSD: lexi.c,v 1.191 2023/05/15 18:22:40 rillig Exp $");
+__RCSID("$NetBSD: lexi.c,v 1.192 2023/05/15 22:52:21 rillig Exp $");
 
 #include <stdlib.h>
 #include <string.h>
@@ -401,8 +401,8 @@ lexi_alnum(void)
 found_typename:
 	if (ps.nparen > 0) {
 	    /* inside parentheses: cast, param list, offsetof or sizeof */
-	    if (!ps.paren[ps.nparen - 1].no_cast)
-		ps.paren[ps.nparen - 1].maybe_cast = true;
+	    if (ps.paren[ps.nparen - 1].cast == cast_unknown)
+		ps.paren[ps.nparen - 1].cast = cast_maybe;
 	}
 	if (ps.prev_token != lsym_period && ps.prev_token != lsym_unary_op) {
 	    if (kw != NULL && kw->lsym == lsym_tag) {

Reply via email to