Module Name: src
Committed By: rillig
Date: Tue Jan 7 02:55:31 UTC 2025
Modified Files:
src/tests/usr.bin/indent: psym_else.c
src/usr.bin/indent: parse.c
Log Message:
indent: fix indentation of comment above 'else'
Previously, indent assumed that no 'else' would follow.
To generate a diff of this commit:
cvs rdiff -u -r1.7 -r1.8 src/tests/usr.bin/indent/psym_else.c
cvs rdiff -u -r1.82 -r1.83 src/usr.bin/indent/parse.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/psym_else.c
diff -u src/tests/usr.bin/indent/psym_else.c:1.7 src/tests/usr.bin/indent/psym_else.c:1.8
--- src/tests/usr.bin/indent/psym_else.c:1.7 Fri Jun 16 23:19:01 2023
+++ src/tests/usr.bin/indent/psym_else.c Tue Jan 7 02:55:31 2025
@@ -1,4 +1,4 @@
-/* $NetBSD: psym_else.c,v 1.7 2023/06/16 23:19:01 rillig Exp $ */
+/* $NetBSD: psym_else.c,v 1.8 2025/01/07 02:55:31 rillig Exp $ */
/*
* Tests for the parser symbol psym_else, which represents the keyword 'else'
@@ -42,7 +42,7 @@ example(bool cond)
stmt();
else
stmt();
- /* comment */
+ /* comment */
else
stmt();
}
Index: src/usr.bin/indent/parse.c
diff -u src/usr.bin/indent/parse.c:1.82 src/usr.bin/indent/parse.c:1.83
--- src/usr.bin/indent/parse.c:1.82 Sat Jan 4 21:54:26 2025
+++ src/usr.bin/indent/parse.c Tue Jan 7 02:55:30 2025
@@ -1,4 +1,4 @@
-/* $NetBSD: parse.c,v 1.82 2025/01/04 21:54:26 rillig Exp $ */
+/* $NetBSD: parse.c,v 1.83 2025/01/07 02:55:30 rillig Exp $ */
/*-
* SPDX-License-Identifier: BSD-4-Clause
@@ -38,7 +38,7 @@
*/
#include <sys/cdefs.h>
-__RCSID("$NetBSD: parse.c,v 1.82 2025/01/04 21:54:26 rillig Exp $");
+__RCSID("$NetBSD: parse.c,v 1.83 2025/01/07 02:55:30 rillig Exp $");
#include <stdlib.h>
@@ -73,14 +73,7 @@ psyms_reduce_stmt(void)
case psym_if_expr:
psyms_replace2(psym_if_expr_stmt);
- /* For the time being, assume that there is no 'else' on this
- * 'if', and set the indentation level accordingly. If an
- * 'else' is scanned, it will be fixed up later. */
- size_t i = ps.psyms.len - 2;
- while (ps.psyms.sym[i] != psym_stmt
- && ps.psyms.sym[i] != psym_lbrace_block)
- i--;
- ps.ind_level_follow = ps.psyms.ind_level[i];
+ ps.ind_level_follow = ps.psyms.ind_level[ps.psyms.len - 1];
return true;
case psym_switch_expr:
@@ -163,6 +156,8 @@ parse(parser_symbol psym)
if (psym != psym_else) {
while (ps.psyms.sym[ps.psyms.len - 1] == psym_if_expr_stmt) {
ps.psyms.sym[ps.psyms.len - 1] = psym_stmt;
+ ps.ind_level = ps.ind_level_follow
+ = ps.psyms.ind_level[ps.psyms.len - 2];
psyms_reduce();
}
}