Skip to content

Commit 04e3949

Browse files
committed
[ASTPrint] Introduce a printing option to hide underscored protocols in the stdlib.
1 parent 2740ad6 commit 04e3949

File tree

7 files changed

+24
-8
lines changed

7 files changed

+24
-8
lines changed

include/swift/AST/PrintOptions.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,9 @@ struct PrintOptions {
9797
/// Whether to skip internal stdlib declarations.
9898
bool SkipPrivateStdlibDecls = false;
9999

100+
/// Whether to skip underscored stdlib protocols.
101+
bool SkipUnderscoredStdlibProtocols = false;
102+
100103
/// Whether to skip extensions that don't add protocols or no members.
101104
bool SkipEmptyExtensionDecls = true;
102105

include/swift/AST/Type.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ class Type {
137137
Type subst(ModuleDecl *module, TypeSubstitutionMap &substitutions,
138138
SubstOptions options) const;
139139

140-
bool isPrivateStdlibType() const;
140+
bool isPrivateStdlibType(bool whitelistProtocols=true) const;
141141

142142
void dump() const;
143143
void dump(raw_ostream &os, unsigned indent = 0) const;

lib/AST/ASTPrinter.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -775,7 +775,9 @@ bool swift::shouldPrint(const Decl *D, PrintOptions &Options) {
775775
return false;
776776
}
777777

778-
if (Options.SkipPrivateStdlibDecls && D->isPrivateStdlibDecl())
778+
if (Options.SkipPrivateStdlibDecls &&
779+
D->isPrivateStdlibDecl(
780+
/*whitelistProtocols=*/!Options.SkipUnderscoredStdlibProtocols))
779781
return false;
780782

781783
if (Options.SkipEmptyExtensionDecls && isa<ExtensionDecl>(D)) {

lib/AST/Decl.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -382,7 +382,7 @@ bool Decl::isBeingTypeChecked() {
382382
bool Decl::isPrivateStdlibDecl(bool whitelistProtocols) const {
383383
const Decl *D = this;
384384
if (auto ExtD = dyn_cast<ExtensionDecl>(D))
385-
return ExtD->getExtendedType().isPrivateStdlibType();
385+
return ExtD->getExtendedType().isPrivateStdlibType(whitelistProtocols);
386386

387387
DeclContext *DC = D->getDeclContext()->getModuleScopeContext();
388388
if (DC->getParentModule()->isBuiltinModule() ||

lib/AST/Type.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3116,23 +3116,23 @@ TypeTraitResult TypeBase::canBeClass() {
31163116
return TypeTraitResult::IsNot;
31173117
}
31183118

3119-
bool Type::isPrivateStdlibType() const {
3119+
bool Type::isPrivateStdlibType(bool whitelistProtocols) const {
31203120
Type Ty = *this;
31213121
if (!Ty)
31223122
return false;
31233123

31243124
// A 'public' typealias can have an 'internal' type.
31253125
if (NameAliasType *NAT = dyn_cast<NameAliasType>(Ty.getPointer()))
3126-
return NAT->getDecl()->isPrivateStdlibDecl();
3126+
return NAT->getDecl()->isPrivateStdlibDecl(whitelistProtocols);
31273127

31283128
if (auto Paren = dyn_cast<ParenType>(Ty.getPointer()))
3129-
return Paren->getUnderlyingType().isPrivateStdlibType();
3129+
return Paren->getUnderlyingType().isPrivateStdlibType(whitelistProtocols);
31303130

31313131
if (Type Unwrapped = Ty->getAnyOptionalObjectType())
3132-
return Unwrapped.isPrivateStdlibType();
3132+
return Unwrapped.isPrivateStdlibType(whitelistProtocols);
31333133

31343134
if (auto TyD = Ty->getAnyNominal())
3135-
if (TyD->isPrivateStdlibDecl())
3135+
if (TyD->isPrivateStdlibDecl(whitelistProtocols))
31363136
return true;
31373137

31383138
return false;

test/IDE/print_stdlib.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@
1111
// RUN: %target-swift-ide-test -print-module -module-to-print=Swift -source-filename %s -print-interface-doc > %t-doc.txt
1212
// RUN: FileCheck %s < %t-doc.txt
1313

14+
// RUN: %target-swift-ide-test -print-module -module-to-print=Swift -source-filename %s -print-interface -skip-underscored-stdlib-protocols > %t-prot.txt
15+
// RUN: FileCheck -check-prefix=CHECK-UNDERSCORED-PROT %s < %t-prot.txt
16+
// CHECK-UNDERSCORED-PROT-NOT: protocol _
17+
1418
// CHECK-ARGC: static var argc: CInt { get }
1519

1620
// CHECK-NOT: @rethrows

tools/swift-ide-test/swift-ide-test.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -451,6 +451,11 @@ SkipPrivateStdlibDecls("skip-private-stdlib-decls",
451451
llvm::cl::desc("Don't print declarations that start with '_'"),
452452
llvm::cl::init(false));
453453

454+
static llvm::cl::opt<bool>
455+
SkipUnderscoredStdlibProtocols("skip-underscored-stdlib-protocols",
456+
llvm::cl::desc("Don't print protocols that start with '_'"),
457+
llvm::cl::init(false));
458+
454459
static llvm::cl::opt<bool>
455460
PrintRegularComments("print-regular-comments",
456461
llvm::cl::desc("Print regular comments from clang module headers"),
@@ -2556,6 +2561,8 @@ int main(int argc, char *argv[]) {
25562561
= PrintOptions::ArgAndParamPrintingMode::ArgumentOnly;
25572562
}
25582563
}
2564+
PrintOpts.SkipUnderscoredStdlibProtocols =
2565+
options::SkipUnderscoredStdlibProtocols;
25592566

25602567
if (PrintOpts.PrintDocumentationComments) {
25612568
InitInvok.getLangOptions().AttachCommentsToDecls = true;

0 commit comments

Comments
 (0)