Fix ptype bug actually exercised in userdef.exp
authorPedro Alves <palves@redhat.com>
Thu, 14 Feb 2013 12:43:46 +0000 (12:43 +0000)
committerPedro Alves <palves@redhat.com>
Thu, 14 Feb 2013 12:43:46 +0000 (12:43 +0000)
I happened to notice a bug with ptype &Ref, and found out userdef.exp
actually exercises the bug.  With:

class Container
{
public:
  Member m;

  Member& operator* ();
};

Member& Container::operator* ()
{
  return this->m;
}

And 'c' is of type Container:

(gdb) p c
$1 = {m = {z = -9192}}
(gdb) p *c
$2 = (Member &) @0x7fffffffda20: {z = -9192}
(gdb) ptype *c
type = class Member {
  public:
    int z;
} &

(gdb) p &*c
$3 = (Member *) 0x7fffffffda20

(gdb) ptype &*c
type = class Member {
  public:
    int z;
} &*
(gdb)

Notice that last print (&*c) on says the type is a pointer - that's
how you get the address behind a reference.  But notice the last ptype
instead says the type of the same expression is a pointer _reference_.
This looks like a bug to me.

This patch fixes it.  The issue is that we're entering the VALUE_LVAL
(x) == lval_memory branch by mistake for references.  The fix is just
to swap the tests so references are checked first, like value_addr
also handles references first.

Tested on x86_64 Fedora 17.

2013-02-14  Pedro Alves  <palves@redhat.com>

* eval.c (evaluate_subexp_for_address) <default_case_after_eval,
EVAL_AVOID_SIDE_EFFECTS>: Swap and handle TYPE_CODE_REF before
lval_memory.

2013-02-14  Pedro Alves  <palves@redhat.com>

* gdb.cp/userdef.exp (ptype &*c): Don't expect an &.

gdb/ChangeLog
gdb/eval.c
gdb/testsuite/ChangeLog
gdb/testsuite/gdb.cp/userdef.exp

index ae09cc1e275e324efd1f1398ab85ee8c7c5d21a2..0a3f85de16e73643d45c35faad2a95b8a780ce4d 100644 (file)
@@ -1,3 +1,9 @@
+2013-02-14  Pedro Alves  <palves@redhat.com>
+
+       * eval.c (evaluate_subexp_for_address) <default_case_after_eval,
+       EVAL_AVOID_SIDE_EFFECTS>: Swap and handle TYPE_CODE_REF before
+       lval_memory.
+
 2013-02-14  Pedro Alves  <pedro@codesourcery.com>
            Hafiz Abid Qadeer  <abidh@codesourcery.com>
 
index f4b39cbc2b05f9686898f6a1a564a2a368df7682..d7f80e2d4f545f10b8574384f24254235363e731 100644 (file)
@@ -2929,12 +2929,12 @@ evaluate_subexp_for_address (struct expression *exp, int *pos,
        {
          struct type *type = check_typedef (value_type (x));
 
-         if (VALUE_LVAL (x) == lval_memory || value_must_coerce_to_target (x))
-           return value_zero (lookup_pointer_type (value_type (x)),
-                              not_lval);
-         else if (TYPE_CODE (type) == TYPE_CODE_REF)
+         if (TYPE_CODE (type) == TYPE_CODE_REF)
            return value_zero (lookup_pointer_type (TYPE_TARGET_TYPE (type)),
                               not_lval);
+         else if (VALUE_LVAL (x) == lval_memory || value_must_coerce_to_target (x))
+           return value_zero (lookup_pointer_type (value_type (x)),
+                              not_lval);
          else
            error (_("Attempt to take address of "
                     "value not located in memory."));
index 9a377259c04cf0d14fa6b47a44c254d77b3fd4ed..c2affd18c3a290fe6d320f6114e1ad517a1ef281 100644 (file)
@@ -1,3 +1,7 @@
+2013-02-14  Pedro Alves  <palves@redhat.com>
+
+       * gdb.cp/userdef.exp (ptype &*c): Don't expect an &.
+
 2013-02-14  Pedro Alves  <pedro@codesourcery.com>
            Hafiz Abid Qadeer  <abidh@codesourcery.com>
 
index 7c20b33b7fa3badcd477971652f1085fe3d036e0..51a1bb5f67ba5217273911623dc66ba6493dbfcd 100644 (file)
@@ -133,7 +133,7 @@ gdb_test "break A2::operator +" ".*Breakpoint $decimal at.*"
 gdb_test "print c" "\\\$\[0-9\]* = {m = {z = .*}}"
 gdb_test "print *c" "\\\$\[0-9\]* = \\(Member &\\) @$hex: {z = .*}"
 gdb_test "print &*c" "\\\$\[0-9\]* = \\(Member \\*\\) $hex"
-gdb_test "ptype &*c" "type = (struct|class) Member {(\[\r\n \]+public:)?\[\r\n \]+int z;\[\r\n\].*} &\\*"
+gdb_test "ptype &*c" "type = (struct|class) Member {(\[\r\n \]+public:)?\[\r\n \]+int z;\[\r\n\].*} \\*"
 
 gdb_test "print operator== (mem1, mem2)" " = false"
 gdb_test "print operator== (mem1, mem1)" " = true"
This page took 0.040108 seconds and 4 git commands to generate.