Skip to content

Commit ff706c6

Browse files
author
Timofey Solonin
committed
Improve indentation in debugging output
1 parent d8af6e8 commit ff706c6

11 files changed

+159
-125
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
@@ -6436,9 +6436,10 @@ class DisjunctionChoice {
64366436
bool isSymmetricOperator() const;
64376437
bool isUnaryOperator() const;
64386438

6439-
void print(llvm::raw_ostream &Out, SourceManager *SM, unsigned indent = 0) const {
6439+
void print(llvm::raw_ostream &Out, SourceManager *SM,
6440+
unsigned indent = 0) const {
64406441
Out << "disjunction choice ";
6441-
Choice->print(Out, SM);
6442+
Choice->print(Out, SM, indent);
64426443
}
64436444

64446445
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.getCurrentIndent();
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.getCurrentIndent();
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: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1365,9 +1365,9 @@ ConstraintSystem::findBestSolution(SmallVectorImpl<Solution> &viable,
13651365
log.indent(indent) << "Comparing " << viable.size()
13661366
<< " viable solutions\n";
13671367
for (unsigned i = 0, n = viable.size(); i != n; ++i) {
1368-
log.indent(indent)
1369-
<< "\n--- Solution #" << i << " ---\n";
1370-
viable[i].dump(llvm::errs().indent(indent));
1368+
log << "\n";
1369+
log.indent(indent) << "--- Solution #" << i << " ---\n";
1370+
viable[i].dump(llvm::errs(), indent);
13711371
}
13721372
}
13731373

lib/Sema/CSSolver.cpp

Lines changed: 39 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -361,7 +361,7 @@ bool ConstraintSystem::simplify() {
361361
auto &log = llvm::errs();
362362
log.indent(getCurrentIndent());
363363
log << "(considering -> ";
364-
constraint->print(log, &getASTContext().SourceMgr);
364+
constraint->print(log, &getASTContext().SourceMgr, getCurrentIndent());
365365
log << "\n";
366366

367367
// {Dis, Con}junction are returned unsolved in \c simplifyConstraint() and
@@ -378,7 +378,7 @@ bool ConstraintSystem::simplify() {
378378
if (isDebugMode()) {
379379
auto &log = llvm::errs();
380380
if (hasSimplificationResult) {
381-
log.indent(getCurrentIndent() + 2) << ")\n";
381+
log.indent(getCurrentIndent() + 2) << ")\n";
382382
}
383383
log.indent(getCurrentIndent() + 2) << "(outcome: error)\n";
384384
}
@@ -391,7 +391,7 @@ bool ConstraintSystem::simplify() {
391391
if (isDebugMode()) {
392392
auto &log = llvm::errs();
393393
if (hasSimplificationResult) {
394-
log.indent(getCurrentIndent() + 2) << ")\n";
394+
log.indent(getCurrentIndent() + 2) << ")\n";
395395
}
396396
log.indent(getCurrentIndent() + 2) << "(outcome: simplified)\n";
397397
}
@@ -495,7 +495,8 @@ ConstraintSystem::SolverState::SolverState(
495495
if (tyOpts.DebugConstraintSolverAttempt &&
496496
tyOpts.DebugConstraintSolverAttempt == SolutionAttempt) {
497497
CS.Options |= ConstraintSystemFlags::DebugConstraints;
498-
llvm::errs() << "---Constraint system #" << SolutionAttempt << "---\n";
498+
llvm::errs().indent(CS.getCurrentIndent())
499+
<< "---Constraint system #" << SolutionAttempt << "---\n";
499500
CS.print(llvm::errs());
500501
}
501502
}
@@ -799,7 +800,8 @@ bool ConstraintSystem::Candidate::solve(
799800
auto &ctx = cs.getASTContext();
800801
if (cs.isDebugMode()) {
801802
auto &log = llvm::errs();
802-
log << "--- Solving candidate for shrinking at ";
803+
auto indent = cs.getCurrentIndent();
804+
log.indent(indent) << "--- Solving candidate for shrinking at ";
803805
auto R = E->getSourceRange();
804806
if (R.isValid()) {
805807
R.print(log, ctx.SourceMgr, /*PrintText=*/ false);
@@ -808,7 +810,7 @@ bool ConstraintSystem::Candidate::solve(
808810
}
809811
log << " ---\n";
810812

811-
E->dump(log);
813+
E->dump(log, indent);
812814
log << '\n';
813815
cs.print(log);
814816
}
@@ -836,14 +838,18 @@ bool ConstraintSystem::Candidate::solve(
836838

837839
if (cs.isDebugMode()) {
838840
auto &log = llvm::errs();
841+
auto indent = cs.getCurrentIndent();
839842
if (solutions.empty()) {
840-
log << "--- No Solutions ---\n";
843+
log << "\n";
844+
log.indent(indent) << "--- No Solutions ---\n";
841845
} else {
842-
log << "--- Solutions ---\n";
846+
log << "\n";
847+
log.indent(indent) << "--- Solutions ---\n";
843848
for (unsigned i = 0, n = solutions.size(); i != n; ++i) {
844849
auto &solution = solutions[i];
845-
log << "\n--- Solution #" << i << " ---\n";
846-
solution.dump(log);
850+
log << "\n";
851+
log.indent(indent) << "--- Solution #" << i << " ---\n";
852+
solution.dump(log, indent);
847853
}
848854
}
849855
}
@@ -1326,14 +1332,18 @@ Optional<std::vector<Solution>> ConstraintSystem::solve(
13261332
auto dumpSolutions = [&](const SolutionResult &result) {
13271333
// Debug-print the set of solutions.
13281334
if (isDebugMode()) {
1335+
auto &log = llvm::errs();
1336+
auto indent = getCurrentIndent();
13291337
if (result.getKind() == SolutionResult::Success) {
1330-
llvm::errs() << "\n---Solution---\n";
1331-
result.getSolution().dump(llvm::errs());
1338+
log << "\n";
1339+
log.indent(indent) << "---Solution---\n";
1340+
result.getSolution().dump(llvm::errs(), indent);
13321341
} else if (result.getKind() == SolutionResult::Ambiguous) {
13331342
auto solutions = result.getAmbiguousSolutions();
13341343
for (unsigned i : indices(solutions)) {
1335-
llvm::errs() << "\n--- Solution #" << i << " ---\n";
1336-
solutions[i].dump(llvm::errs());
1344+
log << "\n";
1345+
log.indent(indent) << "--- Solution #" << i << " ---\n";
1346+
solutions[i].dump(llvm::errs(), indent);
13371347
}
13381348
}
13391349
}
@@ -1602,10 +1612,12 @@ void ConstraintSystem::solveForCodeCompletion(
16021612

16031613
if (isDebugMode()) {
16041614
auto &log = llvm::errs();
1605-
log << "--- Discovered " << solutions.size() << " solutions ---\n";
1615+
auto indent = getCurrentIndent();
1616+
log.indent(indent) << "--- Discovered " << solutions.size()
1617+
<< " solutions ---\n";
16061618
for (const auto &solution : solutions) {
1607-
log << "--- Solution ---\n";
1608-
solution.dump(log);
1619+
log.indent(indent) << "--- Solution ---\n";
1620+
solution.dump(log, indent);
16091621
}
16101622
}
16111623

@@ -1627,7 +1639,7 @@ bool ConstraintSystem::solveForCodeCompletion(
16271639

16281640
if (isDebugMode()) {
16291641
auto &log = llvm::errs();
1630-
log << "--- Code Completion ---\n";
1642+
log.indent(getCurrentIndent()) << "--- Code Completion ---\n";
16311643
}
16321644

16331645
if (generateConstraints(target, FreeTypeVariableBinding::Disallow))
@@ -1671,10 +1683,10 @@ ConstraintSystem::filterDisjunction(
16711683
}
16721684

16731685
if (isDebugMode()) {
1674-
llvm::errs().indent(getCurrentIndent())
1675-
<< "(disabled disjunction term ";
1676-
constraint->print(llvm::errs(), &ctx.SourceMgr);
1677-
llvm::errs() << ")\n";
1686+
auto indent = getCurrentIndent() + 4;
1687+
llvm::errs().indent(indent) << "(disabled disjunction term ";
1688+
constraint->print(llvm::errs(), &ctx.SourceMgr, indent);
1689+
llvm::errs().indent(indent) << ")\n";
16781690
}
16791691

16801692
if (restoreOnFail)
@@ -1730,10 +1742,11 @@ ConstraintSystem::filterDisjunction(
17301742
}
17311743

17321744
if (isDebugMode()) {
1733-
llvm::errs().indent(getCurrentIndent())
1734-
<< "(introducing single enabled disjunction term ";
1735-
choice->print(llvm::errs(), &ctx.SourceMgr);
1736-
llvm::errs() << ")\n";
1745+
auto indent = getCurrentIndent() + 4;
1746+
llvm::errs().indent(indent)
1747+
<< "(introducing single enabled disjunction term ";
1748+
choice->print(llvm::errs(), &ctx.SourceMgr, indent);
1749+
llvm::errs().indent(indent) << ")\n";
17371750
}
17381751

17391752
simplifyDisjunctionChoice(choice);

lib/Sema/CSStep.cpp

Lines changed: 10 additions & 8 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.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,17 +383,17 @@ StepResult ComponentStep::take(bool prevFailed) {
382383

383384
if (!overloadDisjunctions.empty()) {
384385
auto &log = getDebugLogger();
385-
log.indent(2);
386+
log.indent(CS.getCurrentIndent() + 2);
386387
log << "Disjunction(s) = [";
387388
interleave(overloadDisjunctions, log, ", ");
388389
log << "]\n";
389390
}
390391

391-
if (!potentialBindings.empty() || !overloadDisjunctions.empty()) {
392-
auto &log = getDebugLogger();
393-
log << ")\n";
394-
}
392+
if (!potentialBindings.empty() || !overloadDisjunctions.empty()) {
393+
auto &log = getDebugLogger();
394+
log << ")\n";
395395
}
396+
}
396397

397398
if (CS.shouldAttemptFixes()) {
398399
if ((bestBindings &&
@@ -429,7 +430,8 @@ 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(getDebugLogger().indent(CS.getCurrentIndent()),
434+
&CS.getASTContext().SourceMgr, CS.getCurrentIndent());
433435
};
434436

435437
// If we don't have any disjunction or type variable choices left, we're done

lib/Sema/CSStep.h

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

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

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

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

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: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1442,7 +1442,8 @@ bool ConstraintGraph::contractEdges() {
14421442
auto &log = llvm::errs().indent(indent);
14431443

14441444
log << "Contracting constraint ";
1445-
constraint->print(log, &CS.getASTContext().SourceMgr);
1445+
constraint->print(log.indent(indent), &CS.getASTContext().SourceMgr,
1446+
indent);
14461447
log << "\n";
14471448
}
14481449

@@ -1486,8 +1487,8 @@ void ConstraintGraphNode::print(llvm::raw_ostream &out, unsigned indent,
14861487
std::sort(sortedConstraints.begin(), sortedConstraints.end());
14871488

14881489
for (auto constraint : sortedConstraints) {
1489-
out.indent(indent + 4);
1490-
constraint->print(out, &TypeVar->getASTContext().SourceMgr);
1490+
constraint->print(out.indent(indent + 4),
1491+
&TypeVar->getASTContext().SourceMgr, indent + 4);
14911492
out << "\n";
14921493
}
14931494
}
@@ -1544,7 +1545,7 @@ void ConstraintGraph::print(ArrayRef<TypeVariableType *> typeVars,
15441545
PO.PrintTypesForDebugging = true;
15451546

15461547
for (auto typeVar : typeVars) {
1547-
(*this)[typeVar].print(out, 2, PO);
1548+
(*this)[typeVar].print(out, CS.getCurrentIndent() + 2, PO);
15481549
out << "\n";
15491550
}
15501551
}
@@ -1681,7 +1682,7 @@ void ConstraintGraph::printConnectedComponents(
16811682
PrintOptions PO;
16821683
PO.PrintTypesForDebugging = true;
16831684
for (const auto& component : components) {
1684-
out.indent(2);
1685+
out.indent(CS.getCurrentIndent() + 2);
16851686
out << component.solutionIndex << ": ";
16861687
SWIFT_DEFER {
16871688
out << '\n';

lib/Sema/ConstraintSystem.cpp

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3958,7 +3958,7 @@ SolutionResult ConstraintSystem::salvage() {
39583958
int i = 0;
39593959
for (auto &solution : viable) {
39603960
log << "---Ambiguous solution #" << i++ << "---\n";
3961-
solution.dump(log);
3961+
solution.dump(log, getCurrentIndent());
39623962
log << "\n";
39633963
}
39643964
}
@@ -4692,13 +4692,15 @@ bool ConstraintSystem::diagnoseAmbiguityWithFixes(
46924692
return true;
46934693

46944694
if (isDebugMode()) {
4695-
auto &log = llvm::errs();
4695+
auto indent = getCurrentIndent();
4696+
auto &log = llvm::errs().indent(indent);
46964697
log << "--- Ambiguity: Considering #" << solutions.size()
46974698
<< " solutions with fixes ---\n";
46984699
int i = 0;
46994700
for (auto &solution : solutions) {
4700-
log << "\n--- Solution #" << i++ << "---\n";
4701-
solution.dump(log);
4701+
log << "\n";
4702+
log.indent(indent) << "--- Solution #" << i++ << "---\n";
4703+
solution.dump(log, indent);
47024704
log << "\n";
47034705
}
47044706
}

0 commit comments

Comments
 (0)