Mirror of the gdb-patches mailing list
 help / color / mirror / Atom feed
From: Lancelot SIX via Gdb-patches <gdb-patches@sourceware.org>
To: Simon Marchi <simon.marchi@polymtl.ca>
Cc: gdb-patches@sourceware.org
Subject: Re: [PATCH 15/16] gdb: make cmd_list_element var an optional union
Date: Sun, 18 Jul 2021 15:44:29 +0000	[thread overview]
Message-ID: <20210718154429.27arnsgirctcjett@ubuntu.lan> (raw)
In-Reply-To: <20210714045520.1623120-16-simon.marchi@polymtl.ca>

> A note on the Guile situation: something that makes our life a little
> bit complicated is that is possible to assign and read the value of a
> Guile-created parameter that is not yet registered (see previous patch
> that adds a test for this).  It would have been much more simple to say
> that this is simply not supported anymore, but that could break existing
> scripts, so I don't think it is a good idea.  In some cases, for example
> when printing the value of a built-in parameter, we have access to a
> show command and its union setting_variable.  When we have an
> un-registered Guile param, we don't have a show command associated to
> it, but we can pass the parameter's pascm_variable union, stored in
> param_smob.  Because we have these two kinds of incompatible parameters,
> we need two versions of the pascm_param_value function.
> 

Hi,

I believe you can create an intermediate abstraction that works for
both the registered and unregistered settings.  In both cases, you can
use or build a cmd_list_element::setting_variable easily, and then have
only one function that does the conversion to a SCM value logic.

Given how a pascm_value is laid out (i.e. a union), you can create a
cmd_list_element setting_variable referencing it with something like:

const cmd_list_element::setting_variable v { .bool_var = &param_value->boolval };

This solution is not perfect either, and it forces the const qualifier
of the param_value of pascm_param_value to be dropped, but this avoids
code duplication which I think is good for easier maintenance.

What do you think?

I have built the above changes on top of your patch series (up to this
patch) and tested gdb.guile/*.exp with no regression noticed.

Lancelot.

---
diff --git a/gdb/guile/scm-param.c b/gdb/guile/scm-param.c
index 57a8c7ec68a..9dc4a5d910d 100644
--- a/gdb/guile/scm-param.c
+++ b/gdb/guile/scm-param.c
@@ -113,6 +113,8 @@ struct param_smob
   SCM containing_scm;
 };
 
+using setting_variable = cmd_list_element::setting_variable;
+
 static const char param_smob_name[] = "gdb:parameter";
 
 /* The tag Guile knows the param smob by.  */
@@ -133,8 +135,11 @@ static SCM unlimited_keyword;
 
 static int pascm_is_valid (param_smob *);
 static const char *pascm_param_type_name (enum var_types type);
-static SCM pascm_param_value (enum var_types type,
-			      const pascm_variable *var,
+static SCM pascm_param_value (enum var_types type, pascm_variable *var,
+			      int arg_pos, const char *func_name);
+static SCM pascm_param_value (const cmd_list_element *,
+			      int arg_pos, const char *func_name);
+static SCM pascm_param_value (enum var_types type, const setting_variable v,
 			      int arg_pos, const char *func_name);
 \f
 /* Administrivia for parameter smobs.  */
@@ -570,79 +575,11 @@ pascm_param_type_name (enum var_types param_type)
    haven't been registered yet.  */
 
 static SCM
-pascm_param_value (enum var_types type, const pascm_variable *param_value,
+pascm_param_value (enum var_types type, pascm_variable *param_value,
 		   int arg_pos, const char *func_name)
 {
-  /* Note: We *could* support var_integer here in case someone is trying to get
-     the value of a Python-created parameter (which is the only place that
-     still supports var_integer).  To further discourage its use we do not.  */
-
-  switch (type)
-    {
-    case var_string:
-    case var_string_noescape:
-    case var_optional_filename:
-    case var_filename:
-      {
-	const char *str = param_value->stringval;
-
-	if (str == NULL)
-	  str = "";
-
-	return gdbscm_scm_from_host_string (str, strlen (str));
-      }
-
-    case var_enum:
-      {
-	const char *str = param_value->cstringval;
-
-	if (str == NULL)
-	  str = "";
-	return gdbscm_scm_from_host_string (str, strlen (str));
-      }
-
-    case var_boolean:
-      {
-	if (param_value->boolval)
-	  return SCM_BOOL_T;
-	else
-	  return SCM_BOOL_F;
-      }
-
-    case var_auto_boolean:
-      {
-	enum auto_boolean ab = param_value->autoboolval;
-
-	if (ab == AUTO_BOOLEAN_TRUE)
-	  return SCM_BOOL_T;
-	else if (ab == AUTO_BOOLEAN_FALSE)
-	  return SCM_BOOL_F;
-	else
-	  return auto_keyword;
-      }
-
-    case var_zuinteger_unlimited:
-      if (param_value->intval == -1)
-	return unlimited_keyword;
-      gdb_assert (param_value->intval >= 0);
-      /* Fall through.  */
-    case var_zinteger:
-      return scm_from_int (param_value->intval);
-
-    case var_uinteger:
-      if (param_value->uintval == UINT_MAX)
-	return unlimited_keyword;
-      /* Fall through.  */
-    case var_zuinteger:
-      return scm_from_uint (param_value->uintval);
-
-    default:
-      break;
-    }
-
-  return gdbscm_make_out_of_range_error (func_name, arg_pos,
-					 scm_from_int (type),
-					 _("program error: unhandled type"));
+  setting_variable v { .bool_var = &param_value->boolval };
+  return pascm_param_value (type, v, arg_pos, func_name);
 }
 
 /* Return the value of a gdb parameter as a Scheme value.
@@ -655,33 +592,37 @@ pascm_param_value (enum var_types type, const pascm_variable *param_value,
 static SCM
 pascm_param_value (const cmd_list_element *show_cmd,
 		   int arg_pos, const char *func_name)
+{
+  gdb_assert (show_cmd->type == cmd_types::show_cmd);
+  gdb_assert (show_cmd->var.has_value ());
+  return pascm_param_value(show_cmd->var_type, *show_cmd->var,
+			   arg_pos, func_name);
+}
+
+/* Return the value of a gdb parameter as a Scheme value.
+
+   contains the common code paths to handle a variable wether is has been
+   registered or not. */
+
+static SCM
+pascm_param_value (enum var_types var_type, const setting_variable var,
+		   int arg_pos, const char *func_name)
 {
   /* Note: We *could* support var_integer here in case someone is trying to get
      the value of a Python-created parameter (which is the only place that
      still supports var_integer).  To further discourage its use we do not.  */
 
-  gdb_assert (show_cmd->type == cmd_types::show_cmd);
-
-  switch (show_cmd->var_type)
+  switch (var_type)
     {
     case var_string:
     case var_string_noescape:
     case var_optional_filename:
     case var_filename:
-      {
-	const char *str = *show_cmd->var->char_ptr_var;
-
-	if (str == NULL)
-	  str = "";
-
-	return gdbscm_scm_from_host_string (str, strlen (str));
-      }
-
     case var_enum:
       {
-	const char *str = *show_cmd->var->const_char_ptr_var;
+	const char *str = *var.const_char_ptr_var;
 
-	if (str == NULL)
+	if (str == nullptr)
 	  str = "";
 
 	return gdbscm_scm_from_host_string (str, strlen (str));
@@ -689,7 +630,7 @@ pascm_param_value (const cmd_list_element *show_cmd,
 
     case var_boolean:
       {
-	if (*show_cmd->var->bool_var)
+	if (*var.bool_var)
 	  return SCM_BOOL_T;
 	else
 	  return SCM_BOOL_F;
@@ -697,7 +638,7 @@ pascm_param_value (const cmd_list_element *show_cmd,
 
     case var_auto_boolean:
       {
-	enum auto_boolean ab = *show_cmd->var->auto_boolean_var;
+	enum auto_boolean ab = *var.auto_boolean_var;
 
 	if (ab == AUTO_BOOLEAN_TRUE)
 	  return SCM_BOOL_T;
@@ -708,26 +649,26 @@ pascm_param_value (const cmd_list_element *show_cmd,
       }
 
     case var_zuinteger_unlimited:
-      if (*show_cmd->var->int_var == -1)
+      if (*var.int_var == -1)
 	return unlimited_keyword;
-      gdb_assert (*show_cmd->var->int_var >= 0);
+      gdb_assert (*var.int_var >= 0);
       /* Fall through.  */
     case var_zinteger:
-      return scm_from_int (*show_cmd->var->int_var);
+      return scm_from_int (*var.int_var);
 
     case var_uinteger:
-      if (*show_cmd->var->unsigned_int_var == UINT_MAX)
+      if (*var.unsigned_int_var == UINT_MAX)
 	return unlimited_keyword;
       /* Fall through.  */
     case var_zuinteger:
-      return scm_from_uint (*show_cmd->var->unsigned_int_var);
+      return scm_from_uint (*var.unsigned_int_var);
 
     default:
       break;
     }
 
   return gdbscm_make_out_of_range_error (func_name, arg_pos,
-					 scm_from_int (show_cmd->var_type),
+					 scm_from_int (var_type),
 					 _("program error: unhandled type"));
 }
 

  parent reply	other threads:[~2021-07-18 15:44 UTC|newest]

Thread overview: 50+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-07-14  4:55 [PATCH 00/16] Bunch of commands related cleanups Simon Marchi via Gdb-patches
2021-07-14  4:55 ` [PATCH 01/16] gdb/testsuite: split gdb.python/py-parameter.exp in procs Simon Marchi via Gdb-patches
2021-07-14  4:55 ` [PATCH 02/16] gdb.base/setshow.exp: use save_vars to save/restore gdb_prompt Simon Marchi via Gdb-patches
2021-07-14  4:55 ` [PATCH 03/16] gdb.base/setshow.exp: split in procs Simon Marchi via Gdb-patches
2021-07-14  4:55 ` [PATCH 04/16] gdb.base/setshow.exp: fix duplicate test name Simon Marchi via Gdb-patches
2021-07-14  4:55 ` [PATCH 05/16] gdb: un-share set_inferior_cwd declaration Simon Marchi via Gdb-patches
2021-07-14  4:55 ` [PATCH 06/16] gdb: remove inferior::{argc,argv} Simon Marchi via Gdb-patches
2021-07-14  4:55 ` [PATCH 07/16] gdb: add setter/getter for inferior arguments Simon Marchi via Gdb-patches
2021-07-14  4:55 ` [PATCH 08/16] gdb: add setter/getter for inferior cwd Simon Marchi via Gdb-patches
2021-07-14  4:55 ` [PATCH 09/16] gdb: make inferior::m_args an std::string Simon Marchi via Gdb-patches
2021-07-14  4:55 ` [PATCH 10/16] gdb: make inferior::m_cwd " Simon Marchi via Gdb-patches
2021-07-14  4:55 ` [PATCH 11/16] gdb: make inferior::m_terminal " Simon Marchi via Gdb-patches
2021-07-14  4:55 ` [PATCH 12/16] gdb: rename cfunc to simple_func Simon Marchi via Gdb-patches
2021-07-14  4:55 ` [PATCH 13/16] gdb: remove cmd_list_element::function::sfunc Simon Marchi via Gdb-patches
2021-07-28 19:10   ` Tom Tromey
2021-07-28 21:17     ` Simon Marchi via Gdb-patches
2021-07-29 17:33       ` Tom Tromey
2021-07-14  4:55 ` [PATCH 14/16] gdb/testsuite: test get/set value of unregistered Guile parameter Simon Marchi via Gdb-patches
2021-07-23 19:42   ` Simon Marchi via Gdb-patches
2021-07-14  4:55 ` [PATCH 15/16] gdb: make cmd_list_element var an optional union Simon Marchi via Gdb-patches
2021-07-14 12:08   ` Lancelot SIX via Gdb-patches
2021-07-14 17:12     ` Lancelot SIX via Gdb-patches
2021-07-14 19:22       ` Simon Marchi via Gdb-patches
2021-07-14 23:22         ` Lancelot SIX via Gdb-patches
2021-07-19 14:32           ` Simon Marchi via Gdb-patches
2021-07-19 19:52             ` Simon Marchi via Gdb-patches
2021-07-20 23:03               ` Lancelot SIX via Gdb-patches
2021-07-23 19:56                 ` Simon Marchi via Gdb-patches
2021-07-23 20:46                   ` Lancelot SIX via Gdb-patches
2021-07-23 21:15                     ` Simon Marchi via Gdb-patches
2021-07-23 22:55                       ` Lancelot SIX via Gdb-patches
2021-07-24  2:04                         ` Simon Marchi via Gdb-patches
2021-07-28 20:13                 ` Tom Tromey
2021-07-28 20:45                   ` Lancelot SIX via Gdb-patches
2021-07-29 17:47                     ` Tom Tromey
2021-07-29 20:12                       ` Lancelot SIX via Gdb-patches
2021-07-30  2:09                         ` Simon Marchi via Gdb-patches
2021-07-30 17:47                           ` Lancelot SIX via Gdb-patches
2021-07-18 15:44   ` Lancelot SIX via Gdb-patches [this message]
2021-07-19 14:19     ` Simon Marchi via Gdb-patches
2021-07-19 20:58       ` Lancelot SIX via Gdb-patches
2021-07-28 19:47   ` Tom Tromey
2021-07-28 20:59     ` Simon Marchi via Gdb-patches
2021-07-29 17:41       ` Tom Tromey
2021-07-29 17:44         ` Simon Marchi via Gdb-patches
2021-07-29 17:49           ` Tom Tromey
2021-07-29 18:00             ` Simon Marchi via Gdb-patches
2021-07-14  4:55 ` [PATCH 16/16] gdb: make string-like set show commands use std::string variable Simon Marchi via Gdb-patches
2021-07-28 20:27   ` Tom Tromey
2021-07-28 21:03     ` Simon Marchi via Gdb-patches

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20210718154429.27arnsgirctcjett@ubuntu.lan \
    --to=gdb-patches@sourceware.org \
    --cc=lsix@lancelotsix.com \
    --cc=simon.marchi@polymtl.ca \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox