From: Nick Bowler Date: Thu, 20 Jul 2023 02:22:10 +0000 (-0400) Subject: libcdecl: Simplify Bison error message reporting. X-Git-Tag: v1.3~104 X-Git-Url: https://git.draconx.ca/gitweb/cdecl99.git/commitdiff_plain/41ff7ec97691736525bb3da095afd5ca9a8ef003 libcdecl: Simplify Bison error message reporting. Bison emits a bunch of code to pretty print token names for error messages, doing things like removing quotes and backslashes. But this is all totally pointless: all of the relevant strings are already correctly formatted (in part because fix-yytname.awk does some of this at compile time), and the process will not alter them. They simply need to be copied as-is. Fortunately, Bison provides a simple mechanism to replace this with our own function, and wouldn't you know it we already have a suitable copying function in the library, so let's use that. --- diff --git a/src/cdecl-internal.h b/src/cdecl-internal.h index 4c5ed0b..a5f3272 100644 --- a/src/cdecl-internal.h +++ b/src/cdecl-internal.h @@ -80,6 +80,7 @@ struct output_state { size_t cdecl__advance(struct output_state *dst, size_t amount); size_t cdecl__emit(struct output_state *dst, const char *src); +size_t cdecl__strlcpy(char *dst, const char *src, size_t len); const char *cdecl__emit_specs(struct output_state *dst, struct cdecl_declspec *s, diff --git a/src/output.c b/src/output.c index 1075221..7206eb9 100644 --- a/src/output.c +++ b/src/output.c @@ -38,7 +38,7 @@ size_t cdecl__advance(struct output_state *dst, size_t amount) return amount; } -static size_t cdecl__strlcpy(char *dst, const char *src, size_t dstlen) +size_t cdecl__strlcpy(char *dst, const char *src, size_t dstlen) { if (dst) snprintf(dst, dstlen, "%s", src); diff --git a/src/parse.y b/src/parse.y index a993ea6..f507d5b 100644 --- a/src/parse.y +++ b/src/parse.y @@ -53,6 +53,13 @@ ALLOC(ptr, sizeof (type)); \ *(ptr) = (type) { __VA_ARGS__ }; \ } while (0) + +/* + * With the postprocessing performed by fix-yytname.awk, all the symbol + * name strings can be used directly in error messages and there is no + * need for any string processing. + */ +#define yytnamerr(a, b) cdecl__strlcpy(a, b, (a) ? INT_MAX : 0) %} %code requires {