Skip to content

Commit 344965b

Browse files
committed
Address comments
1 parent a7876a2 commit 344965b

File tree

3 files changed

+24
-7
lines changed

3 files changed

+24
-7
lines changed

lldb/tools/lldb-dap/JSONUtils.cpp

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1210,6 +1210,14 @@ bool HasValueLocation(lldb::SBValue v) {
12101210
return line_entry.IsValid();
12111211
}
12121212

1213+
int64_t PackLocation(int64_t var_ref, bool is_value_location) {
1214+
return var_ref << 1 | is_value_location;
1215+
}
1216+
1217+
std::pair<int64_t, bool> UnpackLocation(int64_t location_id) {
1218+
return std::pair{ location_id >> 1, location_id & 1};
1219+
}
1220+
12131221
// "Variable": {
12141222
// "type": "object",
12151223
// "description": "A Variable is a name/value pair. Optionally a variable
@@ -1414,10 +1422,10 @@ llvm::json::Value CreateVariable(lldb::SBValue v, int64_t var_ref,
14141422
object.try_emplace("variablesReference", 0);
14151423

14161424
if (v.GetDeclaration().IsValid())
1417-
object.try_emplace("declarationLocationReference", var_ref << 1);
1425+
object.try_emplace("declarationLocationReference", PackLocation(var_ref, false));
14181426

14191427
if (HasValueLocation(v))
1420-
object.try_emplace("valueLocationReference", (var_ref << 1) | 1);
1428+
object.try_emplace("valueLocationReference", PackLocation(var_ref, true));
14211429

14221430
if (lldb::addr_t addr = v.GetLoadAddress(); addr != LLDB_INVALID_ADDRESS)
14231431
object.try_emplace("memoryReference", EncodeMemoryReference(addr));

lldb/tools/lldb-dap/JSONUtils.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -461,6 +461,13 @@ struct VariableDescription {
461461
/// Does the given variable have an associated value location?
462462
bool HasValueLocation(lldb::SBValue v);
463463

464+
/// Pack a location into a single integer which we can send via
465+
/// the debug adapter protocol.
466+
int64_t PackLocation(int64_t var_ref, bool is_value_location);
467+
468+
/// Reverse of `PackLocation`
469+
std::pair<int64_t, bool> UnpackLocation(int64_t location_id);
470+
464471
/// Create a "Variable" object for a LLDB thread object.
465472
///
466473
/// This function will fill in the following keys in the returned

lldb/tools/lldb-dap/lldb-dap.cpp

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1624,7 +1624,9 @@ void request_evaluate(const llvm::json::Object &request) {
16241624
VariableDescription desc(value);
16251625
EmplaceSafeString(body, "result", desc.GetResult(context));
16261626
EmplaceSafeString(body, "type", desc.display_type_name);
1627-
auto var_ref = g_dap.variables.InsertVariable(
1627+
int64_t var_ref = 0;
1628+
if (value.MightHaveChildren() || HasValueLocation(value))
1629+
var_ref = g_dap.variables.InsertVariable(
16281630
value, /*is_permanent=*/context == "repl");
16291631
if (value.MightHaveChildren())
16301632
body.try_emplace("variablesReference", var_ref);
@@ -4116,11 +4118,11 @@ void request_locations(const llvm::json::Object &request) {
41164118
FillResponse(request, response);
41174119
auto *arguments = request.getObject("arguments");
41184120

4119-
uint64_t reference_id = GetUnsigned(arguments, "locationReference", 0);
4121+
uint64_t location_id = GetUnsigned(arguments, "locationReference", 0);
41204122
// We use the lowest bit to distinguish between value location and declaration
41214123
// location
4122-
bool isValueLocation = reference_id & 1;
4123-
lldb::SBValue variable = g_dap.variables.GetVariable(reference_id >> 1);
4124+
auto [var_ref, is_value_location] = UnpackLocation(location_id);
4125+
lldb::SBValue variable = g_dap.variables.GetVariable(var_ref);
41244126
if (!variable.IsValid()) {
41254127
response["success"] = false;
41264128
response["message"] = "Invalid variable reference";
@@ -4129,7 +4131,7 @@ void request_locations(const llvm::json::Object &request) {
41294131
}
41304132

41314133
llvm::json::Object body;
4132-
if (isValueLocation) {
4134+
if (is_value_location) {
41334135
// Get the value location
41344136
if (!variable.GetType().IsPointerType() &&
41354137
!variable.GetType().IsReferenceType()) {

0 commit comments

Comments
 (0)