[PATCH 17/22] Simplify ui-out level code

Simon Marchi
2016-11-24T15:28:28Z

Now that we use a vector to store the levels, we don't have to keep a
separate level field in ui_out to keep track of the current level. We
can efficiently derive it from the vector size. That causes a little
change in the meaning of the level, as in they are now 1-based instead
of 0-based (the initial level has the "id" 1 now), but it shouldn't
change anything in the behavior.

Additionally, push_level and pop_level don't really need to return the
new level, making them return void simplifies the code a bit.

Finally, the ui_out_begin/ui_out_end callbacks in the ui_out_impl
interface don't need to be passed the level, it's never actually used.

gdb/ChangeLog:

* ui-out.h (ui_out_begin_ftype): Remove level parameter.
(ui_out_end_ftype): Likewise.
* ui-out.c (struct ui_out) <level>: Replace field with a method
that dynamically computes the result.
(current_level): Get vector's back item instead of using
uiout->level.
(push_level): Make return type void.
(pop_level): Make return type void and update access to
ui_out::level.
(uo_begin): Remove level parameter.
(uo_end): Likewise.
(ui_out_table_begin): Update access to uiout::level.
(ui_out_begin): Don't read return value from push_level, call
uiout->level() instead, update call to uo_begin.
(ui_out_end): Don't read return value from pop_level, update
call to uo_end.
(verify_field): Update access to uiout->level.
(ui_out_new): Don't initialize ui_out::level, call push_level
to push the initial level instead of doing it by hand.
* cli-out.c (cli_begin): Remove level parameter.
(cli_end): Likewise.
* mi/mi-out.c (mi_begin): Likewise.
(mi_end): Likewise.
---
gdb/cli-out.c | 4 +---
gdb/mi/mi-out.c | 9 ++++-----
gdb/ui-out.c | 59 +++++++++++++++++++++++----------------------------------
gdb/ui-out.h | 8 +++-----
4 files changed, 32 insertions(+), 48 deletions(-)

diff --git a/gdb/cli-out.c b/gdb/cli-out.c
index 9ba1d17..e042926 100644
--- a/gdb/cli-out.c
+++ b/gdb/cli-out.c
@@ -112,7 +112,6 @@ cli_table_header (struct ui_out *uiout, int width, enum ui_align alignment,
static void
cli_begin (struct ui_out *uiout,
enum ui_out_type type,
- int level,
const char *id)
{
cli_out_data *data = (cli_out_data *) ui_out_data (uiout);
@@ -125,8 +124,7 @@ cli_begin (struct ui_out *uiout,

static void
cli_end (struct ui_out *uiout,
- enum ui_out_type type,
- int level)
+ enum ui_out_type type)
{
cli_out_data *data = (cli_out_data *) ui_out_data (uiout);

diff --git a/gdb/mi/mi-out.c b/gdb/mi/mi-out.c
index 72e84b2..b7cd433 100644
--- a/gdb/mi/mi-out.c
+++ b/gdb/mi/mi-out.c
@@ -46,8 +46,8 @@ static void mi_table_header (struct ui_out *uiout, int width,
const std::string &col_name,
const std::string &col_hdr);
static void mi_begin (struct ui_out *uiout, enum ui_out_type type,
- int level, const char *id);
-static void mi_end (struct ui_out *uiout, enum ui_out_type type, int level);
+ const char *id);
+static void mi_end (struct ui_out *uiout, enum ui_out_type type);
static void mi_field_int (struct ui_out *uiout, int fldno, int width,
enum ui_align alig, const char *fldname, int value);
static void mi_field_skip (struct ui_out *uiout, int fldno, int width,
@@ -160,8 +160,7 @@ mi_table_header (struct ui_out *uiout, int width, enum ui_align alignment,
/* Mark beginning of a list. */

void
-mi_begin (struct ui_out *uiout, enum ui_out_type type, int level,
- const char *id)
+mi_begin (struct ui_out *uiout, enum ui_out_type type, const char *id)
{
mi_out_data *data = (mi_out_data *) ui_out_data (uiout);

@@ -174,7 +173,7 @@ mi_begin (struct ui_out *uiout, enum ui_out_type type, int level,
/* Mark end of a list. */

void
-mi_end (struct ui_out *uiout, enum ui_out_type type, int level)
+mi_end (struct ui_out *uiout, enum ui_out_type type)
{
mi_out_data *data = (mi_out_data *) ui_out_data (uiout);

diff --git a/gdb/ui-out.c b/gdb/ui-out.c
index 594338a..25a14f7 100644
--- a/gdb/ui-out.c
+++ b/gdb/ui-out.c
@@ -170,12 +170,14 @@ struct ui_out
const struct ui_out_impl *impl;
void *data;

- /* Current level. */
- int level;
-
/* Vector to store and track the ui-out levels. */
std::vector<std::unique_ptr<ui_out_level>> levels;

+ int level (void) const
+ {
+ return this->levels.size ();
+ }
+
/* A table, if any. At present only a single table is supported. */
struct ui_out_table table;
};
@@ -184,36 +186,30 @@ struct ui_out
static ui_out_level *
current_level (struct ui_out *uiout)
{
- return uiout->levels[uiout->level].get ();
+ return uiout->levels.back ().get ();
}

/* Create a new level, of TYPE. Return the new level's index. */
-static int
+static void
push_level (struct ui_out *uiout,
enum ui_out_type type)
{
std::unique_ptr<ui_out_level> level (new ui_out_level (type));

uiout->levels.push_back (std::move (level));
- uiout->level++;
-
- return uiout->level;
}

/* Discard the current level, return the discarded level's index.
TYPE is the type of the level being discarded. */
-static int
+static void
pop_level (struct ui_out *uiout,
enum ui_out_type type)
{
/* We had better not underflow the buffer. */
- gdb_assert (uiout->level > 0);
+ gdb_assert (uiout->level () > 0);
gdb_assert (current_level (uiout)->type () == type);

uiout->levels.pop_back ();
- uiout->level--;
-
- return uiout->level + 1;
}

/* These are the interfaces to implementation functions. */
@@ -228,10 +224,9 @@ static void uo_table_header (struct ui_out *uiout, int width,
const std::string &col_hdr);
static void uo_begin (struct ui_out *uiout,
enum ui_out_type type,
- int level, const char *id);
+ const char *id);
static void uo_end (struct ui_out *uiout,
- enum ui_out_type type,
- int level);
+ enum ui_out_type type);
static void uo_field_int (struct ui_out *uiout, int fldno, int width,
enum ui_align align, const char *fldname, int value);
static void uo_field_skip (struct ui_out *uiout, int fldno, int width,
@@ -277,7 +272,7 @@ previous table_end."));

uiout->table.flag = 1;
uiout->table.body_flag = 0;
- uiout->table.entry_level = uiout->level + 1;
+ uiout->table.entry_level = uiout->level () + 1;
uiout->table.columns = nbrofcols;
uiout->table.id = tblid;

@@ -357,8 +352,6 @@ ui_out_begin (struct ui_out *uiout,
enum ui_out_type type,
const char *id)
{
- int new_level;
-
if (uiout->table.flag && !uiout->table.body_flag)
internal_error (__FILE__, __LINE__,
_("table header or table_body expected; lists must be \
@@ -379,24 +372,24 @@ specified after table_body."));
verify_field (uiout, &fldno, &width, &align);
}

- new_level = push_level (uiout, type);
+ push_level (uiout, type);

/* If the push puts us at the same level as a table row entry, we've
got a new table row. Put the header pointer back to the start. */
if (uiout->table.body_flag
- && uiout->table.entry_level == new_level)
+ && uiout->table.entry_level == uiout->level ())
uiout->table.headers_iterator = uiout->table.headers.begin ();

- uo_begin (uiout, type, new_level, id);
+ uo_begin (uiout, type, id);
}

void
ui_out_end (struct ui_out *uiout,
enum ui_out_type type)
{
- int old_level = pop_level (uiout, type);
+ pop_level (uiout, type);

- uo_end (uiout, type, old_level);
+ uo_end (uiout, type);
}

struct ui_out_end_cleanup_data
@@ -652,22 +645,20 @@ clear_table (struct ui_out *uiout)
void
uo_begin (struct ui_out *uiout,
enum ui_out_type type,
- int level,
const char *id)
{
if (uiout->impl->begin == NULL)
return;
- uiout->impl->begin (uiout, type, level, id);
+ uiout->impl->begin (uiout, type, id);
}

void
uo_end (struct ui_out *uiout,
- enum ui_out_type type,
- int level)
+ enum ui_out_type type)
{
if (uiout->impl->end == NULL)
return;
- uiout->impl->end (uiout, type, level);
+ uiout->impl->end (uiout, type);
}

void
@@ -833,7 +824,7 @@ verify_field (struct ui_out *uiout, int *fldno, int *width,
_("table_body missing; table fields must be \
specified after table_body and inside a list."));
/* NOTE: cagney/2001-12-08: There was a check here to ensure
- that this code was only executed when uiout->level was
+ that this code was only executed when uiout->level () was
greater than zero. That no longer applies - this code is run
before each table row tuple is started and at that point the
level is zero. */
@@ -842,7 +833,7 @@ specified after table_body and inside a list."));
current->inc_field_count ();

if (uiout->table.body_flag
- && uiout->table.entry_level == uiout->level
+ && uiout->table.entry_level == uiout->level ()
&& get_next_header (uiout, fldno, width, align, &text))
{
if (*fldno != current->field_count ())
@@ -906,11 +897,9 @@ ui_out_new (const struct ui_out_impl *impl, void *data,
uiout->flags = flags;
uiout->table.flag = 0;
uiout->table.body_flag = 0;
- uiout->level = 0;

- /* Create uiout->level 0, the default level. */
- std::unique_ptr<ui_out_level> level (new ui_out_level (ui_out_type_tuple));
- uiout->levels.push_back (std::move (level));
+ /* Create uiout->level () 0, the default level. */
+ push_level (uiout, ui_out_type_tuple);

uiout->table.headers_iterator = uiout->table.headers.end ();

diff --git a/gdb/ui-out.h b/gdb/ui-out.h
index ed2911d..06c05e2 100644
--- a/gdb/ui-out.h
+++ b/gdb/ui-out.h
@@ -157,14 +157,12 @@ typedef void (table_header_ftype) (struct ui_out * uiout, int width,
enum ui_align align,
const std::string &col_name,
const std::string &col_hdr);
-/* Note: level 0 is the top-level so LEVEL is always greater than
- zero. */
+
typedef void (ui_out_begin_ftype) (struct ui_out *uiout,
enum ui_out_type type,
- int level, const char *id);
+ const char *id);
typedef void (ui_out_end_ftype) (struct ui_out *uiout,
- enum ui_out_type type,
- int level);
+ enum ui_out_type type);
typedef void (field_int_ftype) (struct ui_out * uiout, int fldno, int width,
enum ui_align align,
const char *fldname, int value);
--
2.10.0