]> git.saurik.com Git - bison.git/commitdiff
obstacks: simplifications
authorAkim Demaille <akim@lrde.epita.fr>
Sat, 11 Aug 2012 07:02:19 +0000 (09:02 +0200)
committerAkim Demaille <akim@lrde.epita.fr>
Sun, 12 Aug 2012 07:52:11 +0000 (09:52 +0200)
* src/system.h (obstack_finish0): New.
Use it to simplify several uses.
* src/muscle-tab.h (MUSCLE_INSERTF): New.
* src/muscle-tab.c: Use obstack_printf where simpler.

TODO
src/conflicts.c
src/flex-scanner.h
src/muscle-tab.c
src/muscle-tab.h
src/output.c
src/print_graph.c
src/scan-code.l
src/scan-skel.l
src/system.h

diff --git a/TODO b/TODO
index e7e69f8b2b79d1e32d392d5dd9a6f0a210bb4b0f..02f3efb34ff9371010fda9bd2c0680ed260ccdc4 100644 (file)
--- a/TODO
+++ b/TODO
@@ -10,10 +10,6 @@ it should not be mandatory.
 A stray $ is a warning in the actions, but an error in the epilogue.
 IMHO, it should not even be a warning in the epilogue.
 
 A stray $ is a warning in the actions, but an error in the epilogue.
 IMHO, it should not even be a warning in the epilogue.
 
-** obstack_copy etc.
-There seems to be some other interesting functions for obstacks that
-we should consider using.
-
 ** stack.hh
 Get rid of it.  The original idea is nice, but actually it makes
 the code harder to follow, and uselessly different from the other
 ** stack.hh
 Get rid of it.  The original idea is nice, but actually it makes
 the code harder to follow, and uselessly different from the other
index dad6568391e62e318c72117ee338b5c88a3d163f..989dfc256689d7c706c307dee8139f849a670c97 100644 (file)
@@ -360,15 +360,9 @@ set_conflicts (state *s, symbol **errors)
       state_errs_set (s, nerrs, errors);
     }
   if (obstack_object_size (&solved_conflicts_obstack))
       state_errs_set (s, nerrs, errors);
     }
   if (obstack_object_size (&solved_conflicts_obstack))
-    {
-      obstack_1grow (&solved_conflicts_obstack, '\0');
-      s->solved_conflicts = obstack_finish (&solved_conflicts_obstack);
-    }
+    s->solved_conflicts = obstack_finish0 (&solved_conflicts_obstack);
   if (obstack_object_size (&solved_conflicts_xml_obstack))
   if (obstack_object_size (&solved_conflicts_xml_obstack))
-    {
-      obstack_1grow (&solved_conflicts_xml_obstack, '\0');
-      s->solved_conflicts_xml = obstack_finish (&solved_conflicts_xml_obstack);
-    }
+    s->solved_conflicts_xml = obstack_finish0 (&solved_conflicts_xml_obstack);
 
   /* Loop over all rules which require lookahead in this state.  Check
      for conflicts not resolved above.  */
 
   /* Loop over all rules which require lookahead in this state.  Check
      for conflicts not resolved above.  */
index cf2b6b9eb8ec78c1a1ac30f181aa1d6ecb630833..c1e07eae0b2e67b8bded18ad7fad25779454ff28 100644 (file)
@@ -86,10 +86,7 @@ static struct obstack obstack_for_string;
   obstack_grow (&obstack_for_string, yytext, yyleng)
 
 # define STRING_FINISH                                  \
   obstack_grow (&obstack_for_string, yytext, yyleng)
 
 # define STRING_FINISH                                  \
-  do {                                                  \
-    obstack_1grow (&obstack_for_string, '\0');          \
-    last_string = obstack_finish (&obstack_for_string); \
-  } while (0)
+  (last_string = obstack_finish0 (&obstack_for_string))
 
 # define STRING_FREE                                    \
   obstack_free (&obstack_for_string, last_string)
 
 # define STRING_FREE                                    \
   obstack_free (&obstack_for_string, last_string)
index bb54bed75a760100fa804362b7eec974c28d349d..9804fe90223c69875da6806d45941c7bb249699d 100644 (file)
@@ -157,12 +157,9 @@ muscle_grow (const char *key, const char *val, const char *separator)
     {
       /* Grow the current value. */
       char *new_val;
     {
       /* Grow the current value. */
       char *new_val;
-      obstack_sgrow (&muscle_obstack, entry->value);
+      obstack_printf (&muscle_obstack, "%s%s%s", entry->value, separator, val);
       free (entry->storage);
       free (entry->storage);
-      obstack_sgrow (&muscle_obstack, separator);
-      obstack_sgrow (&muscle_obstack, val);
-      obstack_1grow (&muscle_obstack, 0);
-      new_val = obstack_finish (&muscle_obstack);
+      new_val = obstack_finish0 (&muscle_obstack);
       entry->value = entry->storage = xstrdup (new_val);
       obstack_free (&muscle_obstack, new_val);
     }
       entry->value = entry->storage = xstrdup (new_val);
       obstack_free (&muscle_obstack, new_val);
     }
@@ -181,8 +178,7 @@ muscle_syncline_grow (char const *key, location loc)
   obstack_quote (&muscle_obstack,
                  quotearg_style (c_quoting_style, loc.start.file));
   obstack_sgrow (&muscle_obstack, ")[");
   obstack_quote (&muscle_obstack,
                  quotearg_style (c_quoting_style, loc.start.file));
   obstack_sgrow (&muscle_obstack, ")[");
-  obstack_1grow (&muscle_obstack, 0);
-  extension = obstack_finish (&muscle_obstack);
+  extension = obstack_finish0 (&muscle_obstack);
   muscle_grow (key, extension, "");
   obstack_free (&muscle_obstack, extension);
 }
   muscle_grow (key, extension, "");
   obstack_free (&muscle_obstack, extension);
 }
@@ -210,8 +206,7 @@ void muscle_pair_list_grow (const char *muscle,
   obstack_sgrow (&muscle_obstack, ", ");
   obstack_quote (&muscle_obstack, a2);
   obstack_sgrow (&muscle_obstack, "]");
   obstack_sgrow (&muscle_obstack, ", ");
   obstack_quote (&muscle_obstack, a2);
   obstack_sgrow (&muscle_obstack, "]");
-  obstack_1grow (&muscle_obstack, 0);
-  pair = obstack_finish (&muscle_obstack);
+  pair = obstack_finish0 (&muscle_obstack);
   muscle_grow (muscle, pair, ",\n");
   obstack_free (&muscle_obstack, pair);
 }
   muscle_grow (muscle, pair, ",\n");
   obstack_free (&muscle_obstack, pair);
 }
@@ -268,13 +263,8 @@ muscle_boundary_grow (char const *key, boundary bound)
   char *extension;
   obstack_sgrow  (&muscle_obstack, "[[");
   obstack_escape (&muscle_obstack, bound.file);
   char *extension;
   obstack_sgrow  (&muscle_obstack, "[[");
   obstack_escape (&muscle_obstack, bound.file);
-  obstack_1grow (&muscle_obstack, ':');
-  obstack_printf (&muscle_obstack, "%d", bound.line);
-  obstack_1grow (&muscle_obstack, '.');
-  obstack_printf (&muscle_obstack, "%d", bound.column);
-  obstack_sgrow  (&muscle_obstack, "]]");
-  obstack_1grow (&muscle_obstack, '\0');
-  extension = obstack_finish (&muscle_obstack);
+  obstack_printf (&muscle_obstack, ":%d.%d]]", bound.line, bound.column);
+  extension = obstack_finish0 (&muscle_obstack);
   muscle_grow (key, extension, "");
   obstack_free (&muscle_obstack, extension);
 }
   muscle_grow (key, extension, "");
   obstack_free (&muscle_obstack, extension);
 }
@@ -358,8 +348,7 @@ location_decode (char const *key)
           {
             char *boundary_str;
             aver (*++value == ']');
           {
             char *boundary_str;
             aver (*++value == ']');
-            obstack_1grow (&muscle_obstack, '\0');
-            boundary_str = obstack_finish (&muscle_obstack);
+            boundary_str = obstack_finish0 (&muscle_obstack);
             switch (*++value)
               {
                 case ',':
             switch (*++value)
               {
                 case ',':
index ac852978439fbefb542e33c66ed16966d17b28e5..7eff4f5a9e60c01b543a267cd789cf99dfd797e7 100644 (file)
@@ -40,42 +40,34 @@ extern struct obstack muscle_obstack;
     MUSCLE_INSERT_INT (Key, v__);                               \
   } while (0)
 
     MUSCLE_INSERT_INT (Key, v__);                               \
   } while (0)
 
-#define MUSCLE_INSERT_INT(Key, Value)                           \
+#define MUSCLE_INSERTF(Key, Format, Value)                      \
   do {                                                          \
   do {                                                          \
-    obstack_printf (&muscle_obstack, "%d", Value);              \
-    obstack_1grow (&muscle_obstack, 0);                         \
-    muscle_insert (Key, obstack_finish (&muscle_obstack));      \
+    obstack_printf (&muscle_obstack, Format, Value);            \
+    muscle_insert (Key, obstack_finish0 (&muscle_obstack));     \
   } while (0)
 
   } while (0)
 
+#define MUSCLE_INSERT_INT(Key, Value)                           \
+  MUSCLE_INSERTF(Key, "%d", Value)
+
 #define MUSCLE_INSERT_LONG_INT(Key, Value)                      \
 #define MUSCLE_INSERT_LONG_INT(Key, Value)                      \
-  do {                                                          \
-    obstack_printf (&muscle_obstack, "%ld", Value);             \
-    obstack_1grow (&muscle_obstack, 0);                         \
-    muscle_insert (Key, obstack_finish (&muscle_obstack));      \
-  } while (0)
+  MUSCLE_INSERTF(Key, "%ld", Value)
 
 /* Key -> Value, but don't apply escaping to Value. */
 #define MUSCLE_INSERT_STRING_RAW(Key, Value)                    \
 
 /* Key -> Value, but don't apply escaping to Value. */
 #define MUSCLE_INSERT_STRING_RAW(Key, Value)                    \
-  do {                                                          \
-    obstack_sgrow (&muscle_obstack, Value);                     \
-    obstack_1grow (&muscle_obstack, 0);                         \
-    muscle_insert (Key, obstack_finish (&muscle_obstack));      \
-  } while (0)
+  MUSCLE_INSERTF(Key, "%s", Value)
 
 /* Key -> Value, applying M4 escaping to Value. */
 #define MUSCLE_INSERT_STRING(Key, Value)                        \
   do {                                                          \
     obstack_escape (&muscle_obstack, Value);                    \
 
 /* Key -> Value, applying M4 escaping to Value. */
 #define MUSCLE_INSERT_STRING(Key, Value)                        \
   do {                                                          \
     obstack_escape (&muscle_obstack, Value);                    \
-    obstack_1grow (&muscle_obstack, 0);                         \
-    muscle_insert (Key, obstack_finish (&muscle_obstack));      \
+    muscle_insert (Key, obstack_finish0 (&muscle_obstack));     \
   } while (0)
 
 #define MUSCLE_INSERT_C_STRING(Key, Value)                      \
   do {                                                          \
     obstack_escape (&muscle_obstack,                            \
                     quotearg_style (c_quoting_style, Value));   \
   } while (0)
 
 #define MUSCLE_INSERT_C_STRING(Key, Value)                      \
   do {                                                          \
     obstack_escape (&muscle_obstack,                            \
                     quotearg_style (c_quoting_style, Value));   \
-    obstack_1grow (&muscle_obstack, 0);                         \
-    muscle_insert (Key, obstack_finish (&muscle_obstack));      \
+    muscle_insert (Key, obstack_finish0 (&muscle_obstack));     \
   } while (0)
 
 /* Append VALUE to the current value of KEY.  If KEY did not already
   } while (0)
 
 /* Append VALUE to the current value of KEY.  If KEY did not already
index 24bb83cd146331c7ae337072a2cee56fb9c7af09..c64c7aa0b3a98a8edb0c32fbd63517cb3af7b507 100644 (file)
@@ -86,18 +86,15 @@ Name (char const *name,                                                 \
       if (max < table_data[i])                                          \
         max = table_data[i];                                            \
     }                                                                   \
       if (max < table_data[i])                                          \
         max = table_data[i];                                            \
     }                                                                   \
-  obstack_1grow (&format_obstack, 0);                                   \
-  muscle_insert (name, obstack_finish (&format_obstack));               \
+  muscle_insert (name, obstack_finish0 (&format_obstack));              \
                                                                         \
   lmin = min;                                                           \
   lmax = max;                                                           \
   /* Build `NAME_min' and `NAME_max' in the obstack. */                 \
   obstack_printf (&format_obstack, "%s_min", name);                     \
                                                                         \
   lmin = min;                                                           \
   lmax = max;                                                           \
   /* Build `NAME_min' and `NAME_max' in the obstack. */                 \
   obstack_printf (&format_obstack, "%s_min", name);                     \
-  obstack_1grow (&format_obstack, 0);                                   \
-  MUSCLE_INSERT_LONG_INT (obstack_finish (&format_obstack), lmin);      \
+  MUSCLE_INSERT_LONG_INT (obstack_finish0 (&format_obstack), lmin);     \
   obstack_printf (&format_obstack, "%s_max", name);                     \
   obstack_printf (&format_obstack, "%s_max", name);                     \
-  obstack_1grow (&format_obstack, 0);                                   \
-  MUSCLE_INSERT_LONG_INT (obstack_finish (&format_obstack), lmax);      \
+  MUSCLE_INSERT_LONG_INT (obstack_finish0 (&format_obstack), lmax);     \
 }
 
 GENERATE_MUSCLE_INSERT_TABLE(muscle_insert_unsigned_int_table, unsigned int)
 }
 
 GENERATE_MUSCLE_INSERT_TABLE(muscle_insert_unsigned_int_table, unsigned int)
@@ -193,8 +190,7 @@ prepare_symbols (void)
     obstack_sgrow (&format_obstack, " ]b4_null[");
 
     /* Finish table and store. */
     obstack_sgrow (&format_obstack, " ]b4_null[");
 
     /* Finish table and store. */
-    obstack_1grow (&format_obstack, 0);
-    muscle_insert ("tname", obstack_finish (&format_obstack));
+    muscle_insert ("tname", obstack_finish0 (&format_obstack));
   }
 
   /* Output YYTOKNUM. */
   }
 
   /* Output YYTOKNUM. */
@@ -412,14 +408,12 @@ prepare_symbol_definitions (void)
 #define SET_KEY(Entry)                                          \
       obstack_printf (&format_obstack, "symbol(%d, %s)",        \
                       i, Entry);                                \
 #define SET_KEY(Entry)                                          \
       obstack_printf (&format_obstack, "symbol(%d, %s)",        \
                       i, Entry);                                \
-      obstack_1grow (&format_obstack, 0);                       \
-      key = obstack_finish (&format_obstack);
+      key = obstack_finish0 (&format_obstack);
 
 #define SET_KEY2(Entry, Suffix)                                 \
       obstack_printf (&format_obstack, "symbol(%d, %s_%s)",     \
                       i, Entry, Suffix);                        \
 
 #define SET_KEY2(Entry, Suffix)                                 \
       obstack_printf (&format_obstack, "symbol(%d, %s_%s)",     \
                       i, Entry, Suffix);                        \
-      obstack_1grow (&format_obstack, 0);                       \
-      key = obstack_finish (&format_obstack);
+      key = obstack_finish0 (&format_obstack);
 
       // Whether the symbol has an identifier.
       value = symbol_id_get (sym);
 
       // Whether the symbol has an identifier.
       value = symbol_id_get (sym);
index d37afb2cd9d45c3fe36d208101f5941c2251f778..4847c4cc7224b0faefdef47c547ec60bd59b5840 100644 (file)
@@ -155,8 +155,7 @@ print_state (state *s, FILE *fgraph)
   /* A node's label contains its items.  */
   obstack_init (&node_obstack);
   print_core (&node_obstack, s);
   /* A node's label contains its items.  */
   obstack_init (&node_obstack);
   print_core (&node_obstack, s);
-  obstack_1grow (&node_obstack, '\0');
-  output_node (s->number, obstack_finish (&node_obstack), fgraph);
+  output_node (s->number, obstack_finish0 (&node_obstack), fgraph);
   obstack_free (&node_obstack, 0);
 
   /* Output the edges.  */
   obstack_free (&node_obstack, 0);
 
   /* Output the edges.  */
index e7d3600f8499b0b23211b730bc6f358f512afe1a..96cab6d150a002df9e3f2c3fc40713d74ebfd6a0 100644 (file)
@@ -445,9 +445,8 @@ show_sub_message (const char* cp, bool explicit_bracketing,
                         _(", cannot be accessed from mid-rule action at $%d"),
                         midrule_rhs_index);
 
                         _(", cannot be accessed from mid-rule action at $%d"),
                         midrule_rhs_index);
 
-      obstack_1grow (&msg_buf, '\0');
       complain_at_indent (id_loc, wflags, &indent, "%s",
       complain_at_indent (id_loc, wflags, &indent, "%s",
-                          (char *) obstack_finish (&msg_buf));
+                          obstack_finish0 (&msg_buf));
       obstack_free (&msg_buf, 0);
     }
 }
       obstack_free (&msg_buf, 0);
     }
 }
index 32bfe7374914ef6602a51482c7049c653bbefbab..17edc790ecb2c257843e4785239b6971d190d01f 100644 (file)
@@ -81,8 +81,7 @@ static void fail_for_invalid_at (char const *at);
 @[a-z_]+"(" {
   yytext[yyleng-1] = '\0';
   obstack_grow (&obstack_for_string, yytext, yyleng);
 @[a-z_]+"(" {
   yytext[yyleng-1] = '\0';
   obstack_grow (&obstack_for_string, yytext, yyleng);
-  at_directive_argv[at_directive_argc++] =
-    obstack_finish (&obstack_for_string);
+  at_directive_argv[at_directive_argc++] = obstack_finish (&obstack_for_string);
   BEGIN SC_AT_DIRECTIVE_ARGS;
 }
 
   BEGIN SC_AT_DIRECTIVE_ARGS;
 }
 
@@ -114,9 +113,8 @@ static void fail_for_invalid_at (char const *at);
     if (at_directive_argc >= AT_DIRECTIVE_ARGC_MAX)
       fail_for_at_directive_too_many_args (at_directive_argv[0]);
 
     if (at_directive_argc >= AT_DIRECTIVE_ARGC_MAX)
       fail_for_at_directive_too_many_args (at_directive_argv[0]);
 
-    obstack_1grow (&obstack_for_string, '\0');
     at_directive_argv[at_directive_argc++] =
     at_directive_argv[at_directive_argc++] =
-      obstack_finish (&obstack_for_string);
+      obstack_finish0 (&obstack_for_string);
 
     /* Like M4, skip whitespace after a comma.  */
     if (yytext[1] == ',')
 
     /* Like M4, skip whitespace after a comma.  */
     if (yytext[1] == ',')
index a56c058a10360c7d867f350e859844bd7680b864..eba7845ee906c5418534fef1d28219c4d1187a68 100644 (file)
@@ -173,6 +173,8 @@ typedef size_t uintptr_t;
 #define obstack_chunk_free  free
 #include <obstack.h>
 
 #define obstack_chunk_free  free
 #include <obstack.h>
 
+/* String-grow: append Str to Obs.  */
+
 #define obstack_sgrow(Obs, Str) \
   obstack_grow (Obs, Str, strlen (Str))
 
 #define obstack_sgrow(Obs, Str) \
   obstack_grow (Obs, Str, strlen (Str))
 
@@ -217,6 +219,11 @@ typedef size_t uintptr_t;
   } while (0)
 
 
   } while (0)
 
 
+/* Append the ending 0, finish Obs, and return the string.  */
+
+# define obstack_finish0(Obs)                           \
+  (obstack_1grow (Obs, '\0'), (char *) obstack_finish (Obs))
+