@@ -45,8 +45,7 @@ template <typename T> T Take(Expected<T> ExpectedResult, const Twine &Msg) {
45
45
return ret;
46
46
}
47
47
48
- struct ScopePrinterMk2 {
49
-
48
+ struct ScopePrinter {
50
49
std::vector<const LVLine *> Lines;
51
50
std::unordered_map<LVAddress, std::vector<const LVLocation *>> LivetimeBegins;
52
51
std::unordered_map<LVAddress, std::vector<const LVLocation *>>
@@ -95,7 +94,7 @@ struct ScopePrinterMk2 {
95
94
}
96
95
}
97
96
98
- ScopePrinterMk2 (raw_ostream &OS, const LVScopeFunction *Fn) : OS(OS) {
97
+ ScopePrinter (raw_ostream &OS, const LVScopeFunction *Fn) : OS(OS) {
99
98
Walk (OS, Fn);
100
99
std::sort (Lines.begin (), Lines.end (),
101
100
[](const LVLine *a, const LVLine *b) -> bool {
@@ -105,14 +104,11 @@ struct ScopePrinterMk2 {
105
104
return a->getIsLineDebug ();
106
105
return a->getID () < b->getID ();
107
106
});
108
-
109
- // DumpAllInlineFunctionLevels(Fn);
110
107
}
111
108
112
-
113
- static void PrintIndent (int Indent) {
109
+ static void PrintIndent (raw_ostream &OS, int Indent) {
114
110
for (int i = 0 ; i < Indent; i++)
115
- outs () << " " ;
111
+ OS << " " ;
116
112
}
117
113
118
114
static void PrintCallstack (raw_ostream& OS, const LVScope* Scope) {
@@ -132,25 +128,6 @@ struct ScopePrinterMk2 {
132
128
}
133
129
}
134
130
135
- void DumpAllInlineFunctionLevels (const LVScope *Scope) {
136
- if (Scope->getIsInlinedFunction ()) {
137
- outs () << Scope->getName () << " ID:" << Scope->getID ();
138
- if (Scope->rangeCount ()) {
139
- for (auto R : *Scope->getRanges ()) {
140
- auto Lo = Scope->getRanges ()->front ()->getLowerAddress ();
141
- auto Hi = Scope->getRanges ()->front ()->getUpperAddress ();
142
- outs () << " [" << hexValue (Lo) << " :"
143
- << hexValue (Hi) << " ]" ;
144
- }
145
- }
146
-
147
- outs () << " , level: " << Scope->getLevel () << " \n " ;
148
- }
149
- if (Scope->scopeCount ())
150
- for (auto S : *Scope->getScopes ())
151
- DumpAllInlineFunctionLevels (S);
152
- }
153
-
154
131
static bool IsChildScopeOf (const LVScope *A, const LVScope *B) {
155
132
while (A) {
156
133
A = A->getParentScope ();
@@ -194,7 +171,7 @@ struct ScopePrinterMk2 {
194
171
auto LineScope = LineDebug->getParentScope ();
195
172
if (SymScope != LineScope && !IsChildScopeOf (LineScope, SymScope))
196
173
continue ;
197
- PrintIndent (1 );
174
+ PrintIndent (OS, 1 );
198
175
OS << " VAR: " << Sym->getName () << " : "
199
176
<< Sym->getType ()->getName ()
200
177
<< " : " ;
@@ -215,182 +192,6 @@ struct ScopePrinterMk2 {
215
192
}
216
193
};
217
194
218
- struct ScopePrinter {
219
- unsigned NumScopesPrinted = 0 ;
220
- std::unordered_map<LVAddress, std::vector<const LVLocation *>> LivetimeBegins;
221
- std::unordered_map<LVAddress, std::vector<const LVLocation *>>
222
- LivetimeEndsInclusive;
223
- SetVector<const LVLocation *> LiveSymbols; // This needs to be ordered since we're iterating over it.
224
- std::unordered_set<const LVScope *> SeenScopes;
225
-
226
- static void PrintIndent (int Indent) {
227
- for (int i = 0 ; i < Indent; i++)
228
- outs () << " " ;
229
- };
230
-
231
- void PrintScope (const LVScope *Scope, int Indent) {
232
- NumScopesPrinted++;
233
- SeenScopes.insert (Scope);
234
-
235
- bool IsInlined = false ;
236
- if (Scope->getIsInlinedFunction ()) {
237
- Indent++;
238
- auto InlinedFn = cast<LVScopeFunctionInlined>(Scope);
239
- IsInlined = true ;
240
- PrintIndent (Indent);
241
- outs () << " INLINED_FUNCTION: " << InlinedFn->getName () << " \n " ;
242
- }
243
-
244
- if (const LVSymbols *Symbols = Scope->getSymbols ()) {
245
- for (const LVSymbol *Symbol : *Symbols) {
246
- LVLocations SymbolLocations;
247
- Symbol->getLocations (SymbolLocations);
248
- for (const LVLocation *Loc : SymbolLocations) {
249
- if (Loc->getIsGapEntry ())
250
- continue ;
251
-
252
- LVAddress Begin = Loc->getLowerAddress ();
253
- LVAddress End = Loc->getUpperAddress ();
254
- LivetimeBegins[Begin].push_back (Loc);
255
- LivetimeEndsInclusive[End].push_back (Loc);
256
- if (IncludeRanges) {
257
- outs () << " RANGE: " << Symbol->getName () << " : ["
258
- << hexValue (Begin) << " :" << hexValue (End) << " ]\n " ;
259
- }
260
- }
261
- }
262
- }
263
-
264
- auto Lines = Scope->getLines ();
265
- if (Lines) {
266
- int LastLine = -1 ;
267
- StringRef LastFilename;
268
- for (const LVLine *Line : *Lines) {
269
-
270
- if (auto Scopes = Scope->getScopes ()) {
271
- for (const LVScope *SubScope : *Scopes) {
272
- if (SeenScopes.count (SubScope))
273
- continue ;
274
- #if 0
275
- if (SubScope->getBaseAddress() < Line->getAddress()) {
276
- PrintScope(SubScope, Indent);
277
- }
278
- #else
279
-
280
-
281
- struct Local {
282
- static LVAddress GetBaseAddress (const LVScope *S) {
283
- if (S->rangeCount ()) {
284
- return S->getRanges ()->front ()->getLowerAddress ();
285
- }
286
- if (S->lineCount ()) {
287
- return S->getLines ()->front ()->getAddress ();
288
- }
289
- if (S->scopeCount ()) {
290
- for (LVScope *SubS : *S->getScopes ()) {
291
- LVAddress A = GetBaseAddress (SubS);
292
- if (A != -1 )
293
- return A;
294
- }
295
- }
296
- return -1 ;
297
- };
298
- };
299
- #if 0
300
- auto ScopeLines = SubScope->getLines();
301
- if (ScopeLines && ScopeLines->size() &&
302
- ScopeLines->front()->getAddress() < Line->getAddress()) {
303
- PrintScope(SubScope, Indent);
304
- } else {
305
- outs() << "";
306
- }
307
- #else
308
- unsigned BaseAdress = Local::GetBaseAddress (SubScope);
309
- if (BaseAdress < Line->getAddress ()) {
310
- PrintScope (SubScope, Indent);
311
- }
312
- #endif
313
- #endif
314
- }
315
- }
316
-
317
- // Update live list: Add lives
318
- for (auto Loc : LivetimeBegins[Line->getAddress ()])
319
- LiveSymbols.insert (Loc);
320
-
321
- if (Line->getIsLineDebug () && Line->getLineNumber () != 0 ) {
322
- auto LineDebug = cast<LVLineDebug>(Line);
323
- if (LineDebug->getLineNumber () == 605 )
324
- outs () << " " ;
325
- if (LastLine != LineDebug->getLineNumber () ||
326
- LineDebug->getPathname () != LastFilename) {
327
- PrintIndent (Indent+1 );
328
- outs () << " LINE: " << " ["
329
- << hexValue (LineDebug->getAddress ()) << " ] "
330
- << LineDebug->getPathname () << " :"
331
- << LineDebug->getLineNumber () << " \n " ;
332
-
333
- if (IncludeVars) {
334
- for (auto SymLoc : LiveSymbols) {
335
- PrintIndent (Indent+2 );
336
- const LVSymbol *Sym = SymLoc->getParentSymbol ();
337
- outs () << " VAR: " << Sym->getName () << " : "
338
- << Sym->getType ()->getName ()
339
- << " : " ;
340
- SymLoc->printLocations (outs ());
341
- outs () << " \n " ;
342
- }
343
- }
344
- }
345
- LastLine = LineDebug->getLineNumber ();
346
- LastFilename = LineDebug->getPathname ();
347
- } else if (Line->getIsLineAssembler ()) {
348
- if (IncludeCode)
349
- outs () << " CODE: " << " [" << hexValue (Line->getAddress ())
350
- << " ] " << Line->getName () << " \n " ;
351
- }
352
-
353
- // Update live list: remove dead
354
- for (auto Loc : LivetimeEndsInclusive[Line->getAddress ()])
355
- LiveSymbols.remove (Loc);
356
- }
357
- } else if (Scope->scopeCount ()) {
358
- for (auto SubS : *Scope->getScopes ()) {
359
- PrintScope (SubS, Indent);
360
- }
361
- }
362
-
363
- if (IsInlined) {
364
- PrintIndent (Indent);
365
- outs () << " END_INLINED_FUNCTION\n " ;
366
- Indent--;
367
- }
368
- }
369
-
370
- static unsigned CountScopes (const LVScope *Scope) {
371
- unsigned Count = 0 ;
372
- auto Scopes = Scope->getScopes ();
373
- if (Scopes) {
374
- for (auto S : *Scopes)
375
- Count += CountScopes (S);
376
- }
377
- return Count + 1 ;
378
- }
379
-
380
- std::vector<const LVScope *> UnseenScopes;
381
- void CollectUnsceenScopes (const LVScope *Scope) {
382
- if (!SeenScopes.count (Scope)) {
383
- UnseenScopes.push_back (Scope);
384
- Scope->dump ();
385
- }
386
- if (Scope->scopeCount ()) {
387
- for (auto S : *Scope->getScopes ())
388
- CollectUnsceenScopes (S);
389
- }
390
- }
391
-
392
- };
393
-
394
195
int main (int argc, char *argv[]) {
395
196
InitLLVM X (argc, argv);
396
197
@@ -428,28 +229,8 @@ int main(int argc, char *argv[]) {
428
229
continue ;
429
230
outs () << " FUNCTION: " << Child->getName () << " \n " ;
430
231
431
- #if 0
432
- ScopePrinter P;
433
- P.PrintScope(Fn, 0);
434
-
435
- P.CollectUnsceenScopes(Fn);
436
-
437
- for (auto S : P.UnseenScopes) {
438
- if (S->symbolCount()) {
439
- for (auto Sym : *S->getSymbols())
440
- outs() << Sym->getName() << ": " << Sym->getTypeName() << " (line"
441
- << Sym->getLineNumber() << ")" << "\n";
442
- }
443
- }
444
-
445
- //unsigned NumScopes = ScopePrinter::CountScopes(Fn);
446
-
447
- outs() << "";
448
- #else
449
- ScopePrinterMk2 P (outs (), Fn);
232
+ ScopePrinter P (outs (), Fn);
450
233
P.Print ();
451
- #endif
452
-
453
234
}
454
235
}
455
236
0 commit comments