Skip to content

Commit c0598b9

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 1e7fba7 commit c0598b9

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;
@@ -447,6 +450,7 @@ struct PrintOptions {
447450
result.ShouldQualifyNestedDeclarations =
448451
QualifyNestedDeclarations::Always;
449452
result.PrintDocumentationComments = true;
453+
result.SkipUnderscoredKeywords = true;
450454
return result;
451455
}
452456

lib/AST/ASTPrinter.cpp

Lines changed: 31 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -366,7 +366,7 @@ ASTPrinter &operator<<(ASTPrinter &printer, tok keyword) {
366366
SmallString<16> Buffer;
367367
llvm::raw_svector_ostream OS(Buffer);
368368
OS << keyword;
369-
printer.printKeyword(Buffer.str());
369+
printer.printKeyword(Buffer.str(), PrintOptions());
370370
return printer;
371371
}
372372

@@ -591,7 +591,7 @@ class PrintAST : public ASTVisitor<PrintAST> {
591591
Printer << tok::kw_public;
592592
break;
593593
case AccessLevel::Open:
594-
Printer.printKeyword("open");
594+
Printer.printKeyword("open", Options);
595595
break;
596596
}
597597
Printer << suffix << " ";
@@ -1630,11 +1630,9 @@ static StringRef getAccessorLabel(AccessorDecl *accessor) {
16301630

16311631
void PrintAST::printMutatingModifiersIfNeeded(const AccessorDecl *accessor) {
16321632
if (accessor->isAssumedNonMutating() && accessor->isMutating()) {
1633-
Printer.printKeyword("mutating");
1634-
Printer << " ";
1633+
Printer.printKeyword("mutating", Options, " ");
16351634
} else if (accessor->isExplicitNonMutating()) {
1636-
Printer.printKeyword("nonmutating");
1637-
Printer << " ";
1635+
Printer.printKeyword("nonmutating", Options, " ");
16381636
}
16391637
}
16401638

@@ -1691,17 +1689,17 @@ void PrintAST::printAccessors(const AbstractStorageDecl *ASD) {
16911689
Printer << " {";
16921690
if (mutatingGetter) {
16931691
Printer << " ";
1694-
Printer.printKeyword("mutating");
1692+
Printer.printKeyword("mutating", Options);
16951693
}
16961694
Printer << " ";
1697-
Printer.printKeyword("get");
1695+
Printer.printKeyword("get", Options);
16981696
if (settable) {
16991697
if (nonmutatingSetter) {
17001698
Printer << " ";
1701-
Printer.printKeyword("nonmutating");
1699+
Printer.printKeyword("nonmutating", Options);
17021700
}
17031701
Printer << " ";
1704-
Printer.printKeyword("set");
1702+
Printer.printKeyword("set", Options);
17051703
}
17061704
Printer << " }";
17071705
return;
@@ -1744,7 +1742,7 @@ void PrintAST::printAccessors(const AbstractStorageDecl *ASD) {
17441742
if (!PrintAccessorBody) {
17451743
Printer << " ";
17461744
printMutatingModifiersIfNeeded(Accessor);
1747-
Printer.printKeyword(getAccessorLabel(Accessor));
1745+
Printer.printKeyword(getAccessorLabel(Accessor), Options);
17481746
} else {
17491747
{
17501748
IndentRAII IndentMore(*this);
@@ -2380,13 +2378,13 @@ static void printParameterFlags(ASTPrinter &printer, PrintOptions options,
23802378
/* nothing */
23812379
break;
23822380
case ValueOwnership::InOut:
2383-
printer << "inout ";
2381+
printer.printKeyword("inout", options, " ");
23842382
break;
23852383
case ValueOwnership::Shared:
2386-
printer << "__shared ";
2384+
printer.printKeyword("__shared", options, " ");
23872385
break;
23882386
case ValueOwnership::Owned:
2389-
printer << "__owned ";
2387+
printer.printKeyword("__owned", options, " ");
23902388
break;
23912389
}
23922390
}
@@ -2546,7 +2544,7 @@ void PrintAST::printOneParameter(const ParamDecl *param,
25462544
case DefaultArgumentKind::Function:
25472545
case DefaultArgumentKind::DSOHandle:
25482546
case DefaultArgumentKind::NilLiteral:
2549-
Printer.printKeyword(defaultArgStr);
2547+
Printer.printKeyword(defaultArgStr, Options);
25502548
break;
25512549
default:
25522550
Printer << defaultArgStr;
@@ -2681,11 +2679,9 @@ void PrintAST::visitFuncDecl(FuncDecl *decl) {
26812679
if (decl->isStatic())
26822680
printStaticKeyword(decl->getCorrectStaticSpelling());
26832681
if (decl->isMutating() && !decl->getAttrs().hasAttribute<MutatingAttr>()) {
2684-
Printer.printKeyword("mutating");
2685-
Printer << " ";
2682+
Printer.printKeyword("mutating", Options, " ");
26862683
} else if (decl->isConsuming() && !decl->getAttrs().hasAttribute<ConsumingAttr>()) {
2687-
Printer.printKeyword("__consuming");
2688-
Printer << " ";
2684+
Printer.printKeyword("__consuming", Options, " ");
26892685
}
26902686
Printer << tok::kw_func << " ";
26912687
}
@@ -2862,8 +2858,7 @@ void PrintAST::visitConstructorDecl(ConstructorDecl *decl) {
28622858
isClassContext = dc->getSelfClassDecl() != nullptr;
28632859
}
28642860
if (isClassContext) {
2865-
Printer.printKeyword("convenience");
2866-
Printer << " ";
2861+
Printer.printKeyword("convenience", Options, " ");
28672862
} else {
28682863
assert(decl->getDeclContext()->getExtendedProtocolDecl() &&
28692864
"unexpected convenience initializer");
@@ -2912,8 +2907,8 @@ void PrintAST::visitDestructorDecl(DestructorDecl *decl) {
29122907
}
29132908

29142909
void PrintAST::visitInfixOperatorDecl(InfixOperatorDecl *decl) {
2915-
Printer.printKeyword("infix");
2916-
Printer << " " << tok::kw_operator << " ";
2910+
Printer.printKeyword("infix", Options, " ");
2911+
Printer << tok::kw_operator << " ";
29172912
recordDeclLoc(decl,
29182913
[&]{
29192914
Printer.printName(decl->getName());
@@ -2944,33 +2939,30 @@ void PrintAST::visitPrecedenceGroupDecl(PrecedenceGroupDecl *decl) {
29442939
if (!decl->isAssociativityImplicit() ||
29452940
!decl->isNonAssociative()) {
29462941
indent();
2947-
Printer.printKeyword("associativity");
2948-
Printer << ": ";
2942+
Printer.printKeyword("associativity", Options, ": ");
29492943
switch (decl->getAssociativity()) {
29502944
case Associativity::None:
2951-
Printer.printKeyword("none");
2945+
Printer.printKeyword("none", Options);
29522946
break;
29532947
case Associativity::Left:
2954-
Printer.printKeyword("left");
2948+
Printer.printKeyword("left", Options);
29552949
break;
29562950
case Associativity::Right:
2957-
Printer.printKeyword("right");
2951+
Printer.printKeyword("right", Options);
29582952
break;
29592953
}
29602954
Printer.printNewline();
29612955
}
29622956
if (!decl->isAssignmentImplicit() ||
29632957
decl->isAssignment()) {
29642958
indent();
2965-
Printer.printKeyword("assignment");
2966-
Printer << ": ";
2967-
Printer.printKeyword(decl->isAssignment() ? "true" : "false");
2959+
Printer.printKeyword("assignment", Options, ": ");
2960+
Printer.printKeyword(decl->isAssignment() ? "true" : "false", Options);
29682961
Printer.printNewline();
29692962
}
29702963
if (!decl->getHigherThan().empty()) {
29712964
indent();
2972-
Printer.printKeyword("higherThan");
2973-
Printer << ": ";
2965+
Printer.printKeyword("higherThan", Options, ": ");
29742966
if (!decl->getHigherThan().empty()) {
29752967
Printer << decl->getHigherThan()[0].Name;
29762968
for (auto &rel : decl->getHigherThan().slice(1))
@@ -2980,8 +2972,7 @@ void PrintAST::visitPrecedenceGroupDecl(PrecedenceGroupDecl *decl) {
29802972
}
29812973
if (!decl->getLowerThan().empty()) {
29822974
indent();
2983-
Printer.printKeyword("lowerThan");
2984-
Printer << ": ";
2975+
Printer.printKeyword("lowerThan", Options, ": ");
29852976
if (!decl->getLowerThan().empty()) {
29862977
Printer << decl->getLowerThan()[0].Name;
29872978
for (auto &rel : decl->getLowerThan().slice(1))
@@ -2995,8 +2986,8 @@ void PrintAST::visitPrecedenceGroupDecl(PrecedenceGroupDecl *decl) {
29952986
}
29962987

29972988
void PrintAST::visitPrefixOperatorDecl(PrefixOperatorDecl *decl) {
2998-
Printer.printKeyword("prefix");
2999-
Printer << " " << tok::kw_operator << " ";
2989+
Printer.printKeyword("prefix", Options, " ");
2990+
Printer << tok::kw_operator << " ";
30002991
recordDeclLoc(decl,
30012992
[&]{
30022993
Printer.printName(decl->getName());
@@ -3013,8 +3004,8 @@ void PrintAST::visitPrefixOperatorDecl(PrefixOperatorDecl *decl) {
30133004
}
30143005

30153006
void PrintAST::visitPostfixOperatorDecl(PostfixOperatorDecl *decl) {
3016-
Printer.printKeyword("postfix");
3017-
Printer << " " << tok::kw_operator << " ";
3007+
Printer.printKeyword("postfix", Options, " ");
3008+
Printer << tok::kw_operator << " ";
30183009
recordDeclLoc(decl,
30193010
[&]{
30203011
Printer.printName(decl->getName());
@@ -3051,8 +3042,7 @@ void PrintAST::visitReturnStmt(ReturnStmt *stmt) {
30513042
}
30523043

30533044
void PrintAST::visitYieldStmt(YieldStmt *stmt) {
3054-
Printer.printKeyword("yield");
3055-
Printer << " ";
3045+
Printer.printKeyword("yield", Options, " ");
30563046
bool parens = (stmt->getYields().size() != 1
30573047
|| stmt->getLParenLoc().isValid());
30583048
if (parens) Printer << "(";

lib/AST/Attr.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -395,15 +395,14 @@ bool DeclAttribute::printImpl(ASTPrinter &Printer, const PrintOptions &Options,
395395
case DAK_Effects:
396396
case DAK_Optimize:
397397
if (DeclAttribute::isDeclModifier(getKind())) {
398-
Printer.printKeyword(getAttrName());
398+
Printer.printKeyword(getAttrName(), Options);
399399
} else {
400400
Printer.printSimpleAttr(getAttrName(), /*needAt=*/true);
401401
}
402402
return true;
403403

404404
case DAK_SetterAccess:
405-
Printer.printKeyword(getAttrName());
406-
Printer << "(set)";
405+
Printer.printKeyword(getAttrName(), Options, "(set)");
407406
return true;
408407

409408
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)