Module Name: src Committed By: rillig Date: Sun May 21 09:48:22 UTC 2023
Modified Files: src/tests/usr.bin/indent: t_errors.sh src/usr.bin/indent: indent.c Log Message: indent: don't error out on unrecognized preprocessor directives This allows indent to be used on the GCC preprocessor output. To generate a diff of this commit: cvs rdiff -u -r1.28 -r1.29 src/tests/usr.bin/indent/t_errors.sh cvs rdiff -u -r1.300 -r1.301 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/t_errors.sh diff -u src/tests/usr.bin/indent/t_errors.sh:1.28 src/tests/usr.bin/indent/t_errors.sh:1.29 --- src/tests/usr.bin/indent/t_errors.sh:1.28 Sun May 14 11:29:23 2023 +++ src/tests/usr.bin/indent/t_errors.sh Sun May 21 09:48:22 2023 @@ -1,5 +1,5 @@ #! /bin/sh -# $NetBSD: t_errors.sh,v 1.28 2023/05/14 11:29:23 rillig Exp $ +# $NetBSD: t_errors.sh,v 1.29 2023/05/21 09:48:22 rillig Exp $ # # Copyright (c) 2021 The NetBSD Foundation, Inc. # All rights reserved. @@ -366,8 +366,6 @@ preprocessing_unrecognized_body() #else EOF cat <<-\EOF > stderr.exp - 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/usr.bin/indent/indent.c diff -u src/usr.bin/indent/indent.c:1.300 src/usr.bin/indent/indent.c:1.301 --- src/usr.bin/indent/indent.c:1.300 Sat May 20 16:31:31 2023 +++ src/usr.bin/indent/indent.c Sun May 21 09:48:22 2023 @@ -1,4 +1,4 @@ -/* $NetBSD: indent.c,v 1.300 2023/05/20 16:31:31 rillig Exp $ */ +/* $NetBSD: indent.c,v 1.301 2023/05/21 09:48:22 rillig Exp $ */ /*- * SPDX-License-Identifier: BSD-4-Clause @@ -38,7 +38,7 @@ */ #include <sys/cdefs.h> -__RCSID("$NetBSD: indent.c,v 1.300 2023/05/20 16:31:31 rillig Exp $"); +__RCSID("$NetBSD: indent.c,v 1.301 2023/05/21 09:48:22 rillig Exp $"); #include <sys/param.h> #include <err.h> @@ -934,26 +934,6 @@ read_preprocessing_line(void) lab.len--; } -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) { @@ -965,46 +945,33 @@ process_preprocessing(void) ps.is_case_label = false; const char *end = lab.mem + lab.len; - substring dir; - dir.s = lab.st + 1; - while (dir.s < end && ch_isblank(*dir.s)) - dir.s++; - dir.e = dir.s; - while (dir.e < end && ch_isalpha(*dir.e)) - dir.e++; + const char *dir = lab.st + 1; + while (dir < end && ch_isblank(*dir)) + dir++; + const char *dir_end = dir; + while (dir_end < end && ch_isalpha(*dir_end)) + dir_end++; - if (substring_starts_with(dir, "if")) { /* also ifdef, ifndef */ + if (strncmp(dir, "if", 2) == 0) { /* also ifdef, ifndef */ if ((size_t)ifdef_level < array_length(state_stack)) state_stack[ifdef_level++] = ps; else diag(1, "#if stack overflow"); out.line_kind = lk_if; - } else if (substring_starts_with(dir, "el")) { /* else, elif */ + } else if (strncmp(dir, "el", 2) == 0) { /* else, elif */ if (ifdef_level <= 0) - diag(1, dir.s[2] == 'i' + diag(1, dir[2] == 'i' ? "Unmatched #elif" : "Unmatched #else"); else ps = state_stack[ifdef_level - 1]; - } else if (substring_equals(dir, "endif")) { + } else if (dir_end - dir == 5 && memcmp(dir, "endif", 5) == 0) { if (ifdef_level <= 0) diag(1, "Unmatched #endif"); else ifdef_level--; out.line_kind = lk_endif; - - } else { - 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; - } } /* subsequent processing of the newline character will cause the line