Skip to content

Commit 5c3b345

Browse files
committed
[lldb-dap] Provide declarationLocation for variables
TODO: * Adjust comment on `CreateVariable` function with updated jsonschema as soon as the upstream changes were merged to DAP. `declarationLocation` is about to become part of the upstream debug-adapter-protocol. This is a draft implementation, to be finalized and merged after the corresponding changes were merged into DAP.
1 parent a417083 commit 5c3b345

File tree

3 files changed

+32
-4
lines changed

3 files changed

+32
-4
lines changed

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,10 @@ def do_test_scopes_variables_setVariable_evaluate(
168168
"type": "int",
169169
"value": "1",
170170
},
171+
"declarationLocation": {
172+
"equals": {"line": 12, "column": 14},
173+
"contains": {"path": ["lldb-dap", "variables", "main.cpp"]},
174+
},
171175
"$__lldb_extensions": {
172176
"equals": {
173177
"value": "1",

lldb/tools/lldb-dap/JSONUtils.cpp

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -614,9 +614,8 @@ CreateExceptionBreakpointFilter(const ExceptionBreakpoint &bp) {
614614
// }
615615
// }
616616
// }
617-
llvm::json::Value CreateSource(lldb::SBLineEntry &line_entry) {
617+
llvm::json::Value CreateSource(const lldb::SBFileSpec &file) {
618618
llvm::json::Object object;
619-
lldb::SBFileSpec file = line_entry.GetFileSpec();
620619
if (file.IsValid()) {
621620
const char *name = file.GetFilename();
622621
if (name)
@@ -630,6 +629,10 @@ llvm::json::Value CreateSource(lldb::SBLineEntry &line_entry) {
630629
return llvm::json::Value(std::move(object));
631630
}
632631

632+
llvm::json::Value CreateSource(const lldb::SBLineEntry &line_entry) {
633+
return CreateSource(line_entry.GetFileSpec());
634+
}
635+
633636
llvm::json::Value CreateSource(llvm::StringRef source_path) {
634637
llvm::json::Object source;
635638
llvm::StringRef name = llvm::sys::path::filename(source_path);
@@ -1253,6 +1256,17 @@ llvm::json::Value CreateVariable(lldb::SBValue v, int64_t variablesReference,
12531256
else
12541257
object.try_emplace("variablesReference", (int64_t)0);
12551258

1259+
if (lldb::SBDeclaration decl = v.GetDeclaration(); decl.IsValid()) {
1260+
llvm::json::Object decl_obj;
1261+
decl_obj.try_emplace("source", CreateSource(decl.GetFileSpec()));
1262+
if (int line = decl.GetLine())
1263+
decl_obj.try_emplace("line", line);
1264+
if (int column = decl.GetColumn())
1265+
decl_obj.try_emplace("column", column);
1266+
1267+
object.try_emplace("declarationLocation", std::move(decl_obj));
1268+
}
1269+
12561270
object.try_emplace("$__lldb_extensions", desc.GetVariableExtensionsJSON());
12571271
return llvm::json::Value(std::move(object));
12581272
}

lldb/tools/lldb-dap/JSONUtils.h

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -282,16 +282,26 @@ llvm::json::Value CreateScope(const llvm::StringRef name,
282282
int64_t variablesReference,
283283
int64_t namedVariables, bool expensive);
284284

285+
/// Create a "Source" JSON object as described in the debug adaptor definition.
286+
///
287+
/// \param[in] file
288+
/// The SBFileSpec to use when populating out the "Source" object
289+
///
290+
/// \return
291+
/// A "Source" JSON object that follows the formal JSON
292+
/// definition outlined by Microsoft.
293+
llvm::json::Value CreateSource(const lldb::SBFileSpec &file);
294+
285295
/// Create a "Source" JSON object as described in the debug adaptor definition.
286296
///
287297
/// \param[in] line_entry
288298
/// The LLDB line table to use when populating out the "Source"
289299
/// object
290300
///
291301
/// \return
292-
/// A "Source" JSON object with that follows the formal JSON
302+
/// A "Source" JSON object that follows the formal JSON
293303
/// definition outlined by Microsoft.
294-
llvm::json::Value CreateSource(lldb::SBLineEntry &line_entry);
304+
llvm::json::Value CreateSource(const lldb::SBLineEntry &line_entry);
295305

296306
/// Create a "Source" object for a given source path.
297307
///

0 commit comments

Comments
 (0)