Re: New scope checking patch

Rob Quill
2008-07-28T10:31:43Z

[-- Attachment #1: Type: text/plain, Size: 1111 bytes --]

2008/7/28 Eli Zaretskii <eliz@gnu.org>:
>> Date: Mon, 28 Jul 2008 00:45:03 +0100
>> From: "Rob Quill" <rob.quill@gmail.com>
>> Cc: "Jim Blandy" <jimb@red-bean.com>, gdb-patches@sourceware.org
>>
>> This patch (like the remove deprecated_set_value_type one) has been a
>> long time coming. Please find attached what I believe should be the
>> final version of this patch.
>
> Thanks.
>
>> --- gdb/NEWS 18 Jul 2008 20:55:32 -0000 1.282
>> +++ gdb/NEWS 27 Jul 2008 23:02:48 -0000
>> @@ -3,6 +3,12 @@
>>
>> *** Changes since GDB 6.8
>>
>> +* New expression type
>> +
>> +$in_scope(...)
>> + The value of this expression is 1 if the variable within the
>> + parentheses is within the current scope, 0 otherwise.
>
> This is okay.
>
>> +provided that you check if it is in scope before you test it's value. The operator
> ^^^^
> "its"

I thought about that before I submitted it but couldn't make up my
mind as to which was right, as the scope belongs to the variable so
maybe it needed an apostrophe. Anyway, I've attached the fixed patch.

Rob

[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: in_scope.patch --]
[-- Type: text/x-diff; name=in_scope.patch, Size: 4259 bytes --]

2008-07-27 Rob Quill <rob.quill@gmail.com>

Add $in_scope as a type of expression.
* c-exp.y (IN_SCOPE): New token.
(exp): IN_SCOPE (name): evaluates to 1 if name is in scope,
0 otherwise.
(yylex): Match "$in_scope" as IN_SCOPE token
* gdb.texinfo (expressions): Add a small paragraph (with an
example) about the $in_scope operator.
* NEWS: Mentions $in_scope.

Index: gdb/NEWS
===================================================================
RCS file: /cvs/src/src/gdb/NEWS,v
retrieving revision 1.282
diff -u -p -r1.282 NEWS
--- gdb/NEWS 18 Jul 2008 20:55:32 -0000 1.282
+++ gdb/NEWS 27 Jul 2008 23:02:48 -0000
@@ -3,6 +3,12 @@

*** Changes since GDB 6.8

+* New expression type
+
+$in_scope(...)
+ The value of this expression is 1 if the variable within the
+ parentheses is within the current scope, 0 otherwise.
+
* Commands `set debug-file-directory', `set solib-search-path' and `set args'
now complete on file names.

Index: gdb/c-exp.y
===================================================================
RCS file: /cvs/src/src/gdb/c-exp.y,v
retrieving revision 1.46
diff -u -p -r1.46 c-exp.y
--- gdb/c-exp.y 25 Jun 2008 15:49:20 -0000 1.46
+++ gdb/c-exp.y 27 Jul 2008 23:02:49 -0000
@@ -209,6 +209,8 @@ static int parse_number (char *, int, in
%token TRUEKEYWORD
%token FALSEKEYWORD

+/* $in_scope opperator */
+%token IN_SCOPE

%left ','
%left ABOVE_COMMA
@@ -252,6 +254,30 @@ exp1 : exp
;

/* Expressions, not including the comma operator. */
+exp : IN_SCOPE '(' name_not_typename ')'
+ {
+ struct type *int_type;
+ struct minimal_symbol *min_symbol;
+
+ /* If there are no symbols then just stop right away */
+ if (!have_full_symbols () && !have_partial_symbols ())
+ error ("No symbol table is loaded. Use the \"file\" command.");
+
+ /* Otherwise, prepare to write out the value */
+ int_type = builtin_type (current_gdbarch)->builtin_int;
+ write_exp_elt_opcode (OP_LONG);
+ write_exp_elt_type (int_type);
+
+ min_symbol =
+ lookup_minimal_symbol (copy_name($3.stoken), NULL, NULL);
+ if ($3.sym || min_symbol)
+ write_exp_elt_longcst ((LONGEST) 1);
+ else
+ write_exp_elt_longcst ((LONGEST) 0);
+
+ write_exp_elt_opcode (OP_LONG); }
+ ;
+
exp : '*' exp %prec UNARY
{ write_exp_elt_opcode (UNOP_IND); }
;
@@ -1739,6 +1765,9 @@ yylex ()
/* Catch specific keywords. Should be done with a data structure. */
switch (namelen)
{
+ case 9:
+ if (strncmp (tokstart, "$in_scope", 9) == 0)
+ return IN_SCOPE;
case 8:
if (strncmp (tokstart, "unsigned", 8) == 0)
return UNSIGNED;
Index: gdb/doc/gdb.texinfo
===================================================================
RCS file: /cvs/src/src/gdb/doc/gdb.texinfo,v
retrieving revision 1.509
diff -u -p -r1.509 gdb.texinfo
--- gdb/doc/gdb.texinfo 18 Jul 2008 20:55:33 -0000 1.509
+++ gdb/doc/gdb.texinfo 27 Jul 2008 23:03:11 -0000
@@ -5750,6 +5750,24 @@ memory. @var{addr} may be any expressio
pointer (but parentheses are required around binary operators, just as in
a cast). This construct is allowed regardless of what kind of data is
normally supposed to reside at @var{addr}.
+
+@vindex $in_scope
+@cindex variable in scope, testing
+@item $in_scope
+@samp{$in_scope} allows you to check if a variable is in scope,
+returning 1 if it is and 0 if it is not. This is most useful when scripting @value{GDBN}
+as it means that the script will not stop executing if a variable is not in scope,
+provided that you check if it is in scope before you test its value. The operator
+only works on variables and will not work on structure members or array elements
+for instance.
+
+An example usage from a GDB script may be:
+
+@smallexample
+if ($in_scope(a) == 1 && $in_scope(b) == 1)
+ print a+b
+@end smallexample
+
@end table

@node Ambiguous Expressions