obstacks: simplifications

* 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.
This commit is contained in:
Akim Demaille
2012-08-11 09:02:19 +02:00
parent c7324354fb
commit 6fbe73b6a0
10 changed files with 37 additions and 72 deletions

4
TODO
View File

@@ -10,10 +10,6 @@ it should not be mandatory.
A stray $ is a warning in the actions, but an error 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. 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 ** stack.hh
Get rid of it. The original idea is nice, but actually it makes Get rid of it. The original idea is nice, but actually it makes
the code harder to follow, and uselessly different from the other the code harder to follow, and uselessly different from the other

View File

@@ -360,15 +360,9 @@ set_conflicts (state *s, symbol **errors)
state_errs_set (s, nerrs, errors); state_errs_set (s, nerrs, errors);
} }
if (obstack_object_size (&solved_conflicts_obstack)) if (obstack_object_size (&solved_conflicts_obstack))
{ s->solved_conflicts = obstack_finish0 (&solved_conflicts_obstack);
obstack_1grow (&solved_conflicts_obstack, '\0');
s->solved_conflicts = obstack_finish (&solved_conflicts_obstack);
}
if (obstack_object_size (&solved_conflicts_xml_obstack)) if (obstack_object_size (&solved_conflicts_xml_obstack))
{ s->solved_conflicts_xml = obstack_finish0 (&solved_conflicts_xml_obstack);
obstack_1grow (&solved_conflicts_xml_obstack, '\0');
s->solved_conflicts_xml = obstack_finish (&solved_conflicts_xml_obstack);
}
/* Loop over all rules which require lookahead in this state. Check /* Loop over all rules which require lookahead in this state. Check
for conflicts not resolved above. */ for conflicts not resolved above. */

View File

@@ -86,10 +86,7 @@ static struct obstack obstack_for_string;
obstack_grow (&obstack_for_string, yytext, yyleng) obstack_grow (&obstack_for_string, yytext, yyleng)
# define STRING_FINISH \ # define STRING_FINISH \
do { \ (last_string = obstack_finish0 (&obstack_for_string))
obstack_1grow (&obstack_for_string, '\0'); \
last_string = obstack_finish (&obstack_for_string); \
} while (0)
# define STRING_FREE \ # define STRING_FREE \
obstack_free (&obstack_for_string, last_string) obstack_free (&obstack_for_string, last_string)

View File

@@ -157,12 +157,9 @@ muscle_grow (const char *key, const char *val, const char *separator)
{ {
/* Grow the current value. */ /* Grow the current value. */
char *new_val; 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); new_val = obstack_finish0 (&muscle_obstack);
obstack_sgrow (&muscle_obstack, val);
obstack_1grow (&muscle_obstack, 0);
new_val = obstack_finish (&muscle_obstack);
entry->value = entry->storage = xstrdup (new_val); entry->value = entry->storage = xstrdup (new_val);
obstack_free (&muscle_obstack, new_val); obstack_free (&muscle_obstack, new_val);
} }
@@ -181,8 +178,7 @@ muscle_syncline_grow (char const *key, location loc)
obstack_quote (&muscle_obstack, obstack_quote (&muscle_obstack,
quotearg_style (c_quoting_style, loc.start.file)); quotearg_style (c_quoting_style, loc.start.file));
obstack_sgrow (&muscle_obstack, ")["); obstack_sgrow (&muscle_obstack, ")[");
obstack_1grow (&muscle_obstack, 0); extension = obstack_finish0 (&muscle_obstack);
extension = obstack_finish (&muscle_obstack);
muscle_grow (key, extension, ""); muscle_grow (key, extension, "");
obstack_free (&muscle_obstack, extension); obstack_free (&muscle_obstack, extension);
} }
@@ -210,8 +206,7 @@ void muscle_pair_list_grow (const char *muscle,
obstack_sgrow (&muscle_obstack, ", "); obstack_sgrow (&muscle_obstack, ", ");
obstack_quote (&muscle_obstack, a2); obstack_quote (&muscle_obstack, a2);
obstack_sgrow (&muscle_obstack, "]"); obstack_sgrow (&muscle_obstack, "]");
obstack_1grow (&muscle_obstack, 0); pair = obstack_finish0 (&muscle_obstack);
pair = obstack_finish (&muscle_obstack);
muscle_grow (muscle, pair, ",\n"); muscle_grow (muscle, pair, ",\n");
obstack_free (&muscle_obstack, pair); obstack_free (&muscle_obstack, pair);
} }
@@ -268,13 +263,8 @@ muscle_boundary_grow (char const *key, boundary bound)
char *extension; char *extension;
obstack_sgrow (&muscle_obstack, "[["); obstack_sgrow (&muscle_obstack, "[[");
obstack_escape (&muscle_obstack, bound.file); obstack_escape (&muscle_obstack, bound.file);
obstack_1grow (&muscle_obstack, ':'); obstack_printf (&muscle_obstack, ":%d.%d]]", bound.line, bound.column);
obstack_printf (&muscle_obstack, "%d", bound.line); extension = obstack_finish0 (&muscle_obstack);
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);
muscle_grow (key, extension, ""); muscle_grow (key, extension, "");
obstack_free (&muscle_obstack, extension); obstack_free (&muscle_obstack, extension);
} }
@@ -358,8 +348,7 @@ location_decode (char const *key)
{ {
char *boundary_str; char *boundary_str;
aver (*++value == ']'); aver (*++value == ']');
obstack_1grow (&muscle_obstack, '\0'); boundary_str = obstack_finish0 (&muscle_obstack);
boundary_str = obstack_finish (&muscle_obstack);
switch (*++value) switch (*++value)
{ {
case ',': case ',':

View File

@@ -40,42 +40,34 @@ extern struct obstack muscle_obstack;
MUSCLE_INSERT_INT (Key, v__); \ MUSCLE_INSERT_INT (Key, v__); \
} while (0) } while (0)
#define MUSCLE_INSERT_INT(Key, Value) \ #define MUSCLE_INSERTF(Key, Format, Value) \
do { \ do { \
obstack_printf (&muscle_obstack, "%d", Value); \ obstack_printf (&muscle_obstack, Format, Value); \
obstack_1grow (&muscle_obstack, 0); \ muscle_insert (Key, obstack_finish0 (&muscle_obstack)); \
muscle_insert (Key, obstack_finish (&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 { \ MUSCLE_INSERTF(Key, "%ld", Value)
obstack_printf (&muscle_obstack, "%ld", Value); \
obstack_1grow (&muscle_obstack, 0); \
muscle_insert (Key, obstack_finish (&muscle_obstack)); \
} while (0)
/* Key -> Value, but don't apply escaping to Value. */ /* Key -> Value, but don't apply escaping to Value. */
#define MUSCLE_INSERT_STRING_RAW(Key, Value) \ #define MUSCLE_INSERT_STRING_RAW(Key, Value) \
do { \ MUSCLE_INSERTF(Key, "%s", Value)
obstack_sgrow (&muscle_obstack, Value); \
obstack_1grow (&muscle_obstack, 0); \
muscle_insert (Key, obstack_finish (&muscle_obstack)); \
} while (0)
/* Key -> Value, applying M4 escaping to Value. */ /* Key -> Value, applying M4 escaping to Value. */
#define MUSCLE_INSERT_STRING(Key, Value) \ #define MUSCLE_INSERT_STRING(Key, Value) \
do { \ do { \
obstack_escape (&muscle_obstack, Value); \ obstack_escape (&muscle_obstack, Value); \
obstack_1grow (&muscle_obstack, 0); \ muscle_insert (Key, obstack_finish0 (&muscle_obstack)); \
muscle_insert (Key, obstack_finish (&muscle_obstack)); \
} while (0) } while (0)
#define MUSCLE_INSERT_C_STRING(Key, Value) \ #define MUSCLE_INSERT_C_STRING(Key, Value) \
do { \ do { \
obstack_escape (&muscle_obstack, \ obstack_escape (&muscle_obstack, \
quotearg_style (c_quoting_style, Value)); \ quotearg_style (c_quoting_style, Value)); \
obstack_1grow (&muscle_obstack, 0); \ muscle_insert (Key, obstack_finish0 (&muscle_obstack)); \
muscle_insert (Key, obstack_finish (&muscle_obstack)); \
} while (0) } while (0)
/* Append VALUE to the current value of KEY. If KEY did not already /* Append VALUE to the current value of KEY. If KEY did not already

View File

@@ -86,18 +86,15 @@ Name (char const *name, \
if (max < table_data[i]) \ if (max < table_data[i]) \
max = table_data[i]; \ max = table_data[i]; \
} \ } \
obstack_1grow (&format_obstack, 0); \ muscle_insert (name, obstack_finish0 (&format_obstack)); \
muscle_insert (name, obstack_finish (&format_obstack)); \
\ \
lmin = min; \ lmin = min; \
lmax = max; \ lmax = max; \
/* Build `NAME_min' and `NAME_max' in the obstack. */ \ /* Build `NAME_min' and `NAME_max' in the obstack. */ \
obstack_printf (&format_obstack, "%s_min", name); \ obstack_printf (&format_obstack, "%s_min", name); \
obstack_1grow (&format_obstack, 0); \ MUSCLE_INSERT_LONG_INT (obstack_finish0 (&format_obstack), lmin); \
MUSCLE_INSERT_LONG_INT (obstack_finish (&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_finish0 (&format_obstack), lmax); \
MUSCLE_INSERT_LONG_INT (obstack_finish (&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["); obstack_sgrow (&format_obstack, " ]b4_null[");
/* Finish table and store. */ /* Finish table and store. */
obstack_1grow (&format_obstack, 0); muscle_insert ("tname", obstack_finish0 (&format_obstack));
muscle_insert ("tname", obstack_finish (&format_obstack));
} }
/* Output YYTOKNUM. */ /* Output YYTOKNUM. */
@@ -412,14 +408,12 @@ prepare_symbol_definitions (void)
#define SET_KEY(Entry) \ #define SET_KEY(Entry) \
obstack_printf (&format_obstack, "symbol(%d, %s)", \ obstack_printf (&format_obstack, "symbol(%d, %s)", \
i, Entry); \ i, Entry); \
obstack_1grow (&format_obstack, 0); \ key = obstack_finish0 (&format_obstack);
key = obstack_finish (&format_obstack);
#define SET_KEY2(Entry, Suffix) \ #define SET_KEY2(Entry, Suffix) \
obstack_printf (&format_obstack, "symbol(%d, %s_%s)", \ obstack_printf (&format_obstack, "symbol(%d, %s_%s)", \
i, Entry, Suffix); \ i, Entry, Suffix); \
obstack_1grow (&format_obstack, 0); \ key = obstack_finish0 (&format_obstack);
key = obstack_finish (&format_obstack);
// Whether the symbol has an identifier. // Whether the symbol has an identifier.
value = symbol_id_get (sym); value = symbol_id_get (sym);

View File

@@ -155,8 +155,7 @@ print_state (state *s, FILE *fgraph)
/* A node's label contains its items. */ /* A node's label contains its items. */
obstack_init (&node_obstack); obstack_init (&node_obstack);
print_core (&node_obstack, s); print_core (&node_obstack, s);
obstack_1grow (&node_obstack, '\0'); output_node (s->number, obstack_finish0 (&node_obstack), fgraph);
output_node (s->number, obstack_finish (&node_obstack), fgraph);
obstack_free (&node_obstack, 0); obstack_free (&node_obstack, 0);
/* Output the edges. */ /* Output the edges. */

View File

@@ -445,9 +445,8 @@ show_sub_message (const char* cp, bool explicit_bracketing,
_(", cannot be accessed from mid-rule action at $%d"), _(", cannot be accessed from mid-rule action at $%d"),
midrule_rhs_index); 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);
} }
} }

View File

@@ -81,8 +81,7 @@ static void fail_for_invalid_at (char const *at);
@[a-z_]+"(" { @[a-z_]+"(" {
yytext[yyleng-1] = '\0'; yytext[yyleng-1] = '\0';
obstack_grow (&obstack_for_string, yytext, yyleng); obstack_grow (&obstack_for_string, yytext, yyleng);
at_directive_argv[at_directive_argc++] = at_directive_argv[at_directive_argc++] = obstack_finish (&obstack_for_string);
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) if (at_directive_argc >= AT_DIRECTIVE_ARGC_MAX)
fail_for_at_directive_too_many_args (at_directive_argv[0]); 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. */ /* Like M4, skip whitespace after a comma. */
if (yytext[1] == ',') if (yytext[1] == ',')

View File

@@ -173,6 +173,8 @@ typedef size_t uintptr_t;
#define obstack_chunk_free free #define obstack_chunk_free free
#include <obstack.h> #include <obstack.h>
/* String-grow: append Str to Obs. */
#define obstack_sgrow(Obs, Str) \ #define obstack_sgrow(Obs, Str) \
obstack_grow (Obs, Str, strlen (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))