[Lldb-commits] [lldb] de448c0 - [lldb] Make TargetList iterable (NFC)

Jonas Devlieghere via lldb-commits lldb-commits at lists.llvm.org
Wed Jul 14 13:36:03 PDT 2021


Author: Jonas Devlieghere
Date: 2021-07-14T13:35:54-07:00
New Revision: de448c0a9e5088979526e2e67152fe547ae4ccf0

URL: https://github.com/llvm/llvm-project/commit/de448c0a9e5088979526e2e67152fe547ae4ccf0
DIFF: https://github.com/llvm/llvm-project/commit/de448c0a9e5088979526e2e67152fe547ae4ccf0.diff

LOG: [lldb] Make TargetList iterable (NFC)

Make it possible to iterate over the TargetList.

Differential revision: https://reviews.llvm.org/D105914

Added: 
    

Modified: 
    lldb/include/lldb/Target/TargetList.h
    lldb/source/Core/Debugger.cpp

Removed: 
    


################################################################################
diff  --git a/lldb/include/lldb/Target/TargetList.h b/lldb/include/lldb/Target/TargetList.h
index 903ca4bcefbcb..65781a4811fd8 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 @@ class TargetList : public Broadcaster {
     return GetStaticBroadcasterClass();
   }
 
+  typedef std::vector<lldb::TargetSP> collection;
+  typedef LockingAdaptedIterable<collection, lldb::TargetSP, vector_adapter,
+                                 std::recursive_mutex>
+      TargetIterable;
+
   /// Create a new Target.
   ///
   /// Clients must use this function to create a Target. This allows
@@ -179,14 +185,15 @@ class TargetList : public Broadcaster {
 
   lldb::TargetSP GetSelectedTarget();
 
-protected:
-  typedef std::vector<lldb::TargetSP> 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 12210ed541bc1..17c3ba426f719 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();
       }


        


More information about the lldb-commits mailing list