Module Name: src Committed By: rillig Date: Sat May 13 08:33:39 UTC 2023
Modified Files: src/tests/usr.bin/indent: lsym_preprocessing.c t_errors.sh t_misc.sh src/usr.bin/indent: indent.c Log Message: indent: preserve indentation of preprocessor directives To generate a diff of this commit: cvs rdiff -u -r1.9 -r1.10 src/tests/usr.bin/indent/lsym_preprocessing.c cvs rdiff -u -r1.25 -r1.26 src/tests/usr.bin/indent/t_errors.sh cvs rdiff -u -r1.23 -r1.24 src/tests/usr.bin/indent/t_misc.sh cvs rdiff -u -r1.256 -r1.257 src/usr.bin/indent/indent.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_preprocessing.c diff -u src/tests/usr.bin/indent/lsym_preprocessing.c:1.9 src/tests/usr.bin/indent/lsym_preprocessing.c:1.10 --- src/tests/usr.bin/indent/lsym_preprocessing.c:1.9 Thu May 11 21:36:31 2023 +++ src/tests/usr.bin/indent/lsym_preprocessing.c Sat May 13 08:33:39 2023 @@ -1,4 +1,4 @@ -/* $NetBSD: lsym_preprocessing.c,v 1.9 2023/05/11 21:36:31 rillig Exp $ */ +/* $NetBSD: lsym_preprocessing.c,v 1.10 2023/05/13 08:33:39 rillig Exp $ */ /* * Tests for the token lsym_preprocessing, which represents a '#' that starts @@ -74,22 +74,7 @@ #endif /* outer endif comment */ //indent end -//indent run -#if 0 -#else -#endif - -#if 0 /* if comment */ -#else /* else comment */ -#endif /* endif comment */ - -#if 0 /* outer if comment */ -/* $ XXX: The indentation is removed, which can get confusing */ -#if nested /* inner if comment */ -#else /* inner else comment */ -#endif /* inner endif comment */ -#endif /* outer endif comment */ -//indent end +//indent run-equals-input //indent input Index: src/tests/usr.bin/indent/t_errors.sh diff -u src/tests/usr.bin/indent/t_errors.sh:1.25 src/tests/usr.bin/indent/t_errors.sh:1.26 --- src/tests/usr.bin/indent/t_errors.sh:1.25 Thu May 11 09:28:53 2023 +++ src/tests/usr.bin/indent/t_errors.sh Sat May 13 08:33:39 2023 @@ -1,5 +1,5 @@ #! /bin/sh -# $NetBSD: t_errors.sh,v 1.25 2023/05/11 09:28:53 rillig Exp $ +# $NetBSD: t_errors.sh,v 1.26 2023/05/13 08:33:39 rillig Exp $ # # Copyright (c) 2021 The NetBSD Foundation, Inc. # All rights reserved. @@ -366,8 +366,8 @@ preprocessing_unrecognized_body() #else EOF cat <<-\EOF > stderr.exp - error: code.c:1: Unrecognized cpp directive - error: code.c:2: Unrecognized cpp directive + error: code.c:1: Unrecognized cpp directive "unknown" + error: code.c:2: Unrecognized cpp directive "" error: code.c:3: Unmatched #elif error: code.c:4: Unmatched #else EOF Index: src/tests/usr.bin/indent/t_misc.sh diff -u src/tests/usr.bin/indent/t_misc.sh:1.23 src/tests/usr.bin/indent/t_misc.sh:1.24 --- src/tests/usr.bin/indent/t_misc.sh:1.23 Sat May 13 06:52:48 2023 +++ src/tests/usr.bin/indent/t_misc.sh Sat May 13 08:33:39 2023 @@ -1,5 +1,5 @@ #! /bin/sh -# $NetBSD: t_misc.sh,v 1.23 2023/05/13 06:52:48 rillig Exp $ +# $NetBSD: t_misc.sh,v 1.24 2023/05/13 08:33:39 rillig Exp $ # # Copyright (c) 2021 The NetBSD Foundation, Inc. # All rights reserved. @@ -198,7 +198,7 @@ option_P_in_profile_file_body() echo 'syntax # error' > code.c - atf_check -o 'inline:syntax\n#error\n' \ + atf_check -o 'inline:syntax\n# error\n' \ "$indent" < code.c } Index: src/usr.bin/indent/indent.c diff -u src/usr.bin/indent/indent.c:1.256 src/usr.bin/indent/indent.c:1.257 --- src/usr.bin/indent/indent.c:1.256 Fri May 12 22:38:47 2023 +++ src/usr.bin/indent/indent.c Sat May 13 08:33:39 2023 @@ -1,4 +1,4 @@ -/* $NetBSD: indent.c,v 1.256 2023/05/12 22:38:47 rillig Exp $ */ +/* $NetBSD: indent.c,v 1.257 2023/05/13 08:33:39 rillig Exp $ */ /*- * SPDX-License-Identifier: BSD-4-Clause @@ -43,7 +43,7 @@ static char sccsid[] = "@(#)indent.c 5.1 #include <sys/cdefs.h> #if defined(__NetBSD__) -__RCSID("$NetBSD: indent.c,v 1.256 2023/05/12 22:38:47 rillig Exp $"); +__RCSID("$NetBSD: indent.c,v 1.257 2023/05/13 08:33:39 rillig Exp $"); #elif defined(__FreeBSD__) __FBSDID("$FreeBSD: head/usr.bin/indent/indent.c 340138 2018-11-04 19:24:49Z oshogbo $"); #endif @@ -915,7 +915,7 @@ read_preprocessing_line(void) state = PLAIN; while (ch_isblank(inp_peek())) - inp_skip(); + buf_add_char(&lab, inp_next()); while (inp_peek() != '\n' || (state == COMM && !had_eof)) { buf_reserve(&lab, 2); @@ -957,6 +957,26 @@ read_preprocessing_line(void) buf_terminate(&lab); } +typedef struct { + const char *s; + const char *e; +} substring; + +static bool +substring_equals(substring ss, const char *str) +{ + size_t len = (size_t)(ss.e - ss.s); + return len == strlen(str) && memcmp(ss.s, str, len) == 0; +} + +static bool +substring_starts_with(substring ss, const char *prefix) +{ + while (ss.s < ss.e && *prefix != '\0' && *ss.s == *prefix) + ss.s++, prefix++; + return *prefix == '\0'; +} + static void process_preprocessing(void) { @@ -967,32 +987,41 @@ process_preprocessing(void) ps.is_case_label = false; - if (strncmp(lab.s, "#if", 3) == 0) { /* also ifdef, ifndef */ + substring dir; + dir.s = lab.s + 1; + while (dir.s < lab.e && ch_isblank(*dir.s)) + dir.s++; + dir.e = dir.s; + while (dir.e < lab.e && ch_isalpha(*dir.e)) + dir.e++; + + if (substring_starts_with(dir, "if")) { /* also ifdef, ifndef */ if ((size_t)ifdef_level < array_length(state_stack)) state_stack[ifdef_level++] = ps; else diag(1, "#if stack overflow"); - } else if (strncmp(lab.s, "#el", 3) == 0) { /* else, elif */ + } else if (substring_starts_with(dir, "el")) { /* else, elif */ if (ifdef_level <= 0) - diag(1, lab.s[3] == 'i' ? "Unmatched #elif" : "Unmatched #else"); + diag(1, dir.s[2] == 'i' ? "Unmatched #elif" : "Unmatched #else"); else ps = state_stack[ifdef_level - 1]; - } else if (strncmp(lab.s, "#endif", 6) == 0) { + } else if (substring_equals(dir, "endif")) { if (ifdef_level <= 0) diag(1, "Unmatched #endif"); else ifdef_level--; } else { - if (strncmp(lab.s + 1, "pragma", 6) != 0 && - strncmp(lab.s + 1, "error", 5) != 0 && - strncmp(lab.s + 1, "line", 4) != 0 && - strncmp(lab.s + 1, "undef", 5) != 0 && - strncmp(lab.s + 1, "define", 6) != 0 && - strncmp(lab.s + 1, "include", 7) != 0) { - diag(1, "Unrecognized cpp directive"); + if (!substring_equals(dir, "pragma") && + !substring_equals(dir, "error") && + !substring_equals(dir, "line") && + !substring_equals(dir, "undef") && + !substring_equals(dir, "define") && + !substring_equals(dir, "include")) { + diag(1, "Unrecognized cpp directive \"%.*s\"", + (int)(dir.e - dir.s), dir.s); return; } }