Author: Tatyana Krasnukha Date: 2020-12-12T16:40:58+03:00 New Revision: 7832d7e95ace589b2275bafe701ccb377a16b1b2
URL: https://github.com/llvm/llvm-project/commit/7832d7e95ace589b2275bafe701ccb377a16b1b2 DIFF: https://github.com/llvm/llvm-project/commit/7832d7e95ace589b2275bafe701ccb377a16b1b2.diff LOG: [lldb] Modernize TargetList for-loops, NFC Replace loops with standard algorithms or range-based loops. Added: Modified: lldb/source/Target/TargetList.cpp Removed: ################################################################################ diff --git a/lldb/source/Target/TargetList.cpp b/lldb/source/Target/TargetList.cpp index bbef3b63ba42..5bb6ca2a73e9 100644 --- a/lldb/source/Target/TargetList.cpp +++ b/lldb/source/Target/TargetList.cpp @@ -405,80 +405,76 @@ Status TargetList::CreateTargetInternal(Debugger &debugger, bool TargetList::DeleteTarget(TargetSP &target_sp) { std::lock_guard<std::recursive_mutex> guard(m_target_list_mutex); - collection::iterator pos, end = m_target_list.end(); + auto it = std::find(m_target_list.begin(), m_target_list.end(), target_sp); + if (it == m_target_list.end()) + return false; - for (pos = m_target_list.begin(); pos != end; ++pos) { - if (pos->get() == target_sp.get()) { - m_target_list.erase(pos); - return true; - } - } - return false; + m_target_list.erase(it); + return true; } TargetSP TargetList::FindTargetWithExecutableAndArchitecture( const FileSpec &exe_file_spec, const ArchSpec *exe_arch_ptr) const { std::lock_guard<std::recursive_mutex> guard(m_target_list_mutex); - TargetSP target_sp; - collection::const_iterator pos, end = m_target_list.end(); - for (pos = m_target_list.begin(); pos != end; ++pos) { - Module *exe_module = (*pos)->GetExecutableModulePointer(); - - if (exe_module) { - if (FileSpec::Match(exe_file_spec, exe_module->GetFileSpec())) { - if (exe_arch_ptr) { - if (!exe_arch_ptr->IsCompatibleMatch(exe_module->GetArchitecture())) - continue; - } - target_sp = *pos; - break; - } - } - } - return target_sp; + auto it = std::find_if(m_target_list.begin(), m_target_list.end(), + [&exe_file_spec, exe_arch_ptr](const TargetSP &item) { + Module *exe_module = item->GetExecutableModulePointer(); + if (!exe_module || + !FileSpec::Match(exe_file_spec, exe_module->GetFileSpec())) + return false; + + return !exe_arch_ptr || + exe_arch_ptr->IsCompatibleMatch(exe_module->GetArchitecture()); + }); + + if (it != m_target_list.end()) + return *it; + + return TargetSP(); } TargetSP TargetList::FindTargetWithProcessID(lldb::pid_t pid) const { std::lock_guard<std::recursive_mutex> guard(m_target_list_mutex); - TargetSP target_sp; - collection::const_iterator pos, end = m_target_list.end(); - for (pos = m_target_list.begin(); pos != end; ++pos) { - Process *process = (*pos)->GetProcessSP().get(); - if (process && process->GetID() == pid) { - target_sp = *pos; - break; - } - } - return target_sp; + auto it = std::find_if(m_target_list.begin(), m_target_list.end(), + [pid](const TargetSP &item) { + auto *process_ptr = item->GetProcessSP().get(); + return process_ptr && (process_ptr->GetID() == pid); + }); + + if (it != m_target_list.end()) + return *it; + + return TargetSP(); } TargetSP TargetList::FindTargetWithProcess(Process *process) const { TargetSP target_sp; - if (process) { - std::lock_guard<std::recursive_mutex> guard(m_target_list_mutex); - collection::const_iterator pos, end = m_target_list.end(); - for (pos = m_target_list.begin(); pos != end; ++pos) { - if (process == (*pos)->GetProcessSP().get()) { - target_sp = *pos; - break; - } - } - } + if (!process) + return target_sp; + + std::lock_guard<std::recursive_mutex> guard(m_target_list_mutex); + auto it = std::find_if(m_target_list.begin(), m_target_list.end(), + [process](const TargetSP &item) { + return item->GetProcessSP().get() == process; + }); + + if (it != m_target_list.end()) + target_sp = *it; + return target_sp; } TargetSP TargetList::GetTargetSP(Target *target) const { TargetSP target_sp; - if (target) { - std::lock_guard<std::recursive_mutex> guard(m_target_list_mutex); - collection::const_iterator pos, end = m_target_list.end(); - for (pos = m_target_list.begin(); pos != end; ++pos) { - if (target == (*pos).get()) { - target_sp = *pos; - break; - } - } - } + if (!target) + return target_sp; + + std::lock_guard<std::recursive_mutex> guard(m_target_list_mutex); + auto it = std::find_if(m_target_list.begin(), m_target_list.end(), + [target](const TargetSP &item) { return item.get() == target; }); + if (it != m_target_list.end()) + target_sp = *it; + return target_sp; } @@ -509,14 +505,11 @@ uint32_t TargetList::SignalIfRunning(lldb::pid_t pid, int signo) { if (pid == LLDB_INVALID_PROCESS_ID) { // Signal all processes with signal std::lock_guard<std::recursive_mutex> guard(m_target_list_mutex); - collection::iterator pos, end = m_target_list.end(); - for (pos = m_target_list.begin(); pos != end; ++pos) { - process = (*pos)->GetProcessSP().get(); - if (process) { - if (process->IsAlive()) { - ++num_signals_sent; - process->Signal(signo); - } + for (const auto &target_sp : m_target_list) { + process = target_sp->GetProcessSP().get(); + if (process && process->IsAlive()) { + ++num_signals_sent; + process->Signal(signo); } } } else { @@ -524,11 +517,9 @@ uint32_t TargetList::SignalIfRunning(lldb::pid_t pid, int signo) { TargetSP target_sp(FindTargetWithProcessID(pid)); if (target_sp) { process = target_sp->GetProcessSP().get(); - if (process) { - if (process->IsAlive()) { - ++num_signals_sent; - process->Signal(signo); - } + if (process && process->IsAlive()) { + ++num_signals_sent; + process->Signal(signo); } } } @@ -550,11 +541,9 @@ lldb::TargetSP TargetList::GetTargetAtIndex(uint32_t idx) const { uint32_t TargetList::GetIndexOfTarget(lldb::TargetSP target_sp) const { std::lock_guard<std::recursive_mutex> guard(m_target_list_mutex); - size_t num_targets = m_target_list.size(); - for (size_t idx = 0; idx < num_targets; idx++) { - if (target_sp == m_target_list[idx]) - return idx; - } + auto it = std::find(m_target_list.begin(), m_target_list.end(), target_sp); + if (it != m_target_list.end()) + return std::distance(m_target_list.begin(), it); return UINT32_MAX; } _______________________________________________ lldb-commits mailing list lldb-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/lldb-commits