Skip to content

Commit e8a01e7

Browse files
authored
[lldb] Make sure Blocks always have a parent (#117683)
It's basically true already (except for a brief time during construction). This patch makes sure the objects are constructed with a valid parent and enforces this in the type system, which allows us to get rid of some nullptr checks.
1 parent 0723870 commit e8a01e7

File tree

7 files changed

+36
-66
lines changed

7 files changed

+36
-66
lines changed

lldb/include/lldb/Symbol/Block.h

Lines changed: 11 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,13 @@ class Block : public UserID, public SymbolContextScope {
4343
typedef RangeVector<uint32_t, uint32_t, 1> RangeList;
4444
typedef RangeList::Entry Range;
4545

46-
/// Construct with a User ID \a uid, \a depth.
47-
///
48-
/// Initialize this block with the specified UID \a uid. The \a depth in the
49-
/// \a block_list is used to represent the parent, sibling, and child block
50-
/// information and also allows for partial parsing at the block level.
46+
// Creates a block representing the whole function. Only meant to be used from
47+
// the Function class.
48+
Block(Function &function, lldb::user_id_t function_uid);
49+
50+
~Block() override;
51+
52+
/// Creates a block with the specified UID \a uid.
5153
///
5254
/// \param[in] uid
5355
/// The UID for a given block. This value is given by the
@@ -56,19 +58,7 @@ class Block : public UserID, public SymbolContextScope {
5658
/// information data that it parses for further or more in
5759
/// depth parsing. Common values would be the index into a
5860
/// table, or an offset into the debug information.
59-
///
60-
/// \see BlockList
61-
Block(lldb::user_id_t uid);
62-
63-
/// Destructor.
64-
~Block() override;
65-
66-
/// Add a child to this object.
67-
///
68-
/// \param[in] child_block_sp
69-
/// A shared pointer to a child block that will get added to
70-
/// this block.
71-
void AddChild(const lldb::BlockSP &child_block_sp);
61+
lldb::BlockSP CreateChild(lldb::user_id_t uid);
7262

7363
/// Add a new offset range to this block.
7464
void AddRange(const Range &range);
@@ -317,10 +307,6 @@ class Block : public UserID, public SymbolContextScope {
317307
const Declaration *decl_ptr,
318308
const Declaration *call_decl_ptr);
319309

320-
void SetParentScope(SymbolContextScope *parent_scope) {
321-
m_parent_scope = parent_scope;
322-
}
323-
324310
/// Set accessor for the variable list.
325311
///
326312
/// Called by the SymbolFile plug-ins after they have parsed the variable
@@ -364,7 +350,7 @@ class Block : public UserID, public SymbolContextScope {
364350
protected:
365351
typedef std::vector<lldb::BlockSP> collection;
366352
// Member variables.
367-
SymbolContextScope *m_parent_scope;
353+
SymbolContextScope &m_parent_scope;
368354
collection m_children;
369355
RangeList m_ranges;
370356
lldb::InlineFunctionInfoSP m_inlineInfoSP; ///< Inlined function information.
@@ -382,6 +368,8 @@ class Block : public UserID, public SymbolContextScope {
382368
private:
383369
Block(const Block &) = delete;
384370
const Block &operator=(const Block &) = delete;
371+
372+
Block(lldb::user_id_t uid, SymbolContextScope &parent_scope);
385373
};
386374

387375
} // namespace lldb_private

lldb/source/Plugins/SymbolFile/Breakpad/SymbolFileBreakpad.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -315,7 +315,8 @@ size_t SymbolFileBreakpad::ParseBlocksRecursive(Function &func) {
315315
if (record->InlineNestLevel == 0 ||
316316
record->InlineNestLevel <= last_added_nest_level + 1) {
317317
last_added_nest_level = record->InlineNestLevel;
318-
BlockSP block_sp = std::make_shared<Block>(It.GetBookmark().offset);
318+
BlockSP block_sp = blocks[record->InlineNestLevel]->CreateChild(
319+
It.GetBookmark().offset);
319320
FileSpec callsite_file;
320321
if (record->CallSiteFileNum < m_files->size())
321322
callsite_file = (*m_files)[record->CallSiteFileNum];
@@ -333,7 +334,6 @@ size_t SymbolFileBreakpad::ParseBlocksRecursive(Function &func) {
333334
}
334335
block_sp->FinalizeRanges();
335336

336-
blocks[record->InlineNestLevel]->AddChild(block_sp);
337337
if (record->InlineNestLevel + 1 >= blocks.size()) {
338338
blocks.resize(blocks.size() + 1);
339339
}

lldb/source/Plugins/SymbolFile/DWARF/SymbolFileDWARF.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1328,9 +1328,7 @@ size_t SymbolFileDWARF::ParseBlocksRecursive(
13281328

13291329
block = parent_block;
13301330
} else {
1331-
BlockSP block_sp(new Block(die.GetID()));
1332-
parent_block->AddChild(block_sp);
1333-
block = block_sp.get();
1331+
block = parent_block->CreateChild(die.GetID()).get();
13341332
}
13351333
DWARFRangeList ranges;
13361334
const char *name = nullptr;

lldb/source/Plugins/SymbolFile/NativePDB/SymbolFileNativePDB.cpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -424,7 +424,7 @@ Block *SymbolFileNativePDB::CreateBlock(PdbCompilandSymId block_id) {
424424
m_index->MakeVirtualAddress(block.Segment, block.CodeOffset);
425425
lldb::addr_t func_base =
426426
func->GetAddressRange().GetBaseAddress().GetFileAddress();
427-
BlockSP child_block = std::make_shared<Block>(opaque_block_uid);
427+
BlockSP child_block = parent_block->CreateChild(opaque_block_uid);
428428
if (block_base >= func_base)
429429
child_block->AddRange(Block::Range(block_base - func_base, block.CodeSize));
430430
else {
@@ -437,7 +437,6 @@ Block *SymbolFileNativePDB::CreateBlock(PdbCompilandSymId block_id) {
437437
block_id.modi, block_id.offset, block_base,
438438
block_base + block.CodeSize, func_base);
439439
}
440-
parent_block->AddChild(child_block);
441440
ast_builder->GetOrCreateBlockDecl(block_id);
442441
m_blocks.insert({opaque_block_uid, child_block});
443442
break;
@@ -450,8 +449,7 @@ Block *SymbolFileNativePDB::CreateBlock(PdbCompilandSymId block_id) {
450449
Block *parent_block = GetOrCreateBlock(inline_site->parent_id);
451450
if (!parent_block)
452451
return nullptr;
453-
BlockSP child_block = std::make_shared<Block>(opaque_block_uid);
454-
parent_block->AddChild(child_block);
452+
BlockSP child_block = parent_block->CreateChild(opaque_block_uid);
455453
ast_builder->GetOrCreateInlinedFunctionDecl(block_id);
456454
// Copy ranges from InlineSite to Block.
457455
for (size_t i = 0; i < inline_site->ranges.GetSize(); ++i) {

lldb/source/Plugins/SymbolFile/PDB/SymbolFilePDB.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -421,9 +421,7 @@ static size_t ParseFunctionBlocksForPDBSymbol(
421421
if (raw_sym.getVirtualAddress() < func_file_vm_addr)
422422
break;
423423

424-
auto block_sp = std::make_shared<Block>(pdb_symbol->getSymIndexId());
425-
parent_block->AddChild(block_sp);
426-
block = block_sp.get();
424+
block = parent_block->CreateChild(pdb_symbol->getSymIndexId()).get();
427425
} else
428426
llvm_unreachable("Unexpected PDB symbol!");
429427

lldb/source/Symbol/Block.cpp

Lines changed: 18 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,11 @@
2121
using namespace lldb;
2222
using namespace lldb_private;
2323

24-
Block::Block(lldb::user_id_t uid)
25-
: UserID(uid), m_parent_scope(nullptr), m_children(), m_ranges(),
26-
m_inlineInfoSP(), m_variable_list_sp(), m_parsed_block_info(false),
24+
Block::Block(Function &function, user_id_t function_uid)
25+
: Block(function_uid, function) {}
26+
27+
Block::Block(lldb::user_id_t uid, SymbolContextScope &parent_scope)
28+
: UserID(uid), m_parent_scope(parent_scope), m_parsed_block_info(false),
2729
m_parsed_block_variables(false), m_parsed_child_blocks(false) {}
2830

2931
Block::~Block() = default;
@@ -134,27 +136,20 @@ Block *Block::FindInnermostBlockByOffset(const lldb::addr_t offset) {
134136
}
135137

136138
void Block::CalculateSymbolContext(SymbolContext *sc) {
137-
if (m_parent_scope)
138-
m_parent_scope->CalculateSymbolContext(sc);
139+
m_parent_scope.CalculateSymbolContext(sc);
139140
sc->block = this;
140141
}
141142

142143
lldb::ModuleSP Block::CalculateSymbolContextModule() {
143-
if (m_parent_scope)
144-
return m_parent_scope->CalculateSymbolContextModule();
145-
return lldb::ModuleSP();
144+
return m_parent_scope.CalculateSymbolContextModule();
146145
}
147146

148147
CompileUnit *Block::CalculateSymbolContextCompileUnit() {
149-
if (m_parent_scope)
150-
return m_parent_scope->CalculateSymbolContextCompileUnit();
151-
return nullptr;
148+
return m_parent_scope.CalculateSymbolContextCompileUnit();
152149
}
153150

154151
Function *Block::CalculateSymbolContextFunction() {
155-
if (m_parent_scope)
156-
return m_parent_scope->CalculateSymbolContextFunction();
157-
return nullptr;
152+
return m_parent_scope.CalculateSymbolContextFunction();
158153
}
159154

160155
Block *Block::CalculateSymbolContextBlock() { return this; }
@@ -200,9 +195,7 @@ bool Block::Contains(const Range &range) const {
200195
}
201196

202197
Block *Block::GetParent() const {
203-
if (m_parent_scope)
204-
return m_parent_scope->CalculateSymbolContextBlock();
205-
return nullptr;
198+
return m_parent_scope.CalculateSymbolContextBlock();
206199
}
207200

208201
Block *Block::GetContainingInlinedBlock() {
@@ -354,8 +347,8 @@ void Block::AddRange(const Range &range) {
354347
if (parent_block && !parent_block->Contains(range)) {
355348
Log *log = GetLog(LLDBLog::Symbols);
356349
if (log) {
357-
ModuleSP module_sp(m_parent_scope->CalculateSymbolContextModule());
358-
Function *function = m_parent_scope->CalculateSymbolContextFunction();
350+
ModuleSP module_sp(m_parent_scope.CalculateSymbolContextModule());
351+
Function *function = m_parent_scope.CalculateSymbolContextFunction();
359352
const addr_t function_file_addr =
360353
function->GetAddressRange().GetBaseAddress().GetFileAddress();
361354
const addr_t block_start_addr = function_file_addr + range.GetRangeBase();
@@ -399,11 +392,9 @@ size_t Block::MemorySize() const {
399392
return mem_size;
400393
}
401394

402-
void Block::AddChild(const BlockSP &child_block_sp) {
403-
if (child_block_sp) {
404-
child_block_sp->SetParentScope(this);
405-
m_children.push_back(child_block_sp);
406-
}
395+
BlockSP Block::CreateChild(user_id_t uid) {
396+
m_children.push_back(std::shared_ptr<Block>(new Block(uid, *this)));
397+
return m_children.back();
407398
}
408399

409400
void Block::SetInlinedFunctionInfo(const char *name, const char *mangled,
@@ -520,13 +511,11 @@ void Block::SetDidParseVariables(bool b, bool set_children) {
520511
}
521512

522513
Block *Block::GetSibling() const {
523-
if (m_parent_scope) {
524-
Block *parent_block = GetParent();
525-
if (parent_block)
526-
return parent_block->GetSiblingForChild(this);
527-
}
514+
if (Block *parent_block = GetParent())
515+
return parent_block->GetSiblingForChild(this);
528516
return nullptr;
529517
}
518+
530519
// A parent of child blocks can be asked to find a sibling block given
531520
// one of its child blocks
532521
Block *Block::GetSiblingForChild(const Block *child_block) const {

lldb/source/Symbol/Function.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -278,10 +278,9 @@ Function::Function(CompileUnit *comp_unit, lldb::user_id_t func_uid,
278278
lldb::user_id_t type_uid, const Mangled &mangled, Type *type,
279279
AddressRanges ranges)
280280
: UserID(func_uid), m_comp_unit(comp_unit), m_type_uid(type_uid),
281-
m_type(type), m_mangled(mangled), m_block(func_uid),
281+
m_type(type), m_mangled(mangled), m_block(*this, func_uid),
282282
m_ranges(std::move(ranges)), m_range(CollapseRanges(m_ranges)),
283283
m_frame_base(), m_flags(), m_prologue_byte_size(0) {
284-
m_block.SetParentScope(this);
285284
assert(comp_unit != nullptr);
286285
}
287286

0 commit comments

Comments
 (0)