Re: [Patch] Try2: -var-evaluate-expression [-f FORMAT] NAME

Vladimir Prus
2008-04-02T13:23:05Z

Marc Khouzam wrote:

> 2008-04-01 Â Marc Khouzam Â <marc.khouzam@ericsson.com>
>
> Â  Â  Â  Â  * mi/mi-cmd-var.c: Include "mi-getopt.h".
> Â  Â  Â  Â  (mi_parse_format): New. Â Factored out from mi_cmd_var_set_format.
> Â  Â  Â  Â  (mi_cmd_var_set_format): Use new mi_parse_format.
> Â  Â  Â  Â  (mi_cmd_var_evaluate_expression): Support for -f option to specify
> Â  Â  Â  Â  format.
> Â  Â  Â  Â  * Makefile.in (mi-cmd-var.o): Update dependencies.
>
> Â  Â  Â  Â  * varobj.h (varobj_get_formatted_value): Declare.
> Â  Â  Â  Â  * varobj.c (my_value_of_variable): Added format parameter.
> Â  Â  Â  Â  (cplus_value_of_variable): Likewise.
> Â  Â  Â  Â  (java_value_of_variable): Likewise.
> Â  Â  Â  Â  (*(value_of_variable)): Likewise.

Is '*(value_of_variable)' really a name of a function? :-)

> Â  Â  Â  Â  (c_value_of_variable): Likewise. Â Evaluate expression based
> Â  Â  Â  Â  on format parameter.
> Â  Â  Â  Â  (varobj_get_formatted_value): New.
> Â  Â  Â  Â  (varobj_get_value): Added format parameter to method call.

> +/* Parse a string argument into a format value. Â */

As meta-remark, can you pass the "-p" option to diff when generating patches,
so that function names are present right in the patch?

> +
> +static enum varobj_display_formats
> +mi_parse_format (const char *arg)
> +{
> + Â int len;
> +
> + Â len = strlen (arg);
> +
> + Â if (strncmp (arg, "natural", len) == 0)
> + Â  Â return FORMAT_NATURAL;
> + Â else if (strncmp (arg, "binary", len) == 0)
> + Â  Â return FORMAT_BINARY;
> + Â else if (strncmp (arg, "decimal", len) == 0)
> + Â  Â return FORMAT_DECIMAL;
> + Â else if (strncmp (arg, "hexadecimal", len) == 0)
> + Â  Â return FORMAT_HEXADECIMAL;
> + Â else if (strncmp (arg, "octal", len) == 0)
> + Â  Â return FORMAT_OCTAL;
> + Â else
> + Â  Â error (_("Unknown display format: must be: \"natural\", \"binary\", \"decimal\",
\"hexadecimal\", or \"octal\""));
> +}
> +
> Â enum mi_cmd_result
> Â mi_cmd_var_set_format (char *command, char **argv, int argc)
> Â {
> Â  Â enum varobj_display_formats format;
> - Â int len;
> Â  Â struct varobj *var;
> Â  Â char *formspec;
> Â
> @@ -216,21 +239,8 @@
> Â  Â if (formspec == NULL)
> Â  Â  Â error (_("mi_cmd_var_set_format: Must specify the format as: \"natural\", \"binary\",
\"decimal\", \"hexadecimal\", or \"octal\""));

Can we drop this...

> Â
> - Â len = strlen (formspec);
> -
> - Â if (strncmp (formspec, "natural", len) == 0)
> - Â  Â format = FORMAT_NATURAL;
> - Â else if (strncmp (formspec, "binary", len) == 0)
> - Â  Â format = FORMAT_BINARY;
> - Â else if (strncmp (formspec, "decimal", len) == 0)
> - Â  Â format = FORMAT_DECIMAL;
> - Â else if (strncmp (formspec, "hexadecimal", len) == 0)
> - Â  Â format = FORMAT_HEXADECIMAL;
> - Â else if (strncmp (formspec, "octal", len) == 0)
> - Â  Â format = FORMAT_OCTAL;
> - Â else
> - Â  Â error (_("mi_cmd_var_set_format: Unknown display format: must be: \"natural\", \"binary\",
\"decimal\", \"hexadecimal\", or \"octal\""));
> -
> + Â format = mi_parse_format (formspec);

..and make mi_parse_format emit an error message both if the passed format
is NULL and when it's not NULL, but unknown?

> Â  Â /* Set the format of VAR to given format */
> Â  Â varobj_set_display_format (var, format);
> Â
> @@ -493,15 +503,58 @@
> Â {
> Â  Â struct varobj *var;
> Â
> - Â if (argc != 1)
> - Â  Â error (_("mi_cmd_var_evaluate_expression: Usage: NAME."));
> + Â enum varobj_display_formats format;
> + Â int formatFound;
> + Â int optind;
> + Â char *optarg;
> + Â  Â
> + Â enum opt
> + Â  Â {
> + Â  Â  Â OP_FORMAT
> + Â  Â };
> + Â static struct mi_opt opts[] =
> + Â {
> + Â  Â {"f", OP_FORMAT, 1},
> + Â  Â { 0, 0, 0 }
> + Â };
> +
> + Â /* Parse arguments */
> + Â format = FORMAT_NATURAL;
> + Â formatFound = 0;
> + Â optind = 0;
> + Â while (1)
> + Â  Â {
> + Â  Â  Â int opt = mi_getopt ("mi_cmd_var_evaluate_expression", argc, argv, opts, &optind, &optarg);
> + Â  Â  Â if (opt < 0)
> + Â  Â  Â  break;
> + Â  Â  Â switch ((enum opt) opt)
> + Â  Â  Â {
> + Â  Â  Â  case OP_FORMAT:
> + Â  Â  Â  Â  if (formatFound)
> + Â  Â  Â  Â  Â  error (_("mi_cmd_var_evaluate_expression: cannot specify format more than once"));
> + Â

I think the current position is that error messages need not name the name of function, since
if this error message ever make it to the user, he has no idea what
is "mi_cmd_var_evaluate_expression", and when debugging GDB or frontend, it's not very
helpful. Can you drop that prefix, here, and everywhere?

> + Â  Â  Â  Â  format = mi_parse_format (optarg);
> + Â  Â  Â  Â  formatFound = 1;
> + Â  Â  Â  Â  break;
> + Â  Â  Â }
> + Â  Â }
> Â
> - Â /* Get varobj handle, if a valid var obj name was specified */
> - Â var = varobj_get_handle (argv[0]);
> + Â if (optind >= argc)
> + Â  Â error (_("mi_cmd_var_evaluate_expression: Usage: [-f FORMAT] NAME"));
> + Â
> + Â if (optind < argc - 1)
> + Â  Â error (_("mi_cmd_var_evaluate_expression: Garbage at end of command"));
> +
> + Â  Â  /* Get varobj handle, if a valid var obj name was specified */
> + Â var = varobj_get_handle (argv[optind]);
> Â  Â if (var == NULL)
> Â  Â  Â error (_("mi_cmd_var_evaluate_expression: Variable object not found"));
> + Â
> + Â if (formatFound)
> + Â  Â ui_out_field_string (uiout, "value", varobj_get_formatted_value (var, format));

It's not the problem introduced by your patch, but still -- it appears we have a memory
leak here. In c_value_of_variable, we either xstrdup the value, or use value_get_print_value
which uses ui_file_xstrdup, so we end up with newly allocated char* pointer. I think
we have to free it here.

> + Â else
> + Â  Â ui_out_field_string (uiout, "value", varobj_get_value (var));
> Â
> - Â ui_out_field_string (uiout, "value", varobj_get_value (var));
> Â  Â return MI_CMD_DONE;

The code patch is OK with those changes, thanks!

Eli, does the doc patch look good for you?

- Volodya