Skip to content

Commit 6b37764

Browse files
authored
Merge pull request #62053 from abdulowork/improve-type-checker-debugging-indentation
Improve indentation in type checker debugging output
2 parents 8bdfbb6 + 3f36694 commit 6b37764

11 files changed

+193
-144
lines changed

include/swift/Sema/ConstraintSystem.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1708,7 +1708,7 @@ class Solution {
17081708
SWIFT_DEBUG_DUMP;
17091709

17101710
/// Dump this solution.
1711-
void dump(raw_ostream &OS) const LLVM_ATTRIBUTE_USED;
1711+
void dump(raw_ostream &OS, unsigned indent) const LLVM_ATTRIBUTE_USED;
17121712
};
17131713

17141714
/// Describes the differences between several solutions to the same
@@ -6424,9 +6424,10 @@ class DisjunctionChoice {
64246424
bool isSymmetricOperator() const;
64256425
bool isUnaryOperator() const;
64266426

6427-
void print(llvm::raw_ostream &Out, SourceManager *SM, unsigned indent = 0) const {
6427+
void print(llvm::raw_ostream &Out, SourceManager *SM,
6428+
unsigned indent = 0) const {
64286429
Out << "disjunction choice ";
6429-
Choice->print(Out, SM);
6430+
Choice->print(Out, SM, indent);
64306431
}
64316432

64326433
operator Constraint *() { return Choice; }

lib/Sema/BuilderTransform.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2370,9 +2370,10 @@ Optional<BraceStmt *> TypeChecker::applyResultBuilderBodyTransform(
23702370
}
23712371

23722372
if (cs.isDebugMode()) {
2373-
auto &log = llvm::errs();
2373+
auto indent = cs.solverState ? cs.solverState->getCurrentIndent() : 0;
2374+
auto &log = llvm::errs().indent(indent);
23742375
log << "--- Applying Solution ---\n";
2375-
solutions.front().dump(log);
2376+
solutions.front().dump(log, indent);
23762377
log << '\n';
23772378
}
23782379

@@ -2387,7 +2388,8 @@ Optional<BraceStmt *> TypeChecker::applyResultBuilderBodyTransform(
23872388
auto *body = result->getFunctionBody();
23882389

23892390
if (cs.isDebugMode()) {
2390-
auto &log = llvm::errs();
2391+
auto indent = cs.solverState ? cs.solverState->getCurrentIndent() : 0;
2392+
auto &log = llvm::errs().indent(indent);
23912393
log << "--- Type-checked function body ---\n";
23922394
body->dump(log);
23932395
log << '\n';

lib/Sema/CSRanking.cpp

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,8 @@ void ConstraintSystem::increaseScore(ScoreKind kind, unsigned value) {
5555
if (isDebugMode() && value > 0) {
5656
if (solverState)
5757
llvm::errs().indent(solverState->getCurrentIndent());
58-
llvm::errs() << "(increasing '" << Score::getNameFor(kind) << "' score by " << value
59-
<< ")\n";
58+
llvm::errs() << "(increasing '" << Score::getNameFor(kind) << "' score by "
59+
<< value << ")\n";
6060
}
6161

6262
unsigned index = static_cast<unsigned>(kind);
@@ -73,7 +73,7 @@ bool ConstraintSystem::worseThanBestSolution() const {
7373

7474
if (isDebugMode()) {
7575
llvm::errs().indent(solverState->getCurrentIndent())
76-
<< "(solution is worse than the best solution)\n";
76+
<< "(solution is worse than the best solution)\n";
7777
}
7878

7979
return true;
@@ -804,7 +804,7 @@ SolutionCompareResult ConstraintSystem::compareSolutions(
804804
const SolutionDiff &diff, unsigned idx1, unsigned idx2) {
805805
if (cs.isDebugMode()) {
806806
llvm::errs().indent(cs.solverState->getCurrentIndent())
807-
<< "comparing solutions " << idx1 << " and " << idx2 <<"\n";
807+
<< "comparing solutions " << idx1 << " and " << idx2 << "\n";
808808
}
809809

810810
// Whether the solutions are identical.
@@ -1360,13 +1360,15 @@ ConstraintSystem::findBestSolution(SmallVectorImpl<Solution> &viable,
13601360
return 0;
13611361

13621362
if (isDebugMode()) {
1363-
llvm::errs().indent(solverState->getCurrentIndent())
1364-
<< "Comparing " << viable.size() << " viable solutions\n";
1363+
auto indent = solverState->getCurrentIndent();
1364+
auto &log = llvm::errs();
13651365

1366+
log.indent(indent) << "Comparing " << viable.size()
1367+
<< " viable solutions\n";
13661368
for (unsigned i = 0, n = viable.size(); i != n; ++i) {
1367-
llvm::errs().indent(solverState->getCurrentIndent())
1368-
<< "\n--- Solution #" << i << " ---\n";
1369-
viable[i].dump(llvm::errs().indent(solverState->getCurrentIndent()));
1369+
log << "\n";
1370+
log.indent(indent) << "--- Solution #" << i << " ---\n";
1371+
viable[i].dump(llvm::errs(), indent);
13701372
}
13711373
}
13721374

lib/Sema/CSSolver.cpp

Lines changed: 53 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -353,17 +353,21 @@ bool ConstraintSystem::simplify() {
353353
auto *constraint = &ActiveConstraints.front();
354354
deactivateConstraint(constraint);
355355

356+
auto isSimplifiable =
357+
constraint->getKind() != ConstraintKind::Disjunction &&
358+
constraint->getKind() != ConstraintKind::Conjunction;
359+
356360
if (isDebugMode()) {
357361
auto &log = llvm::errs();
358362
log.indent(solverState->getCurrentIndent());
359363
log << "(considering -> ";
360-
constraint->print(log, &getASTContext().SourceMgr);
364+
constraint->print(log, &getASTContext().SourceMgr,
365+
solverState->getCurrentIndent());
361366
log << "\n";
362367

363368
// {Dis, Con}junction are returned unsolved in \c simplifyConstraint() and
364369
// handled separately by solver steps.
365-
if (constraint->getKind() != ConstraintKind::Disjunction &&
366-
constraint->getKind() != ConstraintKind::Conjunction) {
370+
if (isSimplifiable) {
367371
log.indent(solverState->getCurrentIndent() + 2)
368372
<< "(simplification result:\n";
369373
}
@@ -375,7 +379,9 @@ bool ConstraintSystem::simplify() {
375379
retireFailedConstraint(constraint);
376380
if (isDebugMode()) {
377381
auto &log = llvm::errs();
378-
log.indent(solverState->getCurrentIndent() + 2) << ")\n";
382+
if (isSimplifiable) {
383+
log.indent(solverState->getCurrentIndent() + 2) << ")\n";
384+
}
379385
log.indent(solverState->getCurrentIndent() + 2) << "(outcome: error)\n";
380386
}
381387
break;
@@ -386,7 +392,9 @@ bool ConstraintSystem::simplify() {
386392
retireConstraint(constraint);
387393
if (isDebugMode()) {
388394
auto &log = llvm::errs();
389-
log.indent(solverState->getCurrentIndent() + 2) << ")\n";
395+
if (isSimplifiable) {
396+
log.indent(solverState->getCurrentIndent() + 2) << ")\n";
397+
}
390398
log.indent(solverState->getCurrentIndent() + 2)
391399
<< "(outcome: simplified)\n";
392400
}
@@ -397,7 +405,9 @@ bool ConstraintSystem::simplify() {
397405
++solverState->NumUnsimplifiedConstraints;
398406
if (isDebugMode()) {
399407
auto &log = llvm::errs();
400-
log.indent(solverState->getCurrentIndent() + 2) << ")\n";
408+
if (isSimplifiable) {
409+
log.indent(solverState->getCurrentIndent() + 2) << ")\n";
410+
}
401411
log.indent(solverState->getCurrentIndent() + 2)
402412
<< "(outcome: unsolved)\n";
403413
}
@@ -489,7 +499,8 @@ ConstraintSystem::SolverState::SolverState(
489499
if (tyOpts.DebugConstraintSolverAttempt &&
490500
tyOpts.DebugConstraintSolverAttempt == SolutionAttempt) {
491501
CS.Options |= ConstraintSystemFlags::DebugConstraints;
492-
llvm::errs() << "---Constraint system #" << SolutionAttempt << "---\n";
502+
llvm::errs().indent(CS.solverState->getCurrentIndent())
503+
<< "---Constraint system #" << SolutionAttempt << "---\n";
493504
CS.print(llvm::errs());
494505
}
495506
}
@@ -793,7 +804,8 @@ bool ConstraintSystem::Candidate::solve(
793804
auto &ctx = cs.getASTContext();
794805
if (cs.isDebugMode()) {
795806
auto &log = llvm::errs();
796-
log << "--- Solving candidate for shrinking at ";
807+
auto indent = cs.solverState ? cs.solverState->getCurrentIndent() : 0;
808+
log.indent(indent) << "--- Solving candidate for shrinking at ";
797809
auto R = E->getSourceRange();
798810
if (R.isValid()) {
799811
R.print(log, ctx.SourceMgr, /*PrintText=*/ false);
@@ -802,7 +814,7 @@ bool ConstraintSystem::Candidate::solve(
802814
}
803815
log << " ---\n";
804816

805-
E->dump(log);
817+
E->dump(log, indent);
806818
log << '\n';
807819
cs.print(log);
808820
}
@@ -830,14 +842,18 @@ bool ConstraintSystem::Candidate::solve(
830842

831843
if (cs.isDebugMode()) {
832844
auto &log = llvm::errs();
845+
auto indent = cs.solverState ? cs.solverState->getCurrentIndent() : 0;
833846
if (solutions.empty()) {
834-
log << "--- No Solutions ---\n";
847+
log << "\n";
848+
log.indent(indent) << "--- No Solutions ---\n";
835849
} else {
836-
log << "--- Solutions ---\n";
850+
log << "\n";
851+
log.indent(indent) << "--- Solutions ---\n";
837852
for (unsigned i = 0, n = solutions.size(); i != n; ++i) {
838853
auto &solution = solutions[i];
839-
log << "\n--- Solution #" << i << " ---\n";
840-
solution.dump(log);
854+
log << "\n";
855+
log.indent(indent) << "--- Solution #" << i << " ---\n";
856+
solution.dump(log, indent);
841857
}
842858
}
843859
}
@@ -1320,14 +1336,18 @@ Optional<std::vector<Solution>> ConstraintSystem::solve(
13201336
auto dumpSolutions = [&](const SolutionResult &result) {
13211337
// Debug-print the set of solutions.
13221338
if (isDebugMode()) {
1339+
auto &log = llvm::errs();
1340+
auto indent = solverState ? solverState->getCurrentIndent() : 0;
13231341
if (result.getKind() == SolutionResult::Success) {
1324-
llvm::errs() << "\n---Solution---\n";
1325-
result.getSolution().dump(llvm::errs());
1342+
log << "\n";
1343+
log.indent(indent) << "---Solution---\n";
1344+
result.getSolution().dump(llvm::errs(), indent);
13261345
} else if (result.getKind() == SolutionResult::Ambiguous) {
13271346
auto solutions = result.getAmbiguousSolutions();
13281347
for (unsigned i : indices(solutions)) {
1329-
llvm::errs() << "\n--- Solution #" << i << " ---\n";
1330-
solutions[i].dump(llvm::errs());
1348+
log << "\n";
1349+
log.indent(indent) << "--- Solution #" << i << " ---\n";
1350+
solutions[i].dump(llvm::errs(), indent);
13311351
}
13321352
}
13331353
}
@@ -1596,10 +1616,12 @@ void ConstraintSystem::solveForCodeCompletion(
15961616

15971617
if (isDebugMode()) {
15981618
auto &log = llvm::errs();
1599-
log << "--- Discovered " << solutions.size() << " solutions ---\n";
1619+
auto indent = solverState ? solverState->getCurrentIndent() : 0;
1620+
log.indent(indent) << "--- Discovered " << solutions.size()
1621+
<< " solutions ---\n";
16001622
for (const auto &solution : solutions) {
1601-
log << "--- Solution ---\n";
1602-
solution.dump(log);
1623+
log.indent(indent) << "--- Solution ---\n";
1624+
solution.dump(log, indent);
16031625
}
16041626
}
16051627

@@ -1621,7 +1643,8 @@ bool ConstraintSystem::solveForCodeCompletion(
16211643

16221644
if (isDebugMode()) {
16231645
auto &log = llvm::errs();
1624-
log << "--- Code Completion ---\n";
1646+
log.indent(solverState ? solverState->getCurrentIndent() : 0)
1647+
<< "--- Code Completion ---\n";
16251648
}
16261649

16271650
if (generateConstraints(target, FreeTypeVariableBinding::Disallow))
@@ -1665,10 +1688,10 @@ ConstraintSystem::filterDisjunction(
16651688
}
16661689

16671690
if (isDebugMode()) {
1668-
llvm::errs().indent(solverState ? solverState->getCurrentIndent() : 0)
1669-
<< "(disabled disjunction term ";
1670-
constraint->print(llvm::errs(), &ctx.SourceMgr);
1671-
llvm::errs() << ")\n";
1691+
auto indent = (solverState ? solverState->getCurrentIndent() : 0) + 4;
1692+
llvm::errs().indent(indent) << "(disabled disjunction term ";
1693+
constraint->print(llvm::errs(), &ctx.SourceMgr, indent);
1694+
llvm::errs().indent(indent) << ")\n";
16721695
}
16731696

16741697
if (restoreOnFail)
@@ -1724,10 +1747,11 @@ ConstraintSystem::filterDisjunction(
17241747
}
17251748

17261749
if (isDebugMode()) {
1727-
llvm::errs().indent(solverState ? solverState->getCurrentIndent(): 0)
1728-
<< "(introducing single enabled disjunction term ";
1729-
choice->print(llvm::errs(), &ctx.SourceMgr);
1730-
llvm::errs() << ")\n";
1750+
auto indent = (solverState ? solverState->getCurrentIndent() : 0) + 4;
1751+
llvm::errs().indent(indent)
1752+
<< "(introducing single enabled disjunction term ";
1753+
choice->print(llvm::errs(), &ctx.SourceMgr, indent);
1754+
llvm::errs().indent(indent) << ")\n";
17311755
}
17321756

17331757
simplifyDisjunctionChoice(choice);

lib/Sema/CSStep.cpp

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -105,13 +105,14 @@ void SplitterStep::computeFollowupSteps(
105105

106106
if (CS.isDebugMode()) {
107107
auto &log = getDebugLogger();
108+
auto indent = CS.solverState->getCurrentIndent();
108109
// Verify that the constraint graph is valid.
109110
CG.verify();
110111

111-
log << "---Constraint graph---\n";
112+
log.indent(indent) << "---Constraint graph---\n";
112113
CG.print(CS.getTypeVariables(), log);
113114

114-
log << "---Connected components---\n";
115+
log.indent(indent) << "---Connected components---\n";
115116
CG.printConnectedComponents(CS.getTypeVariables(), log);
116117
}
117118

@@ -362,12 +363,6 @@ StepResult ComponentStep::take(bool prevFailed) {
362363
auto *conjunction = CS.selectConjunction();
363364

364365
if (CS.isDebugMode()) {
365-
if (!potentialBindings.empty()) {
366-
auto &log = getDebugLogger();
367-
log << "(Potential Binding(s): " << '\n';
368-
log << potentialBindings;
369-
}
370-
371366
SmallVector<Constraint *, 4> disjunctions;
372367
CS.collectDisjunctions(disjunctions);
373368
std::vector<std::string> overloadDisjunctions;
@@ -380,17 +375,24 @@ StepResult ComponentStep::take(bool prevFailed) {
380375
overloadDisjunctions.push_back(
381376
constraints[0]->getFirstType()->getString(PO));
382377
}
378+
379+
if (!potentialBindings.empty() || !overloadDisjunctions.empty()) {
380+
auto &log = getDebugLogger();
381+
log << "(Potential Binding(s): " << '\n';
382+
log << potentialBindings;
383+
}
384+
383385
if (!overloadDisjunctions.empty()) {
384386
auto &log = getDebugLogger();
385-
log.indent(2);
387+
log.indent(CS.solverState->getCurrentIndent() + 2);
386388
log << "Disjunction(s) = [";
387389
interleave(overloadDisjunctions, log, ", ");
388390
log << "]\n";
391+
}
389392

390-
if (!potentialBindings.empty() || !overloadDisjunctions.empty()) {
391-
auto &log = getDebugLogger();
392-
log << ")\n";
393-
}
393+
if (!potentialBindings.empty() || !overloadDisjunctions.empty()) {
394+
auto &log = getDebugLogger();
395+
log << ")\n";
394396
}
395397
}
396398

@@ -441,7 +443,9 @@ StepResult ComponentStep::take(bool prevFailed) {
441443

442444
auto printConstraints = [&](const ConstraintList &constraints) {
443445
for (auto &constraint : constraints)
444-
constraint.print(getDebugLogger(), &CS.getASTContext().SourceMgr);
446+
constraint.print(
447+
getDebugLogger().indent(CS.solverState->getCurrentIndent()),
448+
&CS.getASTContext().SourceMgr, CS.solverState->getCurrentIndent());
445449
};
446450

447451
// If we don't have any disjunction or type variable choices left, we're done
@@ -683,8 +687,8 @@ bool DisjunctionStep::shouldSkip(const DisjunctionChoice &choice) const {
683687
if (CS.isDebugMode()) {
684688
auto &log = getDebugLogger();
685689
log << "(skipping " + reason + " ";
686-
choice.print(log, &ctx.SourceMgr);
687-
log << '\n';
690+
choice.print(log, &ctx.SourceMgr, CS.solverState->getCurrentIndent());
691+
log << ")\n";
688692
}
689693

690694
return true;

lib/Sema/CSStep.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -707,7 +707,8 @@ class DisjunctionStep final : public BindingStep<DisjunctionChoiceProducer> {
707707

708708
void print(llvm::raw_ostream &Out) override {
709709
Out << "DisjunctionStep for ";
710-
Disjunction->print(Out, &CS.getASTContext().SourceMgr);
710+
Disjunction->print(Out, &CS.getASTContext().SourceMgr,
711+
CS.solverState->getCurrentIndent());
711712
Out << '\n';
712713
}
713714

@@ -1013,7 +1014,8 @@ class ConjunctionStep : public BindingStep<ConjunctionElementProducer> {
10131014

10141015
void print(llvm::raw_ostream &Out) override {
10151016
Out << "ConjunctionStep for ";
1016-
Conjunction->print(Out, &CS.getASTContext().SourceMgr);
1017+
Conjunction->print(Out, &CS.getASTContext().SourceMgr,
1018+
CS.solverState->getCurrentIndent());
10171019
Out << '\n';
10181020
}
10191021

0 commit comments

Comments
 (0)