Mirror of the gdb-patches mailing list
 help / color / mirror / Atom feed
* [PATCH] Function-like cast notation support
@ 2010-02-17 15:46 Sérgio Durigan Júnior
  2010-02-17 18:47 ` Tom Tromey
  0 siblings, 1 reply; 3+ messages in thread
From: Sérgio Durigan Júnior @ 2010-02-17 15:46 UTC (permalink / raw)
  To: gdb-patches

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

Hello,

The following patch adds support for function-like cast notation on GDB.  
Currently it only works for primitive data type (int, char, double, etc), but 
I intend to send another patch soon that will implement this feature for 
classes as well.

A brief explanation of what this specific cast notation means:

While on C you do:

(long) (5 + 6);

C++ allows you to do:

long (5 + 6);

Regards,

-- 
Sérgio Durigan Júnior
Debugger Engineer -- Red Hat Inc.

gdb/ChangeLog:

2010-02-17  Sergio Durigan Junior  <sergiodj@redhat.com>

	* c-exp.y: Add new rule to recognize a function-like cast notation.
	(typebase_single): New rule to match single tokens like `int',
	`char', etc.
	(typebase): Modify existing rule in order to use `typebase_single'.

gdb/testsuite/ChangeLog:

2010-02-17  Sergio Durigan Junior  <sergiodj@redhat.com>

	* gdb.base/function-like-cast-notation.exp: New file.


diff --git a/gdb/c-exp.y b/gdb/c-exp.y
index 845771c..a42a032 100644
--- a/gdb/c-exp.y
+++ b/gdb/c-exp.y
@@ -162,7 +162,7 @@ static struct stoken operator_stoken (const char *);
 
 %type <voidval> exp exp1 type_exp start variable qualified_name lcurly
 %type <lval> rcurly
-%type <tval> type typebase qualified_type
+%type <tval> type typebase qualified_type typebase_single
 %type <tvec> nonempty_typelist
 /* %type <bval> block */
 
@@ -440,6 +440,12 @@ exp	:	'(' type ')' exp  %prec UNARY
 			  write_exp_elt_opcode (UNOP_CAST); }
 	;
 
+exp	:	typebase_single '(' exp ')'
+			{ write_exp_elt_opcode (UNOP_CAST);
+			  write_exp_elt_type ($1);
+			  write_exp_elt_opcode (UNOP_CAST); }
+	;
+
 exp	:	'(' exp1 ')'
 			{ }
 	;
@@ -943,7 +949,7 @@ func_mod:	'(' ')'
 type	:	ptype
 	;
 
-typebase  /* Implements (approximately): (type-qualifier)* type-specifier */
+typebase_single
 	:	TYPENAME
 			{ $$ = $1.type; }
 	|	INT_KEYWORD
@@ -952,6 +958,17 @@ typebase  /* Implements (approximately): (type-
qualifier)* type-specifier */
 			{ $$ = parse_type->builtin_long; }
 	|	SHORT
 			{ $$ = parse_type->builtin_short; }
+	|	DOUBLE_KEYWORD
+			{ $$ = parse_type->builtin_double; }
+	|	UNSIGNED
+			{ $$ = parse_type->builtin_unsigned_int; }
+	|	SIGNED_KEYWORD
+			{ $$ = parse_type->builtin_int; }
+	;
+
+
+typebase  /* Implements (approximately): (type-qualifier)* type-specifier */
+	:	typebase_single
 	|	LONG INT_KEYWORD
 			{ $$ = parse_type->builtin_long; }
 	|	LONG SIGNED_KEYWORD INT_KEYWORD
@@ -998,8 +1015,6 @@ typebase  /* Implements (approximately): (type-
qualifier)* type-specifier */
 			{ $$ = parse_type->builtin_unsigned_short; }
 	|	SHORT UNSIGNED INT_KEYWORD
 			{ $$ = parse_type->builtin_unsigned_short; }
-	|	DOUBLE_KEYWORD
-			{ $$ = parse_type->builtin_double; }
 	|	LONG DOUBLE_KEYWORD
 			{ $$ = parse_type->builtin_long_double; }
 	|	STRUCT name
@@ -1018,14 +1033,10 @@ typebase  /* Implements (approximately): (type-
qualifier)* type-specifier */
 			{ $$ = lookup_unsigned_typename (parse_language,
 							 parse_gdbarch,
 							 TYPE_NAME($2.type)); }
-	|	UNSIGNED
-			{ $$ = parse_type->builtin_unsigned_int; }
 	|	SIGNED_KEYWORD typename
 			{ $$ = lookup_signed_typename (parse_language,
 						       parse_gdbarch,
 						       TYPE_NAME($2.type)); }
-	|	SIGNED_KEYWORD
-			{ $$ = parse_type->builtin_int; }
                 /* It appears that this rule for templates is never
                    reduced; template recognition happens by lookahead
                    in the token processing code in yylex. */         
diff --git a/gdb/testsuite/gdb.base/function-like-cast-notation.exp 
b/gdb/testsuite/gdb.base/function-like-cast-notation.exp
new file mode 100644
index 0000000..de31f63
--- /dev/null
+++ b/gdb/testsuite/gdb.base/function-like-cast-notation.exp
@@ -0,0 +1,62 @@
+#   Copyright 2010 Free Software Foundation, Inc.
+
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 3 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.
+
+gdb_exit
+gdb_start
+gdb_reinitialize_dir $srcdir/$subdir
+
+###
+# Test for primitive types
+###
+
+# This is the main variable of this testcase.  It has elements
+# which contain 4 fields inside each one.
+#
+# The order of the fields inside each element is:
+#
+# - The TYPE to be used in the `whatis' test.
+# - The OUTPUT expected from the `whatis' test.
+# - The VALUE (or EXPRESSION) to be used in the `print' test.
+# - The OUTPUT expected to be printed in the `print' test.
+
+set test_data { { "char"      "char"		"97"		"97 'a'" }
+		{ "short"     "short"		"'a'"		"97"   }
+		{ "int"	      "int"		"97.1 + 18.0"	"115"  }
+		{ "long"      "long"		"'a'"		"97"   }
+		{ "signed"    "int"		"97.1 + 18.0"	"115"  }
+		{ "unsigned"  "unsigned int"	"'a'"		"97"   }
+		{ "float"     "float"		"'a'"		"97"   }
+		{ "double"    "double"		"'a'"		"97"   }
+		{ "void"      "void"		"'a'"		"void" }
+}
+
+
+foreach tuple $test_data {
+    set type [lindex $tuple 0]
+    set out_t [lindex $tuple 1]
+    set val [lindex $tuple 2]
+    set out_v [lindex $tuple 3]
+
+    set thistest "$type type `whatis'"
+    gdb_test "whatis $type ($val)" "type = $out_t" $thistest
+
+    set thistest "$type type `print'"
+    gdb_test "print $type ($val)" " = $out_v" $thistest
+}
+
+###
+# Test for classes
+###
+## NOT IMPLEMENTED YET ##

[-- Attachment #2: This is a digitally signed message part. --]
[-- Type: application/pgp-signature, Size: 836 bytes --]

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH] Function-like cast notation support
  2010-02-17 15:46 [PATCH] Function-like cast notation support Sérgio Durigan Júnior
@ 2010-02-17 18:47 ` Tom Tromey
  2010-02-17 19:02   ` Sérgio Durigan Júnior
  0 siblings, 1 reply; 3+ messages in thread
From: Tom Tromey @ 2010-02-17 18:47 UTC (permalink / raw)
  To: Sérgio Durigan Júnior; +Cc: gdb-patches

>>>>> "Sérgio" == Sérgio Durigan Júnior <sergiodj@redhat.com> writes:

Sérgio> The following patch adds support for function-like cast notation
Sérgio> on GDB.

This looks good.

It could use a NEWS entry.  I suggest waiting until my NEWS change goes
in, then adding it to the C++ improvements section.

Sérgio> Currently it only works for primitive data type (int,
Sérgio> char, double, etc), but I intend to send another patch soon that
Sérgio> will implement this feature for classes as well.

Hmm, you seem to have implemented this already...

Sérgio> +typebase_single
Sérgio>  	:	TYPENAME
Sérgio>  			{ $$ = $1.type; }

... because the lexer can return TYPENAME for a class name.

Sérgio> +		{ "void"      "void"		"'a'"		"void" }

I didn't realize this was supposed to work... learn something every day
:-)

This patch is ok.  Thanks.

For future reference, it is worth noting in the email how and whether
you regression tested it.  I assume that everybody does this, but it is
nice to be explicit about it.  At least in my case, this occasionally
helps me realize that I forgot.

Tom


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH] Function-like cast notation support
  2010-02-17 18:47 ` Tom Tromey
@ 2010-02-17 19:02   ` Sérgio Durigan Júnior
  0 siblings, 0 replies; 3+ messages in thread
From: Sérgio Durigan Júnior @ 2010-02-17 19:02 UTC (permalink / raw)
  To: tromey; +Cc: gdb-patches

On Wednesday 17 February 2010 16:47:29, Tom Tromey wrote:
> >>>>> "Sérgio" == Sérgio Durigan Júnior <sergiodj@redhat.com> writes:
> 
> Sérgio> The following patch adds support for function-like cast notation
> Sérgio> on GDB.
> 
> This looks good.
> 
> It could use a NEWS entry.  I suggest waiting until my NEWS change goes
> in, then adding it to the C++ improvements section.

Sounds fine by me.

> Sérgio> +		{ "void"      "void"		"'a'"		"void" }
> 
> I didn't realize this was supposed to work... learn something every day
> 
> :-)

Cool :-).

> For future reference, it is worth noting in the email how and whether
> you regression tested it.  I assume that everybody does this, but it is
> nice to be explicit about it.  At least in my case, this occasionally
> helps me realize that I forgot.

Oh darn, I knew I forgot something...  Sorry about that.  I tested it on the 
compile farm, using Tom's scripts.  Everything succeeded without regressions.

Regards,

-- 
Sérgio Durigan Júnior
Debugger Engineer -- Red Hat Inc.


^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2010-02-17 19:02 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2010-02-17 15:46 [PATCH] Function-like cast notation support Sérgio Durigan Júnior
2010-02-17 18:47 ` Tom Tromey
2010-02-17 19:02   ` Sérgio Durigan Júnior

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox