Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions include/swift/AST/PrintOptions.h
Original file line number Diff line number Diff line change
Expand Up @@ -694,6 +694,13 @@ struct PrintOptions {
return result;
}

/// Print options suitable for debug printing.
static PrintOptions forDebugging() {
PrintOptions result;
result.PrintTypesForDebugging = true;
return result;
}

/// Retrieve the set of options suitable for diagnostics printing.
static PrintOptions printForDiagnostics(AccessLevel accessFilter,
bool printFullConvention) {
Expand Down
3 changes: 2 additions & 1 deletion include/swift/IDE/Utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -402,7 +402,8 @@ struct ResolvedRangeInfo {
/*Single entry*/true, /*UnhandledEffects*/{},
OrphanKind::None, {}, {}, {}) {}
ResolvedRangeInfo(): ResolvedRangeInfo(ArrayRef<Token>()) {}
void print(llvm::raw_ostream &OS) const;
void print(llvm::raw_ostream &OS,
const PrintOptions &PO = PrintOptions()) const;
ExitState exit() const { return ExitInfo.Exit; }
Type getType() const { return ExitInfo.ReturnType; }

Expand Down
3 changes: 1 addition & 2 deletions include/swift/Sema/ConstraintSystem.h
Original file line number Diff line number Diff line change
Expand Up @@ -6223,8 +6223,7 @@ class TypeVariableBinding {
fixForHole(ConstraintSystem &cs) const;

void print(llvm::raw_ostream &Out, SourceManager *, unsigned indent) const {
PrintOptions PO;
PO.PrintTypesForDebugging = true;
PrintOptions PO = PrintOptions::forDebugging();
Out << "type variable binding " << TypeVar->getString(PO)
<< " := " << Binding.BindingType->getString(PO);
}
Expand Down
32 changes: 12 additions & 20 deletions lib/AST/ASTDumper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3398,13 +3398,11 @@ class PrintExpr : public ExprVisitor<PrintExpr, void, Label>,
/// FIXME: This should use ExprWalker to print children.

void printCommon(Expr *E, const char *C, Label label) {
PrintOptions PO;
PO.PrintTypesForDebugging = true;

printHead(C, ExprColor, label);

printFlag(E->isImplicit(), "implicit", ExprModifierColor);
printTypeField(GetTypeOfExpr(E), Label::always("type"), PO, TypeColor);
printTypeField(GetTypeOfExpr(E), Label::always("type"),
PrintOptions::forDebugging(), TypeColor);

// If we have a source range and an ASTContext, print the source range.
if (auto Ty = GetTypeOfExpr(E)) {
Expand Down Expand Up @@ -4054,10 +4052,8 @@ class PrintExpr : public ExprVisitor<PrintExpr, void, Label>,
void visitForceTryExpr(ForceTryExpr *E, Label label) {
printCommon(E, "force_try_expr", label);

PrintOptions PO;
PO.PrintTypesForDebugging = true;
printTypeField(E->getThrownError(), Label::always("thrown_error"), PO,
TypeColor);
printTypeField(E->getThrownError(), Label::always("thrown_error"),
PrintOptions::forDebugging(), TypeColor);

printRec(E->getSubExpr(), Label::optional("sub_expr"));
printFoot();
Expand All @@ -4066,10 +4062,8 @@ class PrintExpr : public ExprVisitor<PrintExpr, void, Label>,
void visitOptionalTryExpr(OptionalTryExpr *E, Label label) {
printCommon(E, "optional_try_expr", label);

PrintOptions PO;
PO.PrintTypesForDebugging = true;
printTypeField(E->getThrownError(), Label::always("thrown_error"), PO,
TypeColor);
printTypeField(E->getThrownError(), Label::always("thrown_error"),
PrintOptions::forDebugging(), TypeColor);

printRec(E->getSubExpr(), Label::optional("sub_expr"));
printFoot();
Expand Down Expand Up @@ -4553,10 +4547,8 @@ class PrintExpr : public ExprVisitor<PrintExpr, void, Label>,
void visitTypeValueExpr(TypeValueExpr *E, Label label) {
printCommon(E, "type_value_expr", label);

PrintOptions PO;
PO.PrintTypesForDebugging = true;
printTypeField(E->getParamType(), Label::always("param_type"), PO,
TypeColor);
printTypeField(E->getParamType(), Label::always("param_type"),
PrintOptions::forDebugging(), TypeColor);

printFoot();
}
Expand Down Expand Up @@ -6828,7 +6820,7 @@ void RequirementRepr::dump() const {
}

void GenericParamList::dump() const {
print(llvm::errs());
print(llvm::errs(), PrintOptions::forDebugging());
llvm::errs() << '\n';
}

Expand All @@ -6837,11 +6829,11 @@ void LayoutConstraint::dump() const {
llvm::errs() << "(null)\n";
return;
}
getPointer()->print(llvm::errs());
getPointer()->print(llvm::errs(), PrintOptions::forDebugging());
}

void GenericSignature::dump() const {
print(llvm::errs());
print(llvm::errs(), PrintOptions::forDebugging());
llvm::errs() << '\n';
}

Expand Down Expand Up @@ -6876,7 +6868,7 @@ void InheritedEntry::dump(llvm::raw_ostream &os) const {
os << '@' << getDumpString(getExplicitSafety()) << ' ';
if (isSuppressed())
os << "~";
getType().print(os);
getType().print(os, PrintOptions::forDebugging());
}

void InheritedEntry::dump() const { dump(llvm::errs()); }
6 changes: 4 additions & 2 deletions lib/AST/ASTPrinter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6198,9 +6198,11 @@ class TypePrinter : public TypeVisitor<TypePrinter, void, NonRecursivePrintOptio
if (Options.PrintTypesForDebugging || Options.PrintInSILBody)
Printer << "@error_type ";
visit(originalType);
}
else
} else if (Options.PrintTypesForDebugging) {
Printer << "<<error type>>";
} else {
Printer << "_";
}
}

void visitUnresolvedType(UnresolvedType *T,
Expand Down
7 changes: 4 additions & 3 deletions lib/IDE/SwiftSourceDocInfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,8 @@ void XMLEscapingPrinter::printXML(StringRef Text) {
OS << Text;
}

void ResolvedRangeInfo::print(llvm::raw_ostream &OS) const {
void ResolvedRangeInfo::print(llvm::raw_ostream &OS,
const PrintOptions &PO) const {
OS << "<Kind>";
switch (Kind) {
case RangeKind::SingleExpression: OS << "SingleExpression"; break;
Expand All @@ -87,7 +88,7 @@ void ResolvedRangeInfo::print(llvm::raw_ostream &OS) const {

if (auto Ty = getType()) {
OS << "<Type>";
Ty->print(OS);
Ty->print(OS, PO);
OS << "</Type>";
switch(exit()) {
case ExitState::Positive: OS << "<Exit>true</Exit>"; break;
Expand Down Expand Up @@ -147,7 +148,7 @@ void ResolvedRangeInfo::print(llvm::raw_ostream &OS) const {
for (auto &RD : ReferencedDecls) {
OS << "<Referenced>" << RD.VD->getBaseName() << "</Referenced>";
OS << "<Type>";
RD.Ty->print(OS);
RD.Ty->print(OS, PO);
OS << "</Type>\n";
}

Expand Down
36 changes: 15 additions & 21 deletions lib/Sema/CSBindings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2174,13 +2174,8 @@ void PotentialBindings::reset() {
AssociatedCodeCompletionToken = ASTNode();
}

void PotentialBindings::dump(ConstraintSystem &cs,
TypeVariableType *typeVar,
llvm::raw_ostream &out,
unsigned indent) const {
PrintOptions PO;
PO.PrintTypesForDebugging = true;

void PotentialBindings::dump(ConstraintSystem &cs, TypeVariableType *typeVar,
llvm::raw_ostream &out, unsigned indent) const {
out << "Potential bindings for ";
typeVar->getImpl().print(out);
out << "\n";
Expand All @@ -2202,16 +2197,17 @@ void PotentialBindings::dump(ConstraintSystem &cs,
[](auto lhs, auto rhs) {
return lhs.first->getID() < rhs.first->getID();
});
interleave(adjacentVars,
[&](std::pair<TypeVariableType *, Constraint *> pair) {
out << pair.first->getString(PO);
if (pair.first->getImpl().getFixedType(/*record=*/nullptr))
out << " (fixed)";
out << " via ";
pair.second->print(out, &cs.getASTContext().SourceMgr, indent,
/*skipLocator=*/true);
},
[&out]() { out << ", "; });
interleave(
adjacentVars,
[&](std::pair<TypeVariableType *, Constraint *> pair) {
out << pair.first->getString(PrintOptions::forDebugging());
if (pair.first->getImpl().getFixedType(/*record=*/nullptr))
out << " (fixed)";
out << " via ";
pair.second->print(out, &cs.getASTContext().SourceMgr, indent,
/*skipLocator=*/true);
},
[&out]() { out << ", "; });
out << "] ";
}
}
Expand Down Expand Up @@ -2295,8 +2291,7 @@ static std::string getCollectionLiteralAsString(KnownProtocolKind KPK) {
}

void BindingSet::dump(llvm::raw_ostream &out, unsigned indent) const {
PrintOptions PO;
PO.PrintTypesForDebugging = true;
PrintOptions PO = PrintOptions::forDebugging();

if (auto typeVar = getTypeVariable()) {
typeVar->getImpl().print(out);
Expand Down Expand Up @@ -2991,8 +2986,7 @@ bool TypeVariableBinding::attempt(ConstraintSystem &cs) const {

if (result.isFailure()) {
if (cs.isDebugMode()) {
PrintOptions PO;
PO.PrintTypesForDebugging = true;
PrintOptions PO = PrintOptions::forDebugging();

llvm::errs().indent(cs.solverState->getCurrentIndent())
<< "(failed to establish binding " << TypeVar->getString(PO)
Expand Down
7 changes: 2 additions & 5 deletions lib/Sema/CSOptimizer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1738,13 +1738,11 @@ static void determineBestChoicesInContext(
});

if (cs.isDebugMode()) {
PrintOptions PO;
PO.PrintTypesForDebugging = true;

llvm::errs().indent(cs.solverState->getCurrentIndent())
<< "<<< Disjunction "
<< disjunction->getNestedConstraints()[0]->getFirstType()->getString(
PO)
PrintOptions::forDebugging())
<< " with score " << bestScore << "\n";
}

Expand Down Expand Up @@ -1774,8 +1772,7 @@ static void determineBestChoicesInContext(
}

if (cs.isDebugMode() && bestOverallScore > 0) {
PrintOptions PO;
PO.PrintTypesForDebugging = true;
PrintOptions PO = PrintOptions::forDebugging();

auto getLogger = [&](unsigned extraIndent = 0) -> llvm::raw_ostream & {
return llvm::errs().indent(cs.solverState->getCurrentIndent() +
Expand Down
7 changes: 2 additions & 5 deletions lib/Sema/CSSimplify.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13439,12 +13439,9 @@ bool ConstraintSystem::simplifyAppliedOverloadsImpl(
// If we have a common result type, bind the expected result type to it.
if (commonResultType && !commonResultType->is<ErrorType>()) {
if (isDebugMode()) {
PrintOptions PO;
PO.PrintTypesForDebugging = true;
llvm::errs().indent(solverState ? solverState->getCurrentIndent() : 0)
<< "(common result type for $T" << fnTypeVar->getID() << " is "
<< commonResultType.getString(PO)
<< ")\n";
<< "(common result type for $T" << fnTypeVar->getID() << " is "
<< commonResultType.getString(PrintOptions::forDebugging()) << ")\n";
}

// Introduction of a `Bind` constraint here could result in the disconnect
Expand Down
11 changes: 3 additions & 8 deletions lib/Sema/CSStep.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -280,13 +280,11 @@ StepResult ComponentStep::take(bool prevFailed) {
CS.collectDisjunctions(disjunctions);
std::vector<std::string> overloadDisjunctions;
for (const auto &disjunction : disjunctions) {
PrintOptions PO;
PO.PrintTypesForDebugging = true;

auto constraints = disjunction->getNestedConstraints();
if (constraints[0]->getKind() == ConstraintKind::BindOverload)
overloadDisjunctions.push_back(
constraints[0]->getFirstType()->getString(PO));
constraints[0]->getFirstType()->getString(
PrintOptions::forDebugging()));
}

if (!potentialBindings.empty() || !overloadDisjunctions.empty()) {
Expand Down Expand Up @@ -357,14 +355,11 @@ StepResult ComponentStep::take(bool prevFailed) {
// we can't solve this system unless we have free type variables
// allowed in the solution.
if (CS.isDebugMode()) {
PrintOptions PO;
PO.PrintTypesForDebugging = true;

auto &log = getDebugLogger();
log << "(failed due to free variables:";
for (auto *typeVar : CS.getTypeVariables()) {
if (!typeVar->getImpl().hasRepresentativeOrFixed()) {
log << " " << typeVar->getString(PO);
log << " " << typeVar->getString(PrintOptions::forDebugging());
}
}
log << ")\n";
Expand Down
20 changes: 8 additions & 12 deletions lib/Sema/CSStep.h
Original file line number Diff line number Diff line change
Expand Up @@ -407,15 +407,12 @@ class ComponentStep final : public SolverStep {
auto &log = getDebugLogger();
log << "Type variables in scope = "
<< "[";
auto typeVars = CS.getTypeVariables();
PrintOptions PO;
PO.PrintTypesForDebugging = true;
interleave(typeVars, [&](TypeVariableType *typeVar) {
Type(typeVar).print(log, PO);
},
[&] {
log << ", ";
});
interleave(
CS.getTypeVariables(),
[&](TypeVariableType *typeVar) {
Type(typeVar).print(log, PrintOptions::forDebugging());
},
[&] { log << ", "; });
log << "]" << '\n';
}

Expand Down Expand Up @@ -553,9 +550,8 @@ class TypeVariableStep final : public BindingStep<TypeVarBindingProducer> {
StepResult resume(bool prevFailed) override;

void print(llvm::raw_ostream &Out) override {
PrintOptions PO;
PO.PrintTypesForDebugging = true;
Out << "TypeVariableStep for " << TypeVar->getString(PO) << '\n';
Out << "TypeVariableStep for ";
Out << TypeVar->getString(PrintOptions::forDebugging()) << '\n';
}

protected:
Expand Down
6 changes: 2 additions & 4 deletions lib/Sema/CSTrail.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -577,8 +577,7 @@ void SolverTrail::Change::undo(ConstraintSystem &cs) const {
void SolverTrail::Change::dump(llvm::raw_ostream &out,
ConstraintSystem &cs,
unsigned indent) const {
PrintOptions PO;
PO.PrintTypesForDebugging = true;
PrintOptions PO = PrintOptions::forDebugging();

out.indent(indent);

Expand Down Expand Up @@ -887,8 +886,7 @@ void SolverTrail::dumpActiveScopeChanges(llvm::raw_ostream &out,
llvm::set_subtract(removedConstraints, intersects);

// Print out Changes.
PrintOptions PO;
PO.PrintTypesForDebugging = true;
PrintOptions PO = PrintOptions::forDebugging();
out.indent(indent);
out << "(Changes:\n";
if (!addedTypeVars.empty()) {
Expand Down
3 changes: 1 addition & 2 deletions lib/Sema/Constraint.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -338,8 +338,7 @@ ProtocolDecl *Constraint::getProtocol() const {
void Constraint::print(llvm::raw_ostream &Out, SourceManager *sm,
unsigned indent, bool skipLocator) const {
// Print all type variables as $T0 instead of _ here.
PrintOptions PO;
PO.PrintTypesForDebugging = true;
PrintOptions PO = PrintOptions::forDebugging();

if (Kind == ConstraintKind::Disjunction ||
Kind == ConstraintKind::Conjunction) {
Expand Down
Loading