Module Name:    src
Committed By:   rillig
Date:           Fri Nov 19 19:37:13 UTC 2021

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

Log Message:
indent: fix formatting of function definitions (since 2019-04-04)

In the definition of a function with a pointer return type, the
formatting depended on the name of the function.  Function names
matching [A-Za-z+] were formatted correctly, those containing [$0-9_]
weren't.


To generate a diff of this commit:
cvs rdiff -u -r1.17 -r1.18 src/tests/usr.bin/indent/fmt_decl.c
cvs rdiff -u -r1.4 -r1.5 src/tests/usr.bin/indent/opt_bacc.c
cvs rdiff -u -r1.145 -r1.146 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.17 src/tests/usr.bin/indent/fmt_decl.c:1.18
--- src/tests/usr.bin/indent/fmt_decl.c:1.17	Fri Nov 19 18:55:10 2021
+++ src/tests/usr.bin/indent/fmt_decl.c	Fri Nov 19 19:37:13 2021
@@ -1,4 +1,4 @@
-/*	$NetBSD: fmt_decl.c,v 1.17 2021/11/19 18:55:10 rillig Exp $	*/
+/*	$NetBSD: fmt_decl.c,v 1.18 2021/11/19 19:37:13 rillig Exp $	*/
 /* $FreeBSD: head/usr.bin/indent/tests/declarations.0 334478 2018-06-01 09:41:15Z pstef $ */
 
 /* See FreeBSD r303570 */
@@ -160,22 +160,6 @@ MAXALIGN(offsetof(int, test)) + MAXIMUM_
 
 
 #indent input
-int *int_create(void)
-{
-
-}
-#indent end
-
-#indent run
-int	       *
-int_create(void)
-{
-
-}
-#indent end
-
-
-#indent input
 static
 _attribute_printf(1, 2)
 void
@@ -437,6 +421,9 @@ main(void)
  * In some ancient time long before ISO C90, variable declarations with
  * initializer could be written without '='. The C Programming Language from
  * 1978 doesn't mention this form anymore.
+ *
+ * Before NetBSD lexi.c 1.123 from 2021-10-31, indent treated the '-' as a
+ * unary operator.
  */
 #indent input
 int a - 1;
@@ -454,8 +441,11 @@ int a - 1;
 
 
 /*
- * Since 2019-04-04, the indentation of the '*' depends on the function name,
- * which does not make sense.
+ * Between 2019-04-04 and before lexi.c 1.146 from 2021-11-09, the indentation
+ * of the '*' depended on the function name, which did not make sense.  For
+ * function names that matched [A-Za-z]+, the '*' was placed correctly, for
+ * all other function names (containing [$0-9_]) the '*' was right-aligned on
+ * declaration indentation, which defaults to 16.
  */
 #indent input
 int *
@@ -467,21 +457,15 @@ int *
 yy(void)
 {
 }
-#indent end
-
-/* FIXME: Both function definitions must be formatted in the same way. */
-#indent run
-int	       *
-f2(void)
-{
-}
 
 int *
-yy(void)
+int_create(void)
 {
 }
 #indent end
 
+#indent run-equals-input
+
 
 /*
  * Since 2019-04-04, the space between the '){' is missing.

Index: src/tests/usr.bin/indent/opt_bacc.c
diff -u src/tests/usr.bin/indent/opt_bacc.c:1.4 src/tests/usr.bin/indent/opt_bacc.c:1.5
--- src/tests/usr.bin/indent/opt_bacc.c:1.4	Wed Oct 20 05:14:21 2021
+++ src/tests/usr.bin/indent/opt_bacc.c	Fri Nov 19 19:37:13 2021
@@ -1,4 +1,4 @@
-/* $NetBSD: opt_bacc.c,v 1.4 2021/10/20 05:14:21 rillig Exp $ */
+/* $NetBSD: opt_bacc.c,v 1.5 2021/11/19 19:37:13 rillig Exp $ */
 /* $FreeBSD$ */
 
 /*
@@ -86,8 +86,7 @@ os_name(void)
 #indent end
 
 #indent run -bacc
-/* $ XXX: The '*' should not be set apart from the rest of the return type. */
-const char     *
+const char *
 os_name(void)
 {
 /* $ FIXME: expecting a blank line here. */
@@ -104,14 +103,4 @@ os_name(void)
 }
 #indent end
 
-#indent run -nbacc
-const char     *
-os_name(void)
-{
-#if defined(__NetBSD__) || defined(__FreeBSD__)
-	return "BSD";
-#else
-	return "unknown";
-#endif
-}
-#indent end
+#indent run-equals-input -nbacc

Index: src/usr.bin/indent/lexi.c
diff -u src/usr.bin/indent/lexi.c:1.145 src/usr.bin/indent/lexi.c:1.146
--- src/usr.bin/indent/lexi.c:1.145	Fri Nov 19 19:15:55 2021
+++ src/usr.bin/indent/lexi.c	Fri Nov 19 19:37:13 2021
@@ -1,4 +1,4 @@
-/*	$NetBSD: lexi.c,v 1.145 2021/11/19 19:15:55 rillig Exp $	*/
+/*	$NetBSD: lexi.c,v 1.146 2021/11/19 19:37:13 rillig Exp $	*/
 
 /*-
  * SPDX-License-Identifier: BSD-4-Clause
@@ -43,7 +43,7 @@ static char sccsid[] = "@(#)lexi.c	8.1 (
 
 #include <sys/cdefs.h>
 #if defined(__NetBSD__)
-__RCSID("$NetBSD: lexi.c,v 1.145 2021/11/19 19:15:55 rillig Exp $");
+__RCSID("$NetBSD: lexi.c,v 1.146 2021/11/19 19:37:13 rillig Exp $");
 #elif defined(__FreeBSD__)
 __FBSDID("$FreeBSD: head/usr.bin/indent/lexi.c 337862 2018-08-15 18:19:45Z pstef $");
 #endif
@@ -350,6 +350,12 @@ lex_number(void)
 }
 
 static bool
+is_identifier_start(char ch)
+{
+    return isalpha((unsigned char)ch) || ch == '_' || ch == '$';
+}
+
+static bool
 is_identifier_part(char ch)
 {
     return isalnum((unsigned char)ch) || ch == '_' || ch == '$';
@@ -687,10 +693,16 @@ lexi(void)
 	if (ps.in_decl) {
 	    const char *tp = inp_p(), *e = inp_line_end();
 
-	    /* XXX: is_identifier_start */
-	    while (tp < e && (isalpha((unsigned char)*tp) ||
-		    isspace((unsigned char)*tp)))
-		tp++;
+	    while (tp < e) {
+		if (isspace((unsigned char)*tp))
+		    tp++;
+		else if (is_identifier_start(*tp)) {
+		    tp++;
+		    while (tp < e && is_identifier_part(*tp))
+			tp++;
+		} else
+		    break;
+	    }
 
 	    if (tp < e && *tp == '(')
 		ps.procname[0] = ' ';	/* XXX: why not '\0'? */

Reply via email to