[Lldb-commits] [lldb] [lldb][nfc] Factor out code checking if Variable is in scope (PR #143572)
Felipe de Azevedo Piovezan via lldb-commits
lldb-commits at lists.llvm.org
Tue Jun 10 14:56:04 PDT 2025
https://github.com/felipepiovezan updated https://github.com/llvm/llvm-project/pull/143572
>From 9821fd7c4530819f131271f4325123a400ecded4 Mon Sep 17 00:00:00 2001
From: Felipe de Azevedo Piovezan <fpiovezan at apple.com>
Date: Tue, 10 Jun 2025 10:26:52 -0700
Subject: [PATCH 1/2] [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.
---
lldb/include/lldb/Symbol/Variable.h | 3 ++
lldb/source/Symbol/Variable.cpp | 46 +++++++++++++++--------------
2 files changed, 27 insertions(+), 22 deletions(-)
diff --git a/lldb/include/lldb/Symbol/Variable.h b/lldb/include/lldb/Symbol/Variable.h
index c437624d1ea6d..b867369576a41 100644
--- a/lldb/include/lldb/Symbol/Variable.h
+++ b/lldb/include/lldb/Symbol/Variable.h
@@ -89,6 +89,9 @@ class Variable : public UserID, public std::enable_shared_from_this<Variable> {
bool IsInScope(StackFrame *frame);
+ /// Returns true if this variable is in scope at `addr` inside `block`.
+ bool IsInScope(Block &block, Address addr);
+
bool LocationIsValidForFrame(StackFrame *frame);
bool LocationIsValidForAddress(const Address &address);
diff --git a/lldb/source/Symbol/Variable.cpp b/lldb/source/Symbol/Variable.cpp
index 8244725aba545..0415758dd268a 100644
--- a/lldb/source/Symbol/Variable.cpp
+++ b/lldb/source/Symbol/Variable.cpp
@@ -290,28 +290,9 @@ bool Variable::IsInScope(StackFrame *frame) {
// this variable was defined in is currently
Block *deepest_frame_block =
frame->GetSymbolContext(eSymbolContextBlock).block;
- if (deepest_frame_block) {
- SymbolContext variable_sc;
- CalculateSymbolContext(&variable_sc);
-
- // Check for static or global variable defined at the compile unit
- // level that wasn't defined in a block
- if (variable_sc.block == nullptr)
- return true;
-
- // Check if the variable is valid in the current block
- if (variable_sc.block != deepest_frame_block &&
- !variable_sc.block->Contains(deepest_frame_block))
- return false;
-
- // If no scope range is specified then it means that the scope is the
- // same as the scope of the enclosing lexical block.
- if (m_scope_range.IsEmpty())
- return true;
-
- addr_t file_address = frame->GetFrameCodeAddress().GetFileAddress();
- return m_scope_range.FindEntryThatContains(file_address) != nullptr;
- }
+ Address frame_addr = frame->GetFrameCodeAddress();
+ if (deepest_frame_block)
+ return IsInScope(*deepest_frame_block, frame_addr);
}
break;
@@ -321,6 +302,27 @@ bool Variable::IsInScope(StackFrame *frame) {
return false;
}
+bool Variable::IsInScope(Block &block, Address addr) {
+ SymbolContext variable_sc;
+ CalculateSymbolContext(&variable_sc);
+
+ // Check for static or global variable defined at the compile unit
+ // level that wasn't defined in a block
+ if (variable_sc.block == nullptr)
+ return true;
+
+ // Check if the variable is valid in the current block
+ if (variable_sc.block != &block && !variable_sc.block->Contains(&block))
+ return false;
+
+ // If no scope range is specified then it means that the scope is the
+ // same as the scope of the enclosing lexical block.
+ if (m_scope_range.IsEmpty())
+ return true;
+
+ return m_scope_range.FindEntryThatContains(addr.GetFileAddress()) != nullptr;
+}
+
Status Variable::GetValuesForVariableExpressionPath(
llvm::StringRef variable_expr_path, ExecutionContextScope *scope,
GetVariableCallback callback, void *baton, VariableList &variable_list,
>From 2fe9bb4855d092bf77b8f6c6ba274c4d67860ea4 Mon Sep 17 00:00:00 2001
From: Felipe de Azevedo Piovezan <fpiovezan at apple.com>
Date: Tue, 10 Jun 2025 14:55:53 -0700
Subject: [PATCH 2/2] fixup! pass by const reference
---
lldb/include/lldb/Symbol/Variable.h | 2 +-
lldb/source/Symbol/Variable.cpp | 2 +-
2 files changed, 2 insertions(+), 2 deletions(-)
diff --git a/lldb/include/lldb/Symbol/Variable.h b/lldb/include/lldb/Symbol/Variable.h
index b867369576a41..8229b38e4f32d 100644
--- a/lldb/include/lldb/Symbol/Variable.h
+++ b/lldb/include/lldb/Symbol/Variable.h
@@ -90,7 +90,7 @@ class Variable : public UserID, public std::enable_shared_from_this<Variable> {
bool IsInScope(StackFrame *frame);
/// Returns true if this variable is in scope at `addr` inside `block`.
- bool IsInScope(Block &block, Address addr);
+ bool IsInScope(const Block &block, Address addr);
bool LocationIsValidForFrame(StackFrame *frame);
diff --git a/lldb/source/Symbol/Variable.cpp b/lldb/source/Symbol/Variable.cpp
index 0415758dd268a..5df89efc68007 100644
--- a/lldb/source/Symbol/Variable.cpp
+++ b/lldb/source/Symbol/Variable.cpp
@@ -302,7 +302,7 @@ bool Variable::IsInScope(StackFrame *frame) {
return false;
}
-bool Variable::IsInScope(Block &block, Address addr) {
+bool Variable::IsInScope(const Block &block, Address addr) {
SymbolContext variable_sc;
CalculateSymbolContext(&variable_sc);
More information about the lldb-commits
mailing list