[Lldb-commits] [lldb] [lldb] Use Address(section, offset) constructor in more places (NFC) (PR #189101)

Sergei Barannikov via lldb-commits lldb-commits at lists.llvm.org
Fri Mar 27 13:07:01 PDT 2026


https://github.com/s-barannikov created https://github.com/llvm/llvm-project/pull/189101

After this change, Address::SetSection() had only one use left (in a unit test) and was removed. Address::ClearSection() had no uses, now also removed. (It is unlikely that someone needs to change the section without simultaneously changing the section offset, and for that we have a constructor.)


>From 9301e24233f148c173b6f2a6fc20a52dd2726edf Mon Sep 17 00:00:00 2001
From: Sergei Barannikov <barannikov88 at gmail.com>
Date: Fri, 27 Mar 2026 23:05:18 +0300
Subject: [PATCH] [lldb] Use Address(section, offset) constructor in more
 places (NFC)

After this change, Address::SetSection() had only one use left (in a
unit test) and was removed. Address::ClearSection() had no uses, now
also removed. (It is unlikely that someone needs to change the section
without simultaneously changing the section offset.)
---
 lldb/include/lldb/Core/Address.h              | 12 ----------
 lldb/source/API/SBAddress.cpp                 |  3 +--
 lldb/source/Core/Section.cpp                  |  3 +--
 .../ObjectFile/Mach-O/ObjectFileMachO.cpp     | 23 ++++++++-----------
 lldb/unittests/Symbol/SymbolTest.cpp          |  2 +-
 5 files changed, 12 insertions(+), 31 deletions(-)

diff --git a/lldb/include/lldb/Core/Address.h b/lldb/include/lldb/Core/Address.h
index 71078bb44e57f..2b669aeba20ec 100644
--- a/lldb/include/lldb/Core/Address.h
+++ b/lldb/include/lldb/Core/Address.h
@@ -457,18 +457,6 @@ class Address {
     return false;
   }
 
-  /// Set accessor for the section.
-  ///
-  /// \param[in] section_sp
-  ///     A new lldb::Section pointer to use as the section base. Can
-  ///     be NULL for absolute addresses that are not relative to
-  ///     any section.
-  void SetSection(const lldb::SectionSP &section_sp) {
-    m_section_wp = section_sp;
-  }
-
-  void ClearSection() { m_section_wp.reset(); }
-
   /// Reconstruct a symbol context from an address.
   ///
   /// This class doesn't inherit from SymbolContextScope because many address
diff --git a/lldb/source/API/SBAddress.cpp b/lldb/source/API/SBAddress.cpp
index e519f0bcc83c6..ff669a974b54f 100644
--- a/lldb/source/API/SBAddress.cpp
+++ b/lldb/source/API/SBAddress.cpp
@@ -89,8 +89,7 @@ void SBAddress::SetAddress(lldb::SBSection section, lldb::addr_t offset) {
   LLDB_INSTRUMENT_VA(this, section, offset);
 
   Address &addr = ref();
-  addr.SetSection(section.GetSP());
-  addr.SetOffset(offset);
+  addr = Address(section.GetSP(), offset);
 }
 
 void SBAddress::SetAddress(const Address &address) { ref() = address; }
diff --git a/lldb/source/Core/Section.cpp b/lldb/source/Core/Section.cpp
index 75b1bdeec9145..0596946bb9f18 100644
--- a/lldb/source/Core/Section.cpp
+++ b/lldb/source/Core/Section.cpp
@@ -254,8 +254,7 @@ bool Section::ResolveContainedAddress(addr_t offset, Address &so_addr,
       return child_section->ResolveContainedAddress(offset - child_offset,
                                                     so_addr, allow_section_end);
   }
-  so_addr.SetOffset(offset);
-  so_addr.SetSection(const_cast<Section *>(this)->shared_from_this());
+  so_addr = Address(const_cast<Section *>(this)->shared_from_this(), offset);
 
   // Ensure that there are no orphaned (i.e., moduleless) sections.
   assert(GetModule().get());
diff --git a/lldb/source/Plugins/ObjectFile/Mach-O/ObjectFileMachO.cpp b/lldb/source/Plugins/ObjectFile/Mach-O/ObjectFileMachO.cpp
index b89581b8a74d3..01a91d28a7cc2 100644
--- a/lldb/source/Plugins/ObjectFile/Mach-O/ObjectFileMachO.cpp
+++ b/lldb/source/Plugins/ObjectFile/Mach-O/ObjectFileMachO.cpp
@@ -3522,10 +3522,8 @@ void ObjectFileMachO::ParseSymtab(Symtab &symtab) {
                                 // Copy the address, because often the N_GSYM
                                 // address has an invalid address of zero
                                 // when the global is a common symbol
-                                sym[GSYM_sym_idx].GetAddressRef().SetSection(
-                                    symbol_section);
-                                sym[GSYM_sym_idx].GetAddressRef().SetOffset(
-                                    symbol_value);
+                                sym[GSYM_sym_idx].GetAddressRef() =
+                                    Address(symbol_section, symbol_value);
                                 add_symbol_addr(sym[GSYM_sym_idx]
                                                     .GetAddress()
                                                     .GetFileAddress());
@@ -3546,8 +3544,8 @@ void ObjectFileMachO::ParseSymtab(Symtab &symtab) {
                       sym[sym_idx].SetID(nlist_idx);
                       sym[sym_idx].SetType(type);
                       if (set_value) {
-                        sym[sym_idx].GetAddressRef().SetSection(symbol_section);
-                        sym[sym_idx].GetAddressRef().SetOffset(symbol_value);
+                        sym[sym_idx].GetAddressRef() =
+                            Address(symbol_section, symbol_value);
                         add_symbol_addr(
                             sym[sym_idx].GetAddress().GetFileAddress());
                       }
@@ -4259,8 +4257,8 @@ void ObjectFileMachO::ParseSymtab(Symtab &symtab) {
                 m_nlist_idx_to_sym_idx[nlist_idx] = GSYM_sym_idx;
                 // Copy the address, because often the N_GSYM address has an
                 // invalid address of zero when the global is a common symbol.
-                sym[GSYM_sym_idx].GetAddressRef().SetSection(symbol_section);
-                sym[GSYM_sym_idx].GetAddressRef().SetOffset(symbol_value);
+                sym[GSYM_sym_idx].GetAddressRef() =
+                    Address(symbol_section, symbol_value);
                 add_symbol_addr(
                     sym[GSYM_sym_idx].GetAddress().GetFileAddress());
                 // We just need the flags from the linker symbol, so put these
@@ -4278,8 +4276,7 @@ void ObjectFileMachO::ParseSymtab(Symtab &symtab) {
       sym[sym_idx].SetID(nlist_idx);
       sym[sym_idx].SetType(type);
       if (set_value) {
-        sym[sym_idx].GetAddressRef().SetSection(symbol_section);
-        sym[sym_idx].GetAddressRef().SetOffset(symbol_value);
+        sym[sym_idx].GetAddressRef() = Address(symbol_section, symbol_value);
         if (symbol_section)
           add_symbol_addr(sym[sym_idx].GetAddress().GetFileAddress());
       }
@@ -5171,10 +5168,8 @@ lldb_private::Address ObjectFileMachO::GetBaseAddress() {
   if (section_list) {
     SectionSP text_segment_sp(
         section_list->FindSectionByName(GetSegmentNameTEXT()));
-    if (text_segment_sp) {
-      header_addr.SetSection(text_segment_sp);
-      header_addr.SetOffset(0);
-    }
+    if (text_segment_sp)
+      header_addr = Address(text_segment_sp, /*offset=*/0);
   }
   return header_addr;
 }
diff --git a/lldb/unittests/Symbol/SymbolTest.cpp b/lldb/unittests/Symbol/SymbolTest.cpp
index a1c68b63a82ab..5c3176fe47281 100644
--- a/lldb/unittests/Symbol/SymbolTest.cpp
+++ b/lldb/unittests/Symbol/SymbolTest.cpp
@@ -131,6 +131,6 @@ TEST(SymbolTest, EncodeDecodeSymbol) {
   symbol.SetIsWeak(false);
 
   // Test encoding a symbol with no address.
-  symbol.GetAddressRef().SetSection(SectionSP());
+  symbol.GetAddressRef() = Address(SectionSP(), /*offset=*/0);
   EncodeDecode(symbol, &sect_list);
 }



More information about the lldb-commits mailing list