* [PATCH] init-if-undefined command
@ 2005-11-16 14:58 Andrew STUBBS
2005-11-16 20:00 ` Eli Zaretskii
` (2 more replies)
0 siblings, 3 replies; 19+ messages in thread
From: Andrew STUBBS @ 2005-11-16 14:58 UTC (permalink / raw)
To: gdb-patches
[-- Attachment #1: Type: text/plain, Size: 618 bytes --]
Hi,
I have decided that, instead of queuing all my patches here while
waiting for the current ones to go through, I shall post them all now.
Hopefully the low hanging fruit will then get done and only the most
controversial ones will remain when I go away for Christmas.
The attached patch adds a new CLI command named 'init-if-undefined'.
This is essentially the same a 'set variable' (for convenience variables
only), but does not set the variable if it already has a value.
It is useful in scripts whose values can be overridden, or as an
approximation of a statically initialised variable.
Andrew Stubbs
[-- Attachment #2: init-if-undefined.patch --]
[-- Type: text/plain, Size: 3560 bytes --]
2005-11-16 Andrew Stubbs <andrew.stubbs@st.com>
* value.c (init_if_undefined_command): New function.
(_initialize_values): Add command init-if-undefined.
doc/
* gdb.texinfo (Convenience variables): Add init-if-undefined command.
Index: src/gdb/value.c
===================================================================
--- src.orig/gdb/value.c 2005-11-03 17:44:15.000000000 +0000
+++ src/gdb/value.c 2005-11-03 17:48:33.000000000 +0000
@@ -727,6 +727,47 @@ show_values (char *num_exp, int from_tty
static struct internalvar *internalvars;
+/* If the variable does not already exist create it and give it the value given.
+ If no value is given then the default is zero. */
+static void
+init_if_undefined_command (char* args, int from_tty)
+{
+ struct internalvar* intvar;
+
+ /* Parse the expression - this is taken from set_command(). */
+ struct expression *expr = parse_expression (args);
+ register struct cleanup *old_chain =
+ make_cleanup (free_current_contents, &expr);
+
+ /* Validate the expression.
+ Was the expression an assignment?
+ Or even an expression at all? */
+ if (expr->nelts == 0 || expr->elts[0].opcode != BINOP_ASSIGN)
+ {
+ printf_unfiltered ("usage: init-if-undefined $<var> = <expr>\n");
+ do_cleanups (old_chain);
+ return;
+ }
+
+ /* Extract the variable from the parsed expression.
+ In the case of an assign the lvalue will be in elts[1] and elts[2]. */
+ if (expr->elts[1].opcode != OP_INTERNALVAR)
+ {
+ printf_unfiltered ("First parameter to init-if-undefined should be a GDB variable\n");
+ do_cleanups (old_chain);
+ return;
+ }
+ intvar = expr->elts[2].internalvar;
+
+ /* Only evaluate the expression if the lvalue is void.
+ This may still fail if the expresssion is invalid. */
+ if (TYPE_CODE (value_type (intvar->value)) == TYPE_CODE_VOID)
+ evaluate_expression (expr);
+
+ do_cleanups (old_chain);
+}
+
+
/* Look up an internal variable with name NAME. NAME should not
normally include a dollar sign.
@@ -1639,4 +1680,9 @@ A few convenience variables are given va
add_cmd ("values", no_class, show_values,
_("Elements of value history around item number IDX (or last ten)."),
&showlist);
+
+ add_com ("init-if-undefined", class_vars, init_if_undefined_command, _("\
+init-if-undefined <var> = <expr>\n\
+Ensure that an internal variable exists and set it to\n\
+a given value if it does not."));
}
Index: src/gdb/doc/gdb.texinfo
===================================================================
--- src.orig/gdb/doc/gdb.texinfo 2005-11-03 17:44:15.000000000 +0000
+++ src/gdb/doc/gdb.texinfo 2005-11-03 17:45:10.000000000 +0000
@@ -6131,6 +6131,18 @@ variable, when used as an expression, ha
@item show convenience
Print a list of convenience variables used so far, and their values.
Abbreviated @code{show conv}.
+
+@kindex init-if-undefined
+@cindex convenience variables, initializing
+@item init-if-undefined $@var{variable} = @var{expression}
+Set a convenience variable if it has not already been set. This is useful
+for user-defined commands that keep some state. It is similar, in concept,
+to using local static variables with initializers in C (except that
+convenience variables are global). It can also be used to allow users to
+override default values used in a command script.
+
+If the variable is already defined then the expression is not evaluated so
+any side-effects do not occur.
@end table
One of the ways to use a convenience variable is as a counter to be
^ permalink raw reply [flat|nested] 19+ messages in thread* Re: [PATCH] init-if-undefined command
2005-11-16 14:58 [PATCH] init-if-undefined command Andrew STUBBS
@ 2005-11-16 20:00 ` Eli Zaretskii
2005-11-16 20:09 ` Joel Brobecker
2005-11-17 4:47 ` Daniel Jacobowitz
2005-11-30 1:58 ` Andrew STUBBS
2005-11-30 13:59 ` Jim Blandy
2 siblings, 2 replies; 19+ messages in thread
From: Eli Zaretskii @ 2005-11-16 20:00 UTC (permalink / raw)
To: Andrew STUBBS; +Cc: gdb-patches
> Date: Wed, 16 Nov 2005 14:12:38 +0000
> From: Andrew STUBBS <andrew.stubbs@st.com>
>
> The attached patch adds a new CLI command named 'init-if-undefined'.
The documentation patch is approved. It can be committed when the
code patch is approved. Thanks.
What do people think about mentioning new CLI commands in NEWS? We
didn't do that until now, but if we mention command-line options, why
not mention commands?
^ permalink raw reply [flat|nested] 19+ messages in thread
* Re: [PATCH] init-if-undefined command
2005-11-16 20:00 ` Eli Zaretskii
@ 2005-11-16 20:09 ` Joel Brobecker
2005-11-17 1:19 ` Jim Blandy
[not found] ` <8f2776cb0511161507j33208ae1k1e0753b72b992c73@mail.gmail.com>
2005-11-17 4:47 ` Daniel Jacobowitz
1 sibling, 2 replies; 19+ messages in thread
From: Joel Brobecker @ 2005-11-16 20:09 UTC (permalink / raw)
To: Eli Zaretskii; +Cc: Andrew STUBBS, gdb-patches
> What do people think about mentioning new CLI commands in NEWS? We
> didn't do that until now, but if we mention command-line options,
> why not mention commands?
FWIW, I agree with this suggestion (pretty strongly actually).
--
Joel
^ permalink raw reply [flat|nested] 19+ messages in thread* Re: [PATCH] init-if-undefined command
2005-11-16 20:09 ` Joel Brobecker
@ 2005-11-17 1:19 ` Jim Blandy
[not found] ` <8f2776cb0511161507j33208ae1k1e0753b72b992c73@mail.gmail.com>
1 sibling, 0 replies; 19+ messages in thread
From: Jim Blandy @ 2005-11-17 1:19 UTC (permalink / raw)
To: Joel Brobecker; +Cc: Eli Zaretskii, Andrew STUBBS, gdb-patches
On 11/16/05, Joel Brobecker <brobecker@adacore.com> wrote:
> > What do people think about mentioning new CLI commands in NEWS? We
> > didn't do that until now, but if we mention command-line options,
> > why not mention commands?
>
> FWIW, I agree with this suggestion (pretty strongly actually).
If you knew everything about the previous release, you're supposed to
be able to know everything about the new release just by reading NEWS.
You're not even supposed to have to read the manual. Or at least
those were Stallman's rules for Emacs NEWS. I thought it was kind of
a waste; why not just summarize changes in NEWS and point people to
the manual for details?
But yes, of course new commands should be mentioned.
^ permalink raw reply [flat|nested] 19+ messages in thread[parent not found: <8f2776cb0511161507j33208ae1k1e0753b72b992c73@mail.gmail.com>]
* Re: [PATCH] init-if-undefined command
2005-11-16 20:00 ` Eli Zaretskii
2005-11-16 20:09 ` Joel Brobecker
@ 2005-11-17 4:47 ` Daniel Jacobowitz
1 sibling, 0 replies; 19+ messages in thread
From: Daniel Jacobowitz @ 2005-11-17 4:47 UTC (permalink / raw)
To: Eli Zaretskii; +Cc: Andrew STUBBS, gdb-patches
On Wed, Nov 16, 2005 at 09:36:52PM +0200, Eli Zaretskii wrote:
> What do people think about mentioning new CLI commands in NEWS? We
> didn't do that until now, but if we mention command-line options, why
> not mention commands?
I agree.
--
Daniel Jacobowitz
CodeSourcery, LLC
^ permalink raw reply [flat|nested] 19+ messages in thread
* Re: [PATCH] init-if-undefined command
2005-11-16 14:58 [PATCH] init-if-undefined command Andrew STUBBS
2005-11-16 20:00 ` Eli Zaretskii
@ 2005-11-30 1:58 ` Andrew STUBBS
2005-11-30 13:59 ` Jim Blandy
2 siblings, 0 replies; 19+ messages in thread
From: Andrew STUBBS @ 2005-11-30 1:58 UTC (permalink / raw)
To: gdb-patches
Andrew Stubbs wrote:
> The attached patch adds a new CLI command named 'init-if-undefined'.
>
> This is essentially the same a 'set variable' (for convenience variables
> only), but does not set the variable if it already has a value.
>
> It is useful in scripts whose values can be overridden, or as an
> approximation of a statically initialised variable.
Here's another one that ought to be easy to approve/reject.
The docs have been approved and we have decided that this should be
mentioned in the NEWS if/when it goes in, but nobody has said anything
about the code.
I don't think this one should be too much trouble because it does not
change existing behaviour or do much out of keeping with other commands.
Is it OK?
Thanks
Andrew Stubbs
^ permalink raw reply [flat|nested] 19+ messages in thread
* Re: [PATCH] init-if-undefined command
2005-11-16 14:58 [PATCH] init-if-undefined command Andrew STUBBS
2005-11-16 20:00 ` Eli Zaretskii
2005-11-30 1:58 ` Andrew STUBBS
@ 2005-11-30 13:59 ` Jim Blandy
2005-11-30 20:28 ` Andrew STUBBS
2 siblings, 1 reply; 19+ messages in thread
From: Jim Blandy @ 2005-11-30 13:59 UTC (permalink / raw)
To: Andrew STUBBS; +Cc: gdb-patches
On 11/16/05, Andrew STUBBS <andrew.stubbs@st.com> wrote:
> + printf_unfiltered ("usage: init-if-undefined $<var> = <expr>\n");
Provide a full English sentence here, not a traditional Unix "usage:" thingy.
> + printf_unfiltered ("First parameter to init-if-undefined should be a GDB variable\n");
This should be a full sentence, with a preceding "The" and a period.
> + add_com ("init-if-undefined", class_vars, init_if_undefined_command, _("\
> +init-if-undefined <var> = <expr>\n\
> +Ensure that an internal variable exists and set it to\n\
> +a given value if it does not."));
Use upper case for syntactic metavariables, like VAR and EXPR, not
<angle brackets>. Refer to the metavariables in the doc string:
"Ensure that VAR ... and set it to the value of EXPR", etc.
^ permalink raw reply [flat|nested] 19+ messages in thread* Re: [PATCH] init-if-undefined command
2005-11-30 13:59 ` Jim Blandy
@ 2005-11-30 20:28 ` Andrew STUBBS
2005-11-30 21:20 ` Jim Blandy
2005-11-30 23:01 ` Daniel Jacobowitz
0 siblings, 2 replies; 19+ messages in thread
From: Andrew STUBBS @ 2005-11-30 20:28 UTC (permalink / raw)
To: Jim Blandy; +Cc: gdb-patches
[-- Attachment #1: Type: text/plain, Size: 282 bytes --]
Here is another version with the various help strings updated.
I have rewritten the main help because I noticed it did not start with
something suitable for 'help data'. I also added a little more
information about its exact behaviour.
I hope this is better now.
Andrew Stubbs
[-- Attachment #2: init-if-undefined.patch --]
[-- Type: text/plain, Size: 3736 bytes --]
2005-11-30 Andrew Stubbs <andrew.stubbs@st.com>
* value.c (init_if_undefined_command): New function.
(_initialize_values): Add command init-if-undefined.
doc/
* gdb.texinfo (Convenience variables): Add init-if-undefined command.
Index: src/gdb/value.c
===================================================================
--- src.orig/gdb/value.c 2005-11-29 16:35:46.000000000 +0000
+++ src/gdb/value.c 2005-11-30 12:46:34.000000000 +0000
@@ -727,6 +727,47 @@ show_values (char *num_exp, int from_tty
static struct internalvar *internalvars;
+/* If the variable does not already exist create it and give it the value given.
+ If no value is given then the default is zero. */
+static void
+init_if_undefined_command (char* args, int from_tty)
+{
+ struct internalvar* intvar;
+
+ /* Parse the expression - this is taken from set_command(). */
+ struct expression *expr = parse_expression (args);
+ register struct cleanup *old_chain =
+ make_cleanup (free_current_contents, &expr);
+
+ /* Validate the expression.
+ Was the expression an assignment?
+ Or even an expression at all? */
+ if (expr->nelts == 0 || expr->elts[0].opcode != BINOP_ASSIGN)
+ {
+ printf_unfiltered ("Init-if-undefined requires an assignment expression.\n");
+ do_cleanups (old_chain);
+ return;
+ }
+
+ /* Extract the variable from the parsed expression.
+ In the case of an assign the lvalue will be in elts[1] and elts[2]. */
+ if (expr->elts[1].opcode != OP_INTERNALVAR)
+ {
+ printf_unfiltered ("The first parameter to init-if-undefined should be a GDB variable\n");
+ do_cleanups (old_chain);
+ return;
+ }
+ intvar = expr->elts[2].internalvar;
+
+ /* Only evaluate the expression if the lvalue is void.
+ This may still fail if the expresssion is invalid. */
+ if (TYPE_CODE (value_type (intvar->value)) == TYPE_CODE_VOID)
+ evaluate_expression (expr);
+
+ do_cleanups (old_chain);
+}
+
+
/* Look up an internal variable with name NAME. NAME should not
normally include a dollar sign.
@@ -1639,4 +1680,11 @@ A few convenience variables are given va
add_cmd ("values", no_class, show_values,
_("Elements of value history around item number IDX (or last ten)."),
&showlist);
+
+ add_com ("init-if-undefined", class_vars, init_if_undefined_command, _("\
+Initialize a convenience variable if necessary.\n\
+init-if-undefined VARIABLE = EXPRESSION\n\
+Set an internal VARIABLE to the result of the EXPRESSION if it does not\n\
+exist or does not contain a value. The EXPRESSION is not evaluated if the\n\
+VARIABLE is already initialized."));
}
Index: src/gdb/doc/gdb.texinfo
===================================================================
--- src.orig/gdb/doc/gdb.texinfo 2005-11-29 16:36:23.000000000 +0000
+++ src/gdb/doc/gdb.texinfo 2005-11-30 12:24:23.000000000 +0000
@@ -6131,6 +6131,18 @@ variable, when used as an expression, ha
@item show convenience
Print a list of convenience variables used so far, and their values.
Abbreviated @code{show conv}.
+
+@kindex init-if-undefined
+@cindex convenience variables, initializing
+@item init-if-undefined $@var{variable} = @var{expression}
+Set a convenience variable if it has not already been set. This is useful
+for user-defined commands that keep some state. It is similar, in concept,
+to using local static variables with initializers in C (except that
+convenience variables are global). It can also be used to allow users to
+override default values used in a command script.
+
+If the variable is already defined then the expression is not evaluated so
+any side-effects do not occur.
@end table
One of the ways to use a convenience variable is as a counter to be
^ permalink raw reply [flat|nested] 19+ messages in thread* Re: [PATCH] init-if-undefined command
2005-11-30 20:28 ` Andrew STUBBS
@ 2005-11-30 21:20 ` Jim Blandy
2005-11-30 23:01 ` Daniel Jacobowitz
1 sibling, 0 replies; 19+ messages in thread
From: Jim Blandy @ 2005-11-30 21:20 UTC (permalink / raw)
To: Andrew STUBBS; +Cc: gdb-patches
Okay, that looks better. We agreed we need a NEWS entry, too; could
you include that in your patch? Once Eli approves the NEWS entry,
this can go in.
On 11/30/05, Andrew STUBBS <andrew.stubbs@st.com> wrote:
> Here is another version with the various help strings updated.
>
> I have rewritten the main help because I noticed it did not start with
> something suitable for 'help data'. I also added a little more
> information about its exact behaviour.
>
> I hope this is better now.
>
> Andrew Stubbs
>
>
> 2005-11-30 Andrew Stubbs <andrew.stubbs@st.com>
>
> * value.c (init_if_undefined_command): New function.
> (_initialize_values): Add command init-if-undefined.
>
> doc/
> * gdb.texinfo (Convenience variables): Add init-if-undefined command.
>
> Index: src/gdb/value.c
> ===================================================================
> --- src.orig/gdb/value.c 2005-11-29 16:35:46.000000000 +0000
> +++ src/gdb/value.c 2005-11-30 12:46:34.000000000 +0000
> @@ -727,6 +727,47 @@ show_values (char *num_exp, int from_tty
>
> static struct internalvar *internalvars;
>
> +/* If the variable does not already exist create it and give it the value given.
> + If no value is given then the default is zero. */
> +static void
> +init_if_undefined_command (char* args, int from_tty)
> +{
> + struct internalvar* intvar;
> +
> + /* Parse the expression - this is taken from set_command(). */
> + struct expression *expr = parse_expression (args);
> + register struct cleanup *old_chain =
> + make_cleanup (free_current_contents, &expr);
> +
> + /* Validate the expression.
> + Was the expression an assignment?
> + Or even an expression at all? */
> + if (expr->nelts == 0 || expr->elts[0].opcode != BINOP_ASSIGN)
> + {
> + printf_unfiltered ("Init-if-undefined requires an assignment expression.\n");
> + do_cleanups (old_chain);
> + return;
> + }
> +
> + /* Extract the variable from the parsed expression.
> + In the case of an assign the lvalue will be in elts[1] and elts[2]. */
> + if (expr->elts[1].opcode != OP_INTERNALVAR)
> + {
> + printf_unfiltered ("The first parameter to init-if-undefined should be a GDB variable\n");
> + do_cleanups (old_chain);
> + return;
> + }
> + intvar = expr->elts[2].internalvar;
> +
> + /* Only evaluate the expression if the lvalue is void.
> + This may still fail if the expresssion is invalid. */
> + if (TYPE_CODE (value_type (intvar->value)) == TYPE_CODE_VOID)
> + evaluate_expression (expr);
> +
> + do_cleanups (old_chain);
> +}
> +
> +
> /* Look up an internal variable with name NAME. NAME should not
> normally include a dollar sign.
>
> @@ -1639,4 +1680,11 @@ A few convenience variables are given va
> add_cmd ("values", no_class, show_values,
> _("Elements of value history around item number IDX (or last ten)."),
> &showlist);
> +
> + add_com ("init-if-undefined", class_vars, init_if_undefined_command, _("\
> +Initialize a convenience variable if necessary.\n\
> +init-if-undefined VARIABLE = EXPRESSION\n\
> +Set an internal VARIABLE to the result of the EXPRESSION if it does not\n\
> +exist or does not contain a value. The EXPRESSION is not evaluated if the\n\
> +VARIABLE is already initialized."));
> }
> Index: src/gdb/doc/gdb.texinfo
> ===================================================================
> --- src.orig/gdb/doc/gdb.texinfo 2005-11-29 16:36:23.000000000 +0000
> +++ src/gdb/doc/gdb.texinfo 2005-11-30 12:24:23.000000000 +0000
> @@ -6131,6 +6131,18 @@ variable, when used as an expression, ha
> @item show convenience
> Print a list of convenience variables used so far, and their values.
> Abbreviated @code{show conv}.
> +
> +@kindex init-if-undefined
> +@cindex convenience variables, initializing
> +@item init-if-undefined $@var{variable} = @var{expression}
> +Set a convenience variable if it has not already been set. This is useful
> +for user-defined commands that keep some state. It is similar, in concept,
> +to using local static variables with initializers in C (except that
> +convenience variables are global). It can also be used to allow users to
> +override default values used in a command script.
> +
> +If the variable is already defined then the expression is not evaluated so
> +any side-effects do not occur.
> @end table
>
> One of the ways to use a convenience variable is as a counter to be
>
>
>
^ permalink raw reply [flat|nested] 19+ messages in thread* Re: [PATCH] init-if-undefined command
2005-11-30 20:28 ` Andrew STUBBS
2005-11-30 21:20 ` Jim Blandy
@ 2005-11-30 23:01 ` Daniel Jacobowitz
[not found] ` <8f2776cb0511301501t4eb7bb50w484427fd892f5a58@mail.gmail.com>
1 sibling, 1 reply; 19+ messages in thread
From: Daniel Jacobowitz @ 2005-11-30 23:01 UTC (permalink / raw)
To: Andrew STUBBS; +Cc: Jim Blandy, gdb-patches
On Wed, Nov 30, 2005 at 02:12:33PM +0000, Andrew STUBBS wrote:
> + printf_unfiltered ("Init-if-undefined requires an assignment expression.\n");
Is there any reason this, and the other outputs, should not be
_("gettextized")?
Also, should this be an error()?
> + /* Extract the variable from the parsed expression.
> + In the case of an assign the lvalue will be in elts[1] and elts[2]. */
> + if (expr->elts[1].opcode != OP_INTERNALVAR)
> + {
> + printf_unfiltered ("The first parameter to init-if-undefined should be a GDB variable\n");
> + do_cleanups (old_chain);
> + return;
> + }
Ditto.
--
Daniel Jacobowitz
CodeSourcery, LLC
^ permalink raw reply [flat|nested] 19+ messages in thread
end of thread, other threads:[~2005-12-02 11:48 UTC | newest]
Thread overview: 19+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2005-11-16 14:58 [PATCH] init-if-undefined command Andrew STUBBS
2005-11-16 20:00 ` Eli Zaretskii
2005-11-16 20:09 ` Joel Brobecker
2005-11-17 1:19 ` Jim Blandy
[not found] ` <8f2776cb0511161507j33208ae1k1e0753b72b992c73@mail.gmail.com>
[not found] ` <20051116233326.GZ1635@adacore.com>
[not found] ` <8f2776cb0511161602wb469964xdd904aab0d70d2a6@mail.gmail.com>
2005-11-17 3:45 ` Joel Brobecker
2005-11-17 4:47 ` Daniel Jacobowitz
2005-11-30 1:58 ` Andrew STUBBS
2005-11-30 13:59 ` Jim Blandy
2005-11-30 20:28 ` Andrew STUBBS
2005-11-30 21:20 ` Jim Blandy
2005-11-30 23:01 ` Daniel Jacobowitz
[not found] ` <8f2776cb0511301501t4eb7bb50w484427fd892f5a58@mail.gmail.com>
2005-12-01 11:55 ` Andrew STUBBS
2005-12-01 17:32 ` Andrew STUBBS
2005-12-01 23:38 ` Jim Blandy
2005-12-02 10:44 ` Andrew STUBBS
2005-12-02 10:33 ` Eli Zaretskii
2005-12-02 11:18 ` Andrew STUBBS
2005-12-01 22:50 ` Jim Blandy
2005-12-02 13:05 ` Andrew STUBBS
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox