[Lldb-commits] [lldb] [lldb-dap] Improving 'variables' hover requests. (PR #146773)

John Harrison via lldb-commits lldb-commits at lists.llvm.org
Wed Jul 2 13:37:20 PDT 2025


https://github.com/ashgti created https://github.com/llvm/llvm-project/pull/146773

This partially fixes https://github.com/llvm/llvm-project/issues/146559.

When hovering over variables while debugging with lldb-dap we are receiving hover requests that include symbols.

For example, if you have:

```
MyClass *foo;
```

and hover over 'foo', the request will contain the expression `expression="*foo"` and we're evaluating the dereference, so you end up with different hover results vs the variables view.

A more complete solution would be to implement an
[EvaluatableExpressionProvider](https://code.visualstudio.com/api/references/vscode-api#EvaluatableExpressionProvider) in the VS Code extension.

For example, if you have a declaration without any whitespace like

```c
char*foo = "bar";
```

And try to hover over 'foo', the request will be `expression="char*foo"`.

>From 2674e5af817e1e6b06521c7b7f39b23150aed747 Mon Sep 17 00:00:00 2001
From: John Harrison <harjohn at google.com>
Date: Wed, 2 Jul 2025 13:05:31 -0700
Subject: [PATCH] [lldb-dap] Improving 'variables' hover requests.

This partially fixes https://github.com/llvm/llvm-project/issues/146559.

When hovering over variables while debugging with lldb-dap we are
receiving hover requests that include symbols.

For example, if you have:

```
MyClass *foo;
```

and hover over 'foo', the request will contain the expression
`expression="*foo"` and we're evaluating the dereference, so you end up
with different hover results vs the variables view.

A more complete solution would be to implement an
[EvaluatableExpressionProvider](https://code.visualstudio.com/api/references/vscode-api#EvaluatableExpressionProvider)
in the VS Code extension.

For example, if you have a declaration without any whitespace like

```c
char*foo = "bar";
```

And try to hover over 'foo', the request will be `expression="char*foo"`.
---
 lldb/test/API/tools/lldb-dap/memory/TestDAP_memory.py  |  2 +-
 lldb/tools/lldb-dap/Handler/EvaluateRequestHandler.cpp | 10 ++++++++++
 2 files changed, 11 insertions(+), 1 deletion(-)

diff --git a/lldb/test/API/tools/lldb-dap/memory/TestDAP_memory.py b/lldb/test/API/tools/lldb-dap/memory/TestDAP_memory.py
index 55fb4a961e783..30ee7a1631e29 100644
--- a/lldb/test/API/tools/lldb-dap/memory/TestDAP_memory.py
+++ b/lldb/test/API/tools/lldb-dap/memory/TestDAP_memory.py
@@ -92,7 +92,7 @@ def test_readMemory(self):
         )
         self.continue_to_next_stop()
 
-        ptr_deref = self.dap_server.request_evaluate("*rawptr")["body"]
+        ptr_deref = self.dap_server.request_evaluate("*rawptr", context="repl")["body"]
         memref = ptr_deref["memoryReference"]
 
         # We can read the complete string
diff --git a/lldb/tools/lldb-dap/Handler/EvaluateRequestHandler.cpp b/lldb/tools/lldb-dap/Handler/EvaluateRequestHandler.cpp
index e1556846dff19..e2161eb999e82 100644
--- a/lldb/tools/lldb-dap/Handler/EvaluateRequestHandler.cpp
+++ b/lldb/tools/lldb-dap/Handler/EvaluateRequestHandler.cpp
@@ -181,7 +181,17 @@ void EvaluateRequestHandler::operator()(
         expression = dap.last_nonempty_var_expression;
       else
         dap.last_nonempty_var_expression = expression;
+    } else {
+      // If this isn't a REPL context, trim leading pointer/reference characters
+      // to ensure we return the actual value of the expression.
+      // This can come up if you hover over a pointer or reference declaration
+      // like 'MyType *foo;' or `void fn(std::string &arg)`, which results in
+      // the hover request sending '*foo' or `&arg`. When we're not in the REPL,
+      // we should trim these characters to get to the actual variable, which
+      // should have the proper type encoded by the compiler.
+      expression = llvm::StringRef(expression).ltrim("*&").str();
     }
+
     // Always try to get the answer from the local variables if possible. If
     // this fails, then if the context is not "hover", actually evaluate an
     // expression using the expression parser.



More information about the lldb-commits mailing list