jankratochvil created this revision.
jankratochvil added reviewers: clayborg, labath, JDevlieghere.
jankratochvil added a project: LLDB.
Herald added a subscriber: abidh.

These two methods are very similar and various refactorizations need to modify 
both similar ways. Merge them as a template.

One could also just remove `GetCompileUnit` and make 
`GetCompileUnitContainingDIEOffset` to also accept offset of the CU itself 
(currently it accepts only DIE offsets after the CU header). But that would be 
less safe regarding some internal sanity checking.

The whole patchset available for testing as: `git clone -b debugtypes 
git://git.jankratochvil.net/lldb`


Repository:
  rLLDB LLDB

https://reviews.llvm.org/D61498

Files:
  lldb/source/Plugins/SymbolFile/DWARF/DWARFDebugInfo.cpp
  lldb/source/Plugins/SymbolFile/DWARF/DWARFDebugInfo.h

Index: lldb/source/Plugins/SymbolFile/DWARF/DWARFDebugInfo.h
===================================================================
--- lldb/source/Plugins/SymbolFile/DWARF/DWARFDebugInfo.h
+++ lldb/source/Plugins/SymbolFile/DWARF/DWARFDebugInfo.h
@@ -74,7 +74,46 @@
   // accessors are called.
   void ParseCompileUnitHeadersIfNeeded();
 
+  template<class Contains>
+  DWARFUnit *GetCompileUnitTmpl(
+      Contains contains, dw_offset_t offset, uint32_t *idx_ptr = NULL);
+
   DISALLOW_COPY_AND_ASSIGN(DWARFDebugInfo);
 };
 
+template<class Contains>
+DWARFUnit *DWARFDebugInfo::GetCompileUnitTmpl(
+    Contains contains, dw_offset_t offset, uint32_t *idx_ptr) {
+  ParseCompileUnitHeadersIfNeeded();
+
+  DWARFUnitSP cu_sp;
+
+  // Watch out for single compile unit executable as they are pretty common
+  const size_t num_cus = m_compile_units.size();
+  if (num_cus == 1) {
+    if (contains(*m_compile_units[0], offset)) {
+      if (idx_ptr)
+        *idx_ptr = 0;
+      return m_compile_units[0].get();
+    }
+  } else if (num_cus) {
+    CompileUnitColl::const_iterator end_pos = m_compile_units.end();
+    CompileUnitColl::const_iterator begin_pos = m_compile_units.begin();
+    CompileUnitColl::const_iterator pos = std::upper_bound(
+        begin_pos, end_pos, offset, OffsetLessThanCompileUnitOffset);
+    if (pos != begin_pos) {
+      --pos;
+      if (contains(**pos, offset)) {
+        if (idx_ptr)
+          *idx_ptr = pos - begin_pos;
+        return (*pos).get();
+      }
+    }
+  }
+
+  if (idx_ptr)
+    *idx_ptr = DW_INVALID_INDEX;
+  return nullptr;
+}
+
 #endif // SymbolFileDWARF_DWARFDebugInfo_h_
Index: lldb/source/Plugins/SymbolFile/DWARF/DWARFDebugInfo.cpp
===================================================================
--- lldb/source/Plugins/SymbolFile/DWARF/DWARFDebugInfo.cpp
+++ lldb/source/Plugins/SymbolFile/DWARF/DWARFDebugInfo.cpp
@@ -125,35 +125,10 @@
 
 DWARFUnit *DWARFDebugInfo::GetCompileUnit(dw_offset_t cu_offset,
                                                  uint32_t *idx_ptr) {
-  DWARFUnitSP cu_sp;
-  uint32_t cu_idx = DW_INVALID_INDEX;
-  if (cu_offset != DW_INVALID_OFFSET) {
-    ParseCompileUnitHeadersIfNeeded();
-
-    // Watch out for single compile unit executable as they are pretty common
-    const size_t num_cus = m_compile_units.size();
-    if (num_cus == 1) {
-      if (m_compile_units[0]->GetOffset() == cu_offset) {
-        cu_sp = m_compile_units[0];
-        cu_idx = 0;
-      }
-    } else if (num_cus) {
-      CompileUnitColl::const_iterator end_pos = m_compile_units.end();
-      CompileUnitColl::const_iterator begin_pos = m_compile_units.begin();
-      CompileUnitColl::const_iterator pos = std::upper_bound(
-          begin_pos, end_pos, cu_offset, OffsetLessThanCompileUnitOffset);
-      if (pos != begin_pos) {
-        --pos;
-        if ((*pos)->GetOffset() == cu_offset) {
-          cu_sp = *pos;
-          cu_idx = std::distance(begin_pos, pos);
-        }
-      }
-    }
-  }
-  if (idx_ptr)
-    *idx_ptr = cu_idx;
-  return cu_sp.get();
+  return GetCompileUnitTmpl(
+      [](const DWARFUnit &unit, dw_offset_t cu_offset) {
+        return unit.GetOffset() == cu_offset;
+      }, cu_offset, idx_ptr);
 }
 
 DWARFUnit *DWARFDebugInfo::GetCompileUnit(const DIERef &die_ref) {
@@ -165,28 +140,10 @@
 
 DWARFUnit *
 DWARFDebugInfo::GetCompileUnitContainingDIEOffset(dw_offset_t die_offset) {
-  ParseCompileUnitHeadersIfNeeded();
-
-  DWARFUnitSP cu_sp;
-
-  // Watch out for single compile unit executable as they are pretty common
-  const size_t num_cus = m_compile_units.size();
-  if (num_cus == 1) {
-    if (m_compile_units[0]->ContainsDIEOffset(die_offset))
-      return m_compile_units[0].get();
-  } else if (num_cus) {
-    CompileUnitColl::const_iterator end_pos = m_compile_units.end();
-    CompileUnitColl::const_iterator begin_pos = m_compile_units.begin();
-    CompileUnitColl::const_iterator pos = std::upper_bound(
-        begin_pos, end_pos, die_offset, OffsetLessThanCompileUnitOffset);
-    if (pos != begin_pos) {
-      --pos;
-      if ((*pos)->ContainsDIEOffset(die_offset))
-        return (*pos).get();
-    }
-  }
-
-  return nullptr;
+  return GetCompileUnitTmpl(
+      [](const DWARFUnit &unit, dw_offset_t die_offset) {
+        return unit.ContainsDIEOffset(die_offset);
+      }, die_offset);
 }
 
 DWARFDIE
_______________________________________________
lldb-commits mailing list
lldb-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/lldb-commits

Reply via email to