From fcef6471ed3c07d6eac2d9e0010552994f0891aa Mon Sep 17 00:00:00 2001 From: Simon Marchi Date: Sun, 31 Oct 2021 12:02:03 -0400 Subject: [PATCH] gdb: pass/return setting setter/getter scalar values by value The getter and setter in struct setting always receive and return values by const reference. This is not necessary for scalar values (like bool and int), but more importantly it makes it a bit annoying to write a getter, you have to use a scratch static variable or something similar that you can refer to: const bool & my_getter () { static bool value; value = function_returning_bool (); return value; } Change the getter and setter function signatures to receive and return value by value instead of by reference, when the underlying data type is scalar. This means that string-based settings will still use references, but all others will be by value. The getter above would then be re-written as: bool my_getter () { return function_returning_bool (); } This is useful for a patch later in this series that defines a boolean setting with a getter and a setter. Change-Id: Ieca3a2419fcdb75a6f75948b2c920b548a0af0fd --- gdb/cli/cli-decode.c | 52 +++++++++++++-------------- gdb/command.h | 84 ++++++++++++++++++++++++++------------------ gdb/remote.c | 6 +++- 3 files changed, 81 insertions(+), 61 deletions(-) diff --git a/gdb/cli/cli-decode.c b/gdb/cli/cli-decode.c index 019aa629876..667d1558575 100644 --- a/gdb/cli/cli-decode.c +++ b/gdb/cli/cli-decode.c @@ -572,8 +572,8 @@ add_setshow_cmd_full (const char *name, var_types var_type, T *var, const char *set_doc, const char *show_doc, const char *help_doc, - setting_setter_ftype set_setting_func, - setting_getter_ftype get_setting_func, + typename setting_func_types::set set_setting_func, + typename setting_func_types::get get_setting_func, cmd_func_ftype *set_func, show_value_ftype *show_func, struct cmd_list_element **set_list, @@ -628,8 +628,8 @@ set_show_commands add_setshow_enum_cmd (const char *name, command_class theclass, const char *const *enumlist, const char *set_doc, const char *show_doc, const char *help_doc, - setting_setter_ftype set_func, - setting_getter_ftype get_func, + setting_func_types::set set_func, + setting_func_types::get get_func, show_value_ftype *show_func, cmd_list_element **set_list, cmd_list_element **show_list) @@ -682,8 +682,8 @@ set_show_commands add_setshow_auto_boolean_cmd (const char *name, command_class theclass, const char *set_doc, const char *show_doc, const char *help_doc, - setting_setter_ftype set_func, - setting_getter_ftype get_func, + setting_func_types::set set_func, + setting_func_types::get get_func, show_value_ftype *show_func, cmd_list_element **set_list, cmd_list_element **show_list) @@ -737,8 +737,8 @@ set_show_commands add_setshow_boolean_cmd (const char *name, command_class theclass, const char *set_doc, const char *show_doc, const char *help_doc, - setting_setter_ftype set_func, - setting_getter_ftype get_func, + setting_func_types::set set_func, + setting_func_types::get get_func, show_value_ftype *show_func, cmd_list_element **set_list, cmd_list_element **show_list) @@ -784,8 +784,8 @@ set_show_commands add_setshow_filename_cmd (const char *name, command_class theclass, const char *set_doc, const char *show_doc, const char *help_doc, - setting_setter_ftype set_func, - setting_getter_ftype get_func, + setting_func_types::set set_func, + setting_func_types::get get_func, show_value_ftype *show_func, cmd_list_element **set_list, cmd_list_element **show_list) @@ -833,8 +833,8 @@ set_show_commands add_setshow_string_cmd (const char *name, command_class theclass, const char *set_doc, const char *show_doc, const char *help_doc, - setting_setter_ftype set_func, - setting_getter_ftype get_func, + setting_func_types::set set_func, + setting_func_types::get get_func, show_value_ftype *show_func, cmd_list_element **set_list, cmd_list_element **show_list) @@ -883,8 +883,8 @@ set_show_commands add_setshow_string_noescape_cmd (const char *name, command_class theclass, const char *set_doc, const char *show_doc, const char *help_doc, - setting_setter_ftype set_func, - setting_getter_ftype get_func, + setting_func_types::set set_func, + setting_func_types::get get_func, show_value_ftype *show_func, cmd_list_element **set_list, cmd_list_element **show_list) @@ -933,8 +933,8 @@ set_show_commands add_setshow_optional_filename_cmd (const char *name, command_class theclass, const char *set_doc, const char *show_doc, const char *help_doc, - setting_setter_ftype set_func, - setting_getter_ftype get_func, + setting_func_types::set set_func, + setting_func_types::get get_func, show_value_ftype *show_func, cmd_list_element **set_list, cmd_list_element **show_list) @@ -1001,8 +1001,8 @@ set_show_commands add_setshow_integer_cmd (const char *name, command_class theclass, const char *set_doc, const char *show_doc, const char *help_doc, - setting_setter_ftype set_func, - setting_getter_ftype get_func, + setting_func_types::set set_func, + setting_func_types::get get_func, show_value_ftype *show_func, cmd_list_element **set_list, cmd_list_element **show_list) @@ -1050,8 +1050,8 @@ set_show_commands add_setshow_uinteger_cmd (const char *name, command_class theclass, const char *set_doc, const char *show_doc, const char *help_doc, - setting_setter_ftype set_func, - setting_getter_ftype get_func, + setting_func_types::set set_func, + setting_func_types::get get_func, show_value_ftype *show_func, cmd_list_element **set_list, cmd_list_element **show_list) @@ -1095,8 +1095,8 @@ set_show_commands add_setshow_zinteger_cmd (const char *name, command_class theclass, const char *set_doc, const char *show_doc, const char *help_doc, - setting_setter_ftype set_func, - setting_getter_ftype get_func, + setting_func_types::set set_func, + setting_func_types::get get_func, show_value_ftype *show_func, cmd_list_element **set_list, cmd_list_element **show_list) @@ -1137,8 +1137,8 @@ set_show_commands add_setshow_zuinteger_unlimited_cmd (const char *name, command_class theclass, const char *set_doc, const char *show_doc, const char *help_doc, - setting_setter_ftype set_func, - setting_getter_ftype get_func, + setting_func_types::set set_func, + setting_func_types::get get_func, show_value_ftype *show_func, cmd_list_element **set_list, cmd_list_element **show_list) @@ -1182,8 +1182,8 @@ set_show_commands add_setshow_zuinteger_cmd (const char *name, command_class theclass, const char *set_doc, const char *show_doc, const char *help_doc, - setting_setter_ftype set_func, - setting_getter_ftype get_func, + setting_func_types::set set_func, + setting_func_types::get get_func, show_value_ftype *show_func, cmd_list_element **set_list, cmd_list_element **show_list) diff --git a/gdb/command.h b/gdb/command.h index d6e94b97eb1..bcd3882f811 100644 --- a/gdb/command.h +++ b/gdb/command.h @@ -180,15 +180,31 @@ inline bool var_type_uses (var_types t) return t == var_enum; } -/* Function signature for a callback used to get a value from a setting. */ +template struct setting_func_types_1; template -using setting_getter_ftype = const T &(*) (); +struct setting_func_types_1 +{ + using type = T; + using set = void (*) (type); + using get = type (*) (); +}; -/* Function signature for a callback used to set a value to a setting. */ +template +struct setting_func_types_1 +{ + using type = const T &; + using set = void (*) (type); + using get = type (*) (); +}; template -using setting_setter_ftype = void (*) (const T &); +struct setting_func_types +{ + using type = typename setting_func_types_1::value, T>::type; + using set = typename setting_func_types_1::value, T>::set; + using get = typename setting_func_types_1::value, T>::get; +}; /* Generic/type-erased function pointer. */ @@ -224,8 +240,8 @@ struct setting template static erased_args erase_args (var_types var_type, T *var, - setting_setter_ftype set_setting_func, - setting_getter_ftype get_setting_func) + typename setting_func_types::set set_setting_func, + typename setting_func_types::get get_setting_func) { gdb_assert (var_type_uses (var_type)); /* The getter and the setter must be both provided or both omitted. */ @@ -259,8 +275,8 @@ struct setting Type T must match the var type VAR_TYPE (see VAR_TYPE_USES). */ template setting (var_types var_type, - setting_setter_ftype setter, - setting_getter_ftype getter) + typename setting_func_types::set setter, + typename setting_func_types::get getter) : m_var_type (var_type) { gdb_assert (var_type_uses (var_type)); @@ -284,14 +300,14 @@ struct setting The template parameter T is the type of the variable used to store the setting. */ template - const T &get () const + typename setting_func_types::type get () const { gdb_assert (var_type_uses (m_var_type)); if (m_var == nullptr) { gdb_assert (m_getter != nullptr); - auto getter = reinterpret_cast> (m_getter); + auto getter = reinterpret_cast::get> (m_getter); return getter (); } else @@ -321,7 +337,7 @@ struct setting if (m_var == nullptr) { gdb_assert (m_setter != nullptr); - auto setter = reinterpret_cast> (m_setter); + auto setter = reinterpret_cast::set> (m_setter); setter (v); } else @@ -643,8 +659,8 @@ extern set_show_commands add_setshow_enum_cmd extern set_show_commands add_setshow_enum_cmd (const char *name, command_class theclass, const char *const *enumlist, const char *set_doc, const char *show_doc, - const char *help_doc, setting_setter_ftype set_func, - setting_getter_ftype get_func, show_value_ftype *show_func, + const char *help_doc, setting_func_types::set set_func, + setting_func_types::get get_func, show_value_ftype *show_func, cmd_list_element **set_list, cmd_list_element **show_list); extern set_show_commands add_setshow_auto_boolean_cmd @@ -656,8 +672,8 @@ extern set_show_commands add_setshow_auto_boolean_cmd extern set_show_commands add_setshow_auto_boolean_cmd (const char *name, command_class theclass, const char *set_doc, const char *show_doc, const char *help_doc, - setting_setter_ftype set_func, - setting_getter_ftype get_func, + setting_func_types::set set_func, + setting_func_types::get get_func, show_value_ftype *show_func, cmd_list_element **set_list, cmd_list_element **show_list); @@ -670,8 +686,8 @@ extern set_show_commands add_setshow_boolean_cmd extern set_show_commands add_setshow_boolean_cmd (const char *name, command_class theclass, const char *set_doc, const char *show_doc, const char *help_doc, - setting_setter_ftype set_func, - setting_getter_ftype get_func, show_value_ftype *show_func, + setting_func_types::set set_func, + setting_func_types::get get_func, show_value_ftype *show_func, cmd_list_element **set_list, cmd_list_element **show_list); extern set_show_commands add_setshow_filename_cmd @@ -683,8 +699,8 @@ extern set_show_commands add_setshow_filename_cmd extern set_show_commands add_setshow_filename_cmd (const char *name, command_class theclass, const char *set_doc, const char *show_doc, const char *help_doc, - setting_setter_ftype set_func, - setting_getter_ftype get_func, show_value_ftype *show_func, + setting_func_types::set set_func, + setting_func_types::get get_func, show_value_ftype *show_func, cmd_list_element **set_list, cmd_list_element **show_list); extern set_show_commands add_setshow_string_cmd @@ -696,8 +712,8 @@ extern set_show_commands add_setshow_string_cmd extern set_show_commands add_setshow_string_cmd (const char *name, command_class theclass, const char *set_doc, const char *show_doc, const char *help_doc, - setting_setter_ftype set_func, - setting_getter_ftype get_func, + setting_func_types::set set_func, + setting_func_types::get get_func, show_value_ftype *show_func, cmd_list_element **set_list, cmd_list_element **show_list); @@ -710,8 +726,8 @@ extern set_show_commands add_setshow_string_noescape_cmd extern set_show_commands add_setshow_string_noescape_cmd (const char *name, command_class theclass, const char *set_doc, const char *show_doc, const char *help_doc, - setting_setter_ftype set_func, - setting_getter_ftype get_func, show_value_ftype *show_func, + setting_func_types::set set_func, + setting_func_types::get get_func, show_value_ftype *show_func, cmd_list_element **set_list, cmd_list_element **show_list); extern set_show_commands add_setshow_optional_filename_cmd @@ -723,8 +739,8 @@ extern set_show_commands add_setshow_optional_filename_cmd extern set_show_commands add_setshow_optional_filename_cmd (const char *name, command_class theclass, const char *set_doc, const char *show_doc, const char *help_doc, - setting_setter_ftype set_func, - setting_getter_ftype get_func, + setting_func_types::set set_func, + setting_func_types::get get_func, show_value_ftype *show_func, cmd_list_element **set_list, cmd_list_element **show_list); @@ -737,8 +753,8 @@ extern set_show_commands add_setshow_integer_cmd extern set_show_commands add_setshow_integer_cmd (const char *name, command_class theclass, const char *set_doc, const char *show_doc, const char *help_doc, - setting_setter_ftype set_func, - setting_getter_ftype get_func, show_value_ftype *show_func, + setting_func_types::set set_func, + setting_func_types::get get_func, show_value_ftype *show_func, cmd_list_element **set_list, cmd_list_element **show_list); extern set_show_commands add_setshow_uinteger_cmd @@ -750,8 +766,8 @@ extern set_show_commands add_setshow_uinteger_cmd extern set_show_commands add_setshow_uinteger_cmd (const char *name, command_class theclass, const char *set_doc, const char *show_doc, const char *help_doc, - setting_setter_ftype set_func, - setting_getter_ftype get_func, show_value_ftype *show_func, + setting_func_types::set set_func, + setting_func_types::get get_func, show_value_ftype *show_func, cmd_list_element **set_list, cmd_list_element **show_list); extern set_show_commands add_setshow_zinteger_cmd @@ -763,8 +779,8 @@ extern set_show_commands add_setshow_zinteger_cmd extern set_show_commands add_setshow_zinteger_cmd (const char *name, command_class theclass, const char *set_doc, const char *show_doc, const char *help_doc, - setting_setter_ftype set_func, - setting_getter_ftype get_func, show_value_ftype *show_func, + setting_func_types::set set_func, + setting_func_types::get get_func, show_value_ftype *show_func, cmd_list_element **set_list, cmd_list_element **show_list); extern set_show_commands add_setshow_zuinteger_cmd @@ -776,8 +792,8 @@ extern set_show_commands add_setshow_zuinteger_cmd extern set_show_commands add_setshow_zuinteger_cmd (const char *name, command_class theclass, const char *set_doc, const char *show_doc, const char *help_doc, - setting_setter_ftype set_func, - setting_getter_ftype get_func, show_value_ftype *show_func, + setting_func_types::set set_func, + setting_func_types::get get_func, show_value_ftype *show_func, cmd_list_element **set_list, cmd_list_element **show_list); extern set_show_commands add_setshow_zuinteger_unlimited_cmd @@ -789,7 +805,7 @@ extern set_show_commands add_setshow_zuinteger_unlimited_cmd extern set_show_commands add_setshow_zuinteger_unlimited_cmd (const char *name, command_class theclass, const char *set_doc, const char *show_doc, const char *help_doc, - setting_setter_ftype set_func, setting_getter_ftype get_func, + setting_func_types::set set_func, setting_func_types::get get_func, show_value_ftype *show_func, cmd_list_element **set_list, cmd_list_element **show_list); diff --git a/gdb/remote.c b/gdb/remote.c index 0fb42753596..abf63de622a 100644 --- a/gdb/remote.c +++ b/gdb/remote.c @@ -1873,6 +1873,9 @@ struct packet_config have an associated command always have this set to auto. */ enum auto_boolean detect; + /* The "show remote foo-packet" command created for this packet. */ + cmd_list_element *show_cmd; + /* Does the target support this packet? */ enum packet_support support; }; @@ -1936,6 +1939,7 @@ add_packet_config_cmd (struct packet_config *config, const char *name, NULL, show_remote_protocol_packet_cmd, &remote_set_cmdlist, &remote_show_cmdlist); + config->show_cmd = cmds.show; /* The command code copies the documentation strings. */ xfree (set_doc); @@ -2245,7 +2249,7 @@ show_remote_protocol_packet_cmd (struct ui_file *file, int from_tty, packet < &remote_protocol_packets[PACKET_MAX]; packet++) { - if (&packet->detect == &c->var->get ()) + if (c == packet->show_cmd) { show_packet_config_cmd (packet); return; -- 2.30.2