From: "Sérgio Durigan Júnior" <sergiodj@redhat.com>
To: gdb-patches@sourceware.org
Subject: [PATCH] Function-like cast notation support
Date: Wed, 17 Feb 2010 15:46:00 -0000 [thread overview]
Message-ID: <201002171345.36988.sergiodj@redhat.com> (raw)
[-- 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 --]
next reply other threads:[~2010-02-17 15:46 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
2010-02-17 15:46 Sérgio Durigan Júnior [this message]
2010-02-17 18:47 ` Tom Tromey
2010-02-17 19:02 ` Sérgio Durigan Júnior
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=201002171345.36988.sergiodj@redhat.com \
--to=sergiodj@redhat.com \
--cc=gdb-patches@sourceware.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox