1 /* Output generating routines for GDB.
3 Copyright (C) 1999-2019 Free Software Foundation, Inc.
5 Contributed by Cygnus Solutions.
6 Written by Fernando Nasser for Cygnus.
8 This file is part of GDB.
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 3 of the License, or
13 (at your option) any later version.
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with this program. If not, see <http://www.gnu.org/licenses/>. */
24 #include "expression.h" /* For language.h */
34 /* A header of a ui_out_table. */
40 explicit ui_out_hdr (int number
, int min_width
, ui_align alignment
,
41 const std::string
&name
, const std::string
&header
)
43 m_min_width (min_width
),
44 m_alignment (alignment
),
55 int min_width () const
60 ui_align
alignment () const
65 const std::string
&header () const
70 const std::string
&name () const
77 /* The number of the table column this header represents, 1-based. */
80 /* Minimal column width in characters. May or may not be applicable,
81 depending on the actual implementation of ui_out. */
84 /* Alignment of the content in the column. May or may not be applicable,
85 depending on the actual implementation of ui_out. */
88 /* Internal column name, used to internally refer to the column. */
91 /* Printed header text of the column. */
97 /* A level of nesting (either a list or a tuple) in a ui_out output. */
103 explicit ui_out_level (ui_out_type type
)
109 ui_out_type
type () const
114 int field_count () const
116 return m_field_count
;
119 void inc_field_count ()
126 /* The type of this level. */
129 /* Count each field; the first element is for non-list fields. */
133 /* Tables are special. Maintain a separate structure that tracks
134 their state. At present an output can only contain a single table
135 but that restriction might eventually be lifted. */
141 /* States (steps) of a table generation. */
145 /* We are generating the table headers. */
148 /* We are generating the table body. */
152 explicit ui_out_table (int entry_level
, int nr_cols
, const std::string
&id
)
153 : m_state (state::HEADERS
),
154 m_entry_level (entry_level
),
160 /* Start building the body of the table. */
164 /* Add a new header to the table. */
166 void append_header (int width
, ui_align alignment
,
167 const std::string
&col_name
, const std::string
&col_hdr
);
171 /* Extract the format information for the next header and advance
172 the header iterator. Return false if there was no next header. */
174 bool get_next_header (int *colno
, int *width
, ui_align
*alignment
,
175 const char **col_hdr
);
177 bool query_field (int colno
, int *width
, int *alignment
,
178 const char **col_name
) const;
180 state
current_state () const;
182 int entry_level () const;
188 /* The level at which each entry of the table is to be found. A row
189 (a tuple) is made up of entries. Consequently ENTRY_LEVEL is one
190 above that of the table. */
193 /* Number of table columns (as specified in the table_begin call). */
196 /* String identifying the table (as specified in the table_begin
200 /* Pointers to the column headers. */
201 std::vector
<std::unique_ptr
<ui_out_hdr
>> m_headers
;
203 /* Iterator over the headers vector, used when printing successive fields. */
204 std::vector
<std::unique_ptr
<ui_out_hdr
>>::const_iterator m_headers_iterator
;
209 void ui_out_table::start_body ()
211 if (m_state
!= state::HEADERS
)
212 internal_error (__FILE__
, __LINE__
,
213 _("extra table_body call not allowed; there must be only "
214 "one table_body after a table_begin and before a "
217 /* Check if the number of defined headers matches the number of expected
219 if (m_headers
.size () != m_nr_cols
)
220 internal_error (__FILE__
, __LINE__
,
221 _("number of headers differ from number of table "
224 m_state
= state::BODY
;
225 m_headers_iterator
= m_headers
.begin ();
230 void ui_out_table::append_header (int width
, ui_align alignment
,
231 const std::string
&col_name
,
232 const std::string
&col_hdr
)
234 if (m_state
!= state::HEADERS
)
235 internal_error (__FILE__
, __LINE__
,
236 _("table header must be specified after table_begin and "
237 "before table_body."));
239 std::unique_ptr
<ui_out_hdr
> header (new ui_out_hdr (m_headers
.size () + 1,
243 m_headers
.push_back (std::move (header
));
248 void ui_out_table::start_row ()
250 m_headers_iterator
= m_headers
.begin ();
255 bool ui_out_table::get_next_header (int *colno
, int *width
, ui_align
*alignment
,
256 const char **col_hdr
)
258 /* There may be no headers at all or we may have used all columns. */
259 if (m_headers_iterator
== m_headers
.end ())
262 ui_out_hdr
*hdr
= m_headers_iterator
->get ();
264 *colno
= hdr
->number ();
265 *width
= hdr
->min_width ();
266 *alignment
= hdr
->alignment ();
267 *col_hdr
= hdr
->header ().c_str ();
269 /* Advance the header pointer to the next entry. */
270 m_headers_iterator
++;
277 bool ui_out_table::query_field (int colno
, int *width
, int *alignment
,
278 const char **col_name
) const
280 /* Column numbers are 1-based, so convert to 0-based index. */
281 int index
= colno
- 1;
283 if (index
>= 0 && index
< m_headers
.size ())
285 ui_out_hdr
*hdr
= m_headers
[index
].get ();
287 gdb_assert (colno
== hdr
->number ());
289 *width
= hdr
->min_width ();
290 *alignment
= hdr
->alignment ();
291 *col_name
= hdr
->name ().c_str ();
301 ui_out_table::state
ui_out_table::current_state () const
308 int ui_out_table::entry_level () const
310 return m_entry_level
;
314 ui_out::level () const
316 return m_levels
.size ();
319 /* The current (inner most) level. */
322 ui_out::current_level () const
324 return m_levels
.back ().get ();
327 /* Create a new level, of TYPE. */
329 ui_out::push_level (ui_out_type type
)
331 std::unique_ptr
<ui_out_level
> level (new ui_out_level (type
));
333 m_levels
.push_back (std::move (level
));
336 /* Discard the current level. TYPE is the type of the level being
339 ui_out::pop_level (ui_out_type type
)
341 /* We had better not underflow the buffer. */
342 gdb_assert (m_levels
.size () > 0);
343 gdb_assert (current_level ()->type () == type
);
345 m_levels
.pop_back ();
348 /* Mark beginning of a table. */
351 ui_out::table_begin (int nr_cols
, int nr_rows
, const std::string
&tblid
)
353 if (m_table_up
!= nullptr)
354 internal_error (__FILE__
, __LINE__
,
355 _("tables cannot be nested; table_begin found before \
356 previous table_end."));
358 m_table_up
.reset (new ui_out_table (level () + 1, nr_cols
, tblid
));
360 do_table_begin (nr_cols
, nr_rows
, tblid
.c_str ());
364 ui_out::table_header (int width
, ui_align alignment
,
365 const std::string
&col_name
, const std::string
&col_hdr
)
367 if (m_table_up
== nullptr)
368 internal_error (__FILE__
, __LINE__
,
369 _("table_header outside a table is not valid; it must be \
370 after a table_begin and before a table_body."));
372 m_table_up
->append_header (width
, alignment
, col_name
, col_hdr
);
374 do_table_header (width
, alignment
, col_name
, col_hdr
);
378 ui_out::table_body ()
380 if (m_table_up
== nullptr)
381 internal_error (__FILE__
, __LINE__
,
382 _("table_body outside a table is not valid; it must be "
383 "after a table_begin and before a table_end."));
385 m_table_up
->start_body ();
393 if (m_table_up
== nullptr)
394 internal_error (__FILE__
, __LINE__
,
395 _("misplaced table_end or missing table_begin."));
399 m_table_up
= nullptr;
403 ui_out::begin (ui_out_type type
, const char *id
)
405 /* Be careful to verify the ``field'' before the new tuple/list is
406 pushed onto the stack. That way the containing list/table/row is
407 verified and not the newly created tuple/list. This verification
408 is needed (at least) for the case where a table row entry
409 contains either a tuple/list. For that case bookkeeping such as
410 updating the column count or advancing to the next heading still
411 needs to be performed. */
417 verify_field (&fldno
, &width
, &align
);
422 /* If the push puts us at the same level as a table row entry, we've
423 got a new table row. Put the header pointer back to the start. */
424 if (m_table_up
!= nullptr
425 && m_table_up
->current_state () == ui_out_table::state::BODY
426 && m_table_up
->entry_level () == level ())
427 m_table_up
->start_row ();
433 ui_out::end (ui_out_type type
)
441 ui_out::field_signed (const char *fldname
, LONGEST value
)
447 verify_field (&fldno
, &width
, &align
);
449 do_field_signed (fldno
, width
, align
, fldname
, value
);
453 ui_out::field_fmt_signed (int input_width
, ui_align input_align
,
454 const char *fldname
, LONGEST value
)
460 verify_field (&fldno
, &width
, &align
);
462 do_field_signed (fldno
, input_width
, input_align
, fldname
, value
);
468 ui_out::field_unsigned (const char *fldname
, ULONGEST value
)
474 verify_field (&fldno
, &width
, &align
);
476 do_field_unsigned (fldno
, width
, align
, fldname
, value
);
479 /* Documented in ui-out.h. */
482 ui_out::field_core_addr (const char *fldname
, struct gdbarch
*gdbarch
,
485 field_string (fldname
, print_core_address (gdbarch
, address
),
486 ui_out_style_kind::ADDRESS
);
490 ui_out::field_stream (const char *fldname
, string_file
&stream
,
491 ui_out_style_kind style
)
493 if (!stream
.empty ())
494 field_string (fldname
, stream
.c_str (), style
);
496 field_skip (fldname
);
500 /* Used to omit a field. */
503 ui_out::field_skip (const char *fldname
)
509 verify_field (&fldno
, &width
, &align
);
511 do_field_skip (fldno
, width
, align
, fldname
);
515 ui_out::field_string (const char *fldname
, const char *string
,
516 ui_out_style_kind style
)
522 verify_field (&fldno
, &width
, &align
);
524 do_field_string (fldno
, width
, align
, fldname
, string
, style
);
528 ui_out::field_string (const char *fldname
, const std::string
&string
)
530 field_string (fldname
, string
.c_str ());
535 ui_out::field_fmt (const char *fldname
, const char *format
, ...)
542 verify_field (&fldno
, &width
, &align
);
544 va_start (args
, format
);
546 do_field_fmt (fldno
, width
, align
, fldname
, format
, args
);
552 ui_out::spaces (int numspaces
)
554 do_spaces (numspaces
);
558 ui_out::text (const char *string
)
564 ui_out::message (const char *format
, ...)
568 va_start (args
, format
);
569 do_message (format
, args
);
574 ui_out::wrap_hint (const char *identstring
)
576 do_wrap_hint (identstring
);
586 ui_out::redirect (ui_file
*outstream
)
588 do_redirect (outstream
);
591 /* Test the flags against the mask given. */
593 ui_out::test_flags (ui_out_flags mask
)
595 return m_flags
& mask
;
599 ui_out::is_mi_like_p () const
601 return do_is_mi_like_p ();
604 /* Verify that the field/tuple/list is correctly positioned. Return
605 the field number and corresponding alignment (if
606 available/applicable). */
609 ui_out::verify_field (int *fldno
, int *width
, ui_align
*align
)
611 ui_out_level
*current
= current_level ();
614 if (m_table_up
!= nullptr
615 && m_table_up
->current_state () != ui_out_table::state::BODY
)
617 internal_error (__FILE__
, __LINE__
,
618 _("table_body missing; table fields must be \
619 specified after table_body and inside a list."));
622 current
->inc_field_count ();
624 if (m_table_up
!= nullptr
625 && m_table_up
->current_state () == ui_out_table::state::BODY
626 && m_table_up
->entry_level () == level ()
627 && m_table_up
->get_next_header (fldno
, width
, align
, &text
))
629 if (*fldno
!= current
->field_count ())
630 internal_error (__FILE__
, __LINE__
,
631 _("ui-out internal error in handling headers."));
637 *fldno
= current
->field_count ();
641 /* Access table field parameters. */
644 ui_out::query_table_field (int colno
, int *width
, int *alignment
,
645 const char **col_name
)
647 if (m_table_up
== nullptr)
650 return m_table_up
->query_field (colno
, width
, alignment
, col_name
);
653 /* The constructor. */
655 ui_out::ui_out (ui_out_flags flags
)
658 /* Create the ui-out level #1, the default level. */
659 push_level (ui_out_type_tuple
);