]> git.draconx.ca Git - cdecl99.git/commitdiff
libcdecl: Simplify Bison error message reporting.
authorNick Bowler <nbowler@draconx.ca>
Thu, 20 Jul 2023 02:22:10 +0000 (22:22 -0400)
committerNick Bowler <nbowler@draconx.ca>
Thu, 20 Jul 2023 02:22:10 +0000 (22:22 -0400)
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.

src/cdecl-internal.h
src/output.c
src/parse.y

index 4c5ed0ba1e6203a0343df271110877f528cf56b4..a5f3272298a20aeb3422ce685849cf223f0ee730 100644 (file)
@@ -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,
index 1075221b7e0aabebcd6b779638d3fa3b46d03a54..7206eb9b51f46e64026657325d75f27255bfde7a 100644 (file)
@@ -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);
index a993ea6100459e100bd3f367da553b35f7541d0f..f507d5b6ee106e163f7c98a0e31150c906672efa 100644 (file)
        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 {