]> git.draconx.ca Git - cdecl99.git/blobdiff - src/parse.y
libcdecl: Fix parsing of very long specifier lists.
[cdecl99.git] / src / parse.y
index 14d593ef1622f57a97d0b5bbb26023a07ce0fafb..3a17b998babf5cabc1bc5cfb6e76278304da74f8 100644 (file)
@@ -1,7 +1,7 @@
 %code top {
 /*
  *  Parser for C declarations.
- *  Copyright © 2011-2012, 2021, 2023 Nick Bowler
+ *  Copyright © 2011-2012, 2021, 2023-2024 Nick Bowler
  *
  *  This program is free software: you can redistribute it and/or modify
  *  it under the terms of the GNU General Public License as published by
 #include "cdecl-internal.h"
 #include "errmsg.h"
 
-#define FAIL(msg) do { \
-       yyerror(&yylloc, NULL, NULL, msg); \
-       YYERROR; \
-} while (0)
-
 /*
  * Allocate a parse tree node via cdecl__alloc_item.
  *
@@ -120,6 +115,7 @@ static size_t yytnamerr_copy(char *dst, const char *src)
 
 %code requires {
 #include <inttypes.h>
+#include <stdbool.h>
 }
 
 %code provides {
@@ -131,7 +127,7 @@ const char *cdecl__token_name(unsigned token);
 %union {
        uintmax_t uintval;
        unsigned spectype;
-       _Bool boolval;
+       bool boolval;
        struct cdecl_declspec *declspec;
        struct cdecl_declarator *declarator;
        struct cdecl *decl;
@@ -215,6 +211,21 @@ static void join_specs(struct cdecl_declspec *a, struct cdecl_declspec *b)
        a->next = b;
 }
 
+/*
+ * Join three specifier lists into a single list, and returns the head of
+ * the new list.
+ *
+ * The list "b" is assumed to be a singleton list.
+ */
+static struct cdecl_declspec *join_specs3(struct cdecl_declspec *a,
+                                          struct cdecl_declspec *b,
+                                          struct cdecl_declspec *c)
+{
+       b->next = c;
+       join_specs(b, a);
+       return b;
+}
+
 /*
  * Alter an abstract declarator (type name) to declare an identifier instead,
  * used by the English parser rules to reduce "identifier as type" sequences.
@@ -309,7 +320,7 @@ static struct cdecl_declarator *nulldecl(void)
 %type <spectype>   typespec_simple typespec_tagged
 %type <declspec>   declspec_notype declspec_noid typespec_noid typespec
 %type <declspec>   qualifier qualifiers
-%type <declspec>   declspecs declspecs_noid
+%type <declspec>   declspecs declspecs_notype declspecs_noid
 %type <declarator> direct_declarator declarator pointer array parens postfix
 %type <declarator> direct_declarator_ish declarator_ish parameter_type_list
 %type <decl>       cdecl declaration declarators declarator_wrap parameter
@@ -352,17 +363,18 @@ declaration: declspecs declarators semi {
  * unexpected parses; libcdecl applies a simplification step to the resulting
  * parse tree afterwards.
  */
-declspecs: declspec_notype declspecs {
-       $$ = $1;
-       $$->next = $2;
-} | typespec declspecs_noid {
-       $$ = $1;
-       $$->next = $2;
+declspecs: declspecs_notype typespec declspecs_noid {
+       $$ = join_specs3($1, $2, $3);
 }
 
-declspecs_noid: { $$ = NULL; } | declspec_noid declspecs_noid {
-       $$ = $1;
-       $$->next = $2;
+declspecs_notype: { $$ = NULL; } | declspecs_notype declspec_notype {
+       $$ = $2;
+       $$->next = $1;
+}
+
+declspecs_noid: { $$ = NULL; } | declspecs_noid declspec_noid {
+       $$ = $2;
+       $$->next = $1;
 }
 
 qualifiers: { $$ = NULL; } | qualifiers qualifier {
@@ -494,16 +506,16 @@ english: T_DECLARE T_IDENT T_AS english_declaration {
  * over reducing this empty rule; see below.
  */
 storage_func_specs: %prec T_TYPE { $$ = NULL; }
-storage_func_specs: declspec_simple storage_func_specs {
-       ALLOC_DECLSPEC($$, $1);
-       $$->next = $2;
+storage_func_specs: storage_func_specs declspec_simple {
+       ALLOC_DECLSPEC($$, $2);
+       $$->next = $1;
 }
 
 type_qual_spec: typespec_noid | qualifier
 
-type_qual_specs: { $$ = NULL; } | type_qual_spec type_qual_specs {
-       $$ = $1;
-       $$->next = $2;
+type_qual_specs: { $$ = NULL; } | type_qual_specs type_qual_spec {
+       $$ = $2;
+       $$->next = $1;
 }
 
 /*
@@ -512,9 +524,7 @@ type_qual_specs: { $$ = NULL; } | type_qual_spec type_qual_specs {
  * together three different specifiers lists.
  */
 post_specs: qualifiers typespec type_qual_specs {
-       $2->next = $3;
-       join_specs($2, $1);
-       $$ = $2;
+       $$ = join_specs3($1, $2, $3);
 }
 
 english_declaration: storage_func_specs english_declarator post_specs {
@@ -592,8 +602,10 @@ english_array: T_VLA T_ARRAY english_vla T_OF {
 
 array_length: { $$ = 0; }
 array_length: T_UINT {
-       if (!($$ = $1))
-               FAIL(_("array length must be positive"));
+       if (!($$ = $1)) {
+               cdecl__errmsg(CDECL__EZEROARRAY);
+               YYERROR;
+       }
 }
 
 english_vla: T_IDENT | {
@@ -617,11 +629,16 @@ const char *cdecl__token_name(unsigned token)
        return yytname[YYTRANSLATE(token)];
 }
 
-static void
+/*
+ * Current versions of GCC (up to 13) want to inline this function into the
+ * parser even when optimizing for size and the results are not great, so
+ * try to prevent such inlining.
+ */
+CDECL__NOINLINE static void
 yyerror(YYLTYPE *loc, yyscan_t scanner, struct cdecl **out, const char *err)
 {
        if (strstr(err, yytname[YYTRANSLATE(T_LEX_ERROR)]))
                return;
 
-       cdecl__err(CDECL_ENOPARSE, "%s", err);
+       cdecl__err("%s", err);
 }