Skip to content

Commit b08a3f3

Browse files
committed
[NFC] Add a utility to DiverseStack to simplify stable iteration
1 parent b2e2b85 commit b08a3f3

File tree

3 files changed

+19
-5
lines changed

3 files changed

+19
-5
lines changed

include/swift/Basic/DiverseStack.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -294,6 +294,15 @@ template <class T> class DiverseStackImpl : private DiverseStackBase {
294294
return stable_iterator(End - it.Ptr);
295295
}
296296

297+
T &findAndAdvance(stable_iterator &i) {
298+
auto unstable_i = find(i);
299+
assert(unstable_i != end());
300+
T &value = *unstable_i;
301+
++unstable_i;
302+
i = stabilize(unstable_i);
303+
return value;
304+
}
305+
297306
class const_iterator {
298307
const char *Ptr;
299308
friend class DiverseStackImpl;

lib/SILGen/Cleanup.cpp

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -91,20 +91,21 @@ void CleanupManager::emitCleanups(CleanupsDepth depth, CleanupLocation loc,
9191
auto topOfStack = cur;
9292
#endif
9393
while (cur != depth) {
94+
// Advance the iterator.
95+
auto cleanupHandle = cur;
96+
auto iter = stack.find(cleanupHandle);
97+
Cleanup &stackCleanup = *iter;
98+
cur = stack.stabilize(++iter);
99+
94100
// Copy the cleanup off the stack if it needs to be emitted.
95101
// This is necessary both because we might need to pop the cleanup and
96102
// because the cleanup might push other cleanups that will invalidate
97103
// references onto the stack.
98-
auto iter = stack.find(cur);
99-
Cleanup &stackCleanup = *iter;
100104
Optional<CleanupBuffer> copiedCleanup;
101105
if (stackCleanup.isActive() && SGF.B.hasValidInsertionPoint()) {
102106
copiedCleanup.emplace(stackCleanup);
103107
}
104108

105-
// Advance the iterator.
106-
cur = stack.stabilize(++iter);
107-
108109
// Pop now if that was requested.
109110
if (popCleanups) {
110111
stack.pop();

lib/SILGen/Cleanup.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,10 @@ class LLVM_LIBRARY_VISIBILITY CleanupManager {
165165
return *stack.find(iter);
166166
}
167167

168+
Cleanup &findAndAdvance(CleanupsDepth &iter) {
169+
return stack.findAndAdvance(iter);
170+
}
171+
168172
/// \brief Emit a branch to the given jump destination,
169173
/// threading out through any cleanups we need to run. This does not pop the
170174
/// cleanup stack.

0 commit comments

Comments
 (0)