[lldb] Remove Function null checks in Block.cpp (#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.
This commit is contained in:
Pavel Labath
2025-04-29 08:20:08 +02:00
committed by GitHub
parent ebaeecc429
commit 679cc0a1ed
2 changed files with 43 additions and 47 deletions

View File

@@ -78,6 +78,8 @@ public:
Block *CalculateSymbolContextBlock() override;
Function &GetFunction();
/// Check if an offset is in one of the block offset ranges.
///
/// \param[in] range_offset

View File

@@ -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);
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;
}
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());
}
}