Skip to content

Commit 145b1b0

Browse files
[lldb][nfc] Factor out code checking if Variable is in scope (#143572)
This is useful for checking whether a variable is in scope inside a specific block.
1 parent 469922f commit 145b1b0

File tree

2 files changed

+27
-22
lines changed

2 files changed

+27
-22
lines changed

lldb/include/lldb/Symbol/Variable.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,9 @@ class Variable : public UserID, public std::enable_shared_from_this<Variable> {
8989

9090
bool IsInScope(StackFrame *frame);
9191

92+
/// Returns true if this variable is in scope at `addr` inside `block`.
93+
bool IsInScope(const Block &block, const Address &addr);
94+
9295
bool LocationIsValidForFrame(StackFrame *frame);
9396

9497
bool LocationIsValidForAddress(const Address &address);

lldb/source/Symbol/Variable.cpp

Lines changed: 24 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -290,28 +290,9 @@ bool Variable::IsInScope(StackFrame *frame) {
290290
// this variable was defined in is currently
291291
Block *deepest_frame_block =
292292
frame->GetSymbolContext(eSymbolContextBlock).block;
293-
if (deepest_frame_block) {
294-
SymbolContext variable_sc;
295-
CalculateSymbolContext(&variable_sc);
296-
297-
// Check for static or global variable defined at the compile unit
298-
// level that wasn't defined in a block
299-
if (variable_sc.block == nullptr)
300-
return true;
301-
302-
// Check if the variable is valid in the current block
303-
if (variable_sc.block != deepest_frame_block &&
304-
!variable_sc.block->Contains(deepest_frame_block))
305-
return false;
306-
307-
// If no scope range is specified then it means that the scope is the
308-
// same as the scope of the enclosing lexical block.
309-
if (m_scope_range.IsEmpty())
310-
return true;
311-
312-
addr_t file_address = frame->GetFrameCodeAddress().GetFileAddress();
313-
return m_scope_range.FindEntryThatContains(file_address) != nullptr;
314-
}
293+
Address frame_addr = frame->GetFrameCodeAddress();
294+
if (deepest_frame_block)
295+
return IsInScope(*deepest_frame_block, frame_addr);
315296
}
316297
break;
317298

@@ -321,6 +302,27 @@ bool Variable::IsInScope(StackFrame *frame) {
321302
return false;
322303
}
323304

305+
bool Variable::IsInScope(const Block &block, const Address &addr) {
306+
SymbolContext variable_sc;
307+
CalculateSymbolContext(&variable_sc);
308+
309+
// Check for static or global variable defined at the compile unit
310+
// level that wasn't defined in a block
311+
if (variable_sc.block == nullptr)
312+
return true;
313+
314+
// Check if the variable is valid in the current block
315+
if (variable_sc.block != &block && !variable_sc.block->Contains(&block))
316+
return false;
317+
318+
// If no scope range is specified then it means that the scope is the
319+
// same as the scope of the enclosing lexical block.
320+
if (m_scope_range.IsEmpty())
321+
return true;
322+
323+
return m_scope_range.FindEntryThatContains(addr.GetFileAddress()) != nullptr;
324+
}
325+
324326
Status Variable::GetValuesForVariableExpressionPath(
325327
llvm::StringRef variable_expr_path, ExecutionContextScope *scope,
326328
GetVariableCallback callback, void *baton, VariableList &variable_list,

0 commit comments

Comments
 (0)