[Lldb-commits] [lldb] 346afb8 - [lldb][nfc] Refactor methods with out parameter

Felipe de Azevedo Piovezan via lldb-commits lldb-commits at lists.llvm.org
Thu May 25 10:38:43 PDT 2023


Author: Felipe de Azevedo Piovezan
Date: 2023-05-25T13:38:35-04:00
New Revision: 346afb8572280292139c22a00f80afbbfdb509f6

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

LOG: [lldb][nfc] Refactor methods with out parameter

Currently, the method `GetAttributeAddressRanges` takes a DWARFRangeList as a
parameter, just to immediately clear it. The method also returns the size of
this list. Such an API was obfuscating the intent of the call sites (it's not
clear from the method name what it returns) and it was obfuscating redundant
checks on the size of the list.

This commit refactors the method to return the list and to also make the call
sites use the more explicit `IsEmpty` method.

Differential Revision: https://reviews.llvm.org/D151451

Added: 
    

Modified: 
    lldb/source/Plugins/SymbolFile/DWARF/DWARFCompileUnit.cpp
    lldb/source/Plugins/SymbolFile/DWARF/DWARFDIE.cpp
    lldb/source/Plugins/SymbolFile/DWARF/DWARFDebugInfoEntry.cpp
    lldb/source/Plugins/SymbolFile/DWARF/DWARFDebugInfoEntry.h
    lldb/source/Plugins/SymbolFile/DWARF/SymbolFileDWARF.cpp

Removed: 
    


################################################################################
diff  --git a/lldb/source/Plugins/SymbolFile/DWARF/DWARFCompileUnit.cpp b/lldb/source/Plugins/SymbolFile/DWARF/DWARFCompileUnit.cpp
index dbe86c6ccb9ce..f839a59bf6c39 100644
--- a/lldb/source/Plugins/SymbolFile/DWARF/DWARFCompileUnit.cpp
+++ b/lldb/source/Plugins/SymbolFile/DWARF/DWARFCompileUnit.cpp
@@ -40,18 +40,14 @@ void DWARFCompileUnit::BuildAddressRangeTable(
 
   const dw_offset_t cu_offset = GetOffset();
   if (die) {
-    DWARFRangeList ranges;
-    const size_t num_ranges =
-        die->GetAttributeAddressRanges(this, ranges, /*check_hi_lo_pc=*/true);
-    if (num_ranges > 0) {
-      for (size_t i = 0; i < num_ranges; ++i) {
-        const DWARFRangeList::Entry &range = ranges.GetEntryRef(i);
-        debug_aranges->AppendRange(cu_offset, range.GetRangeBase(),
-                                   range.GetRangeEnd());
-      }
+    DWARFRangeList ranges =
+        die->GetAttributeAddressRanges(this, /*check_hi_lo_pc=*/true);
+    for (const DWARFRangeList::Entry &range : ranges)
+      debug_aranges->AppendRange(cu_offset, range.GetRangeBase(),
+                                 range.GetRangeEnd());
 
+    if (!ranges.IsEmpty())
       return;
-    }
   }
 
   if (debug_aranges->GetNumRanges() == num_debug_aranges) {

diff  --git a/lldb/source/Plugins/SymbolFile/DWARF/DWARFDIE.cpp b/lldb/source/Plugins/SymbolFile/DWARF/DWARFDIE.cpp
index 65fab503deb28..b31c5dcac9185 100644
--- a/lldb/source/Plugins/SymbolFile/DWARF/DWARFDIE.cpp
+++ b/lldb/source/Plugins/SymbolFile/DWARF/DWARFDIE.cpp
@@ -168,10 +168,9 @@ DWARFDIE::LookupDeepestBlock(lldb::addr_t address) const {
   }
 
   if (match_addr_range) {
-    DWARFRangeList ranges;
-    if (m_die->GetAttributeAddressRanges(m_cu, ranges,
-                                         /*check_hi_lo_pc=*/true) &&
-        ranges.FindEntryThatContains(address)) {
+    DWARFRangeList ranges =
+        m_die->GetAttributeAddressRanges(m_cu, /*check_hi_lo_pc=*/true);
+    if (ranges.FindEntryThatContains(address)) {
       check_children = true;
       switch (Tag()) {
       default:

diff  --git a/lldb/source/Plugins/SymbolFile/DWARF/DWARFDebugInfoEntry.cpp b/lldb/source/Plugins/SymbolFile/DWARF/DWARFDebugInfoEntry.cpp
index a929046ca14aa..f26fb2f4c9a4d 100644
--- a/lldb/source/Plugins/SymbolFile/DWARF/DWARFDebugInfoEntry.cpp
+++ b/lldb/source/Plugins/SymbolFile/DWARF/DWARFDebugInfoEntry.cpp
@@ -636,15 +636,16 @@ bool DWARFDebugInfoEntry::GetAttributeAddressRange(
   return false;
 }
 
-size_t DWARFDebugInfoEntry::GetAttributeAddressRanges(
-    DWARFUnit *cu, DWARFRangeList &ranges, bool check_hi_lo_pc,
+DWARFRangeList DWARFDebugInfoEntry::GetAttributeAddressRanges(
+    DWARFUnit *cu, bool check_hi_lo_pc,
     bool check_specification_or_abstract_origin) const {
-  ranges.Clear();
 
   DWARFFormValue form_value;
-  if (GetAttributeValue(cu, DW_AT_ranges, form_value)) {
-    ranges = GetRangesOrReportError(*cu, *this, form_value);
-  } else if (check_hi_lo_pc) {
+  if (GetAttributeValue(cu, DW_AT_ranges, form_value))
+    return GetRangesOrReportError(*cu, *this, form_value);
+
+  DWARFRangeList ranges;
+  if (check_hi_lo_pc) {
     dw_addr_t lo_pc = LLDB_INVALID_ADDRESS;
     dw_addr_t hi_pc = LLDB_INVALID_ADDRESS;
     if (GetAttributeAddressRange(cu, lo_pc, hi_pc, LLDB_INVALID_ADDRESS,
@@ -653,7 +654,7 @@ size_t DWARFDebugInfoEntry::GetAttributeAddressRanges(
         ranges.Append(DWARFRangeList::Entry(lo_pc, hi_pc - lo_pc));
     }
   }
-  return ranges.GetSize();
+  return ranges;
 }
 
 // GetName
@@ -716,9 +717,8 @@ void DWARFDebugInfoEntry::BuildFunctionAddressRangeTable(
     DWARFUnit *cu, DWARFDebugAranges *debug_aranges) const {
   if (m_tag) {
     if (m_tag == DW_TAG_subprogram) {
-      DWARFRangeList ranges;
-      GetAttributeAddressRanges(cu, ranges,
-                                /*check_hi_lo_pc=*/true);
+      DWARFRangeList ranges =
+          GetAttributeAddressRanges(cu, /*check_hi_lo_pc=*/true);
       for (const auto &r : ranges) {
         debug_aranges->AppendRange(GetOffset(), r.GetRangeBase(),
                                    r.GetRangeEnd());

diff  --git a/lldb/source/Plugins/SymbolFile/DWARF/DWARFDebugInfoEntry.h b/lldb/source/Plugins/SymbolFile/DWARF/DWARFDebugInfoEntry.h
index a674db80a3094..c2ea40065232e 100644
--- a/lldb/source/Plugins/SymbolFile/DWARF/DWARFDebugInfoEntry.h
+++ b/lldb/source/Plugins/SymbolFile/DWARF/DWARFDebugInfoEntry.h
@@ -94,8 +94,8 @@ class DWARFDebugInfoEntry {
       uint64_t fail_value,
       bool check_specification_or_abstract_origin = false) const;
 
-  size_t GetAttributeAddressRanges(
-      DWARFUnit *cu, DWARFRangeList &ranges, bool check_hi_lo_pc,
+  DWARFRangeList GetAttributeAddressRanges(
+      DWARFUnit *cu, bool check_hi_lo_pc,
       bool check_specification_or_abstract_origin = false) const;
 
   const char *GetName(const DWARFUnit *cu) const;

diff  --git a/lldb/source/Plugins/SymbolFile/DWARF/SymbolFileDWARF.cpp b/lldb/source/Plugins/SymbolFile/DWARF/SymbolFileDWARF.cpp
index a71849b514ecb..f5edcd7a77a24 100644
--- a/lldb/source/Plugins/SymbolFile/DWARF/SymbolFileDWARF.cpp
+++ b/lldb/source/Plugins/SymbolFile/DWARF/SymbolFileDWARF.cpp
@@ -840,9 +840,9 @@ Function *SymbolFileDWARF::ParseFunction(CompileUnit &comp_unit,
   if (!dwarf_ast)
     return nullptr;
 
-  DWARFRangeList ranges;
-  if (die.GetDIE()->GetAttributeAddressRanges(die.GetCU(), ranges,
-                                              /*check_hi_lo_pc=*/true) == 0)
+  DWARFRangeList ranges = die.GetDIE()->GetAttributeAddressRanges(
+      die.GetCU(), /*check_hi_lo_pc=*/true);
+  if (ranges.IsEmpty())
     return nullptr;
 
   // Union of all ranges in the function DIE (if the function is
@@ -3208,10 +3208,9 @@ size_t SymbolFileDWARF::ParseVariablesForContext(const SymbolContext &sc) {
       DWARFDIE function_die = GetDIE(sc.function->GetID());
 
       dw_addr_t func_lo_pc = LLDB_INVALID_ADDRESS;
-      DWARFRangeList ranges;
-      if (function_die.GetDIE()->GetAttributeAddressRanges(
-              function_die.GetCU(), ranges,
-              /*check_hi_lo_pc=*/true))
+      DWARFRangeList ranges = function_die.GetDIE()->GetAttributeAddressRanges(
+          function_die.GetCU(), /*check_hi_lo_pc=*/true);
+      if (!ranges.IsEmpty())
         func_lo_pc = ranges.GetMinRangeBase(0);
       if (func_lo_pc != LLDB_INVALID_ADDRESS) {
         const size_t num_variables =
@@ -4282,4 +4281,3 @@ void SymbolFileDWARF::GetCompileOptions(
     args.insert({comp_unit, Args(flags)});
   }
 }
-


        


More information about the lldb-commits mailing list