[RFA] fix disassemble foo::bar::~bar

Doug Evans
2013-02-14T00:14:08Z

I happened to try this and noticed it failing with
"name of destructor must equal name of class".

This is because destructor_name_p doesn't handle foo::bar
for the type's name.

Regression tested on amd64-linux.

Ok to commit?

2013-02-13 Doug Evans <dje@google.com>

* valops.c (destructor_name_p): Fix handling of classes in namespaces.

testsuite/
* gdb.cp/cp-disasm.cc: New file.
* gdb.cp/cp-disasm.exp: New file.

Index: valops.c
===================================================================
RCS file: /cvs/src/src/gdb/valops.c,v
retrieving revision 1.312
diff -u -p -r1.312 valops.c
--- valops.c 11 Feb 2013 18:05:33 -0000 1.312
+++ valops.c 14 Feb 2013 00:13:16 -0000
@@ -3160,20 +3160,28 @@ destructor_name_p (const char *name, str
{
if (name[0] == '~')
{
- const char *dname = type_name_no_tag_or_error (type);
- const char *cp = strchr (dname, '<');
+ char *type_name = xstrdup (type_name_no_tag_or_error (type));
+ char *cp = strchr (type_name, '<');
+ char *colon;
+ struct cleanup *cleanups = make_cleanup (xfree, type_name);
unsigned int len;

/* Do not compare the template part for template classes. */
- if (cp == NULL)
- len = strlen (dname);
- else
- len = cp - dname;
- if (strlen (name + 1) != len || strncmp (dname, name + 1, len) != 0)
+ if (cp != NULL)
+ *cp = '\0';
+
+ /* If TYPE_TYPE is foo::bar, we only want bar. */
+ colon = strrchr (type_name, ':');
+ if (colon != NULL)
+ type_name = colon + 1;
+
+ if (strcmp (type_name, name + 1) != 0)
error (_("name of destructor must equal name of class"));
- else
- return 1;
+
+ do_cleanups (cleanups);
+ return 1;
}
+
return 0;
}

Index: testsuite/gdb.cp/cp-disasm.cc
===================================================================
RCS file: testsuite/gdb.cp/cp-disasm.cc
diff -N testsuite/gdb.cp/cp-disasm.cc
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ testsuite/gdb.cp/cp-disasm.cc 14 Feb 2013 00:13:16 -0000
@@ -0,0 +1,44 @@
+/* This test file is part of GDB, the GNU debugger.
+
+ Copyright 2013 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/>. */
+
+class bar
+{
+ public:
+ int x;
+ bar () {}
+ ~bar () {}
+};
+
+namespace foo
+{
+ class bar
+ {
+ public:
+ int x;
+ bar () {}
+ ~bar () {}
+ };
+}
+
+bar x;
+foo::bar y;
+
+int
+main ()
+{
+ return 0;
+}
Index: testsuite/gdb.cp/cp-disasm.exp
===================================================================
RCS file: testsuite/gdb.cp/cp-disasm.exp
diff -N testsuite/gdb.cp/cp-disasm.exp
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ testsuite/gdb.cp/cp-disasm.exp 14 Feb 2013 00:13:16 -0000
@@ -0,0 +1,52 @@
+# Copyright 2013 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/>.
+
+# Test disassembling ctors and dtors.
+
+standard_testfile .cc
+
+if { [skip_cplus_tests] } { continue }
+
+if {[prepare_for_testing $testfile.exp $testfile $srcfile {debug c++}]} {
+ return -1
+}
+
+set name "disassemble ctor"
+gdb_test_multiple "disassemble bar::bar,+1" $name {
+ -re "Dump of assembler code.*$gdb_prompt $" {
+ pass "$name"
+ }
+}
+
+set name "disassemble dtor"
+gdb_test_multiple "disassemble bar::~bar,+1" "$name" {
+ -re "Dump of assembler code.*$gdb_prompt $" {
+ pass "$name"
+ }
+}
+
+set name "disassemble namespace ctor"
+gdb_test_multiple "disassemble foo::bar::bar,+1" "$name" {
+ -re "Dump of assembler code.*$gdb_prompt $" {
+ pass "$name"
+ }
+}
+
+set name "disassemble namespace dtor"
+gdb_test_multiple "disassemble foo::bar::~bar,+1" "$name" {
+ -re "Dump of assembler code.*$gdb_prompt $" {
+ pass "$name"
+ }
+}