Skip to content

Commit c622f6b

Browse files
committed
Simplify swift::getInheritedForPrinting to take a PrintOptions
This lets us make some more assumptions in the next commit, but I think it's also just a nice cleanup to /not/ allow random predicates here. There were three callers of this API: - PrintAST, which was using PrintOptions::shouldPrint but /also/ incorrectly notifying listeners that a declaration would be skipped. - (IDE) Interface generation, which uses PrintOptions::shouldPrint to count how many "inherits" there will be. - SwiftDocSupport's reportRelated, which does no filtering at all. Creating a PrintOptions here is a little more expensive, but still. No intended functionality change.
1 parent e9384bb commit c622f6b

File tree

5 files changed

+11
-15
lines changed

5 files changed

+11
-15
lines changed

include/swift/AST/ASTPrinter.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -337,8 +337,7 @@ void printEnumElementsAsCases(
337337
llvm::DenseSet<EnumElementDecl *> &UnhandledElements,
338338
llvm::raw_ostream &OS);
339339

340-
void getInheritedForPrinting(const Decl *decl,
341-
llvm::function_ref<bool(const Decl*)> shouldPrint,
340+
void getInheritedForPrinting(const Decl *decl, const PrintOptions &options,
342341
llvm::SmallVectorImpl<TypeLoc> &Results);
343342

344343
} // namespace swift

include/swift/AST/PrintOptions.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -505,10 +505,10 @@ struct PrintOptions {
505505

506506
void clearSynthesizedExtension();
507507

508-
bool shouldPrint(const Decl* D) {
508+
bool shouldPrint(const Decl* D) const {
509509
return CurrentPrintabilityChecker->shouldPrint(D, *this);
510510
}
511-
bool shouldPrint(const Pattern* P) {
511+
bool shouldPrint(const Pattern* P) const {
512512
return CurrentPrintabilityChecker->shouldPrint(P, *this);
513513
}
514514

lib/AST/ASTPrinter.cpp

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1989,8 +1989,7 @@ void PrintAST::printGenericDeclGenericRequirements(GenericContext *decl) {
19891989

19901990
void PrintAST::printInherited(const Decl *decl) {
19911991
SmallVector<TypeLoc, 6> TypesToPrint;
1992-
getInheritedForPrinting(decl, [this](const Decl* D) { return shouldPrint(D); },
1993-
TypesToPrint);
1992+
getInheritedForPrinting(decl, Options, TypesToPrint);
19941993
if (TypesToPrint.empty())
19951994
return;
19961995

@@ -4678,8 +4677,7 @@ void swift::printEnumElementsAsCases(
46784677
}
46794678

46804679
void
4681-
swift::getInheritedForPrinting(const Decl *decl,
4682-
llvm::function_ref<bool(const Decl*)> shouldPrint,
4680+
swift::getInheritedForPrinting(const Decl *decl, const PrintOptions &options,
46834681
llvm::SmallVectorImpl<TypeLoc> &Results) {
46844682
ArrayRef<TypeLoc> inherited;
46854683
if (auto td = dyn_cast<TypeDecl>(decl)) {
@@ -4691,11 +4689,11 @@ swift::getInheritedForPrinting(const Decl *decl,
46914689
// Collect explicit inherited types.
46924690
for (auto TL: inherited) {
46934691
if (auto ty = TL.getType()) {
4694-
bool foundUnprintable = ty.findIf([shouldPrint](Type subTy) {
4692+
bool foundUnprintable = ty.findIf([&options](Type subTy) {
46954693
if (auto aliasTy = dyn_cast<TypeAliasType>(subTy.getPointer()))
4696-
return !shouldPrint(aliasTy->getDecl());
4694+
return !options.shouldPrint(aliasTy->getDecl());
46974695
if (auto NTD = subTy->getAnyNominal())
4698-
return !shouldPrint(NTD);
4696+
return !options.shouldPrint(NTD);
46994697
return false;
47004698
});
47014699
if (foundUnprintable)
@@ -4708,7 +4706,7 @@ swift::getInheritedForPrinting(const Decl *decl,
47084706
auto &ctx = decl->getASTContext();
47094707
for (auto attr : decl->getAttrs().getAttributes<SynthesizedProtocolAttr>()) {
47104708
if (auto *proto = ctx.getProtocol(attr->getProtocolKind())) {
4711-
if (!shouldPrint(proto))
4709+
if (!options.shouldPrint(proto))
47124710
continue;
47134711
if (attr->getProtocolKind() == KnownProtocolKind::RawRepresentable &&
47144712
isa<EnumDecl>(decl) &&

lib/IDE/IDETypeChecking.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -267,8 +267,7 @@ struct SynthesizedExtensionAnalyzer::Implementation {
267267

268268
unsigned countInherits(ExtensionDecl *ED) {
269269
SmallVector<TypeLoc, 4> Results;
270-
getInheritedForPrinting(
271-
ED, [&](const Decl *D) { return Options.shouldPrint(D); }, Results);
270+
getInheritedForPrinting(ED, Options, Results);
272271
return Results.size();
273272
}
274273

tools/SourceKit/lib/SwiftLang/SwiftDocSupport.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -547,7 +547,7 @@ static void reportRelated(ASTContext &Ctx, const Decl *D,
547547
passInheritsAndConformancesForValueDecl(TAD, Consumer);
548548
} else if (const auto *TD = dyn_cast<TypeDecl>(D)) {
549549
llvm::SmallVector<TypeLoc, 4> AllInherits;
550-
getInheritedForPrinting(TD, [](const Decl* d) { return true; }, AllInherits);
550+
getInheritedForPrinting(TD, PrintOptions(), AllInherits);
551551
passInherits(AllInherits, Consumer);
552552
passConforms(TD->getSatisfiedProtocolRequirements(/*Sorted=*/true),
553553
Consumer);

0 commit comments

Comments
 (0)