This is the mail archive of the gdb-patches@sourceware.org mailing list for the GDB project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[PATCH] Function-like cast notation support


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: signature.asc
Description: This is a digitally signed message part.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]