Skip to content

Commit 2f32a12

Browse files
committed
ASTPrinter: add an option to skip keywords with a prefix of underscore.
DocSupport will use set this flag to avoid printing __consuming, __owned, and __shared. rdar://47777848
1 parent 2f14c43 commit 2f32a12

File tree

9 files changed

+1331
-1270
lines changed

9 files changed

+1331
-1270
lines changed

include/swift/AST/ASTPrinter.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,10 +186,13 @@ class ASTPrinter {
186186
return *this << StringRef(&c, 1);
187187
}
188188

189-
void printKeyword(StringRef name) {
189+
void printKeyword(StringRef name, PrintOptions Opts, StringRef Suffix = "") {
190+
if (Opts.SkipUnderscoredKeywords && name.startswith("_"))
191+
return;
190192
callPrintNamePre(PrintNameContext::Keyword);
191193
*this << name;
192194
printNamePost(PrintNameContext::Keyword);
195+
*this << Suffix;
193196
}
194197

195198
void printAttrName(StringRef name, bool needAt = false) {

include/swift/AST/PrintOptions.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,9 @@ struct PrintOptions {
251251

252252
bool PrintImplicitAttrs = true;
253253

254+
/// Whether to skip keywords with a prefix of underscore such as __consuming.
255+
bool SkipUnderscoredKeywords = false;
256+
254257
/// Whether to print decl attributes that are only used internally,
255258
/// such as _silgen_name, transparent, etc.
256259
bool PrintUserInaccessibleAttrs = true;
@@ -446,6 +449,7 @@ struct PrintOptions {
446449
result.ShouldQualifyNestedDeclarations =
447450
QualifyNestedDeclarations::Always;
448451
result.PrintDocumentationComments = true;
452+
result.SkipUnderscoredKeywords = true;
449453
return result;
450454
}
451455

lib/AST/ASTPrinter.cpp

Lines changed: 31 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -350,7 +350,7 @@ ASTPrinter &operator<<(ASTPrinter &printer, tok keyword) {
350350
SmallString<16> Buffer;
351351
llvm::raw_svector_ostream OS(Buffer);
352352
OS << keyword;
353-
printer.printKeyword(Buffer.str());
353+
printer.printKeyword(Buffer.str(), PrintOptions());
354354
return printer;
355355
}
356356

@@ -575,7 +575,7 @@ class PrintAST : public ASTVisitor<PrintAST> {
575575
Printer << tok::kw_public;
576576
break;
577577
case AccessLevel::Open:
578-
Printer.printKeyword("open");
578+
Printer.printKeyword("open", Options);
579579
break;
580580
}
581581
Printer << suffix << " ";
@@ -1614,11 +1614,9 @@ static StringRef getAccessorLabel(AccessorDecl *accessor) {
16141614

16151615
void PrintAST::printMutatingModifiersIfNeeded(const AccessorDecl *accessor) {
16161616
if (accessor->isAssumedNonMutating() && accessor->isMutating()) {
1617-
Printer.printKeyword("mutating");
1618-
Printer << " ";
1617+
Printer.printKeyword("mutating", Options, " ");
16191618
} else if (accessor->isExplicitNonMutating()) {
1620-
Printer.printKeyword("nonmutating");
1621-
Printer << " ";
1619+
Printer.printKeyword("nonmutating", Options, " ");
16221620
}
16231621
}
16241622

@@ -1675,17 +1673,17 @@ void PrintAST::printAccessors(const AbstractStorageDecl *ASD) {
16751673
Printer << " {";
16761674
if (mutatingGetter) {
16771675
Printer << " ";
1678-
Printer.printKeyword("mutating");
1676+
Printer.printKeyword("mutating", Options);
16791677
}
16801678
Printer << " ";
1681-
Printer.printKeyword("get");
1679+
Printer.printKeyword("get", Options);
16821680
if (settable) {
16831681
if (nonmutatingSetter) {
16841682
Printer << " ";
1685-
Printer.printKeyword("nonmutating");
1683+
Printer.printKeyword("nonmutating", Options);
16861684
}
16871685
Printer << " ";
1688-
Printer.printKeyword("set");
1686+
Printer.printKeyword("set", Options);
16891687
}
16901688
Printer << " }";
16911689
return;
@@ -1728,7 +1726,7 @@ void PrintAST::printAccessors(const AbstractStorageDecl *ASD) {
17281726
if (!PrintAccessorBody) {
17291727
Printer << " ";
17301728
printMutatingModifiersIfNeeded(Accessor);
1731-
Printer.printKeyword(getAccessorLabel(Accessor));
1729+
Printer.printKeyword(getAccessorLabel(Accessor), Options);
17321730
} else {
17331731
{
17341732
IndentRAII IndentMore(*this);
@@ -2364,13 +2362,13 @@ static void printParameterFlags(ASTPrinter &printer, PrintOptions options,
23642362
/* nothing */
23652363
break;
23662364
case ValueOwnership::InOut:
2367-
printer << "inout ";
2365+
printer.printKeyword("inout", options, " ");
23682366
break;
23692367
case ValueOwnership::Shared:
2370-
printer << "__shared ";
2368+
printer.printKeyword("__shared", options, " ");
23712369
break;
23722370
case ValueOwnership::Owned:
2373-
printer << "__owned ";
2371+
printer.printKeyword("__owned", options, " ");
23742372
break;
23752373
}
23762374
}
@@ -2530,7 +2528,7 @@ void PrintAST::printOneParameter(const ParamDecl *param,
25302528
case DefaultArgumentKind::Function:
25312529
case DefaultArgumentKind::DSOHandle:
25322530
case DefaultArgumentKind::NilLiteral:
2533-
Printer.printKeyword(defaultArgStr);
2531+
Printer.printKeyword(defaultArgStr, Options);
25342532
break;
25352533
default:
25362534
Printer << defaultArgStr;
@@ -2665,11 +2663,9 @@ void PrintAST::visitFuncDecl(FuncDecl *decl) {
26652663
if (decl->isStatic())
26662664
printStaticKeyword(decl->getCorrectStaticSpelling());
26672665
if (decl->isMutating() && !decl->getAttrs().hasAttribute<MutatingAttr>()) {
2668-
Printer.printKeyword("mutating");
2669-
Printer << " ";
2666+
Printer.printKeyword("mutating", Options, " ");
26702667
} else if (decl->isConsuming() && !decl->getAttrs().hasAttribute<ConsumingAttr>()) {
2671-
Printer.printKeyword("__consuming");
2672-
Printer << " ";
2668+
Printer.printKeyword("__consuming", Options, " ");
26732669
}
26742670
Printer << tok::kw_func << " ";
26752671
}
@@ -2846,8 +2842,7 @@ void PrintAST::visitConstructorDecl(ConstructorDecl *decl) {
28462842
isClassContext = dc->getSelfClassDecl() != nullptr;
28472843
}
28482844
if (isClassContext) {
2849-
Printer.printKeyword("convenience");
2850-
Printer << " ";
2845+
Printer.printKeyword("convenience", Options, " ");
28512846
} else {
28522847
assert(decl->getDeclContext()->getExtendedProtocolDecl() &&
28532848
"unexpected convenience initializer");
@@ -2896,8 +2891,8 @@ void PrintAST::visitDestructorDecl(DestructorDecl *decl) {
28962891
}
28972892

28982893
void PrintAST::visitInfixOperatorDecl(InfixOperatorDecl *decl) {
2899-
Printer.printKeyword("infix");
2900-
Printer << " " << tok::kw_operator << " ";
2894+
Printer.printKeyword("infix", Options, " ");
2895+
Printer << tok::kw_operator << " ";
29012896
recordDeclLoc(decl,
29022897
[&]{
29032898
Printer.printName(decl->getName());
@@ -2928,33 +2923,30 @@ void PrintAST::visitPrecedenceGroupDecl(PrecedenceGroupDecl *decl) {
29282923
if (!decl->isAssociativityImplicit() ||
29292924
!decl->isNonAssociative()) {
29302925
indent();
2931-
Printer.printKeyword("associativity");
2932-
Printer << ": ";
2926+
Printer.printKeyword("associativity", Options, ": ");
29332927
switch (decl->getAssociativity()) {
29342928
case Associativity::None:
2935-
Printer.printKeyword("none");
2929+
Printer.printKeyword("none", Options);
29362930
break;
29372931
case Associativity::Left:
2938-
Printer.printKeyword("left");
2932+
Printer.printKeyword("left", Options);
29392933
break;
29402934
case Associativity::Right:
2941-
Printer.printKeyword("right");
2935+
Printer.printKeyword("right", Options);
29422936
break;
29432937
}
29442938
Printer.printNewline();
29452939
}
29462940
if (!decl->isAssignmentImplicit() ||
29472941
decl->isAssignment()) {
29482942
indent();
2949-
Printer.printKeyword("assignment");
2950-
Printer << ": ";
2951-
Printer.printKeyword(decl->isAssignment() ? "true" : "false");
2943+
Printer.printKeyword("assignment", Options, ": ");
2944+
Printer.printKeyword(decl->isAssignment() ? "true" : "false", Options);
29522945
Printer.printNewline();
29532946
}
29542947
if (!decl->getHigherThan().empty()) {
29552948
indent();
2956-
Printer.printKeyword("higherThan");
2957-
Printer << ": ";
2949+
Printer.printKeyword("higherThan", Options, ": ");
29582950
if (!decl->getHigherThan().empty()) {
29592951
Printer << decl->getHigherThan()[0].Name;
29602952
for (auto &rel : decl->getHigherThan().slice(1))
@@ -2964,8 +2956,7 @@ void PrintAST::visitPrecedenceGroupDecl(PrecedenceGroupDecl *decl) {
29642956
}
29652957
if (!decl->getLowerThan().empty()) {
29662958
indent();
2967-
Printer.printKeyword("lowerThan");
2968-
Printer << ": ";
2959+
Printer.printKeyword("lowerThan", Options, ": ");
29692960
if (!decl->getLowerThan().empty()) {
29702961
Printer << decl->getLowerThan()[0].Name;
29712962
for (auto &rel : decl->getLowerThan().slice(1))
@@ -2979,8 +2970,8 @@ void PrintAST::visitPrecedenceGroupDecl(PrecedenceGroupDecl *decl) {
29792970
}
29802971

29812972
void PrintAST::visitPrefixOperatorDecl(PrefixOperatorDecl *decl) {
2982-
Printer.printKeyword("prefix");
2983-
Printer << " " << tok::kw_operator << " ";
2973+
Printer.printKeyword("prefix", Options, " ");
2974+
Printer << tok::kw_operator << " ";
29842975
recordDeclLoc(decl,
29852976
[&]{
29862977
Printer.printName(decl->getName());
@@ -2997,8 +2988,8 @@ void PrintAST::visitPrefixOperatorDecl(PrefixOperatorDecl *decl) {
29972988
}
29982989

29992990
void PrintAST::visitPostfixOperatorDecl(PostfixOperatorDecl *decl) {
3000-
Printer.printKeyword("postfix");
3001-
Printer << " " << tok::kw_operator << " ";
2991+
Printer.printKeyword("postfix", Options, " ");
2992+
Printer << tok::kw_operator << " ";
30022993
recordDeclLoc(decl,
30032994
[&]{
30042995
Printer.printName(decl->getName());
@@ -3035,8 +3026,7 @@ void PrintAST::visitReturnStmt(ReturnStmt *stmt) {
30353026
}
30363027

30373028
void PrintAST::visitYieldStmt(YieldStmt *stmt) {
3038-
Printer.printKeyword("yield");
3039-
Printer << " ";
3029+
Printer.printKeyword("yield", Options, " ");
30403030
bool parens = (stmt->getYields().size() != 1
30413031
|| stmt->getLParenLoc().isValid());
30423032
if (parens) Printer << "(";

lib/AST/Attr.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -382,15 +382,14 @@ bool DeclAttribute::printImpl(ASTPrinter &Printer, const PrintOptions &Options,
382382
case DAK_Effects:
383383
case DAK_Optimize:
384384
if (DeclAttribute::isDeclModifier(getKind())) {
385-
Printer.printKeyword(getAttrName());
385+
Printer.printKeyword(getAttrName(), Options);
386386
} else {
387387
Printer.printSimpleAttr(getAttrName(), /*needAt=*/true);
388388
}
389389
return true;
390390

391391
case DAK_SetterAccess:
392-
Printer.printKeyword(getAttrName());
393-
Printer << "(set)";
392+
Printer.printKeyword(getAttrName(), Options, "(set)");
394393
return true;
395394

396395
default:

lib/AST/TypeRepr.cpp

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -358,7 +358,7 @@ void FunctionTypeRepr::printImpl(ASTPrinter &Printer,
358358
printTypeRepr(ArgsTy, Printer, Opts);
359359
if (throws()) {
360360
Printer << " ";
361-
Printer.printKeyword("throws");
361+
Printer.printKeyword("throws", Opts);
362362
}
363363
Printer << " -> ";
364364
Printer.callPrintStructurePre(PrintStructureKind::FunctionReturnType);
@@ -553,19 +553,18 @@ void SpecifierTypeRepr::printImpl(ASTPrinter &Printer,
553553
const PrintOptions &Opts) const {
554554
switch (getKind()) {
555555
case TypeReprKind::InOut:
556-
Printer.printKeyword("inout");
556+
Printer.printKeyword("inout", Opts, " ");
557557
break;
558558
case TypeReprKind::Shared:
559-
Printer.printKeyword("__shared");
559+
Printer.printKeyword("__shared", Opts, " ");
560560
break;
561561
case TypeReprKind::Owned:
562-
Printer.printKeyword("__owned");
562+
Printer.printKeyword("__owned", Opts, " ");
563563
break;
564564
default:
565565
llvm_unreachable("unknown specifier type repr");
566566
break;
567567
}
568-
Printer << " ";
569568
printTypeRepr(Base, Printer, Opts);
570569
}
571570

@@ -577,7 +576,7 @@ void FixedTypeRepr::printImpl(ASTPrinter &Printer,
577576
void SILBoxTypeRepr::printImpl(ASTPrinter &Printer,
578577
const PrintOptions &Opts) const {
579578
// TODO
580-
Printer.printKeyword("sil_box");
579+
Printer.printKeyword("sil_box", Opts);
581580
}
582581

583582
// See swift/Basic/Statistic.h for declaration: this enables tracing

test/IDE/print_type_interface.swift

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -71,15 +71,15 @@ extension D {
7171
// RUN: %target-swift-ide-test -print-type-interface -usr=_TtGSaSi_ -module-name print_type_interface -source-filename %s | %FileCheck %s -check-prefix=TYPE4
7272
// TYPE4-DAG: public typealias Index = Int
7373
// TYPE4-DAG: public func min() -> Int?
74-
// TYPE4-DAG: public mutating func insert<C>(contentsOf newElements: __owned C, at i: Int)
74+
// TYPE4-DAG: public mutating func insert<C>(contentsOf newElements: C, at i: Int)
7575
// TYPE4-DAG: public mutating func removeFirst(_ k: Int)
76-
// TYPE4-DAG: public __consuming func makeIterator() -> IndexingIterator<Array<Int>>
76+
// TYPE4-DAG: public func makeIterator() -> IndexingIterator<Array<Int>>
7777
// TYPE4-NOT: public func joined
7878

7979
// RUN: %target-swift-ide-test -print-type-interface -usr=_TtGSaSS_ -module-name print_type_interface -source-filename %s | %FileCheck %s -check-prefix=TYPE5
80-
// TYPE5-DAG: public __consuming func prefix(_ maxLength: Int) -> ArraySlice<String>
81-
// TYPE5-DAG: public __consuming func suffix(_ maxLength: Int) -> ArraySlice<String>
82-
// TYPE5-DAG: public __consuming func split(separator: String, maxSplits: Int = Int.max, omittingEmptySubsequences: Bool = true) -> [ArraySlice<String>]
80+
// TYPE5-DAG: public func prefix(_ maxLength: Int) -> ArraySlice<String>
81+
// TYPE5-DAG: public func suffix(_ maxLength: Int) -> ArraySlice<String>
82+
// TYPE5-DAG: public func split(separator: String, maxSplits: Int = Int.max, omittingEmptySubsequences: Bool = true) -> [ArraySlice<String>]
8383
// TYPE5-DAG: public func formIndex(_ i: inout Int, offsetBy distance: Int)
8484
// TYPE5-DAG: public func distance(from start: Int, to end: Int) -> Int
8585
// TYPE5-DAG: public func joined(separator: String = "") -> String

test/SourceKit/DocSupport/Inputs/cake.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ public class C1 : Prot {
99
public typealias Element = Int
1010
public var p : Int = 0
1111
public func foo() {}
12+
public __consuming func foo1(i0: __owned Int, i1: __shared Int) {}
1213

1314
public subscript(index: Int) -> Int { return 0 }
1415
public subscript(index i: Float) -> Int { return 0 }

0 commit comments

Comments
 (0)