Skip to content

Commit 165638b

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 bb21216 commit 165638b

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
@@ -688,21 +688,9 @@ namespace {
688688
case StmtConditionElement::CK_Availability:
689689
printRecArbitrary([&](Label label) {
690690
printHead("#available", PatternColor, label);
691-
for (auto *Query : C.getAvailability()->getQueries()) {
692-
OS << '\n';
693-
switch (Query->getKind()) {
694-
case AvailabilitySpecKind::PlatformVersionConstraint:
695-
cast<PlatformVersionConstraintAvailabilitySpec>(Query)->print(OS, Indent + 2);
696-
break;
697-
case AvailabilitySpecKind::LanguageVersionConstraint:
698-
case AvailabilitySpecKind::PackageDescriptionVersionConstraint:
699-
cast<PlatformVersionConstraintAvailabilitySpec>(Query)->print(OS, Indent + 2);
700-
break;
701-
case AvailabilitySpecKind::OtherPlatform:
702-
cast<OtherPlatformAvailabilitySpec>(Query)->print(OS, Indent + 2);
703-
break;
704-
}
705-
}
691+
printList(C.getAvailability()->getQueries(),
692+
[&](auto *query, Label label) { printRec(query, label); },
693+
Label::optional("queries"));
706694
printFoot();
707695
}, label);
708696
break;
@@ -718,6 +706,48 @@ namespace {
718706
}
719707
}
720708

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

0 commit comments

Comments
 (0)