[lldb] Remove VMRange class (NFC) (#190475)

We have a template class `Range` that provides similar functionality and
is much more widely used.
This commit is contained in:
Sergei Barannikov 2026-04-05 21:39:44 +03:00 committed by GitHub
parent f8e394b6f8
commit 11e7a49a58
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
10 changed files with 26 additions and 337 deletions

View File

@ -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 {

View File

@ -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 <cstddef>
#include <cstdint>
#include <vector>
namespace lldb_private {
// A vm address range. These can represent offsets ranges or actual
// addresses.
class VMRange {
public:
typedef std::vector<VMRange> 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

View File

@ -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 <cinttypes>
#include <limits>
#include <utility>
@ -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 ",

View File

@ -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"

View File

@ -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<addr_t, addr_t> vm_range;
SectionSP section_sp;
};
SectionList *m_section_list;

View File

@ -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

View File

@ -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 <algorithm>
#include <iterator>
#include <vector>
#include <cstddef>
#include <cstdint>
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);
}

View File

@ -48,7 +48,6 @@ add_lldb_unittest(UtilityTests
UUIDTest.cpp
VASprintfTest.cpp
VirtualDataExtractorTest.cpp
VMRangeTest.cpp
XcodeSDKTest.cpp
LINK_COMPONENTS

View File

@ -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);

View File

@ -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 <limits>
#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<lldb::addr_t>::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<lldb::addr_t>::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.
}