Skip to content

Commit 8f39c77

Browse files
committed
Move helpers into Variable
1 parent 95f575e commit 8f39c77

File tree

6 files changed

+61
-116
lines changed

6 files changed

+61
-116
lines changed

lldb/tools/lldb-dap/DAP.cpp

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1201,4 +1201,58 @@ DAP::GetInstructionBPFromStopReason(lldb::SBThread &thread) {
12011201
return inst_bp;
12021202
}
12031203

1204+
lldb::SBValueList *Variables::GetTopLevelScope(int64_t variablesReference) {
1205+
switch (variablesReference) {
1206+
case VARREF_LOCALS:
1207+
return &locals;
1208+
case VARREF_GLOBALS:
1209+
return &globals;
1210+
case VARREF_REGS:
1211+
return &registers;
1212+
default:
1213+
return nullptr;
1214+
}
1215+
}
1216+
1217+
lldb::SBValue Variables::FindVariable(uint64_t variablesReference,
1218+
llvm::StringRef name) {
1219+
lldb::SBValue variable;
1220+
if (lldb::SBValueList *top_scope = GetTopLevelScope(variablesReference)) {
1221+
bool is_duplicated_variable_name = name.contains(" @");
1222+
// variablesReference is one of our scopes, not an actual variable it is
1223+
// asking for a variable in locals or globals or registers
1224+
int64_t end_idx = top_scope->GetSize();
1225+
// Searching backward so that we choose the variable in closest scope
1226+
// among variables of the same name.
1227+
for (int64_t i = end_idx - 1; i >= 0; --i) {
1228+
lldb::SBValue curr_variable = top_scope->GetValueAtIndex(i);
1229+
std::string variable_name = CreateUniqueVariableNameForDisplay(
1230+
curr_variable, is_duplicated_variable_name);
1231+
if (variable_name == name) {
1232+
variable = curr_variable;
1233+
break;
1234+
}
1235+
}
1236+
} else {
1237+
// This is not under the globals or locals scope, so there are no duplicated
1238+
// names.
1239+
1240+
// We have a named item within an actual variable so we need to find it
1241+
// withing the container variable by name.
1242+
lldb::SBValue container = GetVariable(variablesReference);
1243+
variable = container.GetChildMemberWithName(name.data());
1244+
if (!variable.IsValid()) {
1245+
if (name.starts_with("[")) {
1246+
llvm::StringRef index_str(name.drop_front(1));
1247+
uint64_t index = 0;
1248+
if (!index_str.consumeInteger(0, index)) {
1249+
if (index_str == "]")
1250+
variable = container.GetChildAtIndex(index);
1251+
}
1252+
}
1253+
}
1254+
}
1255+
return variable;
1256+
}
1257+
12041258
} // namespace lldb_dap

lldb/tools/lldb-dap/DAP.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,10 @@ struct Variables {
116116
/// \return variableReference assigned to this expandable variable.
117117
int64_t InsertVariable(lldb::SBValue variable, bool is_permanent);
118118

119+
lldb::SBValueList *GetTopLevelScope(int64_t variablesReference);
120+
121+
lldb::SBValue FindVariable(uint64_t variablesReference, llvm::StringRef name);
122+
119123
/// Clear all scope variables and non-permanent expandable variables.
120124
void Clear();
121125
};

lldb/tools/lldb-dap/Handler/DataBreakpointInfoRequestHandler.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ void DataBreakpointInfoRequestHandler::operator()(
119119
GetUnsigned(arguments, "variablesReference", 0);
120120
llvm::StringRef name = GetString(arguments, "name");
121121
lldb::SBFrame frame = dap.GetLLDBFrame(*arguments);
122-
lldb::SBValue variable = FindVariable(variablesReference, name);
122+
lldb::SBValue variable = dap.variables.FindVariable(variablesReference, name);
123123
std::string addr, size;
124124

125125
if (variable.IsValid()) {

lldb/tools/lldb-dap/Handler/RequestHandler.cpp

Lines changed: 0 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -232,59 +232,4 @@ bool RequestHandler::HasInstructionGranularity(
232232
return false;
233233
}
234234

235-
lldb::SBValueList *
236-
RequestHandler::GetTopLevelScope(int64_t variablesReference) {
237-
switch (variablesReference) {
238-
case VARREF_LOCALS:
239-
return &dap.variables.locals;
240-
case VARREF_GLOBALS:
241-
return &dap.variables.globals;
242-
case VARREF_REGS:
243-
return &dap.variables.registers;
244-
default:
245-
return nullptr;
246-
}
247-
}
248-
249-
lldb::SBValue RequestHandler::FindVariable(uint64_t variablesReference,
250-
llvm::StringRef name) {
251-
lldb::SBValue variable;
252-
if (lldb::SBValueList *top_scope = GetTopLevelScope(variablesReference)) {
253-
bool is_duplicated_variable_name = name.contains(" @");
254-
// variablesReference is one of our scopes, not an actual variable it is
255-
// asking for a variable in locals or globals or registers
256-
int64_t end_idx = top_scope->GetSize();
257-
// Searching backward so that we choose the variable in closest scope
258-
// among variables of the same name.
259-
for (int64_t i = end_idx - 1; i >= 0; --i) {
260-
lldb::SBValue curr_variable = top_scope->GetValueAtIndex(i);
261-
std::string variable_name = CreateUniqueVariableNameForDisplay(
262-
curr_variable, is_duplicated_variable_name);
263-
if (variable_name == name) {
264-
variable = curr_variable;
265-
break;
266-
}
267-
}
268-
} else {
269-
// This is not under the globals or locals scope, so there are no duplicated
270-
// names.
271-
272-
// We have a named item within an actual variable so we need to find it
273-
// withing the container variable by name.
274-
lldb::SBValue container = dap.variables.GetVariable(variablesReference);
275-
variable = container.GetChildMemberWithName(name.data());
276-
if (!variable.IsValid()) {
277-
if (name.starts_with("[")) {
278-
llvm::StringRef index_str(name.drop_front(1));
279-
uint64_t index = 0;
280-
if (!index_str.consumeInteger(0, index)) {
281-
if (index_str == "]")
282-
variable = container.GetChildAtIndex(index);
283-
}
284-
}
285-
}
286-
}
287-
return variable;
288-
}
289-
290235
} // namespace lldb_dap

lldb/tools/lldb-dap/Handler/RequestHandler.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,6 @@ class RequestHandler {
5252
// Check if the step-granularity is `instruction`.
5353
bool HasInstructionGranularity(const llvm::json::Object &request);
5454

55-
lldb::SBValueList *GetTopLevelScope(int64_t variablesReference);
56-
lldb::SBValue FindVariable(uint64_t variablesReference, llvm::StringRef name);
57-
5855
/// @}
5956

6057
DAP &dap;

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

Lines changed: 2 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -130,61 +130,6 @@ typedef void (*RequestCallback)(const llvm::json::Object &command);
130130
/// Page size used for reporting addtional frames in the 'stackTrace' request.
131131
constexpr int StackPageSize = 20;
132132

133-
lldb::SBValueList *GetTopLevelScope(DAP &dap, int64_t variablesReference) {
134-
switch (variablesReference) {
135-
case VARREF_LOCALS:
136-
return &dap.variables.locals;
137-
case VARREF_GLOBALS:
138-
return &dap.variables.globals;
139-
case VARREF_REGS:
140-
return &dap.variables.registers;
141-
default:
142-
return nullptr;
143-
}
144-
}
145-
146-
lldb::SBValue FindVariable(DAP &dap, uint64_t variablesReference,
147-
llvm::StringRef name) {
148-
lldb::SBValue variable;
149-
if (lldb::SBValueList *top_scope =
150-
GetTopLevelScope(dap, variablesReference)) {
151-
bool is_duplicated_variable_name = name.contains(" @");
152-
// variablesReference is one of our scopes, not an actual variable it is
153-
// asking for a variable in locals or globals or registers
154-
int64_t end_idx = top_scope->GetSize();
155-
// Searching backward so that we choose the variable in closest scope
156-
// among variables of the same name.
157-
for (int64_t i = end_idx - 1; i >= 0; --i) {
158-
lldb::SBValue curr_variable = top_scope->GetValueAtIndex(i);
159-
std::string variable_name = CreateUniqueVariableNameForDisplay(
160-
curr_variable, is_duplicated_variable_name);
161-
if (variable_name == name) {
162-
variable = curr_variable;
163-
break;
164-
}
165-
}
166-
} else {
167-
// This is not under the globals or locals scope, so there are no duplicated
168-
// names.
169-
170-
// We have a named item within an actual variable so we need to find it
171-
// withing the container variable by name.
172-
lldb::SBValue container = dap.variables.GetVariable(variablesReference);
173-
variable = container.GetChildMemberWithName(name.data());
174-
if (!variable.IsValid()) {
175-
if (name.starts_with("[")) {
176-
llvm::StringRef index_str(name.drop_front(1));
177-
uint64_t index = 0;
178-
if (!index_str.consumeInteger(0, index)) {
179-
if (index_str == "]")
180-
variable = container.GetChildAtIndex(index);
181-
}
182-
}
183-
}
184-
}
185-
return variable;
186-
}
187-
188133
// Fill in the stack frames of the thread.
189134
//
190135
// Threads stacks may contain runtime specific extended backtraces, when
@@ -741,7 +686,7 @@ void request_setVariable(DAP &dap, const llvm::json::Object &request) {
741686
if (id_value != UINT64_MAX) {
742687
variable = dap.variables.GetVariable(id_value);
743688
} else {
744-
variable = FindVariable(dap, variablesReference, name);
689+
variable = dap.variables.FindVariable(variablesReference, name);
745690
}
746691

747692
if (variable.IsValid()) {
@@ -867,7 +812,7 @@ void request_variables(DAP &dap, const llvm::json::Object &request) {
867812
hex = GetBoolean(format, "hex", false);
868813

869814
if (lldb::SBValueList *top_scope =
870-
GetTopLevelScope(dap, variablesReference)) {
815+
dap.variables.GetTopLevelScope(variablesReference)) {
871816
// variablesReference is one of our scopes, not an actual variable it is
872817
// asking for the list of args, locals or globals.
873818
int64_t start_idx = 0;

0 commit comments

Comments
 (0)