Skip to content

Commit 0ea19bd

Browse files
[lldb-dap] Emit declarations along with variables (#74865)
This is an extension to the protocol that emits the declaration information along with the metadata of each variable. This can be used by vscode extensions to implement, for example, a "goToDefinition" action in the debug tab, or for showing the value of a variable right next to where it's declared during a debug session. As this is cheap, I'm not gating this information under any setting.
1 parent 631c6e8 commit 0ea19bd

File tree

2 files changed

+49
-2
lines changed

2 files changed

+49
-2
lines changed

lldb/test/API/tools/lldb-dap/variables/TestDAP_variables.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44

55
import os
66

7-
import lldbdap_testcase
87
import dap_server
8+
import lldbdap_testcase
99
from lldbsuite.test import lldbutil
1010
from lldbsuite.test.decorators import *
1111
from lldbsuite.test.lldbtest import *
@@ -152,7 +152,13 @@ def do_test_scopes_variables_setVariable_evaluate(
152152
globals = self.dap_server.get_global_variables()
153153
buffer_children = make_buffer_verify_dict(0, 32)
154154
verify_locals = {
155-
"argc": {"equals": {"type": "int", "value": "1"}},
155+
"argc": {
156+
"equals": {"type": "int", "value": "1"},
157+
"declaration": {
158+
"equals": {"line": 12, "column": 14},
159+
"contains": {"path": ["lldb-dap", "variables", "main.cpp"]},
160+
},
161+
},
156162
"argv": {
157163
"equals": {"type": "const char **"},
158164
"startswith": {"value": "0x"},

lldb/tools/lldb-dap/JSONUtils.cpp

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1103,6 +1103,29 @@ std::string CreateUniqueVariableNameForDisplay(lldb::SBValue v,
11031103
// can use this optional information to present the
11041104
// children in a paged UI and fetch them in chunks."
11051105
// }
1106+
// "declaration": {
1107+
// "type": "object | undefined",
1108+
// "description": "Extension to the protocol that indicates the source
1109+
// location where the variable was declared. This value
1110+
// might not be present if no declaration is available.",
1111+
// "properties": {
1112+
// "path": {
1113+
// "type": "string | undefined",
1114+
// "description": "The source file path where the variable was
1115+
// declared."
1116+
// },
1117+
// "line": {
1118+
// "type": "number | undefined",
1119+
// "description": "The 1-indexed source line where the variable was
1120+
// declared."
1121+
// },
1122+
// "column": {
1123+
// "type": "number | undefined",
1124+
// "description": "The 1-indexed source column where the variable was
1125+
// declared."
1126+
// }
1127+
// }
1128+
// }
11061129
// },
11071130
// "required": [ "name", "value", "variablesReference" ]
11081131
// }
@@ -1167,6 +1190,24 @@ llvm::json::Value CreateVariable(lldb::SBValue v, int64_t variablesReference,
11671190
const char *evaluateName = evaluateStream.GetData();
11681191
if (evaluateName && evaluateName[0])
11691192
EmplaceSafeString(object, "evaluateName", std::string(evaluateName));
1193+
1194+
if (lldb::SBDeclaration decl = v.GetDeclaration(); decl.IsValid()) {
1195+
llvm::json::Object decl_obj;
1196+
if (lldb::SBFileSpec file = decl.GetFileSpec(); file.IsValid()) {
1197+
char path[PATH_MAX] = "";
1198+
if (file.GetPath(path, sizeof(path)) &&
1199+
lldb::SBFileSpec::ResolvePath(path, path, PATH_MAX)) {
1200+
decl_obj.try_emplace("path", std::string(path));
1201+
}
1202+
}
1203+
1204+
if (int line = decl.GetLine())
1205+
decl_obj.try_emplace("line", line);
1206+
if (int column = decl.GetColumn())
1207+
decl_obj.try_emplace("column", column);
1208+
1209+
object.try_emplace("declaration", std::move(decl_obj));
1210+
}
11701211
return llvm::json::Value(std::move(object));
11711212
}
11721213

0 commit comments

Comments
 (0)