[RFA 19/22] Convert tid_range_parser to class

Tom Tromey
2016-09-27T04:42:12Z

This converts tid_range_parser to be a class, and converts various
tid_range_parser_* functions to be methods on this class; then it
updates the users to follow.

2016-09-26 Tom Tromey <tom@tromey.com>

* tid-parse.h (tid_range_parser): New class.
(tid_range_parser_get_tid, tid_range_parser_get_tid_range)
(tid_range_parser_star_range, tid_range_parser_finished)
(tid_range_parser_skip, tid_range_parser_qualified): Don't
declare.
* tid-parse.c (init, finished, get_string, skip, is_qualified)
(get_tid_or_range, get_tid_range, get_tid, star_range): Rename;
turn into methods.
(tid_is_in_list): Update.
* thread.c (thread_apply_command): Update.
---
gdb/ChangeLog | 13 +++++
gdb/thread.c | 26 ++++-----
gdb/tid-parse.c | 128 ++++++++++++++++++++----------------------
gdb/tid-parse.h | 169 +++++++++++++++++++++++++++++---------------------------
4 files changed, 173 insertions(+), 163 deletions(-)

diff --git a/gdb/ChangeLog b/gdb/ChangeLog
index 1e2fc29..bae4443 100644
--- a/gdb/ChangeLog
+++ b/gdb/ChangeLog
@@ -1,5 +1,18 @@
2016-09-26 Tom Tromey <tom@tromey.com>

+ * tid-parse.h (tid_range_parser): New class.
+ (tid_range_parser_get_tid, tid_range_parser_get_tid_range)
+ (tid_range_parser_star_range, tid_range_parser_finished)
+ (tid_range_parser_skip, tid_range_parser_qualified): Don't
+ declare.
+ * tid-parse.c (init, finished, get_string, skip, is_qualified)
+ (get_tid_or_range, get_tid_range, get_tid, star_range): Rename;
+ turn into methods.
+ (tid_is_in_list): Update.
+ * thread.c (thread_apply_command): Update.
+
+2016-09-26 Tom Tromey <tom@tromey.com>
+
* dwarf2loc.c: Include <vector>.
(read_pieced_value, write_pieced_value)
(dwarf2_compile_expr_to_ax): Use std::vector.
diff --git a/gdb/thread.c b/gdb/thread.c
index a66a2b5..718c567 100644
--- a/gdb/thread.c
+++ b/gdb/thread.c
@@ -1825,20 +1825,18 @@ thread_apply_command (char *tidlist, int from_tty)
char *cmd = NULL;
struct cleanup *old_chain;
char *saved_cmd;
- struct tid_range_parser parser;

if (tidlist == NULL || *tidlist == '\000')
error (_("Please specify a thread ID list"));

- tid_range_parser_init (&parser, tidlist, current_inferior ()->num);
- while (!tid_range_parser_finished (&parser))
+ tid_range_parser parser (tidlist, current_inferior ()->num);
+ while (!parser.finished ())
{
int inf_num, thr_start, thr_end;

- if (!tid_range_parser_get_tid_range (&parser,
- &inf_num, &thr_start, &thr_end))
+ if (!parser.get_tid_range (&inf_num, &thr_start, &thr_end))
{
- cmd = (char *) tid_range_parser_string (&parser);
+ cmd = (char *) parser.get_string ();
break;
}
}
@@ -1856,32 +1854,31 @@ thread_apply_command (char *tidlist, int from_tty)

make_cleanup_restore_current_thread ();

- tid_range_parser_init (&parser, tidlist, current_inferior ()->num);
- while (!tid_range_parser_finished (&parser)
- && tid_range_parser_string (&parser) < cmd)
+ parser.init (tidlist, current_inferior ()->num);
+ while (!parser.finished () && parser.get_string () < cmd)
{
struct thread_info *tp = NULL;
struct inferior *inf;
int inf_num, thr_num;

- tid_range_parser_get_tid (&parser, &inf_num, &thr_num);
+ parser.get_tid (&inf_num, &thr_num);
inf = find_inferior_id (inf_num);
if (inf != NULL)
tp = find_thread_id (inf, thr_num);

- if (tid_range_parser_star_range (&parser))
+ if (parser.star_range ())
{
if (inf == NULL)
{
warning (_("Unknown inferior %d"), inf_num);
- tid_range_parser_skip (&parser);
+ parser.skip ();
continue;
}

/* No use looking for threads past the highest thread number
the inferior ever had. */
if (thr_num >= inf->highest_thread_num)
- tid_range_parser_skip (&parser);
+ parser.skip ();

/* Be quiet about unknown threads numbers. */
if (tp == NULL)
@@ -1890,8 +1887,7 @@ thread_apply_command (char *tidlist, int from_tty)

if (tp == NULL)
{
- if (show_inferior_qualified_tids ()
- || tid_range_parser_qualified (&parser))
+ if (show_inferior_qualified_tids () || parser.is_qualified ())
warning (_("Unknown thread %d.%d"), inf_num, thr_num);
else
warning (_("Unknown thread %d"), thr_num);
diff --git a/gdb/tid-parse.c b/gdb/tid-parse.c
index 68133c6..0e775bc 100644
--- a/gdb/tid-parse.c
+++ b/gdb/tid-parse.c
@@ -114,28 +114,27 @@ parse_thread_id (const char *tidstr, const char **end)
/* See tid-parse.h. */

void
-tid_range_parser_init (struct tid_range_parser *parser, const char *tidlist,
- int default_inferior)
+tid_range_parser::init (const char *tidlist, int default_inferior)
{
- parser->state = TID_RANGE_STATE_INFERIOR;
- parser->string = tidlist;
- parser->inf_num = 0;
- parser->qualified = 0;
- parser->default_inferior = default_inferior;
+ this->state = TID_RANGE_STATE_INFERIOR;
+ this->string = tidlist;
+ this->inf_num = 0;
+ this->qualified = 0;
+ this->default_inferior = default_inferior;
}

/* See tid-parse.h. */

int
-tid_range_parser_finished (struct tid_range_parser *parser)
+tid_range_parser::finished () const
{
- switch (parser->state)
+ switch (this->state)
{
case TID_RANGE_STATE_INFERIOR:
- return *parser->string == '\0';
+ return *this->string == '\0';
case TID_RANGE_STATE_THREAD_RANGE:
case TID_RANGE_STATE_STAR_RANGE:
- return parser->range_parser.finished;
+ return this->range_parser.finished;
}

gdb_assert_not_reached (_("unhandled state"));
@@ -144,15 +143,15 @@ tid_range_parser_finished (struct tid_range_parser *parser)
/* See tid-parse.h. */

const char *
-tid_range_parser_string (struct tid_range_parser *parser)
+tid_range_parser::get_string () const
{
- switch (parser->state)
+ switch (this->state)
{
case TID_RANGE_STATE_INFERIOR:
- return parser->string;
+ return this->string;
case TID_RANGE_STATE_THREAD_RANGE:
case TID_RANGE_STATE_STAR_RANGE:
- return parser->range_parser.string;
+ return this->range_parser.string;
}

gdb_assert_not_reached (_("unhandled state"));
@@ -161,40 +160,38 @@ tid_range_parser_string (struct tid_range_parser *parser)
/* See tid-parse.h. */

void
-tid_range_parser_skip (struct tid_range_parser *parser)
+tid_range_parser::skip ()
{
- gdb_assert ((parser->state == TID_RANGE_STATE_THREAD_RANGE
- || parser->state == TID_RANGE_STATE_STAR_RANGE)
- && parser->range_parser.in_range);
+ gdb_assert ((this->state == TID_RANGE_STATE_THREAD_RANGE
+ || this->state == TID_RANGE_STATE_STAR_RANGE)
+ && this->range_parser.in_range);

- tid_range_parser_init (parser, parser->range_parser.end_ptr,
- parser->default_inferior);
+ init (this->range_parser.end_ptr, this->default_inferior);
}

/* See tid-parse.h. */

int
-tid_range_parser_qualified (struct tid_range_parser *parser)
+tid_range_parser::is_qualified () const
{
- return parser->qualified;
+ return this->qualified;
}

/* Helper for tid_range_parser_get_tid and
tid_range_parser_get_tid_range. Return the next range if THR_END
is non-NULL, return a single thread ID otherwise. */

-static int
-get_tid_or_range (struct tid_range_parser *parser, int *inf_num,
- int *thr_start, int *thr_end)
+int
+tid_range_parser::get_tid_or_range (int *inf_num, int *thr_start, int *thr_end)
{
- if (parser->state == TID_RANGE_STATE_INFERIOR)
+ if (this->state == TID_RANGE_STATE_INFERIOR)
{
const char *p;
const char *space;

- space = skip_to_space (parser->string);
+ space = skip_to_space (this->string);

- p = parser->string;
+ p = this->string;
while (p < space && *p != '.')
p++;
if (p < space)
@@ -202,13 +199,13 @@ get_tid_or_range (struct tid_range_parser *parser, int *inf_num,
const char *dot = p;

/* Parse number to the left of the dot. */
- p = parser->string;
- parser->inf_num
- = get_positive_number_trailer (&p, '.', parser->string);
- if (parser->inf_num == 0)
+ p = this->string;
+ this->inf_num
+ = get_positive_number_trailer (&p, '.', this->string);
+ if (this->inf_num == 0)
return 0;

- parser->qualified = 1;
+ this->qualified = 1;
p = dot + 1;

if (isspace (*p))
@@ -216,42 +213,42 @@ get_tid_or_range (struct tid_range_parser *parser, int *inf_num,
}
else
{
- parser->inf_num = parser->default_inferior;
- parser->qualified = 0;
- p = parser->string;
+ this->inf_num = this->default_inferior;
+ this->qualified = 0;
+ p = this->string;
}

- init_number_or_range (&parser->range_parser, p);
+ init_number_or_range (&this->range_parser, p);
if (p[0] == '*' && (p[1] == '\0' || isspace (p[1])))
{
/* Setup the number range parser to return numbers in the
whole [1,INT_MAX] range. */
- number_range_setup_range (&parser->range_parser, 1, INT_MAX,
+ number_range_setup_range (&this->range_parser, 1, INT_MAX,
skip_spaces_const (p + 1));
- parser->state = TID_RANGE_STATE_STAR_RANGE;
+ this->state = TID_RANGE_STATE_STAR_RANGE;
}
else
- parser->state = TID_RANGE_STATE_THREAD_RANGE;
+ this->state = TID_RANGE_STATE_THREAD_RANGE;
}

- *inf_num = parser->inf_num;
- *thr_start = get_number_or_range (&parser->range_parser);
+ *inf_num = this->inf_num;
+ *thr_start = get_number_or_range (&this->range_parser);
if (*thr_start < 0)
- error (_("negative value: %s"), parser->string);
+ error (_("negative value: %s"), this->string);
if (*thr_start == 0)
{
- parser->state = TID_RANGE_STATE_INFERIOR;
+ this->state = TID_RANGE_STATE_INFERIOR;
return 0;
}

/* If we successfully parsed a thread number or finished parsing a
thread range, switch back to assuming the next TID is
inferior-qualified. */
- if (parser->range_parser.end_ptr == NULL
- || parser->range_parser.string == parser->range_parser.end_ptr)
+ if (this->range_parser.end_ptr == NULL
+ || this->range_parser.string == this->range_parser.end_ptr)
{
- parser->state = TID_RANGE_STATE_INFERIOR;
- parser->string = parser->range_parser.string;
+ this->state = TID_RANGE_STATE_INFERIOR;
+ this->string = this->range_parser.string;

if (thr_end != NULL)
*thr_end = *thr_start;
@@ -260,11 +257,11 @@ get_tid_or_range (struct tid_range_parser *parser, int *inf_num,
/* If we're midway through a range, and the caller wants the end
value, return it and skip to the end of the range. */
if (thr_end != NULL
- && (parser->state == TID_RANGE_STATE_THREAD_RANGE
- || parser->state == TID_RANGE_STATE_STAR_RANGE))
+ && (this->state == TID_RANGE_STATE_THREAD_RANGE
+ || this->state == TID_RANGE_STATE_STAR_RANGE))
{
- *thr_end = parser->range_parser.end_value;
- tid_range_parser_skip (parser);
+ *thr_end = this->range_parser.end_value;
+ skip ();
}

return (*inf_num != 0 && *thr_start != 0);
@@ -273,31 +270,29 @@ get_tid_or_range (struct tid_range_parser *parser, int *inf_num,
/* See tid-parse.h. */

int
-tid_range_parser_get_tid_range (struct tid_range_parser *parser, int *inf_num,
- int *thr_start, int *thr_end)
+tid_range_parser::get_tid_range (int *inf_num, int *thr_start, int *thr_end)
{
gdb_assert (inf_num != NULL && thr_start != NULL && thr_end != NULL);

- return get_tid_or_range (parser, inf_num, thr_start, thr_end);
+ return get_tid_or_range (inf_num, thr_start, thr_end);
}

/* See tid-parse.h. */

int
-tid_range_parser_get_tid (struct tid_range_parser *parser,
- int *inf_num, int *thr_num)
+tid_range_parser::get_tid (int *inf_num, int *thr_num)
{
gdb_assert (inf_num != NULL && thr_num != NULL);

- return get_tid_or_range (parser, inf_num, thr_num, NULL);
+ return get_tid_or_range (inf_num, thr_num, NULL);
}

/* See tid-parse.h. */

int
-tid_range_parser_star_range (struct tid_range_parser *parser)
+tid_range_parser::star_range ()
{
- return parser->state == TID_RANGE_STATE_STAR_RANGE;
+ return this->state == TID_RANGE_STATE_STAR_RANGE;
}

/* See gdbthread.h. */
@@ -306,19 +301,16 @@ int
tid_is_in_list (const char *list, int default_inferior,
int inf_num, int thr_num)
{
- struct tid_range_parser parser;
-
if (list == NULL || *list == '\0')
return 1;

- tid_range_parser_init (&parser, list, default_inferior);
- while (!tid_range_parser_finished (&parser))
+ tid_range_parser parser (list, default_inferior);
+ while (!parser.finished ())
{
int tmp_inf, tmp_thr_start, tmp_thr_end;

- if (!tid_range_parser_get_tid_range (&parser, &tmp_inf,
- &tmp_thr_start, &tmp_thr_end))
- invalid_thread_id_error (parser.string);
+ if (!parser.get_tid_range (&tmp_inf, &tmp_thr_start, &tmp_thr_end))
+ invalid_thread_id_error (parser.get_string ());
if (tmp_inf == inf_num
&& tmp_thr_start <= thr_num && thr_num <= tmp_thr_end)
return 1;
diff --git a/gdb/tid-parse.h b/gdb/tid-parse.h
index 830cf36..e02dc16 100644
--- a/gdb/tid-parse.h
+++ b/gdb/tid-parse.h
@@ -49,119 +49,128 @@ enum tid_range_state
TID_RANGE_STATE_STAR_RANGE,
};

-/* An object of this type is passed to tid_range_parser_get_tid. It
- must be initialized by calling tid_range_parser_init. This type is
- defined here so that it can be stack-allocated, but all members
- should be treated as opaque. */
-struct tid_range_parser
+/* An object of this type is passed to tid_range_parser_get_tid. */
+class tid_range_parser
{
- /* What sub-component are we expecting. */
- enum tid_range_state state;
-
- /* The string being parsed. When parsing has finished, this points
- past the last parsed token. */
- const char *string;
-
- /* The range parser state when we're parsing the thread number
- sub-component. */
- struct get_number_or_range_state range_parser;
-
- /* Last inferior number returned. */
- int inf_num;
-
- /* True if the TID last parsed was explicitly inferior-qualified.
- IOW, whether the spec specified an inferior number
- explicitly. */
- int qualified;
+ public:

- /* The inferior number to assume if the TID is not qualified. */
- int default_inferior;
-};
+ /* Initialize a tid_range_parser for use with get_tid. TIDLIST is
+ the string to be parsed. DEFAULT_INFERIOR is the inferior number
+ to assume if a non-qualified thread ID is found. */
+ tid_range_parser (const char *tidlist, int default_inferior)
+ {
+ init (tidlist, default_inferior);
+ }

-/* Initialize a tid_range_parser for use with
- tid_range_parser_get_tid. TIDLIST is the string to be parsed.
- DEFAULT_INFERIOR is the inferior number to assume if a
- non-qualified thread ID is found. */
-extern void tid_range_parser_init (struct tid_range_parser *parser,
- const char *tidlist,
- int default_inferior);
+ /* Initialize a tid_range_parser for use with get_tid. TIDLIST is
+ the string to be parsed. DEFAULT_INFERIOR is the inferior number
+ to assume if a non-qualified thread ID is found. */
+ void init (const char *tidlist, int default_inferior);

-/* Parse a thread ID or a thread range list.
+ /* Parse a thread ID or a thread range list.

- A range will be of the form
+ A range will be of the form

<inferior_num>.<thread_number1>-<thread_number2>

- and will represent all the threads of inferior INFERIOR_NUM with
- number between THREAD_NUMBER1 and THREAD_NUMBER2, inclusive.
- <inferior_num> can also be omitted, as in
+ and will represent all the threads of inferior INFERIOR_NUM with
+ number between THREAD_NUMBER1 and THREAD_NUMBER2, inclusive.
+ <inferior_num> can also be omitted, as in

<thread_number1>-<thread_number2>

- in which case GDB infers the inferior number from the default
- passed to the tid_range_parser_init function.
+ in which case GDB infers the inferior number from the default
+ passed to the constructor.

- This function is designed to be called iteratively. While
- processing a thread ID range list, at each call it will return (in
- the INF_NUM and THR_NUM output parameters) the next thread ID in
- the range (irrespective of whether the thread actually exists).
+ This function is designed to be called iteratively. While
+ processing a thread ID range list, at each call it will return (in
+ the INF_NUM and THR_NUM output parameters) the next thread ID in
+ the range (irrespective of whether the thread actually exists).

- At the beginning of parsing a thread range, the char pointer
- PARSER->string will be advanced past <thread_number1> and left
- pointing at the '-' token. Subsequent calls will not advance the
- pointer until the range is completed. The call that completes the
- range will advance the pointer past <thread_number2>.
+ At the beginning of parsing a thread range, the char pointer
+ THIS->string will be advanced past <thread_number1> and left
+ pointing at the '-' token. Subsequent calls will not advance the
+ pointer until the range is completed. The call that completes the
+ range will advance the pointer past <thread_number2>.

- This function advances through the input string for as long you
- call it. Once the end of the input string is reached, a call to
- tid_range_parser_finished returns false (see below).
+ This function advances through the input string for as long you
+ call it. Once the end of the input string is reached, a call to
+ finished returns false (see below).

- E.g., with list: "1.2 3.4-6":
+ E.g., with list: "1.2 3.4-6":

1st call: *INF_NUM=1; *THR_NUM=2 (finished==0)
2nd call: *INF_NUM=3; *THR_NUM=4 (finished==0)
3rd call: *INF_NUM=3; *THR_NUM=5 (finished==0)
4th call: *INF_NUM=3; *THR_NUM=6 (finished==1)

- Returns true if parsed a thread/range successfully, false
- otherwise. */
-extern int tid_range_parser_get_tid (struct tid_range_parser *parser,
- int *inf_num, int *thr_num);
+ Returns true if parsed a thread/range successfully, false
+ otherwise. */
+ int get_tid (int *inf_num, int *thr_num);

-/* Like tid_range_parser_get_tid, but return a thread ID range per
- call, rather then a single thread ID.
+ /* Like get_tid, but return a thread ID range per
+ call, rather then a single thread ID.

- If the next element in the list is a single thread ID, then
- *THR_START and *THR_END are set to the same value.
+ If the next element in the list is a single thread ID, then
+ *THR_START and *THR_END are set to the same value.

- E.g.,. with list: "1.2 3.4-6"
+ E.g.,. with list: "1.2 3.4-6"

1st call: *INF_NUM=1; *THR_START=2; *THR_END=2 (finished==0)
2nd call: *INF_NUM=3; *THR_START=4; *THR_END=6 (finished==1)

- Returns true if parsed a thread/range successfully, false
- otherwise. */
-extern int tid_range_parser_get_tid_range (struct tid_range_parser *parser,
- int *inf_num,
- int *thr_start, int *thr_end);
+ Returns true if parsed a thread/range successfully, false
+ otherwise. */
+ int get_tid_range (int *inf_num, int *thr_start, int *thr_end);
+
+ /* Returns non-zero if processing a star wildcard (e.g., "1.*")
+ range. */
+ int star_range ();
+
+ /* Returns non-zero if parsing has completed. */
+ int finished () const;
+
+ /* Return the string being parsed. When parsing has finished, this
+ points past the last parsed token. */
+ const char *get_string () const;

-/* Returns non-zero if processing a star wildcard (e.g., "1.*")
- range. */
-extern int tid_range_parser_star_range (struct tid_range_parser *parser);
+ /* When parsing a range, advance past the final token in the range. */
+ void skip ();

-/* Returns non-zero if parsing has completed. */
-extern int tid_range_parser_finished (struct tid_range_parser *parser);
+ /* True if the TID last parsed was explicitly inferior-qualified.
+ IOW, whether the spec specified an inferior number explicitly. */
+ int is_qualified () const;
+
+ private:
+
+ // No need for these. They are intentionally not defined anywhere.
+ tid_range_parser &operator= (const tid_range_parser &);
+ tid_range_parser (const tid_range_parser &);
+
+ int get_tid_or_range (int *inf_num, int *thr_start, int *thr_end);
+
+ /* What sub-component are we expecting. */
+ enum tid_range_state state;
+
+ /* The string being parsed. When parsing has finished, this points
+ past the last parsed token. */
+ const char *string;
+
+ /* The range parser state when we're parsing the thread number
+ sub-component. */
+ struct get_number_or_range_state range_parser;

-/* Return the string being parsed. When parsing has finished, this
- points past the last parsed token. */
-const char *tid_range_parser_string (struct tid_range_parser *parser);
+ /* Last inferior number returned. */
+ int inf_num;

-/* When parsing a range, advance past the final token in the range. */
-extern void tid_range_parser_skip (struct tid_range_parser *parser);
+ /* True if the TID last parsed was explicitly inferior-qualified.
+ IOW, whether the spec specified an inferior number
+ explicitly. */
+ int qualified;

-/* True if the TID last parsed was explicitly inferior-qualified.
- IOW, whether the spec specified an inferior number explicitly. */
-extern int tid_range_parser_qualified (struct tid_range_parser *parser);
+ /* The inferior number to assume if the TID is not qualified. */
+ int default_inferior;
+};

/* Accept a string-form list of thread IDs such as is accepted by
tid_range_parser_get_tid. Return true if the INF_NUM.THR.NUM
--
2.7.4