Skip to content

Commit 6af14bd

Browse files
committed
[parse] Implement a dump method on Scope using a new debugVisit method on TreeScopedHashTable.
This is just for use in the debugger when one may want to know what is in the current scope. The order is not guaranteed but at least it can provide /some/ info ignoring that property. These are no-ops when not in asserts and I put in a compile time warnign to make sure it is not used in the actual code base.
1 parent 32d5b32 commit 6af14bd

File tree

3 files changed

+46
-0
lines changed

3 files changed

+46
-0
lines changed

include/swift/Basic/TreeScopedHashTable.h

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -357,6 +357,16 @@ class TreeScopedHashTable {
357357
return iterator(I->second);
358358
}
359359

360+
using DebugVisitValueTy = TreeScopedHashTableVal<K, V> *;
361+
362+
/// Visit each entry in the map without regard to order. Meant to be used with
363+
/// in the debugger in coordination with other dumpers that can dump whatever
364+
/// is stored in the map. No-op when asserts are disabled.
365+
LLVM_ATTRIBUTE_DEPRECATED(
366+
void debugVisit(std::function<void(const DebugVisitValueTy &)> &&func)
367+
const LLVM_ATTRIBUTE_USED,
368+
"Only for use in the debugger");
369+
360370
/// This inserts the specified key/value at the specified
361371
/// (possibly not the current) scope. While it is ok to insert into a scope
362372
/// that isn't the current one, it isn't ok to insert *underneath* an existing
@@ -383,6 +393,16 @@ class TreeScopedHashTable {
383393
}
384394
};
385395

396+
template <typename K, typename V, typename Allocator>
397+
void TreeScopedHashTable<K, V, Allocator>::debugVisit(
398+
std::function<void(const DebugVisitValueTy &)> &&func) const {
399+
#ifndef NDEBUG
400+
for (auto entry : TopLevelMap) {
401+
func(entry.second);
402+
}
403+
#endif
404+
}
405+
386406
template <typename K, typename V, typename Allocator>
387407
TreeScopedHashTableScopeImpl<K, V, Allocator>::~TreeScopedHashTableScopeImpl() {
388408
if (MovedFrom)

include/swift/Parse/Scope.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,9 @@ class ScopeInfo {
5757
bool isInactiveConfigBlock() const;
5858

5959
SavedScope saveCurrentScope();
60+
61+
LLVM_ATTRIBUTE_DEPRECATED(void dump() const LLVM_ATTRIBUTE_USED,
62+
"Only for use in the debugger");
6063
};
6164

6265
enum class ScopeKind {

lib/Parse/Scope.cpp

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,3 +139,26 @@ void ScopeInfo::addToScope(ValueDecl *D, Parser &TheParser) {
139139
D->getFullName(),
140140
std::make_pair(CurScope->getDepth(), D));
141141
}
142+
143+
void ScopeInfo::dump() const {
144+
#ifndef NDEBUG
145+
// Dump out the current list of scopes.
146+
if (!CurScope->isResolvable())
147+
return;
148+
149+
assert(CurScope->getDepth() >= ResolvableDepth &&
150+
"Attempting to dump a non-resolvable scope?!");
151+
152+
llvm::dbgs() << "--- Dumping ScopeInfo ---\n";
153+
std::function<void(decltype(HT)::DebugVisitValueTy)> func =
154+
[&](const decltype(HT)::DebugVisitValueTy &iter) -> void {
155+
llvm::dbgs() << "DeclName: " << iter->getKey() << "\n"
156+
<< "KeyScopeID: " << iter->getValue().first << "\n"
157+
<< "Decl: ";
158+
iter->getValue().second->dumpRef(llvm::dbgs());
159+
llvm::dbgs() << "\n";
160+
};
161+
HT.debugVisit(std::move(func));
162+
llvm::dbgs() << "\n";
163+
#endif
164+
}

0 commit comments

Comments
 (0)