| Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1 | //===-- Block.cpp -----------------------------------------------*- C++ -*-===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | |
| 10 | #include "lldb/Symbol/Block.h" |
| Greg Clayton | 6c7f561 | 2011-09-29 23:41:34 +0000 | [diff] [blame] | 11 | |
| Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 12 | #include "lldb/Core/Module.h" |
| 13 | #include "lldb/Core/Section.h" |
| Greg Clayton | 6c7f561 | 2011-09-29 23:41:34 +0000 | [diff] [blame] | 14 | #include "lldb/Symbol/Function.h" |
| Sean Callanan | 72e4940 | 2011-08-05 23:43:37 +0000 | [diff] [blame] | 15 | #include "lldb/Symbol/SymbolFile.h" |
| Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 16 | #include "lldb/Symbol/SymbolVendor.h" |
| 17 | #include "lldb/Symbol/VariableList.h" |
| Zachary Turner | 6f9e690 | 2017-03-03 20:56:28 +0000 | [diff] [blame] | 18 | #include "lldb/Utility/Log.h" |
| Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 19 | |
| 20 | using namespace lldb; |
| 21 | using namespace lldb_private; |
| 22 | |
| Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 23 | Block::Block(lldb::user_id_t uid) |
| 24 | : UserID(uid), m_parent_scope(nullptr), m_children(), m_ranges(), |
| 25 | m_inlineInfoSP(), m_variable_list_sp(), m_parsed_block_info(false), |
| 26 | m_parsed_block_variables(false), m_parsed_child_blocks(false) {} |
| 27 | |
| 28 | Block::~Block() {} |
| 29 | |
| 30 | void Block::GetDescription(Stream *s, Function *function, |
| 31 | lldb::DescriptionLevel level, Target *target) const { |
| 32 | *s << "id = " << ((const UserID &)*this); |
| 33 | |
| 34 | size_t num_ranges = m_ranges.GetSize(); |
| 35 | if (num_ranges > 0) { |
| 36 | |
| 37 | addr_t base_addr = LLDB_INVALID_ADDRESS; |
| 38 | if (target) |
| 39 | base_addr = |
| 40 | function->GetAddressRange().GetBaseAddress().GetLoadAddress(target); |
| 41 | if (base_addr == LLDB_INVALID_ADDRESS) |
| 42 | base_addr = function->GetAddressRange().GetBaseAddress().GetFileAddress(); |
| 43 | |
| 44 | s->Printf(", range%s = ", num_ranges > 1 ? "s" : ""); |
| 45 | for (size_t i = 0; i < num_ranges; ++i) { |
| 46 | const Range &range = m_ranges.GetEntryRef(i); |
| 47 | s->AddressRange(base_addr + range.GetRangeBase(), |
| 48 | base_addr + range.GetRangeEnd(), 4); |
| 49 | } |
| 50 | } |
| 51 | |
| 52 | if (m_inlineInfoSP.get() != nullptr) { |
| 53 | bool show_fullpaths = (level == eDescriptionLevelVerbose); |
| 54 | m_inlineInfoSP->Dump(s, show_fullpaths); |
| 55 | } |
| Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 56 | } |
| 57 | |
| Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 58 | void Block::Dump(Stream *s, addr_t base_addr, int32_t depth, |
| 59 | bool show_context) const { |
| 60 | if (depth < 0) { |
| 61 | Block *parent = GetParent(); |
| 62 | if (parent) { |
| Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 63 | // We have a depth that is less than zero, print our parent blocks first |
| Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 64 | parent->Dump(s, base_addr, depth + 1, show_context); |
| 65 | } |
| 66 | } |
| Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 67 | |
| Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 68 | s->Printf("%p: ", static_cast<const void *>(this)); |
| 69 | s->Indent(); |
| 70 | *s << "Block" << static_cast<const UserID &>(*this); |
| 71 | const Block *parent_block = GetParent(); |
| 72 | if (parent_block) { |
| 73 | s->Printf(", parent = {0x%8.8" PRIx64 "}", parent_block->GetID()); |
| 74 | } |
| 75 | if (m_inlineInfoSP.get() != nullptr) { |
| 76 | bool show_fullpaths = false; |
| 77 | m_inlineInfoSP->Dump(s, show_fullpaths); |
| 78 | } |
| 79 | |
| 80 | if (!m_ranges.IsEmpty()) { |
| 81 | *s << ", ranges ="; |
| Greg Clayton | c980066 | 2010-09-10 01:30:46 +0000 | [diff] [blame] | 82 | |
| Greg Clayton | ea3e7d5 | 2011-10-08 00:49:15 +0000 | [diff] [blame] | 83 | size_t num_ranges = m_ranges.GetSize(); |
| Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 84 | for (size_t i = 0; i < num_ranges; ++i) { |
| 85 | const Range &range = m_ranges.GetEntryRef(i); |
| 86 | if (parent_block != nullptr && parent_block->Contains(range) == false) |
| 87 | *s << '!'; |
| 88 | else |
| 89 | *s << ' '; |
| 90 | s->AddressRange(base_addr + range.GetRangeBase(), |
| 91 | base_addr + range.GetRangeEnd(), 4); |
| 92 | } |
| 93 | } |
| 94 | s->EOL(); |
| Greg Clayton | 0c5cd90 | 2010-06-28 21:30:43 +0000 | [diff] [blame] | 95 | |
| Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 96 | if (depth > 0) { |
| 97 | s->IndentMore(); |
| 98 | |
| 99 | if (m_variable_list_sp.get()) { |
| 100 | m_variable_list_sp->Dump(s, show_context); |
| Greg Clayton | 0c5cd90 | 2010-06-28 21:30:43 +0000 | [diff] [blame] | 101 | } |
| Greg Clayton | 0c5cd90 | 2010-06-28 21:30:43 +0000 | [diff] [blame] | 102 | |
| Greg Clayton | 624784a | 2011-09-29 21:19:25 +0000 | [diff] [blame] | 103 | collection::const_iterator pos, end = m_children.end(); |
| 104 | for (pos = m_children.begin(); pos != end; ++pos) |
| Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 105 | (*pos)->Dump(s, base_addr, depth - 1, show_context); |
| 106 | |
| 107 | s->IndentLess(); |
| 108 | } |
| Greg Clayton | 0b76a2c | 2010-08-21 02:22:51 +0000 | [diff] [blame] | 109 | } |
| 110 | |
| Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 111 | Block *Block::FindBlockByID(user_id_t block_id) { |
| 112 | if (block_id == GetID()) |
| Greg Clayton | 7e9b1fd | 2011-08-12 21:40:01 +0000 | [diff] [blame] | 113 | return this; |
| Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 114 | |
| 115 | Block *matching_block = nullptr; |
| 116 | collection::const_iterator pos, end = m_children.end(); |
| 117 | for (pos = m_children.begin(); pos != end; ++pos) { |
| 118 | matching_block = (*pos)->FindBlockByID(block_id); |
| 119 | if (matching_block) |
| 120 | break; |
| 121 | } |
| 122 | return matching_block; |
| Greg Clayton | 7e9b1fd | 2011-08-12 21:40:01 +0000 | [diff] [blame] | 123 | } |
| 124 | |
| Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 125 | void Block::CalculateSymbolContext(SymbolContext *sc) { |
| 126 | if (m_parent_scope) |
| 127 | m_parent_scope->CalculateSymbolContext(sc); |
| 128 | sc->block = this; |
| 129 | } |
| 130 | |
| 131 | lldb::ModuleSP Block::CalculateSymbolContextModule() { |
| 132 | if (m_parent_scope) |
| 133 | return m_parent_scope->CalculateSymbolContextModule(); |
| 134 | return lldb::ModuleSP(); |
| 135 | } |
| 136 | |
| 137 | CompileUnit *Block::CalculateSymbolContextCompileUnit() { |
| 138 | if (m_parent_scope) |
| 139 | return m_parent_scope->CalculateSymbolContextCompileUnit(); |
| 140 | return nullptr; |
| 141 | } |
| 142 | |
| 143 | Function *Block::CalculateSymbolContextFunction() { |
| 144 | if (m_parent_scope) |
| 145 | return m_parent_scope->CalculateSymbolContextFunction(); |
| 146 | return nullptr; |
| 147 | } |
| 148 | |
| 149 | Block *Block::CalculateSymbolContextBlock() { return this; } |
| 150 | |
| 151 | void Block::DumpSymbolContext(Stream *s) { |
| 152 | Function *function = CalculateSymbolContextFunction(); |
| 153 | if (function) |
| 154 | function->DumpSymbolContext(s); |
| 155 | s->Printf(", Block{0x%8.8" PRIx64 "}", GetID()); |
| 156 | } |
| 157 | |
| 158 | void Block::DumpAddressRanges(Stream *s, lldb::addr_t base_addr) { |
| 159 | if (!m_ranges.IsEmpty()) { |
| 160 | size_t num_ranges = m_ranges.GetSize(); |
| 161 | for (size_t i = 0; i < num_ranges; ++i) { |
| 162 | const Range &range = m_ranges.GetEntryRef(i); |
| 163 | s->AddressRange(base_addr + range.GetRangeBase(), |
| 164 | base_addr + range.GetRangeEnd(), 4); |
| 165 | } |
| 166 | } |
| 167 | } |
| 168 | |
| 169 | bool Block::Contains(addr_t range_offset) const { |
| 170 | return m_ranges.FindEntryThatContains(range_offset) != nullptr; |
| 171 | } |
| 172 | |
| 173 | bool Block::Contains(const Block *block) const { |
| 174 | if (this == block) |
| 175 | return false; // This block doesn't contain itself... |
| 176 | |
| 177 | // Walk the parent chain for "block" and see if any if them match this block |
| 178 | const Block *block_parent; |
| 179 | for (block_parent = block->GetParent(); block_parent != nullptr; |
| 180 | block_parent = block_parent->GetParent()) { |
| 181 | if (this == block_parent) |
| 182 | return true; // One of the parents of "block" is this object! |
| 183 | } |
| 184 | return false; |
| 185 | } |
| 186 | |
| 187 | bool Block::Contains(const Range &range) const { |
| 188 | return m_ranges.FindEntryThatContains(range) != nullptr; |
| 189 | } |
| 190 | |
| 191 | Block *Block::GetParent() const { |
| 192 | if (m_parent_scope) |
| 193 | return m_parent_scope->CalculateSymbolContextBlock(); |
| 194 | return nullptr; |
| 195 | } |
| 196 | |
| 197 | Block *Block::GetContainingInlinedBlock() { |
| 198 | if (GetInlinedFunctionInfo()) |
| 199 | return this; |
| 200 | return GetInlinedParent(); |
| 201 | } |
| 202 | |
| 203 | Block *Block::GetInlinedParent() { |
| 204 | Block *parent_block = GetParent(); |
| 205 | if (parent_block) { |
| 206 | if (parent_block->GetInlinedFunctionInfo()) |
| 207 | return parent_block; |
| 208 | else |
| 209 | return parent_block->GetInlinedParent(); |
| 210 | } |
| 211 | return nullptr; |
| 212 | } |
| 213 | |
| 214 | bool Block::GetRangeContainingOffset(const addr_t offset, Range &range) { |
| 215 | const Range *range_ptr = m_ranges.FindEntryThatContains(offset); |
| 216 | if (range_ptr) { |
| 217 | range = *range_ptr; |
| 218 | return true; |
| 219 | } |
| 220 | range.Clear(); |
| 221 | return false; |
| 222 | } |
| 223 | |
| 224 | bool Block::GetRangeContainingAddress(const Address &addr, |
| 225 | AddressRange &range) { |
| 226 | Function *function = CalculateSymbolContextFunction(); |
| 227 | if (function) { |
| 228 | const AddressRange &func_range = function->GetAddressRange(); |
| 229 | if (addr.GetSection() == func_range.GetBaseAddress().GetSection()) { |
| 230 | const addr_t addr_offset = addr.GetOffset(); |
| 231 | const addr_t func_offset = func_range.GetBaseAddress().GetOffset(); |
| 232 | if (addr_offset >= func_offset && |
| 233 | addr_offset < func_offset + func_range.GetByteSize()) { |
| 234 | addr_t offset = addr_offset - func_offset; |
| 235 | |
| 236 | const Range *range_ptr = m_ranges.FindEntryThatContains(offset); |
| 237 | |
| 238 | if (range_ptr) { |
| 239 | range.GetBaseAddress() = func_range.GetBaseAddress(); |
| 240 | range.GetBaseAddress().SetOffset(func_offset + |
| 241 | range_ptr->GetRangeBase()); |
| 242 | range.SetByteSize(range_ptr->GetByteSize()); |
| 243 | return true; |
| 244 | } |
| 245 | } |
| 246 | } |
| 247 | } |
| 248 | range.Clear(); |
| 249 | return false; |
| 250 | } |
| 251 | |
| 252 | bool Block::GetRangeContainingLoadAddress(lldb::addr_t load_addr, |
| 253 | Target &target, AddressRange &range) { |
| 254 | Address load_address; |
| 255 | load_address.SetLoadAddress(load_addr, &target); |
| 256 | AddressRange containing_range; |
| 257 | return GetRangeContainingAddress(load_address, containing_range); |
| 258 | } |
| 259 | |
| 260 | uint32_t Block::GetRangeIndexContainingAddress(const Address &addr) { |
| 261 | Function *function = CalculateSymbolContextFunction(); |
| 262 | if (function) { |
| 263 | const AddressRange &func_range = function->GetAddressRange(); |
| 264 | if (addr.GetSection() == func_range.GetBaseAddress().GetSection()) { |
| 265 | const addr_t addr_offset = addr.GetOffset(); |
| 266 | const addr_t func_offset = func_range.GetBaseAddress().GetOffset(); |
| 267 | if (addr_offset >= func_offset && |
| 268 | addr_offset < func_offset + func_range.GetByteSize()) { |
| 269 | addr_t offset = addr_offset - func_offset; |
| 270 | return m_ranges.FindEntryIndexThatContains(offset); |
| 271 | } |
| 272 | } |
| 273 | } |
| 274 | return UINT32_MAX; |
| 275 | } |
| 276 | |
| 277 | bool Block::GetRangeAtIndex(uint32_t range_idx, AddressRange &range) { |
| 278 | if (range_idx < m_ranges.GetSize()) { |
| Greg Clayton | 7e9b1fd | 2011-08-12 21:40:01 +0000 | [diff] [blame] | 279 | Function *function = CalculateSymbolContextFunction(); |
| Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 280 | if (function) { |
| 281 | const Range &vm_range = m_ranges.GetEntryRef(range_idx); |
| 282 | range.GetBaseAddress() = function->GetAddressRange().GetBaseAddress(); |
| 283 | range.GetBaseAddress().Slide(vm_range.GetRangeBase()); |
| 284 | range.SetByteSize(vm_range.GetByteSize()); |
| 285 | return true; |
| Caroline Tice | dde9cff | 2010-09-20 05:20:02 +0000 | [diff] [blame] | 286 | } |
| Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 287 | } |
| 288 | return false; |
| Caroline Tice | dde9cff | 2010-09-20 05:20:02 +0000 | [diff] [blame] | 289 | } |
| 290 | |
| Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 291 | bool Block::GetStartAddress(Address &addr) { |
| 292 | if (m_ranges.IsEmpty()) |
| Greg Clayton | 6dadd50 | 2010-09-02 21:44:10 +0000 | [diff] [blame] | 293 | return false; |
| Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 294 | |
| 295 | Function *function = CalculateSymbolContextFunction(); |
| 296 | if (function) { |
| 297 | addr = function->GetAddressRange().GetBaseAddress(); |
| 298 | addr.Slide(m_ranges.GetEntryRef(0).GetRangeBase()); |
| 299 | return true; |
| 300 | } |
| 301 | return false; |
| Greg Clayton | 6dadd50 | 2010-09-02 21:44:10 +0000 | [diff] [blame] | 302 | } |
| 303 | |
| Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 304 | void Block::FinalizeRanges() { |
| 305 | m_ranges.Sort(); |
| 306 | m_ranges.CombineConsecutiveRanges(); |
| Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 307 | } |
| 308 | |
| Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 309 | void Block::AddRange(const Range &range) { |
| 310 | Block *parent_block = GetParent(); |
| 311 | if (parent_block && !parent_block->Contains(range)) { |
| 312 | Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_SYMBOLS)); |
| 313 | if (log) { |
| 314 | ModuleSP module_sp(m_parent_scope->CalculateSymbolContextModule()); |
| 315 | Function *function = m_parent_scope->CalculateSymbolContextFunction(); |
| 316 | const addr_t function_file_addr = |
| 317 | function->GetAddressRange().GetBaseAddress().GetFileAddress(); |
| 318 | const addr_t block_start_addr = function_file_addr + range.GetRangeBase(); |
| 319 | const addr_t block_end_addr = function_file_addr + range.GetRangeEnd(); |
| 320 | Type *func_type = function->GetType(); |
| Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 321 | |
| Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 322 | const Declaration &func_decl = func_type->GetDeclaration(); |
| 323 | if (func_decl.GetLine()) { |
| 324 | log->Printf("warning: %s:%u block {0x%8.8" PRIx64 |
| 325 | "} has range[%u] [0x%" PRIx64 " - 0x%" PRIx64 |
| 326 | ") which is not contained in parent block {0x%8.8" PRIx64 |
| 327 | "} in function {0x%8.8" PRIx64 "} from %s", |
| 328 | func_decl.GetFile().GetPath().c_str(), func_decl.GetLine(), |
| 329 | GetID(), (uint32_t)m_ranges.GetSize(), block_start_addr, |
| 330 | block_end_addr, parent_block->GetID(), function->GetID(), |
| 331 | module_sp->GetFileSpec().GetPath().c_str()); |
| 332 | } else { |
| 333 | log->Printf("warning: block {0x%8.8" PRIx64 |
| 334 | "} has range[%u] [0x%" PRIx64 " - 0x%" PRIx64 |
| 335 | ") which is not contained in parent block {0x%8.8" PRIx64 |
| 336 | "} in function {0x%8.8" PRIx64 "} from %s", |
| 337 | GetID(), (uint32_t)m_ranges.GetSize(), block_start_addr, |
| 338 | block_end_addr, parent_block->GetID(), function->GetID(), |
| 339 | module_sp->GetFileSpec().GetPath().c_str()); |
| 340 | } |
| Greg Clayton | 1b72fcb | 2010-08-24 00:45:41 +0000 | [diff] [blame] | 341 | } |
| Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 342 | parent_block->AddRange(range); |
| 343 | } |
| 344 | m_ranges.Append(range); |
| Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 345 | } |
| 346 | |
| Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 347 | // Return the current number of bytes that this object occupies in memory |
| Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 348 | size_t Block::MemorySize() const { |
| 349 | size_t mem_size = sizeof(Block) + m_ranges.GetSize() * sizeof(Range); |
| 350 | if (m_inlineInfoSP.get()) |
| 351 | mem_size += m_inlineInfoSP->MemorySize(); |
| 352 | if (m_variable_list_sp.get()) |
| 353 | mem_size += m_variable_list_sp->MemorySize(); |
| 354 | return mem_size; |
| Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 355 | } |
| 356 | |
| Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 357 | void Block::AddChild(const BlockSP &child_block_sp) { |
| 358 | if (child_block_sp) { |
| 359 | child_block_sp->SetParentScope(this); |
| 360 | m_children.push_back(child_block_sp); |
| 361 | } |
| 362 | } |
| 363 | |
| 364 | void Block::SetInlinedFunctionInfo(const char *name, const char *mangled, |
| 365 | const Declaration *decl_ptr, |
| 366 | const Declaration *call_decl_ptr) { |
| 367 | m_inlineInfoSP.reset( |
| 368 | new InlineFunctionInfo(name, mangled, decl_ptr, call_decl_ptr)); |
| 369 | } |
| 370 | |
| 371 | VariableListSP Block::GetBlockVariableList(bool can_create) { |
| 372 | if (m_parsed_block_variables == false) { |
| 373 | if (m_variable_list_sp.get() == nullptr && can_create) { |
| 374 | m_parsed_block_variables = true; |
| 375 | SymbolContext sc; |
| 376 | CalculateSymbolContext(&sc); |
| 377 | assert(sc.module_sp); |
| 378 | sc.module_sp->GetSymbolVendor()->ParseVariablesForContext(sc); |
| Greg Clayton | 0b76a2c | 2010-08-21 02:22:51 +0000 | [diff] [blame] | 379 | } |
| Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 380 | } |
| 381 | return m_variable_list_sp; |
| Greg Clayton | c662ec8 | 2011-06-17 22:10:16 +0000 | [diff] [blame] | 382 | } |
| Jim Ingham | e1be14d | 2010-08-18 19:29:16 +0000 | [diff] [blame] | 383 | |
| Greg Clayton | c662ec8 | 2011-06-17 22:10:16 +0000 | [diff] [blame] | 384 | uint32_t |
| Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 385 | Block::AppendBlockVariables(bool can_create, bool get_child_block_variables, |
| 386 | bool stop_if_child_block_is_inlined_function, |
| 387 | const std::function<bool(Variable *)> &filter, |
| 388 | VariableList *variable_list) { |
| 389 | uint32_t num_variables_added = 0; |
| 390 | VariableList *block_var_list = GetBlockVariableList(can_create).get(); |
| 391 | if (block_var_list) { |
| 392 | for (size_t i = 0; i < block_var_list->GetSize(); ++i) { |
| 393 | VariableSP variable = block_var_list->GetVariableAtIndex(i); |
| 394 | if (filter(variable.get())) { |
| 395 | num_variables_added++; |
| 396 | variable_list->AddVariable(variable); |
| 397 | } |
| Greg Clayton | c662ec8 | 2011-06-17 22:10:16 +0000 | [diff] [blame] | 398 | } |
| Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 399 | } |
| Tamas Berghammer | 72ac8a8 | 2016-02-25 12:23:37 +0000 | [diff] [blame] | 400 | |
| Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 401 | if (get_child_block_variables) { |
| 402 | collection::const_iterator pos, end = m_children.end(); |
| 403 | for (pos = m_children.begin(); pos != end; ++pos) { |
| 404 | Block *child_block = pos->get(); |
| 405 | if (stop_if_child_block_is_inlined_function == false || |
| 406 | child_block->GetInlinedFunctionInfo() == nullptr) { |
| 407 | num_variables_added += child_block->AppendBlockVariables( |
| 408 | can_create, get_child_block_variables, |
| 409 | stop_if_child_block_is_inlined_function, filter, variable_list); |
| 410 | } |
| Jim Ingham | e1be14d | 2010-08-18 19:29:16 +0000 | [diff] [blame] | 411 | } |
| Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 412 | } |
| 413 | return num_variables_added; |
| Jim Ingham | e1be14d | 2010-08-18 19:29:16 +0000 | [diff] [blame] | 414 | } |
| 415 | |
| Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 416 | uint32_t Block::AppendVariables(bool can_create, bool get_parent_variables, |
| 417 | bool stop_if_block_is_inlined_function, |
| 418 | const std::function<bool(Variable *)> &filter, |
| 419 | VariableList *variable_list) { |
| 420 | uint32_t num_variables_added = 0; |
| 421 | VariableListSP variable_list_sp(GetBlockVariableList(can_create)); |
| Jim Ingham | e1be14d | 2010-08-18 19:29:16 +0000 | [diff] [blame] | 422 | |
| Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 423 | bool is_inlined_function = GetInlinedFunctionInfo() != nullptr; |
| 424 | if (variable_list_sp) { |
| 425 | for (size_t i = 0; i < variable_list_sp->GetSize(); ++i) { |
| 426 | VariableSP variable = variable_list_sp->GetVariableAtIndex(i); |
| 427 | if (filter(variable.get())) { |
| 428 | num_variables_added++; |
| 429 | variable_list->AddVariable(variable); |
| 430 | } |
| Jim Ingham | e1be14d | 2010-08-18 19:29:16 +0000 | [diff] [blame] | 431 | } |
| Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 432 | } |
| Tamas Berghammer | 72ac8a8 | 2016-02-25 12:23:37 +0000 | [diff] [blame] | 433 | |
| Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 434 | if (get_parent_variables) { |
| 435 | if (stop_if_block_is_inlined_function && is_inlined_function) |
| 436 | return num_variables_added; |
| 437 | |
| 438 | Block *parent_block = GetParent(); |
| 439 | if (parent_block) |
| 440 | num_variables_added += parent_block->AppendVariables( |
| 441 | can_create, get_parent_variables, stop_if_block_is_inlined_function, |
| 442 | filter, variable_list); |
| 443 | } |
| 444 | return num_variables_added; |
| Jim Ingham | e1be14d | 2010-08-18 19:29:16 +0000 | [diff] [blame] | 445 | } |
| 446 | |
| Vedant Kumar | 4b36f79 | 2018-10-05 23:23:15 +0000 | [diff] [blame^] | 447 | SymbolFile *Block::GetSymbolFile() { |
| 448 | if (ModuleSP module_sp = CalculateSymbolContextModule()) |
| 449 | if (SymbolVendor *sym_vendor = module_sp->GetSymbolVendor()) |
| 450 | return sym_vendor->GetSymbolFile(); |
| 451 | return nullptr; |
| 452 | } |
| 453 | |
| Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 454 | CompilerDeclContext Block::GetDeclContext() { |
| Vedant Kumar | 4b36f79 | 2018-10-05 23:23:15 +0000 | [diff] [blame^] | 455 | if (SymbolFile *sym_file = GetSymbolFile()) |
| 456 | return sym_file->GetDeclContextForUID(GetID()); |
| Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 457 | return CompilerDeclContext(); |
| Sean Callanan | 72e4940 | 2011-08-05 23:43:37 +0000 | [diff] [blame] | 458 | } |
| 459 | |
| Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 460 | void Block::SetBlockInfoHasBeenParsed(bool b, bool set_children) { |
| 461 | m_parsed_block_info = b; |
| 462 | if (set_children) { |
| 463 | m_parsed_child_blocks = true; |
| 464 | collection::const_iterator pos, end = m_children.end(); |
| 465 | for (pos = m_children.begin(); pos != end; ++pos) |
| 466 | (*pos)->SetBlockInfoHasBeenParsed(b, true); |
| 467 | } |
| Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 468 | } |
| Greg Clayton | c662ec8 | 2011-06-17 22:10:16 +0000 | [diff] [blame] | 469 | |
| Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 470 | void Block::SetDidParseVariables(bool b, bool set_children) { |
| 471 | m_parsed_block_variables = b; |
| 472 | if (set_children) { |
| 473 | collection::const_iterator pos, end = m_children.end(); |
| 474 | for (pos = m_children.begin(); pos != end; ++pos) |
| 475 | (*pos)->SetDidParseVariables(b, true); |
| 476 | } |
| Greg Clayton | c662ec8 | 2011-06-17 22:10:16 +0000 | [diff] [blame] | 477 | } |
| 478 | |
| Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 479 | Block *Block::GetSibling() const { |
| 480 | if (m_parent_scope) { |
| 481 | Block *parent_block = GetParent(); |
| 482 | if (parent_block) |
| 483 | return parent_block->GetSiblingForChild(this); |
| 484 | } |
| 485 | return nullptr; |
| Greg Clayton | 624784a | 2011-09-29 21:19:25 +0000 | [diff] [blame] | 486 | } |
| 487 | // A parent of child blocks can be asked to find a sibling block given |
| 488 | // one of its child blocks |
| Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 489 | Block *Block::GetSiblingForChild(const Block *child_block) const { |
| 490 | if (!m_children.empty()) { |
| 491 | collection::const_iterator pos, end = m_children.end(); |
| 492 | for (pos = m_children.begin(); pos != end; ++pos) { |
| 493 | if (pos->get() == child_block) { |
| 494 | if (++pos != end) |
| 495 | return pos->get(); |
| 496 | break; |
| 497 | } |
| Greg Clayton | 624784a | 2011-09-29 21:19:25 +0000 | [diff] [blame] | 498 | } |
| Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 499 | } |
| 500 | return nullptr; |
| Greg Clayton | 624784a | 2011-09-29 21:19:25 +0000 | [diff] [blame] | 501 | } |