From bfd282882d534cd4f48e2fc29d4ce0923c52352b Mon Sep 17 00:00:00 2001 From: Pedro Alves Date: Thu, 13 Oct 2016 01:54:07 +0100 Subject: Convert tid_range_parser and get_number_or_range to classes This converts tid_range_parser and get_number_or_range to be classes. The various tid_range_parser_* and get_number_or_range_* functions become methods on the respective classes. Then it updates the users to follow. The rationale for the change is that this provides better encapsulation. For example, this forced me to think of a better interface between tid_range_parser and get_number_or_range, since the former peeked into the latter's internals a bit too much. That ended up resulting mostly in these two not-just-straight-1-1 changes: void -tid_range_parser_skip (struct tid_range_parser *parser) +tid_range_parser::skip_range () { ... - tid_range_parser_init (parser, parser->range_parser.end_ptr, - parser->default_inferior); + m_range_parser.skip_range (); + init (m_range_parser.string (), m_default_inferior); } and: /* 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 (!m_range_parser.in_range ()) { For the same reason (encapsulation), this moves the enum tid_range_state definition to within the tid_parser class's scope, since that is private implementation detail. While at it, switch to use "bool" for booleans. gdb/ChangeLog: 2016-10-13 Pedro Alves Tom Tromey * tid-parse.h (tid_range_parser): New class. (enum tid_range_state): Move into tid_range_parser's scope. Remove TID_RANGE_ prefix from all values. (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_is_in_list): Update comment. * tid-parse.c (tid_range_parser::tid_range_parser): New. (init, finished, get_string, skip, tid_is_qualified) (get_tid_or_range, get_tid_range, get_tid, star_range): Rename; turn into methods. (tid_is_in_list): Adjust. * cli/cli-utils.h (number_or_range_parser): New class. (init_number_or_range, get_number_or_range) (number_range_setup_range): Don't declare. * cli/cli-utils.c (number_or_range_parser::number_or_range_parser): New. (init_number_or_range, get_number_or_range) (number_range_setup_range): Rename; turn into methods. (number_is_in_list): Adjust. * breakpoint.c (map_breakpoint_numbers): Adjust. Use bool. (trace_pass_command, get_tracepoint_by_number): Adjust. * breakpoint.h (get_tracepoint_by_number): Adjust. * inferior.c (detach_inferior_command, kill_inferior_command) (remove_inferior_command): Adjust. * linespec.c (decode_line_2): Adjust. * memattr.c (mem_enable_command, mem_disable_command) (mem_delete_command): Adjust. * printcmd.c (map_display_numbers): Adjust. * reverse.c (delete_bookmark_command, bookmarks_info): Adjust. * thread.c (thread_apply_command): Adjust. --- gdb/cli/cli-utils.c | 76 +++++++++++++++++++---------------- gdb/cli/cli-utils.h | 114 ++++++++++++++++++++++++++++++++-------------------- 2 files changed, 112 insertions(+), 78 deletions(-) (limited to 'gdb/cli') diff --git a/gdb/cli/cli-utils.c b/gdb/cli/cli-utils.c index 0946db0..0fb68f2 100644 --- a/gdb/cli/cli-utils.c +++ b/gdb/cli/cli-utils.c @@ -121,39 +121,49 @@ get_number (char **pp) /* See documentation in cli-utils.h. */ +number_or_range_parser::number_or_range_parser (const char *string) +{ + init (string); +} + +/* See documentation in cli-utils.h. */ + void -init_number_or_range (struct get_number_or_range_state *state, - const char *string) +number_or_range_parser::init (const char *string) { - memset (state, 0, sizeof (*state)); - state->string = string; + m_finished = false; + m_cur_tok = string; + m_last_retval = 0; + m_end_value = 0; + m_end_ptr = NULL; + m_in_range = false; } /* See documentation in cli-utils.h. */ int -get_number_or_range (struct get_number_or_range_state *state) +number_or_range_parser::get_number () { - if (state->in_range) + if (m_in_range) { /* All number-parsing has already been done. Return the next integer value (one greater than the saved previous value). Do not advance the token pointer until the end of range is reached. */ - if (++state->last_retval == state->end_value) + if (++m_last_retval == m_end_value) { /* End of range reached; advance token pointer. */ - state->string = state->end_ptr; - state->in_range = 0; + m_cur_tok = m_end_ptr; + m_in_range = false; } } - else if (*state->string != '-') + else if (*m_cur_tok != '-') { - /* Default case: state->string is pointing either to a solo + /* Default case: state->m_cur_tok is pointing either to a solo number, or to the first number of a range. */ - state->last_retval = get_number_trailer (&state->string, '-'); - if (*state->string == '-') + m_last_retval = get_number_trailer (&m_cur_tok, '-'); + if (*m_cur_tok == '-') { const char **temp; @@ -161,42 +171,42 @@ get_number_or_range (struct get_number_or_range_state *state) Skip the '-', parse and remember the second number, and also remember the end of the final token. */ - temp = &state->end_ptr; - state->end_ptr = skip_spaces_const (state->string + 1); - state->end_value = get_number_const (temp); - if (state->end_value < state->last_retval) + temp = &m_end_ptr; + m_end_ptr = skip_spaces_const (m_cur_tok + 1); + m_end_value = get_number_const (temp); + if (m_end_value < m_last_retval) { error (_("inverted range")); } - else if (state->end_value == state->last_retval) + else if (m_end_value == m_last_retval) { /* Degenerate range (number1 == number2). Advance the token pointer so that the range will be treated as a - single number. */ - state->string = state->end_ptr; + single number. */ + m_cur_tok = m_end_ptr; } else - state->in_range = 1; + m_in_range = true; } } else error (_("negative value")); - state->finished = *state->string == '\0'; - return state->last_retval; + m_finished = *m_cur_tok == '\0'; + return m_last_retval; } /* See documentation in cli-utils.h. */ void -number_range_setup_range (struct get_number_or_range_state *state, - int start_value, int end_value, const char *end_ptr) +number_or_range_parser::setup_range (int start_value, int end_value, + const char *end_ptr) { gdb_assert (start_value > 0); - state->in_range = 1; - state->end_ptr = end_ptr; - state->last_retval = start_value - 1; - state->end_value = end_value; + m_in_range = true; + m_end_ptr = end_ptr; + m_last_retval = start_value - 1; + m_end_value = end_value; } /* Accept a number and a string-form list of numbers such as is @@ -210,15 +220,13 @@ number_range_setup_range (struct get_number_or_range_state *state, int number_is_in_list (const char *list, int number) { - struct get_number_or_range_state state; - if (list == NULL || *list == '\0') return 1; - init_number_or_range (&state, list); - while (!state.finished) + number_or_range_parser parser (list); + while (!parser.finished ()) { - int gotnum = get_number_or_range (&state); + int gotnum = parser.get_number (); if (gotnum == 0) error (_("Args must be numbers or '$' variables.")); diff --git a/gdb/cli/cli-utils.h b/gdb/cli/cli-utils.h index a31fff5..3188bb7 100644 --- a/gdb/cli/cli-utils.h +++ b/gdb/cli/cli-utils.h @@ -39,65 +39,91 @@ extern int get_number_const (const char **); extern int get_number (char **); -/* An object of this type is passed to get_number_or_range. It must - be initialized by calling init_number_or_range. This type is - defined here so that it can be stack-allocated, but all members - other than `finished' and `string' should be treated as opaque. */ +/* Parse a number or a range. + A number will be of the form handled by get_number. + A range will be of the form - , and + will represent all the integers between number1 and number2, + inclusive. */ -struct get_number_or_range_state +class number_or_range_parser { - /* Non-zero if parsing has completed. */ - int finished; +public: + /* Default construction. Must call init before calling + get_next. */ + number_or_range_parser () {} + + /* Calls init automatically. */ + number_or_range_parser (const char *string); + + /* STRING is the string to be parsed. */ + void init (const char *string); + + /* While processing a range, this fuction is called iteratively; At + each call it will return the next value in the range. + + At the beginning of parsing a range, the char pointer + STATE->m_cur_tok will be advanced past 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 . */ + int get_number (); + + /* Setup internal state such that get_next() returns numbers in the + START_VALUE to END_VALUE range. END_PTR is where the string is + advanced to when get_next() returns END_VALUE. */ + void setup_range (int start_value, int end_value, + const char *end_ptr); + + /* Returns true if parsing has completed. */ + bool finished () const + { return m_finished; } + + /* Return the string being parsed. When parsing has finished, this + points past the last parsed token. */ + const char *cur_tok () const + { return m_cur_tok; } + + /* True when parsing a range. */ + bool in_range () const + { return m_in_range; } + + /* When parsing a range, the final value in the range. */ + int end_value () const + { return m_end_value; } + + /* When parsing a range, skip past the final token in the range. */ + void skip_range () + { + gdb_assert (m_in_range); + m_cur_tok = m_end_ptr; + } + +private: + /* No need for these. They are intentionally not defined anywhere. */ + number_or_range_parser (const number_or_range_parser &); + number_or_range_parser &operator= (const number_or_range_parser &); + + /* True if parsing has completed. */ + bool m_finished; /* The string being parsed. When parsing has finished, this points past the last parsed token. */ - const char *string; + const char *m_cur_tok; /* Last value returned. */ - int last_retval; + int m_last_retval; /* When parsing a range, the final value in the range. */ - int end_value; + int m_end_value; /* When parsing a range, a pointer past the final token in the range. */ - const char *end_ptr; + const char *m_end_ptr; - /* Non-zero when parsing a range. */ - int in_range; + /* True when parsing a range. */ + bool m_in_range; }; -/* Initialize a get_number_or_range_state for use with - get_number_or_range_state. STRING is the string to be parsed. */ - -extern void init_number_or_range (struct get_number_or_range_state *state, - const char *string); - -/* Parse a number or a range. - A number will be of the form handled by get_number. - A range will be of the form - , and - will represent all the integers between number1 and number2, - inclusive. - - While processing a range, this fuction is called iteratively; - At each call it will return the next value in the range. - - At the beginning of parsing a range, the char pointer STATE->string will - be advanced past 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 . */ - -extern int get_number_or_range (struct get_number_or_range_state *state); - -/* Setups STATE such that get_number_or_range returns numbers in range - START_VALUE to END_VALUE. When get_number_or_range returns - END_VALUE, the STATE string is advanced to END_PTR. */ - -extern void number_range_setup_range (struct get_number_or_range_state *state, - int start_value, int end_value, - const char *end_ptr); - /* Accept a number and a string-form list of numbers such as is accepted by get_number_or_range. Return TRUE if the number is in the list. -- cgit v1.1