From 11e7a49a58e18aded5ebe513b8cd095be440e800 Mon Sep 17 00:00:00 2001 From: Sergei Barannikov Date: Sun, 5 Apr 2026 21:39:44 +0300 Subject: [PATCH] [lldb] Remove VMRange class (NFC) (#190475) We have a template class `Range` that provides similar functionality and is much more widely used. --- lldb/include/lldb/Symbol/DWARFCallFrameInfo.h | 1 - lldb/include/lldb/Utility/VMRange.h | 104 ------------ lldb/source/Core/Section.cpp | 5 +- lldb/source/Expression/DWARFExpression.cpp | 1 - .../ObjectFile/Mach-O/ObjectFileMachO.cpp | 7 +- lldb/source/Utility/CMakeLists.txt | 1 - lldb/source/Utility/VMRange.cpp | 69 -------- lldb/unittests/Utility/CMakeLists.txt | 1 - lldb/unittests/Utility/RangeTest.cpp | 23 ++- lldb/unittests/Utility/VMRangeTest.cpp | 151 ------------------ 10 files changed, 26 insertions(+), 337 deletions(-) delete mode 100644 lldb/include/lldb/Utility/VMRange.h delete mode 100644 lldb/source/Utility/VMRange.cpp delete mode 100644 lldb/unittests/Utility/VMRangeTest.cpp diff --git a/lldb/include/lldb/Symbol/DWARFCallFrameInfo.h b/lldb/include/lldb/Symbol/DWARFCallFrameInfo.h index c214ed1f6091..d318b664fb50 100644 --- a/lldb/include/lldb/Symbol/DWARFCallFrameInfo.h +++ b/lldb/include/lldb/Symbol/DWARFCallFrameInfo.h @@ -19,7 +19,6 @@ #include "lldb/Symbol/UnwindPlan.h" #include "lldb/Utility/Flags.h" #include "lldb/Utility/RangeMap.h" -#include "lldb/Utility/VMRange.h" #include "lldb/lldb-private.h" namespace lldb_private { diff --git a/lldb/include/lldb/Utility/VMRange.h b/lldb/include/lldb/Utility/VMRange.h deleted file mode 100644 index 095092c1a381..000000000000 --- a/lldb/include/lldb/Utility/VMRange.h +++ /dev/null @@ -1,104 +0,0 @@ -//===-- VMRange.h -----------------------------------------------*- C++ -*-===// -// -// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. -// See https://llvm.org/LICENSE.txt for license information. -// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception -// -//===----------------------------------------------------------------------===// - -#ifndef LLDB_UTILITY_VMRANGE_H -#define LLDB_UTILITY_VMRANGE_H - -#include "lldb/lldb-types.h" -#include "llvm/Support/raw_ostream.h" - -#include -#include -#include - -namespace lldb_private { - -// A vm address range. These can represent offsets ranges or actual -// addresses. -class VMRange { -public: - typedef std::vector collection; - typedef collection::iterator iterator; - typedef collection::const_iterator const_iterator; - - VMRange() = default; - - VMRange(lldb::addr_t start_addr, lldb::addr_t end_addr) - : m_base_addr(start_addr), - m_byte_size(end_addr > start_addr ? end_addr - start_addr : 0) {} - - ~VMRange() = default; - - void Clear() { - m_base_addr = 0; - m_byte_size = 0; - } - - // Set the start and end values - void Reset(lldb::addr_t start_addr, lldb::addr_t end_addr) { - SetBaseAddress(start_addr); - SetEndAddress(end_addr); - } - - // Set the start value for the range, and keep the same size - void SetBaseAddress(lldb::addr_t base_addr) { m_base_addr = base_addr; } - - void SetEndAddress(lldb::addr_t end_addr) { - const lldb::addr_t base_addr = GetBaseAddress(); - if (end_addr > base_addr) - m_byte_size = end_addr - base_addr; - else - m_byte_size = 0; - } - - lldb::addr_t GetByteSize() const { return m_byte_size; } - - void SetByteSize(lldb::addr_t byte_size) { m_byte_size = byte_size; } - - lldb::addr_t GetBaseAddress() const { return m_base_addr; } - - lldb::addr_t GetEndAddress() const { return GetBaseAddress() + m_byte_size; } - - bool IsValid() const { return m_byte_size > 0; } - - bool Contains(lldb::addr_t addr) const { - return (GetBaseAddress() <= addr) && (addr < GetEndAddress()); - } - - bool Contains(const VMRange &range) const { - if (Contains(range.GetBaseAddress())) { - lldb::addr_t range_end = range.GetEndAddress(); - return (GetBaseAddress() <= range_end) && (range_end <= GetEndAddress()); - } - return false; - } - - void Dump(llvm::raw_ostream &s, lldb::addr_t base_addr = 0, - uint32_t addr_width = 8) const; - - static bool ContainsValue(const VMRange::collection &coll, - lldb::addr_t value); - - static bool ContainsRange(const VMRange::collection &coll, - const VMRange &range); - -protected: - lldb::addr_t m_base_addr = 0; - lldb::addr_t m_byte_size = 0; -}; - -bool operator==(const VMRange &lhs, const VMRange &rhs); -bool operator!=(const VMRange &lhs, const VMRange &rhs); -bool operator<(const VMRange &lhs, const VMRange &rhs); -bool operator<=(const VMRange &lhs, const VMRange &rhs); -bool operator>(const VMRange &lhs, const VMRange &rhs); -bool operator>=(const VMRange &lhs, const VMRange &rhs); - -} // namespace lldb_private - -#endif // LLDB_UTILITY_VMRANGE_H diff --git a/lldb/source/Core/Section.cpp b/lldb/source/Core/Section.cpp index 0596946bb9f1..2409c067d3ad 100644 --- a/lldb/source/Core/Section.cpp +++ b/lldb/source/Core/Section.cpp @@ -13,8 +13,6 @@ #include "lldb/Target/SectionLoadList.h" #include "lldb/Target/Target.h" #include "lldb/Utility/FileSpec.h" -#include "lldb/Utility/VMRange.h" - #include #include #include @@ -291,8 +289,7 @@ void Section::Dump(llvm::raw_ostream &s, unsigned indent, Target *target, addr = GetFileAddress(); } - VMRange range(addr, addr + m_byte_size); - range.Dump(s, 0); + DumpAddressRange(s, addr, addr + m_byte_size, 8); } s << llvm::format("%c %c%c%c 0x%8.8" PRIx64 " 0x%8.8" PRIx64 " 0x%8.8x ", diff --git a/lldb/source/Expression/DWARFExpression.cpp b/lldb/source/Expression/DWARFExpression.cpp index 5ab01554b7fa..1e12b868191a 100644 --- a/lldb/source/Expression/DWARFExpression.cpp +++ b/lldb/source/Expression/DWARFExpression.cpp @@ -21,7 +21,6 @@ #include "lldb/Utility/RegisterValue.h" #include "lldb/Utility/Scalar.h" #include "lldb/Utility/StreamString.h" -#include "lldb/Utility/VMRange.h" #include "lldb/Host/Host.h" #include "lldb/Utility/Endian.h" diff --git a/lldb/source/Plugins/ObjectFile/Mach-O/ObjectFileMachO.cpp b/lldb/source/Plugins/ObjectFile/Mach-O/ObjectFileMachO.cpp index 4b34cec517f3..7e2543fc583c 100644 --- a/lldb/source/Plugins/ObjectFile/Mach-O/ObjectFileMachO.cpp +++ b/lldb/source/Plugins/ObjectFile/Mach-O/ObjectFileMachO.cpp @@ -1916,7 +1916,7 @@ public: SectionSP section_sp(m_section_list->FindSectionByID(n_sect)); m_section_infos[n_sect].section_sp = section_sp; if (section_sp) { - m_section_infos[n_sect].vm_range.SetBaseAddress( + m_section_infos[n_sect].vm_range.SetRangeBase( section_sp->GetFileAddress()); m_section_infos[n_sect].vm_range.SetByteSize( section_sp->GetByteSize()); @@ -1936,8 +1936,7 @@ public: // Symbol is in section. return m_section_infos[n_sect].section_sp; } else if (m_section_infos[n_sect].vm_range.GetByteSize() == 0 && - m_section_infos[n_sect].vm_range.GetBaseAddress() == - file_addr) { + m_section_infos[n_sect].vm_range.GetRangeBase() == file_addr) { // Symbol is in section with zero size, but has the same start address // as the section. This can happen with linker symbols (symbols that // start with the letter 'l' or 'L'. @@ -1951,7 +1950,7 @@ protected: struct SectionInfo { SectionInfo() : vm_range(), section_sp() {} - VMRange vm_range; + Range vm_range; SectionSP section_sp; }; SectionList *m_section_list; diff --git a/lldb/source/Utility/CMakeLists.txt b/lldb/source/Utility/CMakeLists.txt index 04f1692e53b3..3836ab0ec6c2 100644 --- a/lldb/source/Utility/CMakeLists.txt +++ b/lldb/source/Utility/CMakeLists.txt @@ -75,7 +75,6 @@ add_lldb_library(lldbUtility NO_INTERNAL_DEPENDENCIES UserID.cpp UserIDResolver.cpp VASprintf.cpp - VMRange.cpp VirtualDataExtractor.cpp XcodeSDK.cpp ZipFile.cpp diff --git a/lldb/source/Utility/VMRange.cpp b/lldb/source/Utility/VMRange.cpp deleted file mode 100644 index ddd2a67c29b2..000000000000 --- a/lldb/source/Utility/VMRange.cpp +++ /dev/null @@ -1,69 +0,0 @@ -//===-- VMRange.cpp -------------------------------------------------------===// -// -// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. -// See https://llvm.org/LICENSE.txt for license information. -// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception -// -//===----------------------------------------------------------------------===// - -#include "lldb/Utility/VMRange.h" - -#include "lldb/Utility/Stream.h" -#include "lldb/lldb-types.h" - -#include -#include -#include - -#include -#include - -using namespace lldb; -using namespace lldb_private; - -bool VMRange::ContainsValue(const VMRange::collection &coll, - lldb::addr_t value) { - return llvm::any_of(coll, - [&](const VMRange &r) { return r.Contains(value); }); -} - -bool VMRange::ContainsRange(const VMRange::collection &coll, - const VMRange &range) { - return llvm::any_of(coll, - [&](const VMRange &r) { return r.Contains(range); }); -} - -void VMRange::Dump(llvm::raw_ostream &s, lldb::addr_t offset, - uint32_t addr_width) const { - DumpAddressRange(s, offset + GetBaseAddress(), offset + GetEndAddress(), - addr_width); -} - -bool lldb_private::operator==(const VMRange &lhs, const VMRange &rhs) { - return lhs.GetBaseAddress() == rhs.GetBaseAddress() && - lhs.GetEndAddress() == rhs.GetEndAddress(); -} - -bool lldb_private::operator!=(const VMRange &lhs, const VMRange &rhs) { - return !(lhs == rhs); -} - -bool lldb_private::operator<(const VMRange &lhs, const VMRange &rhs) { - if (lhs.GetBaseAddress() < rhs.GetBaseAddress()) - return true; - else if (lhs.GetBaseAddress() > rhs.GetBaseAddress()) - return false; - return lhs.GetEndAddress() < rhs.GetEndAddress(); -} - -bool lldb_private::operator<=(const VMRange &lhs, const VMRange &rhs) { - return !(lhs > rhs); -} - -bool lldb_private::operator>(const VMRange &lhs, const VMRange &rhs) { - return rhs < lhs; -} - -bool lldb_private::operator>=(const VMRange &lhs, const VMRange &rhs) { - return !(lhs < rhs); -} diff --git a/lldb/unittests/Utility/CMakeLists.txt b/lldb/unittests/Utility/CMakeLists.txt index 47c5e815d5b3..9970c2f36e44 100644 --- a/lldb/unittests/Utility/CMakeLists.txt +++ b/lldb/unittests/Utility/CMakeLists.txt @@ -48,7 +48,6 @@ add_lldb_unittest(UtilityTests UUIDTest.cpp VASprintfTest.cpp VirtualDataExtractorTest.cpp - VMRangeTest.cpp XcodeSDKTest.cpp LINK_COMPONENTS diff --git a/lldb/unittests/Utility/RangeTest.cpp b/lldb/unittests/Utility/RangeTest.cpp index cfc281c8fd64..22022a6fcecc 100644 --- a/lldb/unittests/Utility/RangeTest.cpp +++ b/lldb/unittests/Utility/RangeTest.cpp @@ -43,6 +43,14 @@ TEST(RangeTest, Constructor) { EXPECT_EQ(8U, r.GetRangeEnd()); } +TEST(RangeTest, IsValid) { + RangeT range; + EXPECT_FALSE(range.IsValid()); + + range = RangeT(0x1, 0x0); + EXPECT_FALSE(range.IsValid()); +} + TEST(RangeTest, Copy) { RangeT orig(3, 5); RangeT r = orig; @@ -54,8 +62,9 @@ TEST(RangeTest, Copy) { TEST(RangeTest, Clear) { RangeT r(3, 5); + EXPECT_NE(r, RangeT()); r.Clear(); - EXPECT_TRUE(r == RangeT()); + EXPECT_EQ(r, RangeT()); } TEST(RangeTest, ClearWithStarAddress) { @@ -72,6 +81,18 @@ TEST(RangeTest, SetRangeBase) { EXPECT_EQ(5U, r.GetByteSize()); } +TEST(RangeTest, SetRangeEnd) { + RangeT r(0x100, 0x100); + + r.SetRangeEnd(0xFF); + EXPECT_EQ(r.GetByteSize(), 0U); + EXPECT_FALSE(r.IsValid()); + + r.SetRangeEnd(0x101); + EXPECT_EQ(r.GetByteSize(), 1U); + EXPECT_TRUE(r.IsValid()); +} + TEST(RangeTest, Slide) { RangeT r(3, 5); r.Slide(1); diff --git a/lldb/unittests/Utility/VMRangeTest.cpp b/lldb/unittests/Utility/VMRangeTest.cpp deleted file mode 100644 index 064f12090b90..000000000000 --- a/lldb/unittests/Utility/VMRangeTest.cpp +++ /dev/null @@ -1,151 +0,0 @@ -//===-- VMRangeTest.cpp ---------------------------------------------------===// -// -// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. -// See https://llvm.org/LICENSE.txt for license information. -// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception -// -//===----------------------------------------------------------------------===// - -#include "gtest/gtest.h" - -#include - -#include "lldb/Utility/VMRange.h" - -using namespace lldb_private; - -namespace lldb_private { -void PrintTo(const VMRange &v, std::ostream *os) { - (*os) << "VMRange(" << v.GetBaseAddress() << ", " << v.GetEndAddress() << ")"; -} -} // namespace lldb_private - -TEST(VMRange, IsValid) { - VMRange range; - EXPECT_FALSE(range.IsValid()); - - range.Reset(0x1, 0x100); - EXPECT_TRUE(range.IsValid()); - - range.Reset(0x1, 0x1); - EXPECT_FALSE(range.IsValid()); -} - -TEST(VMRange, Clear) { - VMRange range(0x100, 0x200); - EXPECT_NE(VMRange(), range); - range.Clear(); - EXPECT_EQ(VMRange(), range); -} - -TEST(VMRange, Comparison) { - VMRange range1(0x100, 0x200); - VMRange range2(0x100, 0x200); - EXPECT_EQ(range1, range2); - - EXPECT_NE(VMRange(0x100, 0x1ff), range1); - EXPECT_NE(VMRange(0x100, 0x201), range1); - EXPECT_NE(VMRange(0x0ff, 0x200), range1); - EXPECT_NE(VMRange(0x101, 0x200), range1); - - range2.Clear(); - EXPECT_NE(range1, range2); -} - -TEST(VMRange, Reset) { - VMRange range(0x100, 0x200); - EXPECT_FALSE(VMRange(0x200, 0x200) == range); - range.Reset(0x200, 0x200); - EXPECT_TRUE(VMRange(0x200, 0x200) == range); -} - -TEST(VMRange, SetEndAddress) { - VMRange range(0x100, 0x200); - - range.SetEndAddress(0xFF); - EXPECT_EQ(0U, range.GetByteSize()); - EXPECT_FALSE(range.IsValid()); - - range.SetEndAddress(0x101); - EXPECT_EQ(1U, range.GetByteSize()); - EXPECT_TRUE(range.IsValid()); -} - -TEST(VMRange, ContainsAddr) { - VMRange range(0x100, 0x200); - - EXPECT_FALSE(range.Contains(0x00)); - EXPECT_FALSE(range.Contains(0xFF)); - EXPECT_TRUE(range.Contains(0x100)); - EXPECT_TRUE(range.Contains(0x101)); - EXPECT_TRUE(range.Contains(0x1FF)); - EXPECT_FALSE(range.Contains(0x200)); - EXPECT_FALSE(range.Contains(0x201)); - EXPECT_FALSE(range.Contains(0xFFF)); - EXPECT_FALSE(range.Contains(std::numeric_limits::max())); -} - -TEST(VMRange, ContainsRange) { - VMRange range(0x100, 0x200); - - EXPECT_FALSE(range.Contains(VMRange(0x0, 0x0))); - - EXPECT_FALSE(range.Contains(VMRange(0x0, 0x100))); - EXPECT_FALSE(range.Contains(VMRange(0x0, 0x101))); - EXPECT_TRUE(range.Contains(VMRange(0x100, 0x105))); - EXPECT_TRUE(range.Contains(VMRange(0x101, 0x105))); - EXPECT_TRUE(range.Contains(VMRange(0x100, 0x1FF))); - EXPECT_TRUE(range.Contains(VMRange(0x105, 0x200))); - EXPECT_FALSE(range.Contains(VMRange(0x105, 0x201))); - EXPECT_FALSE(range.Contains(VMRange(0x200, 0x201))); - EXPECT_TRUE(range.Contains(VMRange(0x100, 0x200))); - EXPECT_FALSE( - range.Contains(VMRange(0x105, std::numeric_limits::max()))); - - // Empty range. - EXPECT_TRUE(range.Contains(VMRange(0x100, 0x100))); - - range.Clear(); - EXPECT_FALSE(range.Contains(VMRange(0x0, 0x0))); -} - -TEST(VMRange, Ordering) { - VMRange range1(0x44, 0x200); - VMRange range2(0x100, 0x1FF); - VMRange range3(0x100, 0x200); - - EXPECT_LE(range1, range1); - EXPECT_GE(range1, range1); - - EXPECT_LT(range1, range2); - EXPECT_LT(range2, range3); - - EXPECT_GT(range2, range1); - EXPECT_GT(range3, range2); - - // Ensure that < and > are always false when comparing ranges with themselves. - EXPECT_FALSE(range1 < range1); - EXPECT_FALSE(range2 < range2); - EXPECT_FALSE(range3 < range3); - - EXPECT_FALSE(range1 > range1); - EXPECT_FALSE(range2 > range2); - EXPECT_FALSE(range3 > range3); -} - -TEST(VMRange, CollectionContains) { - VMRange::collection collection = {VMRange(0x100, 0x105), - VMRange(0x108, 0x110)}; - - EXPECT_FALSE(VMRange::ContainsValue(collection, 0xFF)); - EXPECT_TRUE(VMRange::ContainsValue(collection, 0x100)); - EXPECT_FALSE(VMRange::ContainsValue(collection, 0x105)); - EXPECT_TRUE(VMRange::ContainsValue(collection, 0x109)); - - EXPECT_TRUE(VMRange::ContainsRange(collection, VMRange(0x100, 0x104))); - EXPECT_TRUE(VMRange::ContainsRange(collection, VMRange(0x108, 0x100))); - EXPECT_FALSE(VMRange::ContainsRange(collection, VMRange(0xFF, 0x100))); - - // TODO: Implement and test ContainsRange with values that span multiple - // ranges in the collection. -}