From de448c0a9e5088979526e2e67152fe547ae4ccf0 Mon Sep 17 00:00:00 2001 From: Jonas Devlieghere Date: Tue, 13 Jul 2021 10:34:27 -0700 Subject: [PATCH] [lldb] Make TargetList iterable (NFC) Make it possible to iterate over the TargetList. Differential revision: https://reviews.llvm.org/D105914 --- lldb/include/lldb/Target/TargetList.h | 15 +++++++++++---- lldb/source/Core/Debugger.cpp | 7 ++----- 2 files changed, 13 insertions(+), 9 deletions(-) diff --git a/lldb/include/lldb/Target/TargetList.h b/lldb/include/lldb/Target/TargetList.h index 903ca4bcefbc..65781a4811fd 100644 --- a/lldb/include/lldb/Target/TargetList.h +++ b/lldb/include/lldb/Target/TargetList.h @@ -14,6 +14,7 @@ #include "lldb/Target/Target.h" #include "lldb/Utility/Broadcaster.h" +#include "lldb/Utility/Iterable.h" namespace lldb_private { @@ -42,6 +43,11 @@ public: return GetStaticBroadcasterClass(); } + typedef std::vector collection; + typedef LockingAdaptedIterable + TargetIterable; + /// Create a new Target. /// /// Clients must use this function to create a Target. This allows @@ -179,14 +185,15 @@ public: lldb::TargetSP GetSelectedTarget(); -protected: - typedef std::vector collection; - // Member variables. + TargetIterable Targets() { + return TargetIterable(m_target_list, m_target_list_mutex); + } + +private: collection m_target_list; mutable std::recursive_mutex m_target_list_mutex; uint32_t m_selected_target_idx; -private: static Status CreateTargetInternal( Debugger &debugger, llvm::StringRef user_exe_path, llvm::StringRef triple_str, LoadDependentFiles load_dependent_files, diff --git a/lldb/source/Core/Debugger.cpp b/lldb/source/Core/Debugger.cpp index 12210ed541bc..17c3ba426f71 100644 --- a/lldb/source/Core/Debugger.cpp +++ b/lldb/source/Core/Debugger.cpp @@ -773,12 +773,9 @@ void Debugger::Clear() { StopIOHandlerThread(); StopEventHandlerThread(); m_listener_sp->Clear(); - int num_targets = m_target_list.GetNumTargets(); - for (int i = 0; i < num_targets; i++) { - TargetSP target_sp(m_target_list.GetTargetAtIndex(i)); + for (TargetSP target_sp : m_target_list.Targets()) { if (target_sp) { - ProcessSP process_sp(target_sp->GetProcessSP()); - if (process_sp) + if (ProcessSP process_sp = target_sp->GetProcessSP()) process_sp->Finalize(); target_sp->Destroy(); }