Respect `set print repeats' with Fortran arrays

Implement `set print repeats' handling for Fortran arrays.  Currently
the setting is ignored and always treated as if no limit was set.

Unlike the generic array walker implemented decades ago the Fortran one
is a proper C++ class.  Rather than trying to mimic the old walker then,
which turned out a bit of a challenge where interacting with the `set
print elements' setting, write it entirely from scratch, by adding an
extra specialization handler method for processing dimensions other than
the innermost one and letting the specialization class call the `walk_1'
method from the handler as it sees fit.  This way repeats can be tracked
and the next inner dimension recursed into as a need arises only, or
unconditionally in the base class.

Keep track of the dimension number being handled in the class rather as
a parameter to the walker so that it does not have to be passed across
by the specialization class.

Use per-dimension element count tracking, needed to terminate processing
early when the limit set by `set print elements' is hit.  This requires
extra care too where the limit triggers exactly where another element
that is a subarray begins.  In that case rather than recursing we need
to terminate processing or lone `(...)' would be printed.  Additionally
if the skipped element is the last one in the current dimension we need
to print `...' by hand, because `continue_walking' won't print it at the
upper level, because it can see the last element has already been taken
care of.

Preserve the existing semantics of `set print elements' where the total
count of the elements handled is matched against the trigger level which
is unlike with the C/C++ array printer where the per-dimension element
count is used instead.

Output now looks like:

(gdb) set print repeats 4
(gdb) print array_2d
$1 = ((2, <repeats 5 times>) <repeats 5 times>)
(gdb) set print elements 12
(gdb) print array_2d
$2 = ((2, <repeats 5 times>) (2, <repeats 5 times>) (2, 2, ...) ...)
(gdb)

for a 5 by 5 array filled with the value of 2.

Amend existing test cases accordingly that rely on the current incorrect
behavior and explicitly request that there be no limit for printing
repeated elements there.

Add suitable test cases as well covering sliced arrays in particular.

Co-Authored-By: Andrew Burgess <andrew.burgess@embecosm.com>
This commit is contained in:
Maciej W. Rozycki 2022-01-19 21:55:10 +00:00
parent 2ddd4c6082
commit 476f77a94c
8 changed files with 572 additions and 28 deletions

View file

@ -115,11 +115,12 @@ struct fortran_array_walker_base_impl
{ return should_continue; }
/* Called when GDB starts iterating over a dimension of the array. The
argument INNER_P is true for the inner most dimension (the dimension
containing the actual elements of the array), and false for more outer
dimensions. For a concrete example of how this function is called
see the comment on process_element below. */
void start_dimension (bool inner_p)
argument NELTS holds the number of the elements in the dimension and
INNER_P is true for the inner most dimension (the dimension containing
the actual elements of the array), and false for more outer dimensions.
For a concrete example of how this function is called see the comment
on process_element below. */
void start_dimension (LONGEST nelts, bool inner_p)
{ /* Nothing. */ }
/* Called when GDB finishes iterating over a dimension of the array. The
@ -131,21 +132,38 @@ struct fortran_array_walker_base_impl
void finish_dimension (bool inner_p, bool last_p)
{ /* Nothing. */ }
/* Called when processing dimensions of the array other than the
innermost one. WALK_1 is the walker to normally call, ELT_TYPE is
the type of the element being extracted, and ELT_OFF is the offset
of the element from the start of array being walked, and LAST_P is
true only when this is the last element that will be processed in
this dimension. */
void process_dimension (gdb::function_view<void (struct type *,
int, bool)> walk_1,
struct type *elt_type, LONGEST elt_off, bool last_p)
{
walk_1 (elt_type, elt_off, last_p);
}
/* Called when processing the inner most dimension of the array, for
every element in the array. ELT_TYPE is the type of the element being
extracted, and ELT_OFF is the offset of the element from the start of
array being walked, and LAST_P is true only when this is the last
element that will be processed in this dimension.
Given this two dimensional array ((1, 2) (3, 4)), the calls to
Given this two dimensional array ((1, 2) (3, 4) (5, 6)), the calls to
start_dimension, process_element, and finish_dimension look like this:
start_dimension (false);
start_dimension (true);
start_dimension (3, false);
start_dimension (2, true);
process_element (TYPE, OFFSET, false);
process_element (TYPE, OFFSET, true);
finish_dimension (true, false);
start_dimension (true);
start_dimension (2, true);
process_element (TYPE, OFFSET, false);
process_element (TYPE, OFFSET, true);
finish_dimension (true, true);
start_dimension (2, true);
process_element (TYPE, OFFSET, false);
process_element (TYPE, OFFSET, true);
finish_dimension (true, true);
@ -177,22 +195,23 @@ public:
: m_type (type),
m_address (address),
m_impl (type, address, args...),
m_ndimensions (calc_f77_array_dims (m_type))
m_ndimensions (calc_f77_array_dims (m_type)),
m_nss (0)
{ /* Nothing. */ }
/* Walk the array. */
void
walk ()
{
walk_1 (1, m_type, 0, false);
walk_1 (m_type, 0, false);
}
private:
/* The core of the array walking algorithm. NSS is the current
dimension number being processed, TYPE is the type of this dimension,
and OFFSET is the offset (in bytes) for the start of this dimension. */
/* The core of the array walking algorithm. TYPE is the type of
the current dimension being processed and OFFSET is the offset
(in bytes) for the start of this dimension. */
void
walk_1 (int nss, struct type *type, int offset, bool last_p)
walk_1 (struct type *type, int offset, bool last_p)
{
/* Extract the range, and get lower and upper bounds. */
struct type *range_type = check_typedef (type)->index_type ();
@ -204,9 +223,11 @@ private:
dimension. */
fortran_array_offset_calculator calc (type);
m_impl.start_dimension (nss == m_ndimensions);
m_nss++;
m_impl.start_dimension (upperbound - lowerbound + 1,
m_nss == m_ndimensions);
if (nss != m_ndimensions)
if (m_nss != m_ndimensions)
{
struct type *subarray_type = TYPE_TARGET_TYPE (check_typedef (type));
@ -220,7 +241,12 @@ private:
LONGEST new_offset = offset + calc.index_offset (i);
/* Now print the lower dimension. */
walk_1 (nss + 1, subarray_type, new_offset, (i == upperbound));
m_impl.process_dimension
([this] (struct type *w_type, int w_offset, bool w_last_p) -> void
{
this->walk_1 (w_type, w_offset, w_last_p);
},
subarray_type, new_offset, i == upperbound);
}
}
else
@ -245,7 +271,8 @@ private:
}
}
m_impl.finish_dimension (nss == m_ndimensions, last_p || nss == 1);
m_impl.finish_dimension (m_nss == m_ndimensions, last_p || m_nss == 1);
m_nss--;
}
/* The array type being processed. */
@ -260,6 +287,9 @@ private:
/* The total number of dimensions in M_TYPE. */
int m_ndimensions;
/* The current dimension number being processed. */
int m_nss;
};
#endif /* F_ARRAY_WALKER_H */