Skip to content

Commit b04ebcf

Browse files
committed
Demangler: improve debug logging.
Log allocated memory and indent according to the nesting level
1 parent 58f2d37 commit b04ebcf

File tree

2 files changed

+29
-10
lines changed

2 files changed

+29
-10
lines changed

include/swift/Demangling/Demangler.h

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -70,18 +70,27 @@ class NodeFactory {
7070

7171
static void freeSlabs(Slab *slab);
7272

73+
#ifdef NODE_FACTORY_DEBUGGING
74+
size_t allocatedMemory = 0;
75+
static int nestingLevel;
76+
std::string indent() { return std::string(nestingLevel * 2, ' '); }
77+
#endif
78+
7379
public:
7480

7581
NodeFactory() {
7682
#ifdef NODE_FACTORY_DEBUGGING
77-
std::cerr << "## New NodeFactory " << this << "\n";
83+
std::cerr << indent() << "## New NodeFactory\n";
84+
nestingLevel++;
7885
#endif
7986
}
8087

8188
virtual ~NodeFactory() {
8289
freeSlabs(CurrentSlab);
8390
#ifdef NODE_FACTORY_DEBUGGING
84-
std::cerr << "Delete NodeFactory " << this << "\n";
91+
nestingLevel--;
92+
std::cerr << indent() << "## Delete NodeFactory: allocated memory = "
93+
<< allocatedMemory << '\n';
8594
#endif
8695
}
8796

@@ -92,8 +101,9 @@ class NodeFactory {
92101
size_t ObjectSize = NumObjects * sizeof(T);
93102
CurPtr = align(CurPtr, alignof(T));
94103
#ifdef NODE_FACTORY_DEBUGGING
95-
std::cerr << " alloc " << ObjectSize << ", CurPtr = "
104+
std::cerr << indent() << "alloc " << ObjectSize << ", CurPtr = "
96105
<< (void *)CurPtr << "\n";
106+
allocatedMemory += ObjectSize;
97107
#endif
98108

99109
// Do we have enough space in the current slab?
@@ -113,7 +123,7 @@ class NodeFactory {
113123
End = (char *)newSlab + AllocSize;
114124
assert(CurPtr + ObjectSize <= End);
115125
#ifdef NODE_FACTORY_DEBUGGING
116-
std::cerr << " ** new slab " << newSlab << ", allocsize = "
126+
std::cerr << indent() << "** new slab " << newSlab << ", allocsize = "
117127
<< AllocSize << ", CurPtr = " << (void *)CurPtr
118128
<< ", End = " << (void *)End << "\n";
119129
#endif
@@ -138,8 +148,8 @@ class NodeFactory {
138148
size_t AdditionalAlloc = MinGrowth * sizeof(T);
139149

140150
#ifdef NODE_FACTORY_DEBUGGING
141-
std::cerr << " realloc " << Objects << ", num = " << NumObjects
142-
<< " (size = " << OldAllocSize << "), Growth = " << Growth
151+
std::cerr << indent() << "realloc: capacity = " << Capacity
152+
<< " (size = " << OldAllocSize << "), growth = " << MinGrowth
143153
<< " (size = " << AdditionalAlloc << ")\n";
144154
#endif
145155
if ((char *)Objects + OldAllocSize == CurPtr
@@ -149,7 +159,8 @@ class NodeFactory {
149159
CurPtr += AdditionalAlloc;
150160
Capacity += MinGrowth;
151161
#ifdef NODE_FACTORY_DEBUGGING
152-
std::cerr << " ** can grow: CurPtr = " << (void *)CurPtr << "\n";
162+
std::cerr << indent() << "** can grow: " << (void *)CurPtr << '\n';
163+
allocatedMemory += AdditionalAlloc;
153164
#endif
154165
return;
155166
}

lib/Demangling/Demangler.cpp

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -402,16 +402,17 @@ void Node::reverseChildren(size_t StartingAt) {
402402
void NodeFactory::freeSlabs(Slab *slab) {
403403
while (slab) {
404404
Slab *prev = slab->Previous;
405-
#ifdef NODE_FACTORY_DEBUGGING
406-
std::cerr << " free slab = " << slab << "\n";
407-
#endif
408405
free(slab);
409406
slab = prev;
410407
}
411408
}
412409

413410
void NodeFactory::clear() {
414411
if (CurrentSlab) {
412+
#ifdef NODE_FACTORY_DEBUGGING
413+
std::cerr << indent() << "## clear: allocated memory = " << allocatedMemory << "\n";
414+
#endif
415+
415416
freeSlabs(CurrentSlab->Previous);
416417

417418
// Recycle the last allocated slab.
@@ -423,6 +424,9 @@ void NodeFactory::clear() {
423424
CurrentSlab->Previous = nullptr;
424425
CurPtr = (char *)(CurrentSlab + 1);
425426
assert(End == CurPtr + SlabSize);
427+
#ifdef NODE_FACTORY_DEBUGGING
428+
allocatedMemory = 0;
429+
#endif
426430
}
427431
}
428432

@@ -443,6 +447,10 @@ NodePointer NodeFactory::createNode(Node::Kind K, const char *Text) {
443447
return new (Allocate<Node>()) Node(K, llvm::StringRef(Text));
444448
}
445449

450+
#ifdef NODE_FACTORY_DEBUGGING
451+
int NodeFactory::nestingLevel = 0;
452+
#endif
453+
446454
//////////////////////////////////
447455
// CharVector member functions //
448456
//////////////////////////////////

0 commit comments

Comments
 (0)