Skip to content

Commit ad6510e

Browse files
walter-erquinigoadrian-prantl
authored andcommitted
[lldb-dap] Emit declarations along with variables (llvm#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. (cherry picked from commit 0ea19bd)
1 parent 42ee7f7 commit ad6510e

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 *
@@ -151,7 +151,13 @@ def do_test_scopes_variables_setVariable_evaluate(
151151
globals = self.dap_server.get_global_variables()
152152
buffer_children = make_buffer_verify_dict(0, 32)
153153
verify_locals = {
154-
"argc": {"equals": {"type": "int", "value": "1"}},
154+
"argc": {
155+
"equals": {"type": "int", "value": "1"},
156+
"declaration": {
157+
"equals": {"line": 12, "column": 14},
158+
"contains": {"path": ["lldb-dap", "variables", "main.cpp"]},
159+
},
160+
},
155161
"argv": {
156162
"equals": {"type": "const char **"},
157163
"startswith": {"value": "0x"},

lldb/tools/lldb-dap/JSONUtils.cpp

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1098,6 +1098,29 @@ std::string CreateUniqueVariableNameForDisplay(lldb::SBValue v,
10981098
// can use this optional information to present the
10991099
// children in a paged UI and fetch them in chunks."
11001100
// }
1101+
// "declaration": {
1102+
// "type": "object | undefined",
1103+
// "description": "Extension to the protocol that indicates the source
1104+
// location where the variable was declared. This value
1105+
// might not be present if no declaration is available.",
1106+
// "properties": {
1107+
// "path": {
1108+
// "type": "string | undefined",
1109+
// "description": "The source file path where the variable was
1110+
// declared."
1111+
// },
1112+
// "line": {
1113+
// "type": "number | undefined",
1114+
// "description": "The 1-indexed source line where the variable was
1115+
// declared."
1116+
// },
1117+
// "column": {
1118+
// "type": "number | undefined",
1119+
// "description": "The 1-indexed source column where the variable was
1120+
// declared."
1121+
// }
1122+
// }
1123+
// }
11011124
// },
11021125
// "required": [ "name", "value", "variablesReference" ]
11031126
// }
@@ -1162,6 +1185,24 @@ llvm::json::Value CreateVariable(lldb::SBValue v, int64_t variablesReference,
11621185
const char *evaluateName = evaluateStream.GetData();
11631186
if (evaluateName && evaluateName[0])
11641187
EmplaceSafeString(object, "evaluateName", std::string(evaluateName));
1188+
1189+
if (lldb::SBDeclaration decl = v.GetDeclaration(); decl.IsValid()) {
1190+
llvm::json::Object decl_obj;
1191+
if (lldb::SBFileSpec file = decl.GetFileSpec(); file.IsValid()) {
1192+
char path[PATH_MAX] = "";
1193+
if (file.GetPath(path, sizeof(path)) &&
1194+
lldb::SBFileSpec::ResolvePath(path, path, PATH_MAX)) {
1195+
decl_obj.try_emplace("path", std::string(path));
1196+
}
1197+
}
1198+
1199+
if (int line = decl.GetLine())
1200+
decl_obj.try_emplace("line", line);
1201+
if (int column = decl.GetColumn())
1202+
decl_obj.try_emplace("column", column);
1203+
1204+
object.try_emplace("declaration", std::move(decl_obj));
1205+
}
11651206
return llvm::json::Value(std::move(object));
11661207
}
11671208

0 commit comments

Comments
 (0)