diff options
author | Alex Langford <alangford@apple.com> | 2023-05-04 13:26:58 -0700 |
---|---|---|
committer | Alex Langford <alangford@apple.com> | 2023-05-04 16:36:44 -0700 |
commit | 04aa943be8ed5c03092e2a90112ac638360ec253 (patch) | |
tree | 45c211c4b200c275522d16e07890e7fc1a05e8ea /lldb/source/Commands/CommandObjectSource.cpp | |
parent | 4c9c1a4e4f854b2a4891813b2b1d7e1079a52a62 (diff) | |
download | llvm-04aa943be8ed5c03092e2a90112ac638360ec253.zip llvm-04aa943be8ed5c03092e2a90112ac638360ec253.tar.gz llvm-04aa943be8ed5c03092e2a90112ac638360ec253.tar.bz2 |
[lldb] Expose a const iterator for SymbolContextList
There are many situations where we'll iterate over a SymbolContextList
with the pattern:
```
SymbolContextList sc_list;
// Fill in sc_list here
for (auto i = 0; i < sc_list.GetSize(); i++) {
SymbolContext sc;
sc_list.GetSymbolAtContext(i, sc);
// Do work with sc
}
```
Adding an iterator to iterate over the instances directly means we don't
have to do bounds checking or create a copy of every element of the
SymbolContextList.
Differential Revision: https://reviews.llvm.org/D149900
Diffstat (limited to 'lldb/source/Commands/CommandObjectSource.cpp')
-rw-r--r-- | lldb/source/Commands/CommandObjectSource.cpp | 81 |
1 files changed, 22 insertions, 59 deletions
diff --git a/lldb/source/Commands/CommandObjectSource.cpp b/lldb/source/Commands/CommandObjectSource.cpp index 44561cf..bbc3142 100644 --- a/lldb/source/Commands/CommandObjectSource.cpp +++ b/lldb/source/Commands/CommandObjectSource.cpp @@ -146,10 +146,7 @@ protected: uint32_t num_matches = 0; // Dump all the line entries for the file in the list. ConstString last_module_file_name; - uint32_t num_scs = sc_list.GetSize(); - for (uint32_t i = 0; i < num_scs; ++i) { - SymbolContext sc; - sc_list.GetContextAtIndex(i, sc); + for (const SymbolContext &sc : sc_list) { if (sc.comp_unit) { Module *module = sc.module_sp.get(); CompileUnit *cu = sc.comp_unit; @@ -393,10 +390,7 @@ protected: SymbolContextList sc_list_symbols; module_list.FindFunctionSymbols(name, eFunctionNameTypeAuto, sc_list_symbols); - size_t num_symbol_matches = sc_list_symbols.GetSize(); - for (size_t i = 0; i < num_symbol_matches; i++) { - SymbolContext sc; - sc_list_symbols.GetContextAtIndex(i, sc); + for (const SymbolContext &sc : sc_list_symbols) { if (sc.symbol && sc.symbol->ValueIsAddress()) { const Address &base_address = sc.symbol->GetAddressRef(); Function *function = base_address.CalculateSymbolContextFunction(); @@ -412,9 +406,7 @@ protected: m_options.symbol_name.c_str()); return false; } - for (size_t i = 0; i < num_matches; i++) { - SymbolContext sc; - sc_list_funcs.GetContextAtIndex(i, sc); + for (const SymbolContext &sc : sc_list_funcs) { bool context_found_for_symbol = false; // Loop through all the ranges in the function. AddressRange range; @@ -926,69 +918,45 @@ protected: // Displaying the source for a symbol. Search for function named name. FindMatchingFunctions(target, name, sc_list); - size_t num_matches = sc_list.GetSize(); - if (!num_matches) { + if (sc_list.GetSize() == 0) { // If we didn't find any functions with that name, try searching for // symbols that line up exactly with function addresses. SymbolContextList sc_list_symbols; FindMatchingFunctionSymbols(target, name, sc_list_symbols); - size_t num_symbol_matches = sc_list_symbols.GetSize(); - - for (size_t i = 0; i < num_symbol_matches; i++) { - SymbolContext sc; - sc_list_symbols.GetContextAtIndex(i, sc); + for (const SymbolContext &sc : sc_list_symbols) { if (sc.symbol && sc.symbol->ValueIsAddress()) { const Address &base_address = sc.symbol->GetAddressRef(); Function *function = base_address.CalculateSymbolContextFunction(); if (function) { sc_list.Append(SymbolContext(function)); - num_matches++; break; } } } } - if (num_matches == 0) { + if (sc_list.GetSize() == 0) { result.AppendErrorWithFormat("Could not find function named: \"%s\".\n", m_options.symbol_name.c_str()); return false; } - if (num_matches > 1) { - std::set<SourceInfo> source_match_set; - - bool displayed_something = false; - for (size_t i = 0; i < num_matches; i++) { - SymbolContext sc; - sc_list.GetContextAtIndex(i, sc); - SourceInfo source_info(sc.GetFunctionName(), - sc.GetFunctionStartLineEntry()); - - if (source_info.IsValid()) { - if (source_match_set.find(source_info) == source_match_set.end()) { - source_match_set.insert(source_info); - if (DisplayFunctionSource(sc, source_info, result)) - displayed_something = true; - } - } - } - - if (displayed_something) - result.SetStatus(eReturnStatusSuccessFinishResult); - else - result.SetStatus(eReturnStatusFailed); - } else { - SymbolContext sc; - sc_list.GetContextAtIndex(0, sc); - SourceInfo source_info; - - if (DisplayFunctionSource(sc, source_info, result)) { - result.SetStatus(eReturnStatusSuccessFinishResult); - } else { - result.SetStatus(eReturnStatusFailed); + std::set<SourceInfo> source_match_set; + bool displayed_something = false; + for (const SymbolContext &sc : sc_list) { + SourceInfo source_info(sc.GetFunctionName(), + sc.GetFunctionStartLineEntry()); + if (source_info.IsValid() && + source_match_set.find(source_info) == source_match_set.end()) { + source_match_set.insert(source_info); + if (DisplayFunctionSource(sc, source_info, result)) + displayed_something = true; } } + if (displayed_something) + result.SetStatus(eReturnStatusSuccessFinishResult); + else + result.SetStatus(eReturnStatusFailed); return result.Succeeded(); } else if (m_options.address != LLDB_INVALID_ADDRESS) { Address so_addr; @@ -1052,10 +1020,7 @@ protected: return false; } } - uint32_t num_matches = sc_list.GetSize(); - for (uint32_t i = 0; i < num_matches; ++i) { - SymbolContext sc; - sc_list.GetContextAtIndex(i, sc); + for (const SymbolContext &sc : sc_list) { if (sc.comp_unit) { if (m_options.show_bp_locs) { m_breakpoint_locations.Clear(); @@ -1175,9 +1140,7 @@ protected: bool got_multiple = false; CompileUnit *test_cu = nullptr; - for (unsigned i = 0; i < num_matches; i++) { - SymbolContext sc; - sc_list.GetContextAtIndex(i, sc); + for (const SymbolContext &sc : sc_list) { if (sc.comp_unit) { if (test_cu) { if (test_cu != sc.comp_unit) |