Skip to content

Commit 83d311c

Browse files
authored
Merge pull request #22433 from nkcsgexi/ast-printer-5.0
[5.0]ASTPrinter: add an option to skip keywords with a prefix of underscore.
2 parents bb0a0a3 + fc847a5 commit 83d311c

File tree

9 files changed

+1329
-1265
lines changed

9 files changed

+1329
-1265
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: 3 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;

lib/AST/ASTPrinter.cpp

Lines changed: 34 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,9 @@ PrintOptions PrintOptions::printParseableInterfaceFile() {
106106
result.CollapseSingleGetterProperty = false;
107107
result.VarInitializers = true;
108108

109+
// We should print __consuming, __owned, etc for the module interface file.
110+
result.SkipUnderscoredKeywords = false;
111+
109112
result.FunctionBody = [](const ValueDecl *decl, ASTPrinter &printer) {
110113
auto AFD = dyn_cast<AbstractFunctionDecl>(decl);
111114
if (!AFD || !AFD->hasInlinableBodyText()) return;
@@ -350,7 +353,7 @@ ASTPrinter &operator<<(ASTPrinter &printer, tok keyword) {
350353
SmallString<16> Buffer;
351354
llvm::raw_svector_ostream OS(Buffer);
352355
OS << keyword;
353-
printer.printKeyword(Buffer.str());
356+
printer.printKeyword(Buffer.str(), PrintOptions());
354357
return printer;
355358
}
356359

@@ -575,7 +578,7 @@ class PrintAST : public ASTVisitor<PrintAST> {
575578
Printer << tok::kw_public;
576579
break;
577580
case AccessLevel::Open:
578-
Printer.printKeyword("open");
581+
Printer.printKeyword("open", Options);
579582
break;
580583
}
581584
Printer << suffix << " ";
@@ -1614,11 +1617,9 @@ static StringRef getAccessorLabel(AccessorDecl *accessor) {
16141617

16151618
void PrintAST::printMutatingModifiersIfNeeded(const AccessorDecl *accessor) {
16161619
if (accessor->isAssumedNonMutating() && accessor->isMutating()) {
1617-
Printer.printKeyword("mutating");
1618-
Printer << " ";
1620+
Printer.printKeyword("mutating", Options, " ");
16191621
} else if (accessor->isExplicitNonMutating()) {
1620-
Printer.printKeyword("nonmutating");
1621-
Printer << " ";
1622+
Printer.printKeyword("nonmutating", Options, " ");
16221623
}
16231624
}
16241625

@@ -1675,17 +1676,17 @@ void PrintAST::printAccessors(const AbstractStorageDecl *ASD) {
16751676
Printer << " {";
16761677
if (mutatingGetter) {
16771678
Printer << " ";
1678-
Printer.printKeyword("mutating");
1679+
Printer.printKeyword("mutating", Options);
16791680
}
16801681
Printer << " ";
1681-
Printer.printKeyword("get");
1682+
Printer.printKeyword("get", Options);
16821683
if (settable) {
16831684
if (nonmutatingSetter) {
16841685
Printer << " ";
1685-
Printer.printKeyword("nonmutating");
1686+
Printer.printKeyword("nonmutating", Options);
16861687
}
16871688
Printer << " ";
1688-
Printer.printKeyword("set");
1689+
Printer.printKeyword("set", Options);
16891690
}
16901691
Printer << " }";
16911692
return;
@@ -1728,7 +1729,7 @@ void PrintAST::printAccessors(const AbstractStorageDecl *ASD) {
17281729
if (!PrintAccessorBody) {
17291730
Printer << " ";
17301731
printMutatingModifiersIfNeeded(Accessor);
1731-
Printer.printKeyword(getAccessorLabel(Accessor));
1732+
Printer.printKeyword(getAccessorLabel(Accessor), Options);
17321733
} else {
17331734
{
17341735
IndentRAII IndentMore(*this);
@@ -2364,13 +2365,13 @@ static void printParameterFlags(ASTPrinter &printer, PrintOptions options,
23642365
/* nothing */
23652366
break;
23662367
case ValueOwnership::InOut:
2367-
printer << "inout ";
2368+
printer.printKeyword("inout", options, " ");
23682369
break;
23692370
case ValueOwnership::Shared:
2370-
printer << "__shared ";
2371+
printer.printKeyword("__shared", options, " ");
23712372
break;
23722373
case ValueOwnership::Owned:
2373-
printer << "__owned ";
2374+
printer.printKeyword("__owned", options, " ");
23742375
break;
23752376
}
23762377
}
@@ -2530,7 +2531,7 @@ void PrintAST::printOneParameter(const ParamDecl *param,
25302531
case DefaultArgumentKind::Function:
25312532
case DefaultArgumentKind::DSOHandle:
25322533
case DefaultArgumentKind::NilLiteral:
2533-
Printer.printKeyword(defaultArgStr);
2534+
Printer.printKeyword(defaultArgStr, Options);
25342535
break;
25352536
default:
25362537
Printer << defaultArgStr;
@@ -2665,11 +2666,9 @@ void PrintAST::visitFuncDecl(FuncDecl *decl) {
26652666
if (decl->isStatic())
26662667
printStaticKeyword(decl->getCorrectStaticSpelling());
26672668
if (decl->isMutating() && !decl->getAttrs().hasAttribute<MutatingAttr>()) {
2668-
Printer.printKeyword("mutating");
2669-
Printer << " ";
2669+
Printer.printKeyword("mutating", Options, " ");
26702670
} else if (decl->isConsuming() && !decl->getAttrs().hasAttribute<ConsumingAttr>()) {
2671-
Printer.printKeyword("__consuming");
2672-
Printer << " ";
2671+
Printer.printKeyword("__consuming", Options, " ");
26732672
}
26742673
Printer << tok::kw_func << " ";
26752674
}
@@ -2846,8 +2845,7 @@ void PrintAST::visitConstructorDecl(ConstructorDecl *decl) {
28462845
isClassContext = dc->getSelfClassDecl() != nullptr;
28472846
}
28482847
if (isClassContext) {
2849-
Printer.printKeyword("convenience");
2850-
Printer << " ";
2848+
Printer.printKeyword("convenience", Options, " ");
28512849
} else {
28522850
assert(decl->getDeclContext()->getExtendedProtocolDecl() &&
28532851
"unexpected convenience initializer");
@@ -2896,8 +2894,8 @@ void PrintAST::visitDestructorDecl(DestructorDecl *decl) {
28962894
}
28972895

28982896
void PrintAST::visitInfixOperatorDecl(InfixOperatorDecl *decl) {
2899-
Printer.printKeyword("infix");
2900-
Printer << " " << tok::kw_operator << " ";
2897+
Printer.printKeyword("infix", Options, " ");
2898+
Printer << tok::kw_operator << " ";
29012899
recordDeclLoc(decl,
29022900
[&]{
29032901
Printer.printName(decl->getName());
@@ -2928,33 +2926,30 @@ void PrintAST::visitPrecedenceGroupDecl(PrecedenceGroupDecl *decl) {
29282926
if (!decl->isAssociativityImplicit() ||
29292927
!decl->isNonAssociative()) {
29302928
indent();
2931-
Printer.printKeyword("associativity");
2932-
Printer << ": ";
2929+
Printer.printKeyword("associativity", Options, ": ");
29332930
switch (decl->getAssociativity()) {
29342931
case Associativity::None:
2935-
Printer.printKeyword("none");
2932+
Printer.printKeyword("none", Options);
29362933
break;
29372934
case Associativity::Left:
2938-
Printer.printKeyword("left");
2935+
Printer.printKeyword("left", Options);
29392936
break;
29402937
case Associativity::Right:
2941-
Printer.printKeyword("right");
2938+
Printer.printKeyword("right", Options);
29422939
break;
29432940
}
29442941
Printer.printNewline();
29452942
}
29462943
if (!decl->isAssignmentImplicit() ||
29472944
decl->isAssignment()) {
29482945
indent();
2949-
Printer.printKeyword("assignment");
2950-
Printer << ": ";
2951-
Printer.printKeyword(decl->isAssignment() ? "true" : "false");
2946+
Printer.printKeyword("assignment", Options, ": ");
2947+
Printer.printKeyword(decl->isAssignment() ? "true" : "false", Options);
29522948
Printer.printNewline();
29532949
}
29542950
if (!decl->getHigherThan().empty()) {
29552951
indent();
2956-
Printer.printKeyword("higherThan");
2957-
Printer << ": ";
2952+
Printer.printKeyword("higherThan", Options, ": ");
29582953
if (!decl->getHigherThan().empty()) {
29592954
Printer << decl->getHigherThan()[0].Name;
29602955
for (auto &rel : decl->getHigherThan().slice(1))
@@ -2964,8 +2959,7 @@ void PrintAST::visitPrecedenceGroupDecl(PrecedenceGroupDecl *decl) {
29642959
}
29652960
if (!decl->getLowerThan().empty()) {
29662961
indent();
2967-
Printer.printKeyword("lowerThan");
2968-
Printer << ": ";
2962+
Printer.printKeyword("lowerThan", Options, ": ");
29692963
if (!decl->getLowerThan().empty()) {
29702964
Printer << decl->getLowerThan()[0].Name;
29712965
for (auto &rel : decl->getLowerThan().slice(1))
@@ -2979,8 +2973,8 @@ void PrintAST::visitPrecedenceGroupDecl(PrecedenceGroupDecl *decl) {
29792973
}
29802974

29812975
void PrintAST::visitPrefixOperatorDecl(PrefixOperatorDecl *decl) {
2982-
Printer.printKeyword("prefix");
2983-
Printer << " " << tok::kw_operator << " ";
2976+
Printer.printKeyword("prefix", Options, " ");
2977+
Printer << tok::kw_operator << " ";
29842978
recordDeclLoc(decl,
29852979
[&]{
29862980
Printer.printName(decl->getName());
@@ -2997,8 +2991,8 @@ void PrintAST::visitPrefixOperatorDecl(PrefixOperatorDecl *decl) {
29972991
}
29982992

29992993
void PrintAST::visitPostfixOperatorDecl(PostfixOperatorDecl *decl) {
3000-
Printer.printKeyword("postfix");
3001-
Printer << " " << tok::kw_operator << " ";
2994+
Printer.printKeyword("postfix", Options, " ");
2995+
Printer << tok::kw_operator << " ";
30022996
recordDeclLoc(decl,
30032997
[&]{
30042998
Printer.printName(decl->getName());
@@ -3035,8 +3029,7 @@ void PrintAST::visitReturnStmt(ReturnStmt *stmt) {
30353029
}
30363030

30373031
void PrintAST::visitYieldStmt(YieldStmt *stmt) {
3038-
Printer.printKeyword("yield");
3039-
Printer << " ";
3032+
Printer.printKeyword("yield", Options, " ");
30403033
bool parens = (stmt->getYields().size() != 1
30413034
|| stmt->getLParenLoc().isValid());
30423035
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

lib/IDE/IDETypeChecking.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ PrintOptions PrintOptions::printDocInterface() {
7777
result.PrintDocumentationComments = false;
7878
result.PrintRegularClangComments = false;
7979
result.PrintFunctionRepresentationAttrs = false;
80+
result.SkipUnderscoredKeywords = true;
8081
return result;
8182
}
8283

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)