@@ -281,8 +281,11 @@ SILValue VariableNameInferrer::getRootValueForTemporaryAllocation(
281
281
282
282
SILValue
283
283
VariableNameInferrer::findDebugInfoProvidingValue (SILValue searchValue) {
284
+ // NOTE: This should only return a non-empty SILValue if we actually have a
285
+ // full path (including base name) in the variable name path.
284
286
if (!searchValue)
285
287
return SILValue ();
288
+
286
289
LLVM_DEBUG (llvm::dbgs () << " Searching for debug info providing value for: "
287
290
<< searchValue);
288
291
ValueSet valueSet (searchValue->getFunction ());
@@ -342,6 +345,12 @@ static BeginBorrowInst *hasOnlyBorrowingNonDestroyUse(SILValue searchValue) {
342
345
return result;
343
346
}
344
347
348
+ namespace {
349
+
350
+ constexpr StringLiteral UnknownDeclString = " <unknown decl>" ;
351
+
352
+ } // namespace
353
+
345
354
SILValue VariableNameInferrer::findDebugInfoProvidingValueHelper (
346
355
SILValue searchValue, ValueSet &visitedValues) {
347
356
assert (searchValue);
@@ -362,7 +371,7 @@ SILValue VariableNameInferrer::findDebugInfoProvidingValueHelper(
362
371
if (auto *use = getAnyDebugUse (searchValue)) {
363
372
if (auto debugVar = DebugVarCarryingInst (use->getUser ())) {
364
373
assert (debugVar.getKind () == DebugVarCarryingInst::Kind::DebugValue);
365
- variableNamePath.push_back (use-> getUser ());
374
+ variableNamePath.push_back (debugVar. getName ());
366
375
367
376
// We return the value, not the debug_info.
368
377
return searchValue;
@@ -385,7 +394,7 @@ SILValue VariableNameInferrer::findDebugInfoProvidingValueHelper(
385
394
if (auto debugVar = DebugVarCarryingInst (debugUse->getUser ())) {
386
395
assert (debugVar.getKind () ==
387
396
DebugVarCarryingInst::Kind::DebugValue);
388
- variableNamePath.push_back (debugUse-> getUser ());
397
+ variableNamePath.push_back (debugVar. getName ());
389
398
390
399
// We return the value, not the debug_info.
391
400
return searchValue;
@@ -399,7 +408,7 @@ SILValue VariableNameInferrer::findDebugInfoProvidingValueHelper(
399
408
if (auto *debugUse = getAnyDebugUse (bbi)) {
400
409
if (auto debugVar = DebugVarCarryingInst (debugUse->getUser ())) {
401
410
assert (debugVar.getKind () == DebugVarCarryingInst::Kind::DebugValue);
402
- variableNamePath.push_back (debugUse-> getUser ());
411
+ variableNamePath.push_back (debugVar. getName ());
403
412
404
413
// We return the value, not the debug_info.
405
414
return searchValue;
@@ -426,10 +435,15 @@ SILValue VariableNameInferrer::findDebugInfoProvidingValueHelper(
426
435
return SILValue ();
427
436
}
428
437
429
- variableNamePath.push_back (allocInst);
438
+ variableNamePath.push_back (DebugVarCarryingInst ( allocInst). getName () );
430
439
return allocInst;
431
440
}
432
441
442
+ if (auto *abi = dyn_cast<AllocBoxInst>(searchValue)) {
443
+ variableNamePath.push_back (DebugVarCarryingInst (abi).getName ());
444
+ return abi;
445
+ }
446
+
433
447
// If we have a store_borrow, always look at the dest. We are going to see
434
448
// if we can determine if dest is a temporary alloc_stack.
435
449
if (auto *sbi = dyn_cast<StoreBorrowInst>(searchValue)) {
@@ -438,7 +452,7 @@ SILValue VariableNameInferrer::findDebugInfoProvidingValueHelper(
438
452
}
439
453
440
454
if (auto *globalAddrInst = dyn_cast<GlobalAddrInst>(searchValue)) {
441
- variableNamePath.push_back (globalAddrInst);
455
+ variableNamePath.push_back (VarDeclCarryingInst ( globalAddrInst). getName () );
442
456
return globalAddrInst;
443
457
}
444
458
@@ -448,44 +462,44 @@ SILValue VariableNameInferrer::findDebugInfoProvidingValueHelper(
448
462
}
449
463
450
464
if (auto *rei = dyn_cast<RefElementAddrInst>(searchValue)) {
451
- variableNamePath.push_back (rei);
465
+ variableNamePath.push_back (VarDeclCarryingInst ( rei). getName () );
452
466
searchValue = rei->getOperand ();
453
467
continue ;
454
468
}
455
469
456
470
if (auto *sei = dyn_cast<StructExtractInst>(searchValue)) {
457
- variableNamePath.push_back (sei);
471
+ variableNamePath.push_back (getNameFromDecl ( sei-> getField ()) );
458
472
searchValue = sei->getOperand ();
459
473
continue ;
460
474
}
461
475
462
476
if (auto *uedi = dyn_cast<UncheckedEnumDataInst>(searchValue)) {
463
- variableNamePath.push_back (uedi);
477
+ variableNamePath.push_back (getNameFromDecl ( uedi-> getElement ()) );
464
478
searchValue = uedi->getOperand ();
465
479
continue ;
466
480
}
467
481
468
482
if (auto *tei = dyn_cast<TupleExtractInst>(searchValue)) {
469
- variableNamePath.push_back (tei);
483
+ variableNamePath.push_back (getStringRefForIndex ( tei-> getFieldIndex ()) );
470
484
searchValue = tei->getOperand ();
471
485
continue ;
472
486
}
473
487
474
488
if (auto *sei = dyn_cast<StructElementAddrInst>(searchValue)) {
475
- variableNamePath.push_back (sei);
489
+ variableNamePath.push_back (getNameFromDecl ( sei-> getField ()) );
476
490
searchValue = sei->getOperand ();
477
491
continue ;
478
492
}
479
493
480
494
if (auto *tei = dyn_cast<TupleElementAddrInst>(searchValue)) {
481
- variableNamePath.push_back (tei);
495
+ variableNamePath.push_back (getStringRefForIndex ( tei-> getFieldIndex ()) );
482
496
searchValue = tei->getOperand ();
483
497
continue ;
484
498
}
485
499
486
- if (auto *e = dyn_cast<UncheckedTakeEnumDataAddrInst>(searchValue)) {
487
- variableNamePath.push_back (e );
488
- searchValue = e ->getOperand ();
500
+ if (auto *utedai = dyn_cast<UncheckedTakeEnumDataAddrInst>(searchValue)) {
501
+ variableNamePath.push_back (getNameFromDecl (utedai-> getElement ()) );
502
+ searchValue = utedai ->getOperand ();
489
503
continue ;
490
504
}
491
505
@@ -494,31 +508,32 @@ SILValue VariableNameInferrer::findDebugInfoProvidingValueHelper(
494
508
// them and add the case to the variableNamePath.
495
509
if (auto *e = dyn_cast<EnumInst>(searchValue)) {
496
510
if (e->hasOperand ()) {
497
- variableNamePath.push_back (e );
511
+ variableNamePath.push_back (getNameFromDecl (e-> getElement ()) );
498
512
searchValue = e->getOperand ();
499
513
continue ;
500
514
}
501
515
}
502
516
503
517
if (auto *dti = dyn_cast_or_null<DestructureTupleInst>(
504
518
searchValue->getDefiningInstruction ())) {
505
- // Append searchValue, so we can find the specific tuple index.
506
- variableNamePath. push_back ( searchValue);
519
+ variableNamePath. push_back (
520
+ getStringRefForIndex (*dti-> getIndexOfResult ( searchValue)) );
507
521
searchValue = dti->getOperand ();
508
522
continue ;
509
523
}
510
524
511
525
if (auto *dsi = dyn_cast_or_null<DestructureStructInst>(
512
526
searchValue->getDefiningInstruction ())) {
513
- // Append searchValue, so we can find the specific struct field.
514
- variableNamePath.push_back (searchValue);
527
+ unsigned index = *dsi->getIndexOfResult (searchValue);
528
+ variableNamePath.push_back (
529
+ getNameFromDecl (dsi->getStructDecl ()->getStoredProperties ()[index]));
515
530
searchValue = dsi->getOperand ();
516
531
continue ;
517
532
}
518
533
519
534
if (auto *fArg = dyn_cast<SILFunctionArgument>(searchValue)) {
520
- if (fArg ->getDecl ()) {
521
- variableNamePath.push_back ({ fArg } );
535
+ if (auto *decl = fArg ->getDecl ()) {
536
+ variableNamePath.push_back (decl-> getBaseName (). userFacingName () );
522
537
return fArg ;
523
538
}
524
539
}
@@ -546,15 +561,19 @@ SILValue VariableNameInferrer::findDebugInfoProvidingValueHelper(
546
561
547
562
auto getNamePathComponentFromCallee = [&](FullApplySite call) -> SILValue {
548
563
// Use the name of the property being accessed if we can get to it.
549
- if (isa<FunctionRefBaseInst>(call.getCallee ()) ||
550
- isa<MethodInst>(call.getCallee ())) {
551
- if (call.getSubstCalleeType ()->hasSelfParam ()) {
564
+ if (call.getSubstCalleeType ()->hasSelfParam ()) {
565
+ if (auto *f = dyn_cast<FunctionRefBaseInst>(call.getCallee ())) {
566
+ if (auto dc = f->getInitiallyReferencedFunction ()->getDeclContext ()) {
567
+ variableNamePath.push_back (getNameFromDecl (dc->getAsDecl ()));
568
+ return call.getSelfArgument ();
569
+ }
570
+ }
571
+
572
+ if (auto *mi = dyn_cast<MethodInst>(call.getCallee ())) {
552
573
variableNamePath.push_back (
553
- call. getCallee ()-> getDefiningInstruction ( ));
574
+ getNameFromDecl (mi-> getMember (). getDecl () ));
554
575
return call.getSelfArgument ();
555
576
}
556
-
557
- return SILValue ();
558
577
}
559
578
560
579
return SILValue ();
@@ -650,101 +669,7 @@ StringRef VariableNameInferrer::getNameFromDecl(Decl *d) {
650
669
}
651
670
}
652
671
653
- return " <unknown decl>" ;
654
- }
655
-
656
- void VariableNameInferrer::popSingleVariableName () {
657
- auto next = variableNamePath.pop_back_val ();
658
-
659
- if (auto *inst = next.dyn_cast <SILInstruction *>()) {
660
- if (auto i = DebugVarCarryingInst (inst)) {
661
- resultingString += i.getName ();
662
- return ;
663
- }
664
-
665
- if (auto i = VarDeclCarryingInst (inst)) {
666
- resultingString += i.getName ();
667
- return ;
668
- }
669
-
670
- if (auto f = dyn_cast<FunctionRefBaseInst>(inst)) {
671
- if (auto dc = f->getInitiallyReferencedFunction ()->getDeclContext ()) {
672
- resultingString += getNameFromDecl (dc->getAsDecl ());
673
- return ;
674
- }
675
-
676
- resultingString += " <unknown decl>" ;
677
- return ;
678
- }
679
-
680
- if (auto m = dyn_cast<MethodInst>(inst)) {
681
- resultingString += getNameFromDecl (m->getMember ().getDecl ());
682
- return ;
683
- }
684
-
685
- if (auto *sei = dyn_cast<StructExtractInst>(inst)) {
686
- resultingString += getNameFromDecl (sei->getField ());
687
- return ;
688
- }
689
-
690
- if (auto *tei = dyn_cast<TupleExtractInst>(inst)) {
691
- llvm::raw_svector_ostream stream (resultingString);
692
- stream << tei->getFieldIndex ();
693
- return ;
694
- }
695
-
696
- if (auto *uedi = dyn_cast<UncheckedEnumDataInst>(inst)) {
697
- resultingString += getNameFromDecl (uedi->getElement ());
698
- return ;
699
- }
700
-
701
- if (auto *sei = dyn_cast<StructElementAddrInst>(inst)) {
702
- resultingString += getNameFromDecl (sei->getField ());
703
- return ;
704
- }
705
-
706
- if (auto *tei = dyn_cast<TupleElementAddrInst>(inst)) {
707
- llvm::raw_svector_ostream stream (resultingString);
708
- stream << tei->getFieldIndex ();
709
- return ;
710
- }
711
-
712
- if (auto *uedi = dyn_cast<UncheckedTakeEnumDataAddrInst>(inst)) {
713
- resultingString += getNameFromDecl (uedi->getElement ());
714
- return ;
715
- }
716
-
717
- if (auto *ei = dyn_cast<EnumInst>(inst)) {
718
- resultingString += getNameFromDecl (ei->getElement ());
719
- return ;
720
- }
721
-
722
- resultingString += " <unknown decl>" ;
723
- return ;
724
- }
725
-
726
- auto value = next.get <SILValue>();
727
- if (auto *fArg = dyn_cast<SILFunctionArgument>(value)) {
728
- resultingString += fArg ->getDecl ()->getBaseName ().userFacingName ();
729
- return ;
730
- }
731
-
732
- if (auto *dti = dyn_cast_or_null<DestructureTupleInst>(
733
- value->getDefiningInstruction ())) {
734
- llvm::raw_svector_ostream stream (resultingString);
735
- stream << *dti->getIndexOfResult (value);
736
- return ;
737
- }
738
-
739
- if (auto *dsi = dyn_cast_or_null<DestructureStructInst>(
740
- value->getDefiningInstruction ())) {
741
- unsigned index = *dsi->getIndexOfResult (value);
742
- resultingString +=
743
- getNameFromDecl (dsi->getStructDecl ()->getStoredProperties ()[index]);
744
- return ;
745
- }
746
-
747
- resultingString += " <unknown decl>" ;
672
+ return UnknownDeclString;
748
673
}
749
674
750
675
void VariableNameInferrer::drainVariableNamePath () {
@@ -753,7 +678,7 @@ void VariableNameInferrer::drainVariableNamePath() {
753
678
754
679
// Walk backwards, constructing our string.
755
680
while (true ) {
756
- popSingleVariableName ();
681
+ resultingString += variableNamePath. pop_back_val ();
757
682
758
683
if (variableNamePath.empty ())
759
684
return ;
0 commit comments