Files
binutils-gdb/gdb/cli/cli-setshow.h
Lancelot SIX 5be5fa3ffd gdb: Add typesafe getter/setter for cmd_list_element.var
cmd_list_element can contain a pointer to data that can be set and / or
shown.  This is achieved with a void* that points to the data that can
be accessed, while var_type (of type enum var_types) tells how to
interpret the data pointed to.

With this pattern, the user of the cmd_list_element needs to know what
the storage type associated with a given VAR_TYPES tag, and needs to do
the proper casting.  No automatic check at compile or run time enforces
that the adequate cast is done.

This looks something like:

	if (c->var_type == var_zuinteger)
	  unsigned int v = *(unsigned int*)v->var_type;

With this approach, it is easy to make an error.

This patch aims at addressing the same problem as
https://sourceware.org/pipermail/gdb-patches/2021-July/180920.html but
uses a different approach.

This patch proposes to add an abstraction around the var_types and void*
pointer pair.  The abstraction is meant to prevent the user from having
to handle the cast and verify that the data is read or written as a type
that is coherent with the setting's var_type.  This is achieved by
introducing the setting struct, and a set of templated get / set member
functions.  The template parameter is the type of the variable that
holds the referred variable.

For example, instantiating the member functions with bool will yield
something similar to:

	bool get<bool> () const
	{
	  gdb_assert (this->m_var_type == var_boolean);
	  gdb_assert (this->m_var != nullptr);
	  return *static_cast<bool *> (this->m_var);
	}
	void set<bool> (bool var)
	{
	  gdb_assert (this->m_var_type == var_boolean);
	  gdb_assert (this->m_var != nullptr);
	  *static_cast<bool *> (this->m_var) = var;
	}

With such abstractions available, the var_type and var members of
cmd_list_element are replaced with a setting instance.

No user visible change is expected after this patch.

Tested on GNU/Linux x86_64, with no regression noticed.

Change-Id: Ie1d08c3ceb8b30b3d7bf1efe036eb8acffcd2f34
2021-08-19 17:19:09 -04:00

66 lines
2.7 KiB
C++

/* Header file for GDB CLI set and show commands implementation.
Copyright (C) 2000-2021 Free Software Foundation, Inc.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>. */
#ifndef CLI_CLI_SETSHOW_H
#define CLI_CLI_SETSHOW_H
#include <string>
struct cmd_list_element;
/* Parse ARG, an option to a boolean variable.
Returns 1 for true, 0 for false, and -1 if invalid. */
extern int parse_cli_boolean_value (const char *arg);
/* Same as above, but work with a pointer to pointer. ARG is advanced
past a successfully parsed value. */
extern int parse_cli_boolean_value (const char **arg);
/* Parse ARG, an option to a var_uinteger or var_zuinteger variable.
Either returns the parsed value on success or throws an error. If
EXPRESSION is true, *ARG is parsed as an expression; otherwise, it
is parsed with get_ulongest. It's not possible to parse the
integer as an expression when there may be valid input after the
integer, such as when parsing command options. E.g., "print
-elements NUMBER -obj --". In such case, parsing as an expression
would parse "-obj --" as part of the expression as well. */
extern unsigned int parse_cli_var_uinteger (var_types var_type,
const char **arg,
bool expression);
/* Like parse_cli_var_uinteger, for var_zuinteger_unlimited. */
extern int parse_cli_var_zuinteger_unlimited (const char **arg,
bool expression);
/* Parse ARG, an option to a var_enum variable. ENUM is a
null-terminated array of possible values. Either returns the parsed
value on success or throws an error. ARG is advanced past the
parsed value. */
const char *parse_cli_var_enum (const char **args,
const char *const *enums);
extern void do_set_command (const char *arg, int from_tty,
struct cmd_list_element *c);
extern void do_show_command (const char *arg, int from_tty,
struct cmd_list_element *c);
/* Get a string version of VAR's value. */
extern std::string get_setshow_command_value_string (const setting &var);
extern void cmd_show_list (struct cmd_list_element *list, int from_tty);
#endif /* CLI_CLI_SETSHOW_H */