mirror of
https://git.savannah.gnu.org/git/bison.git
synced 2026-03-13 14:23:04 +00:00
lists: fix various issues with the use of gnulib's list
First, we should avoid code such as
gl_list_iterator_t it = gl_list_iterator (deriv->children);
derivation *child = NULL;
while (gl_list_iterator_next (&it, (const void **) &child, NULL))
{
derivation_print (child, f);
because of -Wstrict-aliasing (whose job is to catch type-punning
issues). See https://lists.gnu.org/r/bug-bison/2020-05/msg00039.html.
Rather we need
gl_list_iterator_t it = gl_list_iterator (deriv->children);
const void **p = NULL;
while (gl_list_iterator_next (&it, &p, NULL))
{
derivation *child = (derivation *) p;
derivation_print (child, f);
Second, list iterators actually have destructors. Even though they
are noop in the case of linked-lists, we should use them.
Let's address both issues with typed wrappers (such as
derivation_list_next) that take care of both issues, and besides allow
to scope the iterators within the loop:
derivation *child;
for (gl_list_iterator_t it = gl_list_iterator (deriv->children);
derivation_list_next (&it, &child);
)
{
derivation_print (child, f);
* src/derivation.h, src/derivation.c (derivation_list_next): New.
Use it where appropriate.
* src/counterexample.c (search_state_list_next): New.
Use it where appropriate.
* src/parse-simulation.h, src/parse-simulation.c
* src/state-item.h (state_item_list_next): New.
Use it where appropriate.
This commit is contained in:
@@ -55,9 +55,10 @@ derivation_list_prepend (derivation_list dl, derivation *d)
|
||||
|
||||
void derivation_list_free (derivation_list dl)
|
||||
{
|
||||
gl_list_iterator_t it = gl_list_iterator (dl);
|
||||
derivation *d;
|
||||
while (gl_list_iterator_next (&it, (const void **) &d, NULL))
|
||||
derivation *d = NULL;
|
||||
for (gl_list_iterator_t it = gl_list_iterator (dl);
|
||||
derivation_list_next (&it, &d);
|
||||
)
|
||||
if (d != &d_dot)
|
||||
derivation_free (d);
|
||||
gl_list_free (dl);
|
||||
@@ -94,9 +95,10 @@ derivation_free (derivation *d)
|
||||
{
|
||||
if (deriv->children)
|
||||
{
|
||||
gl_list_iterator_t it = gl_list_iterator (deriv->children);
|
||||
derivation *child;
|
||||
while (gl_list_iterator_next (&it, (const void **) &child, NULL))
|
||||
derivation *child = NULL;
|
||||
for (gl_list_iterator_t it = gl_list_iterator (deriv->children);
|
||||
derivation_list_next (&it, &child);
|
||||
)
|
||||
if (child != &d_dot)
|
||||
gl_list_add_last (free_queue, child);
|
||||
gl_list_free (deriv->children);
|
||||
@@ -114,9 +116,10 @@ derivation_size (const derivation *deriv)
|
||||
if (!deriv->children)
|
||||
return 1;
|
||||
int size = 1;
|
||||
gl_list_iterator_t it = gl_list_iterator (deriv->children);
|
||||
derivation *child;
|
||||
while (gl_list_iterator_next (&it, (const void **) &child, NULL))
|
||||
derivation *child = NULL;
|
||||
for (gl_list_iterator_t it = gl_list_iterator (deriv->children);
|
||||
derivation_list_next (&it, &child);
|
||||
)
|
||||
size += derivation_size (child);
|
||||
return size;
|
||||
}
|
||||
@@ -136,10 +139,12 @@ derivation_print (const derivation *deriv, FILE *f)
|
||||
fprintf (f, " %s", sym->tag);
|
||||
return;
|
||||
}
|
||||
gl_list_iterator_t it = gl_list_iterator (deriv->children);
|
||||
derivation *child;
|
||||
|
||||
fprintf (f, " %s ::=[", sym->tag);
|
||||
while (gl_list_iterator_next (&it, (const void **) &child, NULL))
|
||||
derivation *child;
|
||||
for (gl_list_iterator_t it = gl_list_iterator (deriv->children);
|
||||
derivation_list_next (&it, &child);
|
||||
)
|
||||
{
|
||||
derivation_print (child, f);
|
||||
fputs (" ", f);
|
||||
@@ -162,10 +167,11 @@ derivation_print_leaves (const derivation *deriv, FILE *f)
|
||||
return;
|
||||
}
|
||||
|
||||
gl_list_iterator_t it = gl_list_iterator (deriv->children);
|
||||
const char *sep = "";
|
||||
derivation *child;
|
||||
while (gl_list_iterator_next (&it, (const void **) &child, NULL))
|
||||
for (gl_list_iterator_t it = gl_list_iterator (deriv->children);
|
||||
derivation_list_next (&it, &child);
|
||||
)
|
||||
{
|
||||
fputs (sep, f);
|
||||
sep = " ";
|
||||
|
||||
Reference in New Issue
Block a user