Module Name:    src
Committed By:   rillig
Date:           Mon May 15 18:22:40 UTC 2023

Modified Files:
        src/tests/usr.bin/indent: lsym_type_outside_parentheses.c
        src/usr.bin/indent: indent.c lexi.c

Log Message:
indent: improve type guessing, fix formatting of declarations


To generate a diff of this commit:
cvs rdiff -u -r1.4 -r1.5 \
    src/tests/usr.bin/indent/lsym_type_outside_parentheses.c
cvs rdiff -u -r1.279 -r1.280 src/usr.bin/indent/indent.c
cvs rdiff -u -r1.190 -r1.191 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_type_outside_parentheses.c
diff -u src/tests/usr.bin/indent/lsym_type_outside_parentheses.c:1.4 src/tests/usr.bin/indent/lsym_type_outside_parentheses.c:1.5
--- src/tests/usr.bin/indent/lsym_type_outside_parentheses.c:1.4	Mon May 15 17:51:49 2023
+++ src/tests/usr.bin/indent/lsym_type_outside_parentheses.c	Mon May 15 18:22:40 2023
@@ -1,4 +1,4 @@
-/* $NetBSD: lsym_type_outside_parentheses.c,v 1.4 2023/05/15 17:51:49 rillig Exp $ */
+/* $NetBSD: lsym_type_outside_parentheses.c,v 1.5 2023/05/15 18:22:40 rillig Exp $ */
 
 /*
  * Tests for the token lsym_type_outside_parentheses, which represents a type
@@ -6,6 +6,7 @@
  * function.
  *
  * See also:
+ *	fmt_decl
  *	lex_ident
  *	lsym_type_in_parentheses
  *	lsym_word
@@ -19,11 +20,12 @@
 //indent input
 t1		       *no_init_ptr;
 t2		       *init_ptr = 0;
-/* $ FIXME: Assume that an identifier after 'const' is a type name. */
-const			t3 * const_no_init_ptr;
+const t3	       *const_no_init_ptr;
 static t4	       *static_no_init_ptr;
-/* $ FIXME: Assume that an identifier after 'typedef' is a type name. */
-typedef t5 * typedef_no_init_ptr;
+typedef t5 *typedef_no_init_ptr;
+
+// $ XXX: There's no point aligning the word 'const' with the other names.
+const char	       *const names[3];
 //indent end
 
 //indent run-equals-input -di24

Index: src/usr.bin/indent/indent.c
diff -u src/usr.bin/indent/indent.c:1.279 src/usr.bin/indent/indent.c:1.280
--- src/usr.bin/indent/indent.c:1.279	Mon May 15 14:55:47 2023
+++ src/usr.bin/indent/indent.c	Mon May 15 18:22:40 2023
@@ -1,4 +1,4 @@
-/*	$NetBSD: indent.c,v 1.279 2023/05/15 14:55:47 rillig Exp $	*/
+/*	$NetBSD: indent.c,v 1.280 2023/05/15 18:22:40 rillig Exp $	*/
 
 /*-
  * SPDX-License-Identifier: BSD-4-Clause
@@ -38,7 +38,7 @@
  */
 
 #include <sys/cdefs.h>
-__RCSID("$NetBSD: indent.c,v 1.279 2023/05/15 14:55:47 rillig Exp $");
+__RCSID("$NetBSD: indent.c,v 1.280 2023/05/15 18:22:40 rillig Exp $");
 
 #include <sys/param.h>
 #include <err.h>
@@ -411,6 +411,18 @@ want_blank_before_lparen(void)
     return true;
 }
 
+static bool
+want_blank_before_lbracket(void)
+{
+    if (code.len == 0)
+	return false;
+    if (ps.prev_token == lsym_comma)
+	return true;
+    if (ps.prev_token == lsym_binary_op)
+	return true;
+    return false;
+}
+
 static void
 process_lparen_or_lbracket(void)
 {
@@ -423,7 +435,8 @@ process_lparen_or_lbracket(void)
     if (is_function_pointer_declaration()) {
 	code_add_decl_indent(ps.decl_ind, ps.tabs_to_var);
 	ps.decl_indent_done = true;
-    } else if (want_blank_before_lparen())
+    } else if (token.st[0] == '('
+	    ? want_blank_before_lparen() : want_blank_before_lbracket())
 	buf_add_char(&code, ' ');
     ps.want_blank = false;
     buf_add_char(&code, token.st[0]);

Index: src/usr.bin/indent/lexi.c
diff -u src/usr.bin/indent/lexi.c:1.190 src/usr.bin/indent/lexi.c:1.191
--- src/usr.bin/indent/lexi.c:1.190	Mon May 15 17:28:14 2023
+++ src/usr.bin/indent/lexi.c	Mon May 15 18:22:40 2023
@@ -1,4 +1,4 @@
-/*	$NetBSD: lexi.c,v 1.190 2023/05/15 17:28:14 rillig Exp $	*/
+/*	$NetBSD: lexi.c,v 1.191 2023/05/15 18:22:40 rillig Exp $	*/
 
 /*-
  * SPDX-License-Identifier: BSD-4-Clause
@@ -38,7 +38,7 @@
  */
 
 #include <sys/cdefs.h>
-__RCSID("$NetBSD: lexi.c,v 1.190 2023/05/15 17:28:14 rillig Exp $");
+__RCSID("$NetBSD: lexi.c,v 1.191 2023/05/15 18:22:40 rillig Exp $");
 
 #include <stdlib.h>
 #include <string.h>
@@ -47,6 +47,7 @@ __RCSID("$NetBSD: lexi.c,v 1.190 2023/05
 
 /* In lexi_alnum, this constant marks a type, independent of parentheses. */
 #define lsym_type lsym_type_outside_parentheses
+#define lsym_type_modifier lsym_storage_class
 
 /* must be sorted alphabetically, is used in binary search */
 static const struct keyword {
@@ -62,7 +63,7 @@ static const struct keyword {
     {"case", lsym_case_label},
     {"char", lsym_type},
     {"complex", lsym_type},
-    {"const", lsym_type},
+    {"const", lsym_type_modifier},
     {"continue", lsym_word},
     {"default", lsym_case_label},
     {"do", lsym_do},
@@ -92,7 +93,7 @@ static const struct keyword {
     {"union", lsym_tag},
     {"unsigned", lsym_type},
     {"void", lsym_type},
-    {"volatile", lsym_type},
+    {"volatile", lsym_type_modifier},
     {"while", lsym_while}
 };
 
@@ -371,7 +372,8 @@ lexi_alnum(void)
     while (ch_isblank(inp_peek()))
 	inp_skip();
 
-    ps.next_unary = ps.prev_token == lsym_tag;	/* for 'struct s *' */
+    ps.next_unary = ps.prev_token == lsym_tag
+	|| ps.prev_token == lsym_typedef;
 
     if (ps.prev_token == lsym_tag && ps.nparen == 0)
 	return lsym_type_outside_parentheses;

Reply via email to