Skip to content

Commit 3f36694

Browse files
committed
Improve indentation in debugging output
1 parent 3c401a1 commit 3f36694

11 files changed

+167
-128
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
@@ -6433,9 +6433,10 @@ class DisjunctionChoice {
64336433
bool isSymmetricOperator() const;
64346434
bool isUnaryOperator() const;
64356435

6436-
void print(llvm::raw_ostream &Out, SourceManager *SM, unsigned indent = 0) const {
6436+
void print(llvm::raw_ostream &Out, SourceManager *SM,
6437+
unsigned indent = 0) const {
64376438
Out << "disjunction choice ";
6438-
Choice->print(Out, SM);
6439+
Choice->print(Out, SM, indent);
64396440
}
64406441

64416442
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: 39 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -361,7 +361,8 @@ bool ConstraintSystem::simplify() {
361361
auto &log = llvm::errs();
362362
log.indent(solverState->getCurrentIndent());
363363
log << "(considering -> ";
364-
constraint->print(log, &getASTContext().SourceMgr);
364+
constraint->print(log, &getASTContext().SourceMgr,
365+
solverState->getCurrentIndent());
365366
log << "\n";
366367

367368
// {Dis, Con}junction are returned unsolved in \c simplifyConstraint() and
@@ -498,7 +499,8 @@ ConstraintSystem::SolverState::SolverState(
498499
if (tyOpts.DebugConstraintSolverAttempt &&
499500
tyOpts.DebugConstraintSolverAttempt == SolutionAttempt) {
500501
CS.Options |= ConstraintSystemFlags::DebugConstraints;
501-
llvm::errs() << "---Constraint system #" << SolutionAttempt << "---\n";
502+
llvm::errs().indent(CS.solverState->getCurrentIndent())
503+
<< "---Constraint system #" << SolutionAttempt << "---\n";
502504
CS.print(llvm::errs());
503505
}
504506
}
@@ -802,7 +804,8 @@ bool ConstraintSystem::Candidate::solve(
802804
auto &ctx = cs.getASTContext();
803805
if (cs.isDebugMode()) {
804806
auto &log = llvm::errs();
805-
log << "--- Solving candidate for shrinking at ";
807+
auto indent = cs.solverState ? cs.solverState->getCurrentIndent() : 0;
808+
log.indent(indent) << "--- Solving candidate for shrinking at ";
806809
auto R = E->getSourceRange();
807810
if (R.isValid()) {
808811
R.print(log, ctx.SourceMgr, /*PrintText=*/ false);
@@ -811,7 +814,7 @@ bool ConstraintSystem::Candidate::solve(
811814
}
812815
log << " ---\n";
813816

814-
E->dump(log);
817+
E->dump(log, indent);
815818
log << '\n';
816819
cs.print(log);
817820
}
@@ -839,14 +842,18 @@ bool ConstraintSystem::Candidate::solve(
839842

840843
if (cs.isDebugMode()) {
841844
auto &log = llvm::errs();
845+
auto indent = cs.solverState ? cs.solverState->getCurrentIndent() : 0;
842846
if (solutions.empty()) {
843-
log << "--- No Solutions ---\n";
847+
log << "\n";
848+
log.indent(indent) << "--- No Solutions ---\n";
844849
} else {
845-
log << "--- Solutions ---\n";
850+
log << "\n";
851+
log.indent(indent) << "--- Solutions ---\n";
846852
for (unsigned i = 0, n = solutions.size(); i != n; ++i) {
847853
auto &solution = solutions[i];
848-
log << "\n--- Solution #" << i << " ---\n";
849-
solution.dump(log);
854+
log << "\n";
855+
log.indent(indent) << "--- Solution #" << i << " ---\n";
856+
solution.dump(log, indent);
850857
}
851858
}
852859
}
@@ -1329,14 +1336,18 @@ Optional<std::vector<Solution>> ConstraintSystem::solve(
13291336
auto dumpSolutions = [&](const SolutionResult &result) {
13301337
// Debug-print the set of solutions.
13311338
if (isDebugMode()) {
1339+
auto &log = llvm::errs();
1340+
auto indent = solverState ? solverState->getCurrentIndent() : 0;
13321341
if (result.getKind() == SolutionResult::Success) {
1333-
llvm::errs() << "\n---Solution---\n";
1334-
result.getSolution().dump(llvm::errs());
1342+
log << "\n";
1343+
log.indent(indent) << "---Solution---\n";
1344+
result.getSolution().dump(llvm::errs(), indent);
13351345
} else if (result.getKind() == SolutionResult::Ambiguous) {
13361346
auto solutions = result.getAmbiguousSolutions();
13371347
for (unsigned i : indices(solutions)) {
1338-
llvm::errs() << "\n--- Solution #" << i << " ---\n";
1339-
solutions[i].dump(llvm::errs());
1348+
log << "\n";
1349+
log.indent(indent) << "--- Solution #" << i << " ---\n";
1350+
solutions[i].dump(llvm::errs(), indent);
13401351
}
13411352
}
13421353
}
@@ -1605,10 +1616,12 @@ void ConstraintSystem::solveForCodeCompletion(
16051616

16061617
if (isDebugMode()) {
16071618
auto &log = llvm::errs();
1608-
log << "--- Discovered " << solutions.size() << " solutions ---\n";
1619+
auto indent = solverState ? solverState->getCurrentIndent() : 0;
1620+
log.indent(indent) << "--- Discovered " << solutions.size()
1621+
<< " solutions ---\n";
16091622
for (const auto &solution : solutions) {
1610-
log << "--- Solution ---\n";
1611-
solution.dump(log);
1623+
log.indent(indent) << "--- Solution ---\n";
1624+
solution.dump(log, indent);
16121625
}
16131626
}
16141627

@@ -1630,7 +1643,8 @@ bool ConstraintSystem::solveForCodeCompletion(
16301643

16311644
if (isDebugMode()) {
16321645
auto &log = llvm::errs();
1633-
log << "--- Code Completion ---\n";
1646+
log.indent(solverState ? solverState->getCurrentIndent() : 0)
1647+
<< "--- Code Completion ---\n";
16341648
}
16351649

16361650
if (generateConstraints(target, FreeTypeVariableBinding::Disallow))
@@ -1674,10 +1688,10 @@ ConstraintSystem::filterDisjunction(
16741688
}
16751689

16761690
if (isDebugMode()) {
1677-
llvm::errs().indent(solverState ? solverState->getCurrentIndent() : 0)
1678-
<< "(disabled disjunction term ";
1679-
constraint->print(llvm::errs(), &ctx.SourceMgr);
1680-
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";
16811695
}
16821696

16831697
if (restoreOnFail)
@@ -1733,10 +1747,11 @@ ConstraintSystem::filterDisjunction(
17331747
}
17341748

17351749
if (isDebugMode()) {
1736-
llvm::errs().indent(solverState ? solverState->getCurrentIndent(): 0)
1737-
<< "(introducing single enabled disjunction term ";
1738-
choice->print(llvm::errs(), &ctx.SourceMgr);
1739-
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";
17401755
}
17411756

17421757
simplifyDisjunctionChoice(choice);

lib/Sema/CSStep.cpp

Lines changed: 8 additions & 5 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

@@ -382,7 +383,7 @@ StepResult ComponentStep::take(bool prevFailed) {
382383

383384
if (!overloadDisjunctions.empty()) {
384385
auto &log = getDebugLogger();
385-
log.indent(2);
386+
log.indent(CS.solverState->getCurrentIndent() + 2);
386387
log << "Disjunction(s) = [";
387388
interleave(overloadDisjunctions, log, ", ");
388389
log << "]\n";
@@ -429,7 +430,9 @@ StepResult ComponentStep::take(bool prevFailed) {
429430

430431
auto printConstraints = [&](const ConstraintList &constraints) {
431432
for (auto &constraint : constraints)
432-
constraint.print(getDebugLogger(), &CS.getASTContext().SourceMgr);
433+
constraint.print(
434+
getDebugLogger().indent(CS.solverState->getCurrentIndent()),
435+
&CS.getASTContext().SourceMgr, CS.solverState->getCurrentIndent());
433436
};
434437

435438
// If we don't have any disjunction or type variable choices left, we're done
@@ -671,7 +674,7 @@ bool DisjunctionStep::shouldSkip(const DisjunctionChoice &choice) const {
671674
if (CS.isDebugMode()) {
672675
auto &log = getDebugLogger();
673676
log << "(skipping " + reason + " ";
674-
choice.print(log, &ctx.SourceMgr);
677+
choice.print(log, &ctx.SourceMgr, CS.solverState->getCurrentIndent());
675678
log << ")\n";
676679
}
677680

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

lib/Sema/Constraint.cpp

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -360,7 +360,8 @@ Constraint *Constraint::clone(ConstraintSystem &cs) const {
360360
llvm_unreachable("Unhandled ConstraintKind in switch.");
361361
}
362362

363-
void Constraint::print(llvm::raw_ostream &Out, SourceManager *sm, unsigned indent, bool skipLocator) const {
363+
void Constraint::print(llvm::raw_ostream &Out, SourceManager *sm,
364+
unsigned indent, bool skipLocator) const {
364365
// Print all type variables as $T0 instead of _ here.
365366
PrintOptions PO;
366367
PO.PrintTypesForDebugging = true;
@@ -395,19 +396,21 @@ void Constraint::print(llvm::raw_ostream &Out, SourceManager *sm, unsigned inden
395396
return false;
396397
});
397398

398-
interleave(sortedConstraints,
399-
[&](Constraint *constraint) {
400-
Out.indent(indent);
401-
if (constraint->isDisabled())
402-
Out << "> [disabled] ";
403-
else if (constraint->isFavored())
404-
Out << "> [favored] ";
405-
else
406-
Out << "> ";
407-
constraint->print(Out, sm, indent,
408-
/*skipLocator=*/constraint->getLocator() == Locator);
409-
},
410-
[&] { Out << "\n"; });
399+
interleave(
400+
sortedConstraints,
401+
[&](Constraint *constraint) {
402+
Out.indent(indent + 2);
403+
if (constraint->isDisabled())
404+
Out << "> [disabled] ";
405+
else if (constraint->isFavored())
406+
Out << "> [favored] ";
407+
else
408+
Out << "> ";
409+
constraint->print(Out, sm, indent,
410+
/*skipLocator=*/constraint->getLocator() ==
411+
Locator);
412+
},
413+
[&] { Out << "\n"; });
411414
return;
412415
}
413416

lib/Sema/ConstraintGraph.cpp

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1438,12 +1438,12 @@ bool ConstraintGraph::contractEdges() {
14381438
auto rep2 = CS.getRepresentative(tyvar2);
14391439

14401440
if (CS.isDebugMode()) {
1441-
auto &log = llvm::errs();
1442-
if (CS.solverState)
1443-
log.indent(CS.solverState->getCurrentIndent());
1441+
auto indent = CS.solverState ? CS.solverState->getCurrentIndent() : 0;
1442+
auto &log = llvm::errs().indent(indent);
14441443

14451444
log << "Contracting constraint ";
1446-
constraint->print(log, &CS.getASTContext().SourceMgr);
1445+
constraint->print(log.indent(indent), &CS.getASTContext().SourceMgr,
1446+
indent);
14471447
log << "\n";
14481448
}
14491449

@@ -1488,7 +1488,7 @@ void ConstraintGraphNode::print(llvm::raw_ostream &out, unsigned indent,
14881488

14891489
for (auto constraint : sortedConstraints) {
14901490
out.indent(indent + 4);
1491-
constraint->print(out, &TypeVar->getASTContext().SourceMgr);
1491+
constraint->print(out, &TypeVar->getASTContext().SourceMgr, indent + 4);
14921492
out << "\n";
14931493
}
14941494
}
@@ -1545,7 +1545,8 @@ void ConstraintGraph::print(ArrayRef<TypeVariableType *> typeVars,
15451545
PO.PrintTypesForDebugging = true;
15461546

15471547
for (auto typeVar : typeVars) {
1548-
(*this)[typeVar].print(out, 2, PO);
1548+
(*this)[typeVar].print(
1549+
out, (CS.solverState ? CS.solverState->getCurrentIndent() : 0) + 2, PO);
15491550
out << "\n";
15501551
}
15511552
}
@@ -1682,7 +1683,7 @@ void ConstraintGraph::printConnectedComponents(
16821683
PrintOptions PO;
16831684
PO.PrintTypesForDebugging = true;
16841685
for (const auto& component : components) {
1685-
out.indent(2);
1686+
out.indent((CS.solverState ? CS.solverState->getCurrentIndent() : 0) + 2);
16861687
out << component.solutionIndex << ": ";
16871688
SWIFT_DEFER {
16881689
out << '\n';

0 commit comments

Comments
 (0)