Skip to content

Commit 450371c

Browse files
committed
[lldb] Fix RangeDataVector::CombineConsecutiveEntriesWithEqualData
Function was merging equal data even if they weren't adjecant. This caused a problem in command-disassemble.s test because the two ranges describing the function would be merged and "swallow" the function between them. This PR copies/adapts the algorithm from RangeVector::CombineConsecutiveEntries (which does not have the same problem) and also adds a call to ComputeUpperBounds as moving entries around invalidates the binary tree. (The lack of this call wasn't noticed until now either because we were not calling methods which rely on upper bounds (right now, it's only the ill-named FindEntryIndexes method), or because we weren't merging anything.
1 parent 4c56096 commit 450371c

File tree

3 files changed

+41
-30
lines changed

3 files changed

+41
-30
lines changed

lldb/include/lldb/Utility/RangeMap.h

Lines changed: 19 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -493,36 +493,27 @@ class RangeDataVector {
493493
#ifdef ASSERT_RANGEMAP_ARE_SORTED
494494
assert(IsSorted());
495495
#endif
496-
typename Collection::iterator pos;
497-
typename Collection::iterator end;
498-
typename Collection::iterator prev;
499-
bool can_combine = false;
500-
// First we determine if we can combine any of the Entry objects so we
501-
// don't end up allocating and making a new collection for no reason
502-
for (pos = m_entries.begin(), end = m_entries.end(), prev = end; pos != end;
503-
prev = pos++) {
504-
if (prev != end && prev->data == pos->data) {
505-
can_combine = true;
506-
break;
507-
}
508-
}
496+
auto first_intersect = std::adjacent_find(
497+
m_entries.begin(), m_entries.end(), [](const Entry &a, const Entry &b) {
498+
return a.DoesAdjoinOrIntersect(b) && a.data == b.data;
499+
});
500+
if (first_intersect == m_entries.end())
501+
return;
509502

510-
// We can combine at least one entry, then we make a new collection and
511-
// populate it accordingly, and then swap it into place.
512-
if (can_combine) {
513-
Collection minimal_ranges;
514-
for (pos = m_entries.begin(), end = m_entries.end(), prev = end;
515-
pos != end; prev = pos++) {
516-
if (prev != end && prev->data == pos->data)
517-
minimal_ranges.back().SetRangeEnd(pos->GetRangeEnd());
518-
else
519-
minimal_ranges.push_back(*pos);
520-
}
521-
// Use the swap technique in case our new vector is much smaller. We must
522-
// swap when using the STL because std::vector objects never release or
523-
// reduce the memory once it has been allocated/reserved.
524-
m_entries.swap(minimal_ranges);
503+
// We can combine at least one entry. Make a new collection and populate it
504+
// accordingly, and then swap it into place.
505+
auto pos = std::next(first_intersect);
506+
Collection minimal_ranges(m_entries.begin(), pos);
507+
for (; pos != m_entries.end(); ++pos) {
508+
Entry &back = minimal_ranges.back();
509+
if (back.DoesAdjoinOrIntersect(*pos) && back.data == pos->data)
510+
back.SetRangeEnd(std::max(back.GetRangeEnd(), pos->GetRangeEnd()));
511+
else
512+
minimal_ranges.push_back(*pos);
525513
}
514+
m_entries.swap(minimal_ranges);
515+
if (!m_entries.empty())
516+
ComputeUpperBounds(0, m_entries.size());
526517
}
527518

528519
void Clear() { m_entries.clear(); }

lldb/test/Shell/Commands/command-disassemble.s

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,8 +94,7 @@
9494
# CHECK-EMPTY:
9595
# CHECK-NEXT: command-disassemble.s.tmp`n2::case3:
9696
# CHECK-NEXT: command-disassemble.s.tmp[0x9046] <+0>: jmp 0x6046 ; <-12288>
97-
## FIXME: This should resolve to `middle_of_case3`
98-
# CHECK-NEXT: command-disassemble.s.tmp[0x904b] <+5>: jmp 0x7046 ; n2::case3 - 8192
97+
# CHECK-NEXT: command-disassemble.s.tmp[0x904b] <+5>: jmp 0x7046 ; middle_of_case3
9998
# CHECK-NEXT: command-disassemble.s.tmp[0x9050] <+10>: int $0x2a
10099
# CHECK-EMPTY:
101100
# CHECK-NEXT: command-disassemble.s.tmp`n1::case3:

lldb/unittests/Utility/RangeMapTest.cpp

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -238,3 +238,24 @@ TEST(RangeDataVector, FindEntryIndexesThatContain_Overlap) {
238238
EXPECT_THAT(FindEntryIndexes(39, Map), testing::ElementsAre(10));
239239
EXPECT_THAT(FindEntryIndexes(40, Map), testing::ElementsAre());
240240
}
241+
242+
TEST(RangeDataVector, CombineConsecutiveEntriesWithEqualData) {
243+
RangeDataVectorT Map;
244+
Map.Append(EntryT(0, 10, 47));
245+
Map.Append(EntryT(10, 10, 47));
246+
Map.Sort();
247+
Map.CombineConsecutiveEntriesWithEqualData();
248+
EXPECT_THAT(FindEntryIndexes(5, Map), testing::ElementsAre(47));
249+
EXPECT_THAT(FindEntryIndexes(15, Map), testing::ElementsAre(47));
250+
EXPECT_THAT(FindEntryIndexes(25, Map), testing::ElementsAre());
251+
252+
Map.Clear();
253+
Map.Append(EntryT(0, 10, 47));
254+
Map.Append(EntryT(20, 10, 47));
255+
Map.Sort();
256+
Map.CombineConsecutiveEntriesWithEqualData();
257+
EXPECT_THAT(FindEntryIndexes(5, Map), testing::ElementsAre(47));
258+
EXPECT_THAT(FindEntryIndexes(15, Map), testing::ElementsAre());
259+
EXPECT_THAT(FindEntryIndexes(25, Map), testing::ElementsAre(47));
260+
EXPECT_THAT(FindEntryIndexes(35, Map), testing::ElementsAre());
261+
}

0 commit comments

Comments
 (0)