[Lldb-commits] [lldb] Fix pointer to reference type (PR #113596)

Pavel Labath via lldb-commits lldb-commits at lists.llvm.org
Wed Oct 30 07:54:39 PDT 2024


labath wrote:

Let's back up a bit. Before this patch, both the ValueObject class and the "frame var" command treated references essentially as pointers (in the example, `xp` is a pointer to `x` and `xpr` is a reference to `xp`):
```
(lldb) v *x x &x
(int) ::x = 47
(int *) &::x = 0x0000555555558010
error: not a pointer or reference type: (int) ::x
(lldb) v *xp xp &xp
(int) *xp = 47
(int *) ::xp = 0x0000555555558010
(int **) &::xp = 0x0000555555558018
(lldb) v *xpr xpr &xpr
(int *) *xpr = 0x0000555555558010
(int *&) ::xpr = 0x0000555555558018: {
  &::xpr = 0x0000555555558010
}
(int *&*) &::xpr = 0x0000555555557dc8
```

Now, you may or may not agree with that behavior, but at least its consistent: notice how "dereferencing" `xpr` gives you the value of `xp` and taking the address of `xpr` gives you its "address" (in spite of what the C++ standard says, the reference does exist somewhere and that place has an address).

After this patch, we get this:

```
(lldb) v *xpr xpr &xpr
(int *) *xpr = 0x0000555555558010
(int *&) ::xpr = 0x0000555555558018: {
  &::xpr = 0x0000555555558010
}
(int **) &&::xpr = 0x0000555555558018
```
I.e. taking the "address of" the reference gives you the same thing as its "value", "dereferencing" the reference still gives you the object that is being referenced (it does *not* dereference the pointer behind it like you would expect according to the c++ rules), and there's no way to determine the location of the reference.

So, either this patch is incorrect (if you believe that the current behavior was intentional) or incomplete (if you believe that we should follow the c++ rules). Either way, I believe we should revert this and discuss the problem in more detail.

FWIW, I agree with what you're trying to achieve (making "frame var" follow c++ rules for references). I just think this needs to be handled more wholistically. And I'm not entirely sure that the fix belongs in the ValueObject class.(*)  I also believe there should be a way to get the "location" of the reference (but that doesn't have to be exposed through "frame var")

(*) I'm not saying it does *not* belong there either, but I think you could make a stronger case for treating references like "fancy pointers" in the ValueObject class -- i.e. maintaining the status quo there.

https://github.com/llvm/llvm-project/pull/113596


More information about the lldb-commits mailing list