Skip to content

Commit 13d04b2

Browse files
[lldb][nfc] Factor out code checking if Variable is in scope
This is useful for checking whether a variable is in scope inside a specific block.
1 parent 60f522c commit 13d04b2

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
@@ -91,6 +91,9 @@ class Variable : public UserID, public std::enable_shared_from_this<Variable> {
9191

9292
bool IsInScope(StackFrame *frame);
9393

94+
/// Returns true if this variable is in scope at `addr` inside `block`.
95+
bool IsInScope(const Block &block, Address addr);
96+
9497
bool LocationIsValidForFrame(StackFrame *frame);
9598

9699
bool LocationIsValidForAddress(const Address &address);

lldb/source/Symbol/Variable.cpp

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

@@ -322,6 +303,27 @@ bool Variable::IsInScope(StackFrame *frame) {
322303
return false;
323304
}
324305

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

0 commit comments

Comments
 (0)