bison-patches
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[PATCH 03/10] obstacks: simplifications


From: Akim Demaille
Subject: [PATCH 03/10] obstacks: simplifications
Date: Mon, 3 Sep 2012 16:26:09 +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               |  4 ----
 src/conflicts.c    | 10 ++--------
 src/flex-scanner.h |  5 +----
 src/muscle-tab.c   | 25 +++++++------------------
 src/muscle-tab.h   | 28 ++++++++++------------------
 src/output.c       | 18 ++++++------------
 src/print_graph.c  |  3 +--
 src/scan-code.l    |  3 +--
 src/scan-skel.l    |  6 ++----
 src/system.h       |  7 +++++++
 10 files changed, 37 insertions(+), 72 deletions(-)

diff --git a/TODO b/TODO
index e7e69f8..02f3efb 100644
--- 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.
 
-** 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
diff --git a/src/conflicts.c b/src/conflicts.c
index dad6568..989dfc2 100644
--- a/src/conflicts.c
+++ b/src/conflicts.c
@@ -360,15 +360,9 @@ set_conflicts (state *s, symbol **errors)
       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))
-    {
-      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.  */
diff --git a/src/flex-scanner.h b/src/flex-scanner.h
index cf2b6b9..c1e07ea 100644
--- a/src/flex-scanner.h
+++ b/src/flex-scanner.h
@@ -86,10 +86,7 @@ static struct obstack obstack_for_string;
   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)
diff --git a/src/muscle-tab.c b/src/muscle-tab.c
index bb54bed..9804fe9 100644
--- a/src/muscle-tab.c
+++ b/src/muscle-tab.c
@@ -157,12 +157,9 @@ muscle_grow (const char *key, const char *val, const char 
*separator)
     {
       /* 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);
-      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);
     }
@@ -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_1grow (&muscle_obstack, 0);
-  extension = obstack_finish (&muscle_obstack);
+  extension = obstack_finish0 (&muscle_obstack);
   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_1grow (&muscle_obstack, 0);
-  pair = obstack_finish (&muscle_obstack);
+  pair = obstack_finish0 (&muscle_obstack);
   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);
-  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);
 }
@@ -358,8 +348,7 @@ location_decode (char const *key)
           {
             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 ',':
diff --git a/src/muscle-tab.h b/src/muscle-tab.h
index ac85297..7eff4f5 100644
--- a/src/muscle-tab.h
+++ b/src/muscle-tab.h
@@ -40,42 +40,34 @@ extern struct obstack muscle_obstack;
     MUSCLE_INSERT_INT (Key, v__);                               \
   } while (0)
 
-#define MUSCLE_INSERT_INT(Key, Value)                           \
+#define MUSCLE_INSERTF(Key, Format, Value)                      \
   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)
 
+#define MUSCLE_INSERT_INT(Key, Value)                           \
+  MUSCLE_INSERTF(Key, "%d", 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)                    \
-  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);                    \
-    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));   \
-    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
diff --git a/src/output.c b/src/output.c
index 24bb83c..c64c7aa 100644
--- a/src/output.c
+++ b/src/output.c
@@ -86,18 +86,15 @@ Name (char const *name,                                     
            \
       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);                     \
-  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_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)
@@ -193,8 +190,7 @@ prepare_symbols (void)
     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. */
@@ -412,14 +408,12 @@ prepare_symbol_definitions (void)
 #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);                        \
-      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);
diff --git a/src/print_graph.c b/src/print_graph.c
index d37afb2..4847c4c 100644
--- a/src/print_graph.c
+++ b/src/print_graph.c
@@ -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);
-  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.  */
diff --git a/src/scan-code.l b/src/scan-code.l
index e7d3600..96cab6d 100644
--- a/src/scan-code.l
+++ b/src/scan-code.l
@@ -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);
 
-      obstack_1grow (&msg_buf, '\0');
       complain_at_indent (id_loc, wflags, &indent, "%s",
-                          (char *) obstack_finish (&msg_buf));
+                          obstack_finish0 (&msg_buf));
       obstack_free (&msg_buf, 0);
     }
 }
diff --git a/src/scan-skel.l b/src/scan-skel.l
index 32bfe73..17edc79 100644
--- a/src/scan-skel.l
+++ b/src/scan-skel.l
@@ -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);
-  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;
 }
 
@@ -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]);
 
-    obstack_1grow (&obstack_for_string, '\0');
     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] == ',')
diff --git a/src/system.h b/src/system.h
index a56c058..eba7845 100644
--- a/src/system.h
+++ b/src/system.h
@@ -173,6 +173,8 @@ typedef size_t uintptr_t;
 #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))
 
@@ -217,6 +219,11 @@ typedef size_t uintptr_t;
   } while (0)
 
 
+/* Append the ending 0, finish Obs, and return the string.  */
+
+# define obstack_finish0(Obs)                           \
+  (obstack_1grow (Obs, '\0'), (char *) obstack_finish (Obs))
+
 
 
 
-- 
1.7.11.5




reply via email to

[Prev in Thread] Current Thread [Next in Thread]