Skip to content

Commit 679cc0a

Browse files
authored
[lldb] Remove Function null checks in Block.cpp (llvm#137611)
As of llvm#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.
1 parent ebaeecc commit 679cc0a

File tree

2 files changed

+44
-48
lines changed

2 files changed

+44
-48
lines changed

lldb/include/lldb/Symbol/Block.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,8 @@ class Block : public UserID, public SymbolContextScope {
7878

7979
Block *CalculateSymbolContextBlock() override;
8080

81+
Function &GetFunction();
82+
8183
/// Check if an offset is in one of the block offset ranges.
8284
///
8385
/// \param[in] range_offset

lldb/source/Symbol/Block.cpp

Lines changed: 42 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -153,10 +153,16 @@ Function *Block::CalculateSymbolContextFunction() {
153153

154154
Block *Block::CalculateSymbolContextBlock() { return this; }
155155

156-
void Block::DumpSymbolContext(Stream *s) {
156+
Function &Block::GetFunction() {
157+
// Blocks always have an enclosing function because their parent is either a
158+
// function or a block (which has a parent, inductively).
157159
Function *function = CalculateSymbolContextFunction();
158-
if (function)
159-
function->DumpSymbolContext(s);
160+
assert(function);
161+
return *function;
162+
}
163+
164+
void Block::DumpSymbolContext(Stream *s) {
165+
GetFunction().DumpSymbolContext(s);
160166
s->Printf(", Block{0x%8.8" PRIx64 "}", GetID());
161167
}
162168

@@ -241,20 +247,17 @@ bool Block::GetRangeContainingOffset(const addr_t offset, Range &range) {
241247

242248
bool Block::GetRangeContainingAddress(const Address &addr,
243249
AddressRange &range) {
244-
Function *function = CalculateSymbolContextFunction();
245-
if (function) {
246-
if (uint32_t idx = GetRangeIndexContainingAddress(addr);
247-
idx != UINT32_MAX) {
248-
const Range *range_ptr = m_ranges.GetEntryAtIndex(idx);
249-
assert(range_ptr);
250-
251-
Address func_addr = function->GetAddress();
252-
range.GetBaseAddress() =
253-
Address(func_addr.GetFileAddress() + range_ptr->GetRangeBase(),
254-
func_addr.GetModule()->GetSectionList());
255-
range.SetByteSize(range_ptr->GetByteSize());
256-
return true;
257-
}
250+
Function &function = GetFunction();
251+
if (uint32_t idx = GetRangeIndexContainingAddress(addr); idx != UINT32_MAX) {
252+
const Range *range_ptr = m_ranges.GetEntryAtIndex(idx);
253+
assert(range_ptr);
254+
255+
Address func_addr = function.GetAddress();
256+
range.GetBaseAddress() =
257+
Address(func_addr.GetFileAddress() + range_ptr->GetRangeBase(),
258+
func_addr.GetModule()->GetSectionList());
259+
range.SetByteSize(range_ptr->GetByteSize());
260+
return true;
258261
}
259262
range.Clear();
260263
return false;
@@ -269,11 +272,9 @@ bool Block::GetRangeContainingLoadAddress(lldb::addr_t load_addr,
269272
}
270273

271274
uint32_t Block::GetRangeIndexContainingAddress(const Address &addr) {
272-
Function *function = CalculateSymbolContextFunction();
273-
if (!function)
274-
return UINT32_MAX;
275+
Function &function = GetFunction();
275276

276-
const Address &func_addr = function->GetAddress();
277+
const Address &func_addr = function.GetAddress();
277278
if (addr.GetModule() != func_addr.GetModule())
278279
return UINT32_MAX;
279280

@@ -283,29 +284,25 @@ uint32_t Block::GetRangeIndexContainingAddress(const Address &addr) {
283284
}
284285

285286
bool Block::GetRangeAtIndex(uint32_t range_idx, AddressRange &range) {
286-
if (range_idx < m_ranges.GetSize()) {
287-
Function *function = CalculateSymbolContextFunction();
288-
if (function) {
289-
const Range &vm_range = m_ranges.GetEntryRef(range_idx);
290-
range.GetBaseAddress() = function->GetAddress();
291-
range.GetBaseAddress().Slide(vm_range.GetRangeBase());
292-
range.SetByteSize(vm_range.GetByteSize());
293-
return true;
294-
}
295-
}
296-
return false;
287+
if (range_idx >= m_ranges.GetSize())
288+
return false;
289+
290+
Function &function = GetFunction();
291+
const Range &vm_range = m_ranges.GetEntryRef(range_idx);
292+
range.GetBaseAddress() = function.GetAddress();
293+
range.GetBaseAddress().Slide(vm_range.GetRangeBase());
294+
range.SetByteSize(vm_range.GetByteSize());
295+
return true;
297296
}
298297

299298
AddressRanges Block::GetRanges() {
300299
AddressRanges ranges;
301-
Function *function = CalculateSymbolContextFunction();
302-
if (!function)
303-
return ranges;
300+
Function &function = GetFunction();
304301
for (size_t i = 0, e = m_ranges.GetSize(); i < e; ++i) {
305302
ranges.emplace_back();
306303
auto &range = ranges.back();
307304
const Range &vm_range = m_ranges.GetEntryRef(i);
308-
range.GetBaseAddress() = function->GetAddress();
305+
range.GetBaseAddress() = function.GetAddress();
309306
range.GetBaseAddress().Slide(vm_range.GetRangeBase());
310307
range.SetByteSize(vm_range.GetByteSize());
311308
}
@@ -316,13 +313,10 @@ bool Block::GetStartAddress(Address &addr) {
316313
if (m_ranges.IsEmpty())
317314
return false;
318315

319-
Function *function = CalculateSymbolContextFunction();
320-
if (function) {
321-
addr = function->GetAddress();
322-
addr.Slide(m_ranges.GetEntryRef(0).GetRangeBase());
323-
return true;
324-
}
325-
return false;
316+
Function &function = GetFunction();
317+
addr = function.GetAddress();
318+
addr.Slide(m_ranges.GetEntryRef(0).GetRangeBase());
319+
return true;
326320
}
327321

328322
void Block::FinalizeRanges() {
@@ -336,11 +330,11 @@ void Block::AddRange(const Range &range) {
336330
Log *log = GetLog(LLDBLog::Symbols);
337331
if (log) {
338332
ModuleSP module_sp(m_parent_scope.CalculateSymbolContextModule());
339-
Function *function = m_parent_scope.CalculateSymbolContextFunction();
340-
const addr_t function_file_addr = function->GetAddress().GetFileAddress();
333+
Function &function = GetFunction();
334+
const addr_t function_file_addr = function.GetAddress().GetFileAddress();
341335
const addr_t block_start_addr = function_file_addr + range.GetRangeBase();
342336
const addr_t block_end_addr = function_file_addr + range.GetRangeEnd();
343-
Type *func_type = function->GetType();
337+
Type *func_type = function.GetType();
344338

345339
const Declaration &func_decl = func_type->GetDeclaration();
346340
if (func_decl.GetLine()) {
@@ -351,7 +345,7 @@ void Block::AddRange(const Range &range) {
351345
"} in function {0x%8.8" PRIx64 "} from %s",
352346
func_decl.GetFile().GetPath().c_str(), func_decl.GetLine(),
353347
GetID(), (uint32_t)m_ranges.GetSize(), block_start_addr,
354-
block_end_addr, parent_block->GetID(), function->GetID(),
348+
block_end_addr, parent_block->GetID(), function.GetID(),
355349
module_sp->GetFileSpec().GetPath().c_str());
356350
} else {
357351
LLDB_LOGF(log,
@@ -360,7 +354,7 @@ void Block::AddRange(const Range &range) {
360354
") which is not contained in parent block {0x%8.8" PRIx64
361355
"} in function {0x%8.8" PRIx64 "} from %s",
362356
GetID(), (uint32_t)m_ranges.GetSize(), block_start_addr,
363-
block_end_addr, parent_block->GetID(), function->GetID(),
357+
block_end_addr, parent_block->GetID(), function.GetID(),
364358
module_sp->GetFileSpec().GetPath().c_str());
365359
}
366360
}

0 commit comments

Comments
 (0)