Skip to content

[lldb] Remove Function null checks in Block.cpp #137611

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 29, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions lldb/include/lldb/Symbol/Block.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
90 changes: 42 additions & 48 deletions lldb/source/Symbol/Block.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}

Expand Down Expand Up @@ -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;
Expand All @@ -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;

Expand All @@ -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());
}
Expand All @@ -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() {
Expand All @@ -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()) {
Expand All @@ -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,
Expand All @@ -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());
}
}
Expand Down
Loading