[Lldb-commits] [lldb] [lldb] Remove Function null checks in Block.cpp (PR #137611)
Pavel Labath via lldb-commits
lldb-commits at lists.llvm.org
Mon Apr 28 03:12:18 PDT 2025
https://github.com/labath created https://github.com/llvm/llvm-project/pull/137611
As of #117683, Blocks are always enclosed in a function, so these checks never fail. We can't change the signature of
`CalculateSymbolContextFunction` as it's an abstract function (and some of its implementations can return nullptr), but we can create a different function that we can call when we know we're dealing with a block.
>From 3db443cb60b1ff580654583855798f87da5fbbe7 Mon Sep 17 00:00:00 2001
From: Pavel Labath <pavel at labath.sk>
Date: Mon, 28 Apr 2025 12:06:59 +0200
Subject: [PATCH] [lldb] Remove Function null checks in Block.cpp
As of #117683, Blocks are always enclosed in a function, so these checks
never fail. We can't change the signature of
`CalculateSymbolContextFunction` as it's an abstract function (and some
of its implementations can return nullptr), but we can create a
different function that we can call when we know we're dealing with a
block.
---
lldb/include/lldb/Symbol/Block.h | 2 +
lldb/source/Symbol/Block.cpp | 90 +++++++++++++++-----------------
2 files changed, 44 insertions(+), 48 deletions(-)
diff --git a/lldb/include/lldb/Symbol/Block.h b/lldb/include/lldb/Symbol/Block.h
index d0063f132cc0f..501c912b674ac 100644
--- a/lldb/include/lldb/Symbol/Block.h
+++ b/lldb/include/lldb/Symbol/Block.h
@@ -78,6 +78,8 @@ class Block : public UserID, public SymbolContextScope {
Block *CalculateSymbolContextBlock() override;
+ Function &GetFunction();
+
/// Check if an offset is in one of the block offset ranges.
///
/// \param[in] range_offset
diff --git a/lldb/source/Symbol/Block.cpp b/lldb/source/Symbol/Block.cpp
index 6ecc988d7a5a9..9d01293ea64e0 100644
--- a/lldb/source/Symbol/Block.cpp
+++ b/lldb/source/Symbol/Block.cpp
@@ -153,10 +153,16 @@ Function *Block::CalculateSymbolContextFunction() {
Block *Block::CalculateSymbolContextBlock() { return this; }
-void Block::DumpSymbolContext(Stream *s) {
+Function &Block::GetFunction() {
+ // Blocks always have an enclosing function because their parent is either a
+ // function or a block (which has a parent, inductively).
Function *function = CalculateSymbolContextFunction();
- if (function)
- function->DumpSymbolContext(s);
+ assert(function);
+ return *function;
+}
+
+void Block::DumpSymbolContext(Stream *s) {
+ GetFunction().DumpSymbolContext(s);
s->Printf(", Block{0x%8.8" PRIx64 "}", GetID());
}
@@ -241,20 +247,17 @@ bool Block::GetRangeContainingOffset(const addr_t offset, Range &range) {
bool Block::GetRangeContainingAddress(const Address &addr,
AddressRange &range) {
- Function *function = CalculateSymbolContextFunction();
- if (function) {
- if (uint32_t idx = GetRangeIndexContainingAddress(addr);
- idx != UINT32_MAX) {
- const Range *range_ptr = m_ranges.GetEntryAtIndex(idx);
- assert(range_ptr);
-
- Address func_addr = function->GetAddress();
- range.GetBaseAddress() =
- Address(func_addr.GetFileAddress() + range_ptr->GetRangeBase(),
- func_addr.GetModule()->GetSectionList());
- range.SetByteSize(range_ptr->GetByteSize());
- return true;
- }
+ Function &function = GetFunction();
+ if (uint32_t idx = GetRangeIndexContainingAddress(addr); idx != UINT32_MAX) {
+ const Range *range_ptr = m_ranges.GetEntryAtIndex(idx);
+ assert(range_ptr);
+
+ Address func_addr = function.GetAddress();
+ range.GetBaseAddress() =
+ Address(func_addr.GetFileAddress() + range_ptr->GetRangeBase(),
+ func_addr.GetModule()->GetSectionList());
+ range.SetByteSize(range_ptr->GetByteSize());
+ return true;
}
range.Clear();
return false;
@@ -269,11 +272,9 @@ bool Block::GetRangeContainingLoadAddress(lldb::addr_t load_addr,
}
uint32_t Block::GetRangeIndexContainingAddress(const Address &addr) {
- Function *function = CalculateSymbolContextFunction();
- if (!function)
- return UINT32_MAX;
+ Function &function = GetFunction();
- const Address &func_addr = function->GetAddress();
+ const Address &func_addr = function.GetAddress();
if (addr.GetModule() != func_addr.GetModule())
return UINT32_MAX;
@@ -283,29 +284,25 @@ uint32_t Block::GetRangeIndexContainingAddress(const Address &addr) {
}
bool Block::GetRangeAtIndex(uint32_t range_idx, AddressRange &range) {
- if (range_idx < m_ranges.GetSize()) {
- Function *function = CalculateSymbolContextFunction();
- if (function) {
- const Range &vm_range = m_ranges.GetEntryRef(range_idx);
- range.GetBaseAddress() = function->GetAddress();
- range.GetBaseAddress().Slide(vm_range.GetRangeBase());
- range.SetByteSize(vm_range.GetByteSize());
- return true;
- }
- }
- return false;
+ if (range_idx >= m_ranges.GetSize())
+ return false;
+
+ Function &function = GetFunction();
+ const Range &vm_range = m_ranges.GetEntryRef(range_idx);
+ range.GetBaseAddress() = function.GetAddress();
+ range.GetBaseAddress().Slide(vm_range.GetRangeBase());
+ range.SetByteSize(vm_range.GetByteSize());
+ return true;
}
AddressRanges Block::GetRanges() {
AddressRanges ranges;
- Function *function = CalculateSymbolContextFunction();
- if (!function)
- return ranges;
+ Function &function = GetFunction();
for (size_t i = 0, e = m_ranges.GetSize(); i < e; ++i) {
ranges.emplace_back();
auto &range = ranges.back();
const Range &vm_range = m_ranges.GetEntryRef(i);
- range.GetBaseAddress() = function->GetAddress();
+ range.GetBaseAddress() = function.GetAddress();
range.GetBaseAddress().Slide(vm_range.GetRangeBase());
range.SetByteSize(vm_range.GetByteSize());
}
@@ -316,13 +313,10 @@ bool Block::GetStartAddress(Address &addr) {
if (m_ranges.IsEmpty())
return false;
- Function *function = CalculateSymbolContextFunction();
- if (function) {
- addr = function->GetAddress();
- addr.Slide(m_ranges.GetEntryRef(0).GetRangeBase());
- return true;
- }
- return false;
+ Function &function = GetFunction();
+ addr = function.GetAddress();
+ addr.Slide(m_ranges.GetEntryRef(0).GetRangeBase());
+ return true;
}
void Block::FinalizeRanges() {
@@ -336,11 +330,11 @@ void Block::AddRange(const Range &range) {
Log *log = GetLog(LLDBLog::Symbols);
if (log) {
ModuleSP module_sp(m_parent_scope.CalculateSymbolContextModule());
- Function *function = m_parent_scope.CalculateSymbolContextFunction();
- const addr_t function_file_addr = function->GetAddress().GetFileAddress();
+ Function &function = GetFunction();
+ const addr_t function_file_addr = function.GetAddress().GetFileAddress();
const addr_t block_start_addr = function_file_addr + range.GetRangeBase();
const addr_t block_end_addr = function_file_addr + range.GetRangeEnd();
- Type *func_type = function->GetType();
+ Type *func_type = function.GetType();
const Declaration &func_decl = func_type->GetDeclaration();
if (func_decl.GetLine()) {
@@ -351,7 +345,7 @@ void Block::AddRange(const Range &range) {
"} in function {0x%8.8" PRIx64 "} from %s",
func_decl.GetFile().GetPath().c_str(), func_decl.GetLine(),
GetID(), (uint32_t)m_ranges.GetSize(), block_start_addr,
- block_end_addr, parent_block->GetID(), function->GetID(),
+ block_end_addr, parent_block->GetID(), function.GetID(),
module_sp->GetFileSpec().GetPath().c_str());
} else {
LLDB_LOGF(log,
@@ -360,7 +354,7 @@ void Block::AddRange(const Range &range) {
") which is not contained in parent block {0x%8.8" PRIx64
"} in function {0x%8.8" PRIx64 "} from %s",
GetID(), (uint32_t)m_ranges.GetSize(), block_start_addr,
- block_end_addr, parent_block->GetID(), function->GetID(),
+ block_end_addr, parent_block->GetID(), function.GetID(),
module_sp->GetFileSpec().GetPath().c_str());
}
}
More information about the lldb-commits
mailing list