This is the mail archive of the
gdb-patches@sourceware.org
mailing list for the GDB project.
[RFA] Fix c++/14819 (implicit this)
- From: Keith Seitz <keiths at redhat dot com>
- To: "gdb-patches at sourceware dot org ml" <gdb-patches at sourceware dot org>
- Date: Tue, 05 Nov 2013 13:08:58 -0800
- Subject: [RFA] Fix c++/14819 (implicit this)
- Authentication-results: sourceware.org; auth=none
Hi,
This is an attempt to address c++/14819. The simplest reproducer is:
class C {
public:
int x;
C():x(42) {}
int f() {
return C::x;
}
} c;
int main() { c.f(); }
(gdb) print C::x
A syntax error in expression, near `x'.
This is happening because all qualified names like this go through
value_aggregate_elt, which dispatches structs and classes to
value_struct_elt_for_reference. That function does not allow references
to non-static member data.
The solution I've implemented (as explained in the comments) is to treat
a qualified name on non-static member with an implicit `this'. So the
qualified name is looked up in the current this pointer (if any), and a
value is computed as if the user had typed, "this->*(&QUALIFIED_NAME)".
No regressions on native x86_64-linux or native-gdbserver.
Comments, questions, concerns?
Keith
ChangeLog
2013-11-05 Keith Seitz <keiths@redhat.com>
PR c++/14819
* valops.c (value_struct_elt_for_reference): If we get
a non-static field, try to get a value based on the
an implicit this pointer.
testsuite/ChangeLog
2013-11-05 Keith Seitz <keiths@redhat.com>
PR c++/14819
* gdb.cp/impl-this.cc: New file.
* gdb.cp/impl-this.exp: New file.
diff --git a/gdb/valops.c b/gdb/valops.c
index 8bff686..2f3ead8 100644
--- a/gdb/valops.c
+++ b/gdb/valops.c
@@ -3131,7 +3131,32 @@ value_struct_elt_for_reference (struct type *domain, int offset,
else if (noside == EVAL_AVOID_SIDE_EFFECTS)
return allocate_value (TYPE_FIELD_TYPE (t, i));
else
- error (_("Cannot reference non-static field \"%s\""), name);
+ {
+ /* Try to evaluate NAME as a qualified name with implicit
+ this pointer. In this case, attempt to return the
+ equivalent to `this->*(&TYPE::NAME)'. */
+ v = value_of_this_silent (current_language);
+ if (v != NULL)
+ {
+ struct value *ptr;
+ long mem_offset;
+ struct type *type, *tmp;
+
+ ptr = value_aggregate_elt (t, name, NULL, 1, noside);
+ type = check_typedef (value_type (ptr));
+ gdb_assert (type != NULL
+ && TYPE_CODE (type) == TYPE_CODE_MEMBERPTR);
+ tmp = lookup_pointer_type (TYPE_DOMAIN_TYPE (type));
+ v = value_cast_pointers (tmp, v, 1);
+ mem_offset = value_as_long (ptr);
+ tmp = lookup_pointer_type (TYPE_TARGET_TYPE (type));
+ result = value_from_pointer (tmp,
+ value_as_long (v) + mem_offset);
+ return value_ind (result);
+ }
+
+ error (_("Cannot reference non-static field \"%s\""), name);
+ }
}
}
diff --git a/gdb/testsuite/gdb.cp/impl-this.cc b/gdb/testsuite/gdb.cp/impl-this.cc
new file mode 100644
index 0000000..5b52f24
--- /dev/null
+++ b/gdb/testsuite/gdb.cp/impl-this.cc
@@ -0,0 +1,52 @@
+/* This testcase 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 A
+{
+public:
+ int a;
+ int x, y, z;
+ A (void) : a (11), x (14), y (15), z (16) {}
+};
+
+class B : public A
+{
+public:
+ int a, b;
+ int x, y;
+ int solo;
+ B (void) : a (21), b (22), x (24), y (25), solo (100) {}
+};
+
+class C : public B
+{
+public:
+ int a, b, c;
+ int x;
+ C (void) : a (31), b (32), c (33), x (34) {}
+ int f (void) const
+ {
+ return (C::a + B::a + C::a - C::b - B::b + C::c
+ - C::x - B::x - C::x + B::y + A::y - A::z); /* break here */
+ }
+} c;
+
+int
+main (void)
+{
+ return c.f();
+}
diff --git a/gdb/testsuite/gdb.cp/impl-this.exp b/gdb/testsuite/gdb.cp/impl-this.exp
new file mode 100644
index 0000000..679b8ce
--- /dev/null
+++ b/gdb/testsuite/gdb.cp/impl-this.exp
@@ -0,0 +1,67 @@
+# 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/>.
+
+# This file is part of the gdb testsuite
+
+# Test expressions which assume an implicit "this" with a qualified
+# name.
+
+if {[skip_cplus_tests]} { continue }
+
+standard_testfile .cc
+
+if {[prepare_for_testing $testfile.exp $testfile $srcfile {debug c++}]} {
+ return -1
+}
+
+if {![runto_main]} {
+ perror "couldn't run to main"
+ continue
+}
+
+gdb_test "print A::a" "Cannot reference non-static field \"a\""
+gdb_test "print A::b" "There is no field named b"
+
+gdb_breakpoint [gdb_get_line_number "break here"]
+gdb_continue_to_breakpoint "run to C::f"
+
+gdb_test "print A::a" "= 11"
+gdb_test "print A::b" "There is no field named b"
+gdb_test "print A::c" "There is no field named c"
+gdb_test "print A::x" "= 14"
+gdb_test "print A::y" "= 15"
+gdb_test "print A::z" "= 16"
+
+gdb_test "print B::a" "= 21"
+gdb_test "print B::b" "= 22"
+gdb_test "print B::c" "There is no field named c"
+gdb_test "print B::x" "= 24"
+gdb_test "print B::y" "= 25"
+gdb_test "print B::z" "= 16"; # B inherits from A!
+gdb_test "print B::solo" "= 100"
+
+gdb_test "print C::a" "= 31"
+gdb_test "print C::b" "= 32"
+gdb_test "print C::c" "= 33"
+gdb_test "print C::x" "= 34"
+gdb_test "print C::y" "= 25"; # C inherits from B!
+gdb_test "print C::z" "= 16"; # C inherits from B and B from A!
+
+gdb_test "print a" "= 31"
+gdb_test "print b" "= 32"
+gdb_test "print c" "= 33"
+gdb_test "print x" "= 34"
+gdb_test "print y" "= 25"; # C inherits from B!
+gdb_test "print z" "= 16"; # C inherits from B and B from A!