Refactor InstBlockStack to use ArrayStack. (#4104)

The use of ArrayStack here is intended to simplify the logic, and also
make better use of the inst heap allocations. Prior changes #4101 and
#4103 removed the less related logic from InstBlockStack, although #4103
is the actual part that blocked using ArrayStack.

BTW, note the PrintForStackDump implementation was incorrect because it
didn't apply size_. This simplification fixes the issue.
This commit is contained in:
Jon Ross-Perkins
2024-07-03 19:19:57 +00:00
committed by GitHub
parent 9581a1867d
commit efa158d496
5 changed files with 88 additions and 62 deletions
+30
View File
@@ -73,5 +73,35 @@ TEST(ArrayStack, Basics) {
EXPECT_THAT(stack.PeekAllValues(), ElementsAre(5));
}
TEST(ArrayStack, AppendArray) {
ArrayStack<int> stack;
stack.PushArray();
stack.AppendToTop(llvm::ArrayRef<int>());
EXPECT_THAT(stack.PeekArray(), IsEmpty());
stack.AppendToTop({1, 2});
EXPECT_THAT(stack.PeekArray(), ElementsAre(1, 2));
}
TEST(ArrayStack, PeekArrayAt) {
ArrayStack<int> stack;
// Verify behavior with a single array.
stack.PushArray();
stack.AppendToTop(1);
stack.AppendToTop(2);
EXPECT_THAT(stack.PeekArrayAt(0), ElementsAre(1, 2));
// Verify behavior with a couple more arrays.
stack.PushArray();
stack.PushArray();
stack.AppendToTop(3);
EXPECT_THAT(stack.PeekArrayAt(0), ElementsAre(1, 2));
EXPECT_THAT(stack.PeekArrayAt(1), IsEmpty());
EXPECT_THAT(stack.PeekArrayAt(2), ElementsAre(3));
}
} // namespace
} // namespace Carbon::Testing