teemperor updated this revision to Diff 160245.
teemperor added a comment.

- Passing shared_ptr to lambda now as const-ref.


https://reviews.llvm.org/D50225

Files:
  include/lldb/Symbol/CompileUnit.h
  source/Core/Module.cpp
  source/Symbol/CompileUnit.cpp

Index: source/Symbol/CompileUnit.cpp
===================================================================
--- source/Symbol/CompileUnit.cpp
+++ source/Symbol/CompileUnit.cpp
@@ -22,7 +22,7 @@
                          lldb::LanguageType language,
                          lldb_private::LazyBool is_optimized)
     : ModuleChild(module_sp), FileSpec(pathname, false), UserID(cu_sym_id),
-      m_user_data(user_data), m_language(language), m_flags(0), m_functions(),
+      m_user_data(user_data), m_language(language), m_flags(0),
       m_support_files(), m_line_table_ap(), m_variables(),
       m_is_optimized(is_optimized) {
   if (language != eLanguageTypeUnknown)
@@ -35,7 +35,7 @@
                          lldb::LanguageType language,
                          lldb_private::LazyBool is_optimized)
     : ModuleChild(module_sp), FileSpec(fspec), UserID(cu_sym_id),
-      m_user_data(user_data), m_language(language), m_flags(0), m_functions(),
+      m_user_data(user_data), m_language(language), m_flags(0),
       m_support_files(), m_line_table_ap(), m_variables(),
       m_is_optimized(is_optimized) {
   if (language != eLanguageTypeUnknown)
@@ -66,6 +66,22 @@
      << (const FileSpec &)*this << "\", language = \"" << language << '"';
 }
 
+void CompileUnit::ForeachFunction(
+    llvm::function_ref<bool(const FunctionSP &)> lambda) const {
+  std::vector<lldb::FunctionSP> sorted_functions;
+  sorted_functions.reserve(m_functions_by_uid.size());
+  for (auto &p : m_functions_by_uid)
+    sorted_functions.push_back(p.second);
+  std::sort(sorted_functions.begin(), sorted_functions.end(),
+            [](const lldb::FunctionSP &a, const lldb::FunctionSP &b) {
+              return a->GetID() < b->GetID();
+            });
+
+  for (auto &f : sorted_functions)
+    if (lambda(f))
+      return;
+}
+
 //----------------------------------------------------------------------
 // Dump the current contents of this object. No functions that cause on demand
 // parsing of functions, globals, statics are called, so this is a good
@@ -89,13 +105,12 @@
     s->IndentLess();
   }
 
-  if (!m_functions.empty()) {
+  if (!m_functions_by_uid.empty()) {
     s->IndentMore();
-    std::vector<FunctionSP>::const_iterator pos;
-    std::vector<FunctionSP>::const_iterator end = m_functions.end();
-    for (pos = m_functions.begin(); pos != end; ++pos) {
-      (*pos)->Dump(s, show_context);
-    }
+    ForeachFunction([&s, show_context](const FunctionSP &f) {
+      f->Dump(s, show_context);
+      return false;
+    });
 
     s->IndentLess();
     s->EOL();
@@ -106,15 +121,7 @@
 // Add a function to this compile unit
 //----------------------------------------------------------------------
 void CompileUnit::AddFunction(FunctionSP &funcSP) {
-  // TODO: order these by address
-  m_functions.push_back(funcSP);
-}
-
-FunctionSP CompileUnit::GetFunctionAtIndex(size_t idx) {
-  FunctionSP funcSP;
-  if (idx < m_functions.size())
-    funcSP = m_functions[idx];
-  return funcSP;
+  m_functions_by_uid[funcSP->GetID()] = funcSP;
 }
 
 //----------------------------------------------------------------------
@@ -163,18 +170,10 @@
 //}
 
 FunctionSP CompileUnit::FindFunctionByUID(lldb::user_id_t func_uid) {
-  FunctionSP funcSP;
-  if (!m_functions.empty()) {
-    std::vector<FunctionSP>::const_iterator pos;
-    std::vector<FunctionSP>::const_iterator end = m_functions.end();
-    for (pos = m_functions.begin(); pos != end; ++pos) {
-      if ((*pos)->GetID() == func_uid) {
-        funcSP = *pos;
-        break;
-      }
-    }
-  }
-  return funcSP;
+  auto it = m_functions_by_uid.find(func_uid);
+  if (it == m_functions_by_uid.end())
+    return FunctionSP();
+  return it->second;
 }
 
 lldb::LanguageType CompileUnit::GetLanguage() {
Index: source/Core/Module.cpp
===================================================================
--- source/Core/Module.cpp
+++ source/Core/Module.cpp
@@ -371,15 +371,13 @@
 
       symbols->ParseCompileUnitFunctions(sc);
 
-      for (size_t func_idx = 0;
-           (sc.function = sc.comp_unit->GetFunctionAtIndex(func_idx).get()) !=
-           nullptr;
-           ++func_idx) {
+      sc.comp_unit->ForeachFunction([&sc, &symbols](const FunctionSP &f) {
+        sc.function = f.get();
         symbols->ParseFunctionBlocks(sc);
-
         // Parse the variables for this function and all its blocks
         symbols->ParseVariablesForContext(sc);
-      }
+        return false;
+      });
 
       // Parse all types for this compile unit
       sc.function = nullptr;
Index: include/lldb/Symbol/CompileUnit.h
===================================================================
--- include/lldb/Symbol/CompileUnit.h
+++ include/lldb/Symbol/CompileUnit.h
@@ -18,6 +18,8 @@
 #include "lldb/Utility/UserID.h"
 #include "lldb/lldb-enumerations.h"
 
+#include "llvm/ADT/DenseMap.h"
+
 namespace lldb_private {
 //----------------------------------------------------------------------
 /// @class CompileUnit CompileUnit.h "lldb/Symbol/CompileUnit.h"
@@ -163,21 +165,19 @@
   void GetDescription(Stream *s, lldb::DescriptionLevel level) const;
 
   //------------------------------------------------------------------
-  /// Get a shared pointer to a function in this compile unit by index.
+  /// Apply a lambda to each function in this compile unit.
   ///
-  /// Typically called when iterating though all functions in a compile unit
-  /// after all functions have been parsed. This provides raw access to the
-  /// function shared pointer list and will not cause the SymbolFile plug-in
-  /// to parse any unparsed functions.
+  /// This provides raw access to the function shared pointer list and will not
+  /// cause the SymbolFile plug-in to parse any unparsed functions.
   ///
-  /// @param[in] idx
-  ///     An index into the function list.
+  /// @note Prefer using FindFunctionByUID over this if possible.
   ///
-  /// @return
-  ///     A shared pointer to a function that might contain a NULL
-  ///     Function class pointer.
+  /// @param[in] lambda
+  ///     The lambda that should be applied to every function. The lambda can
+  ///     return true if the iteration should be aborted earlier.
   //------------------------------------------------------------------
-  lldb::FunctionSP GetFunctionAtIndex(size_t idx);
+  void ForeachFunction(
+      llvm::function_ref<bool(const lldb::FunctionSP &)> lambda) const;
 
   //------------------------------------------------------------------
   /// Dump the compile unit contents to the stream \a s.
@@ -415,9 +415,9 @@
   lldb::LanguageType
       m_language; ///< The programming language enumeration value.
   Flags m_flags;  ///< Compile unit flags that help with partial parsing.
-  std::vector<lldb::FunctionSP> m_functions; ///< The sparsely populated list of
-                                             ///shared pointers to functions
-  ///< that gets populated as functions get partially parsed.
+
+  /// Maps UIDs to functions.
+  llvm::DenseMap<lldb::user_id_t, lldb::FunctionSP> m_functions_by_uid;
   std::vector<ConstString> m_imported_modules; ///< All modules, including the
                                                ///current module, imported by
                                                ///this
_______________________________________________
lldb-commits mailing list
lldb-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/lldb-commits

Reply via email to