Skip to content

Commit 80f713b

Browse files
committed
[AST] NFC-ish: Improve printing of availability specs.
Availability specs are currently dumped by writing a pre-formatted string directly to the output stream instead of using the structured primitives in `PrintBase`. This needs to be fixed before we can introduce the generalized writers. The format here is meant to be the same as the format printed by the original methods, but some colors may differ slightly when dumping to the terminal.
1 parent 8670ecf commit 80f713b

File tree

1 file changed

+45
-15
lines changed

1 file changed

+45
-15
lines changed

lib/AST/ASTDumper.cpp

Lines changed: 45 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -696,21 +696,9 @@ namespace {
696696
case StmtConditionElement::CK_Availability:
697697
printRecArbitrary([&](Label label) {
698698
printHead("#available", PatternColor, label);
699-
for (auto *Query : C.getAvailability()->getQueries()) {
700-
OS << '\n';
701-
switch (Query->getKind()) {
702-
case AvailabilitySpecKind::PlatformVersionConstraint:
703-
cast<PlatformVersionConstraintAvailabilitySpec>(Query)->print(OS, Indent + 2);
704-
break;
705-
case AvailabilitySpecKind::LanguageVersionConstraint:
706-
case AvailabilitySpecKind::PackageDescriptionVersionConstraint:
707-
cast<PlatformVersionConstraintAvailabilitySpec>(Query)->print(OS, Indent + 2);
708-
break;
709-
case AvailabilitySpecKind::OtherPlatform:
710-
cast<OtherPlatformAvailabilitySpec>(Query)->print(OS, Indent + 2);
711-
break;
712-
}
713-
}
699+
printList(C.getAvailability()->getQueries(),
700+
[&](auto *query, Label label) { printRec(query, label); },
701+
Label::optional("queries"));
714702
printFoot();
715703
}, label);
716704
break;
@@ -726,6 +714,48 @@ namespace {
726714
}
727715
}
728716

717+
/// Print an availability spec as a child node.
718+
void printRec(AvailabilitySpec *Spec, Label label) {
719+
printRecArbitrary(
720+
[&](Label label) {
721+
switch (Spec->getKind()) {
722+
case AvailabilitySpecKind::PlatformVersionConstraint: {
723+
auto plat = cast<PlatformVersionConstraintAvailabilitySpec>(Spec);
724+
printHead("platform_version_constraint_availability_spec",
725+
PatternColor, label);
726+
printField(platformString(plat->getPlatform()), "platform");
727+
printFieldRaw(
728+
[&](llvm::raw_ostream &OS) { OS << plat->getVersion(); },
729+
"version");
730+
printFoot();
731+
break;
732+
}
733+
case AvailabilitySpecKind::LanguageVersionConstraint:
734+
case AvailabilitySpecKind::PackageDescriptionVersionConstraint: {
735+
auto agnostic =
736+
cast<PlatformAgnosticVersionConstraintAvailabilitySpec>(Spec);
737+
printHead("platform_agnostic_version_constraint_"
738+
"availability_spec",
739+
PatternColor, label);
740+
printField(agnostic->isLanguageVersionSpecific()
741+
? "swift"
742+
: "package_description",
743+
"kind");
744+
printFieldRaw(
745+
[&](llvm::raw_ostream &OS) { OS << agnostic->getVersion(); },
746+
"version");
747+
printFoot();
748+
break;
749+
}
750+
case AvailabilitySpecKind::OtherPlatform:
751+
printHead("other_constraint_availability_spec", PatternColor,
752+
label);
753+
printFoot();
754+
break;
755+
}
756+
}, label);
757+
}
758+
729759
/// Print a range of nodes as a single "array" child node.
730760
template <typename NodeRange>
731761
void printRecRange(const NodeRange &range, Label topLabel) {

0 commit comments

Comments
 (0)