Source-Changes-HG archive
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]
[src/trunk]: src/usr.bin/indent indent: fix handling of C99 comments after 'i...
details: https://anonhg.NetBSD.org/src/rev/c4e6abd02bf0
branches: trunk
changeset: 1025157:c4e6abd02bf0
user: rillig <rillig%NetBSD.org@localhost>
date: Sun Nov 07 19:18:56 2021 +0000
description:
indent: fix handling of C99 comments after 'if (expr)'
diffstat:
tests/usr.bin/indent/opt_bl_br.c | 58 +++++++++++++++++++++++++++++++++++++++-
tests/usr.bin/indent/t_misc.sh | 20 +-------------
usr.bin/indent/indent.c | 21 ++++++-------
3 files changed, 68 insertions(+), 31 deletions(-)
diffs (152 lines):
diff -r 1a09cf8944a8 -r c4e6abd02bf0 tests/usr.bin/indent/opt_bl_br.c
--- a/tests/usr.bin/indent/opt_bl_br.c Sun Nov 07 19:04:46 2021 +0000
+++ b/tests/usr.bin/indent/opt_bl_br.c Sun Nov 07 19:18:56 2021 +0000
@@ -1,4 +1,4 @@
-/* $NetBSD: opt_bl_br.c,v 1.1 2021/10/22 20:54:36 rillig Exp $ */
+/* $NetBSD: opt_bl_br.c,v 1.2 2021/11/07 19:18:56 rillig Exp $ */
/* $FreeBSD$ */
#indent input
@@ -65,3 +65,59 @@
}
}
#indent end
+
+
+/*
+ * Test C99 comments after 'if (expr)', which is handled by search_stmt.
+ */
+#indent input
+void function(void)
+{
+ if (expr) // C99 comment
+ stmt();
+
+ if (expr) // C99 comment
+ {
+ stmt();
+ }
+}
+#indent end
+
+#indent run
+void
+function(void)
+{
+ if (expr) // C99 comment
+ stmt();
+
+ if (expr) { // C99 comment
+ stmt();
+ }
+}
+#indent end
+
+
+/*
+ * Test multiple mixed comments after 'if (expr)'.
+ */
+#indent input
+void
+function(void)
+{
+ if (expr) // C99 comment 1
+ // C99 comment 2
+ // C99 comment 3
+ stmt();
+}
+#indent end
+
+#indent run
+void
+function(void)
+{
+ if (expr) // C99 comment 1
+ // C99 comment 2
+ // C99 comment 3
+ stmt();
+}
+#indent end
diff -r 1a09cf8944a8 -r c4e6abd02bf0 tests/usr.bin/indent/t_misc.sh
--- a/tests/usr.bin/indent/t_misc.sh Sun Nov 07 19:04:46 2021 +0000
+++ b/tests/usr.bin/indent/t_misc.sh Sun Nov 07 19:18:56 2021 +0000
@@ -1,5 +1,5 @@
#! /bin/sh
-# $NetBSD: t_misc.sh,v 1.14 2021/11/07 19:04:46 rillig Exp $
+# $NetBSD: t_misc.sh,v 1.15 2021/11/07 19:18:56 rillig Exp $
#
# Copyright (c) 2021 The NetBSD Foundation, Inc.
# All rights reserved.
@@ -364,23 +364,6 @@
"$indent" -Pnonexistent.pro -Perror.pro -Plast.pro code.c -st
}
-atf_test_case 'if_expr_c99_comment'
-if_expr_c99_comment_body()
-{
- cat <<-\EOF >> code.c
- void function(void) {
- if (expr) // C99 comment
- stmt();
- }
- EOF
- cat <<-\EOF >> code.err
- error: code.c:2: Internal buffer overflow - Move big comment from right after if, while, or whatever
- EOF
-
- atf_check -s 'exit:1' -o 'ignore' -e 'file:code.err' \
- "$indent" code.c -st
-}
-
atf_init_test_cases()
{
atf_add_test_case 'in_place'
@@ -394,5 +377,4 @@
atf_add_test_case 'line_no_counting'
atf_add_test_case 'default_backup_extension'
atf_add_test_case 'several_profiles'
- atf_add_test_case 'if_expr_c99_comment'
}
diff -r 1a09cf8944a8 -r c4e6abd02bf0 usr.bin/indent/indent.c
--- a/usr.bin/indent/indent.c Sun Nov 07 19:04:46 2021 +0000
+++ b/usr.bin/indent/indent.c Sun Nov 07 19:18:56 2021 +0000
@@ -1,4 +1,4 @@
-/* $NetBSD: indent.c,v 1.217 2021/11/07 18:49:02 rillig Exp $ */
+/* $NetBSD: indent.c,v 1.218 2021/11/07 19:18:56 rillig Exp $ */
/*-
* SPDX-License-Identifier: BSD-4-Clause
@@ -43,7 +43,7 @@
#include <sys/cdefs.h>
#if defined(__NetBSD__)
-__RCSID("$NetBSD: indent.c,v 1.217 2021/11/07 18:49:02 rillig Exp $");
+__RCSID("$NetBSD: indent.c,v 1.218 2021/11/07 19:18:56 rillig Exp $");
#elif defined(__FreeBSD__)
__FBSDID("$FreeBSD: head/usr.bin/indent/indent.c 340138 2018-11-04 19:24:49Z oshogbo $");
#endif
@@ -316,16 +316,15 @@
save_com, sc_end, "\"\n");
}
- sc_add_char('/');
- sc_add_char('*');
-
- for (;;) { /* loop until the end of the comment */
- sc_add_char(inp_next());
- if (sc_end[-1] == '*' && *inp.s == '/') {
+ sc_add_range(token.s, token.e);
+ if (token.e[-1] == '/') {
+ while (inp.s[0] != '\n')
sc_add_char(inp_next());
- debug_save_com("search_stmt_comment end");
- break;
- }
+ debug_save_com("search_stmt_comment end C99");
+ } else {
+ while (!(sc_end[-2] == '*' && sc_end[-1] == '/'))
+ sc_add_char(inp_next());
+ debug_save_com("search_stmt_comment end block");
}
}
Home |
Main Index |
Thread Index |
Old Index