Skip to content

Commit d537009

Browse files
authored
Merge pull request #22365 from nkcsgexi/47777848
ASTPrinter: add an option to skip keywords with an underscore prefix
2 parents fa952d3 + a92ea0c commit d537009

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;
@@ -366,7 +369,7 @@ ASTPrinter &operator<<(ASTPrinter &printer, tok keyword) {
366369
SmallString<16> Buffer;
367370
llvm::raw_svector_ostream OS(Buffer);
368371
OS << keyword;
369-
printer.printKeyword(Buffer.str());
372+
printer.printKeyword(Buffer.str(), PrintOptions());
370373
return printer;
371374
}
372375

@@ -591,7 +594,7 @@ class PrintAST : public ASTVisitor<PrintAST> {
591594
Printer << tok::kw_public;
592595
break;
593596
case AccessLevel::Open:
594-
Printer.printKeyword("open");
597+
Printer.printKeyword("open", Options);
595598
break;
596599
}
597600
Printer << suffix << " ";
@@ -1630,11 +1633,9 @@ static StringRef getAccessorLabel(AccessorDecl *accessor) {
16301633

16311634
void PrintAST::printMutatingModifiersIfNeeded(const AccessorDecl *accessor) {
16321635
if (accessor->isAssumedNonMutating() && accessor->isMutating()) {
1633-
Printer.printKeyword("mutating");
1634-
Printer << " ";
1636+
Printer.printKeyword("mutating", Options, " ");
16351637
} else if (accessor->isExplicitNonMutating()) {
1636-
Printer.printKeyword("nonmutating");
1637-
Printer << " ";
1638+
Printer.printKeyword("nonmutating", Options, " ");
16381639
}
16391640
}
16401641

@@ -1691,17 +1692,17 @@ void PrintAST::printAccessors(const AbstractStorageDecl *ASD) {
16911692
Printer << " {";
16921693
if (mutatingGetter) {
16931694
Printer << " ";
1694-
Printer.printKeyword("mutating");
1695+
Printer.printKeyword("mutating", Options);
16951696
}
16961697
Printer << " ";
1697-
Printer.printKeyword("get");
1698+
Printer.printKeyword("get", Options);
16981699
if (settable) {
16991700
if (nonmutatingSetter) {
17001701
Printer << " ";
1701-
Printer.printKeyword("nonmutating");
1702+
Printer.printKeyword("nonmutating", Options);
17021703
}
17031704
Printer << " ";
1704-
Printer.printKeyword("set");
1705+
Printer.printKeyword("set", Options);
17051706
}
17061707
Printer << " }";
17071708
return;
@@ -1744,7 +1745,7 @@ void PrintAST::printAccessors(const AbstractStorageDecl *ASD) {
17441745
if (!PrintAccessorBody) {
17451746
Printer << " ";
17461747
printMutatingModifiersIfNeeded(Accessor);
1747-
Printer.printKeyword(getAccessorLabel(Accessor));
1748+
Printer.printKeyword(getAccessorLabel(Accessor), Options);
17481749
} else {
17491750
{
17501751
IndentRAII IndentMore(*this);
@@ -2380,13 +2381,13 @@ static void printParameterFlags(ASTPrinter &printer, PrintOptions options,
23802381
/* nothing */
23812382
break;
23822383
case ValueOwnership::InOut:
2383-
printer << "inout ";
2384+
printer.printKeyword("inout", options, " ");
23842385
break;
23852386
case ValueOwnership::Shared:
2386-
printer << "__shared ";
2387+
printer.printKeyword("__shared", options, " ");
23872388
break;
23882389
case ValueOwnership::Owned:
2389-
printer << "__owned ";
2390+
printer.printKeyword("__owned", options, " ");
23902391
break;
23912392
}
23922393
}
@@ -2546,7 +2547,7 @@ void PrintAST::printOneParameter(const ParamDecl *param,
25462547
case DefaultArgumentKind::Function:
25472548
case DefaultArgumentKind::DSOHandle:
25482549
case DefaultArgumentKind::NilLiteral:
2549-
Printer.printKeyword(defaultArgStr);
2550+
Printer.printKeyword(defaultArgStr, Options);
25502551
break;
25512552
default:
25522553
Printer << defaultArgStr;
@@ -2681,11 +2682,9 @@ void PrintAST::visitFuncDecl(FuncDecl *decl) {
26812682
if (decl->isStatic())
26822683
printStaticKeyword(decl->getCorrectStaticSpelling());
26832684
if (decl->isMutating() && !decl->getAttrs().hasAttribute<MutatingAttr>()) {
2684-
Printer.printKeyword("mutating");
2685-
Printer << " ";
2685+
Printer.printKeyword("mutating", Options, " ");
26862686
} else if (decl->isConsuming() && !decl->getAttrs().hasAttribute<ConsumingAttr>()) {
2687-
Printer.printKeyword("__consuming");
2688-
Printer << " ";
2687+
Printer.printKeyword("__consuming", Options, " ");
26892688
}
26902689
Printer << tok::kw_func << " ";
26912690
}
@@ -2862,8 +2861,7 @@ void PrintAST::visitConstructorDecl(ConstructorDecl *decl) {
28622861
isClassContext = dc->getSelfClassDecl() != nullptr;
28632862
}
28642863
if (isClassContext) {
2865-
Printer.printKeyword("convenience");
2866-
Printer << " ";
2864+
Printer.printKeyword("convenience", Options, " ");
28672865
} else {
28682866
assert(decl->getDeclContext()->getExtendedProtocolDecl() &&
28692867
"unexpected convenience initializer");
@@ -2912,8 +2910,8 @@ void PrintAST::visitDestructorDecl(DestructorDecl *decl) {
29122910
}
29132911

29142912
void PrintAST::visitInfixOperatorDecl(InfixOperatorDecl *decl) {
2915-
Printer.printKeyword("infix");
2916-
Printer << " " << tok::kw_operator << " ";
2913+
Printer.printKeyword("infix", Options, " ");
2914+
Printer << tok::kw_operator << " ";
29172915
recordDeclLoc(decl,
29182916
[&]{
29192917
Printer.printName(decl->getName());
@@ -2944,33 +2942,30 @@ void PrintAST::visitPrecedenceGroupDecl(PrecedenceGroupDecl *decl) {
29442942
if (!decl->isAssociativityImplicit() ||
29452943
!decl->isNonAssociative()) {
29462944
indent();
2947-
Printer.printKeyword("associativity");
2948-
Printer << ": ";
2945+
Printer.printKeyword("associativity", Options, ": ");
29492946
switch (decl->getAssociativity()) {
29502947
case Associativity::None:
2951-
Printer.printKeyword("none");
2948+
Printer.printKeyword("none", Options);
29522949
break;
29532950
case Associativity::Left:
2954-
Printer.printKeyword("left");
2951+
Printer.printKeyword("left", Options);
29552952
break;
29562953
case Associativity::Right:
2957-
Printer.printKeyword("right");
2954+
Printer.printKeyword("right", Options);
29582955
break;
29592956
}
29602957
Printer.printNewline();
29612958
}
29622959
if (!decl->isAssignmentImplicit() ||
29632960
decl->isAssignment()) {
29642961
indent();
2965-
Printer.printKeyword("assignment");
2966-
Printer << ": ";
2967-
Printer.printKeyword(decl->isAssignment() ? "true" : "false");
2962+
Printer.printKeyword("assignment", Options, ": ");
2963+
Printer.printKeyword(decl->isAssignment() ? "true" : "false", Options);
29682964
Printer.printNewline();
29692965
}
29702966
if (!decl->getHigherThan().empty()) {
29712967
indent();
2972-
Printer.printKeyword("higherThan");
2973-
Printer << ": ";
2968+
Printer.printKeyword("higherThan", Options, ": ");
29742969
if (!decl->getHigherThan().empty()) {
29752970
Printer << decl->getHigherThan()[0].Name;
29762971
for (auto &rel : decl->getHigherThan().slice(1))
@@ -2980,8 +2975,7 @@ void PrintAST::visitPrecedenceGroupDecl(PrecedenceGroupDecl *decl) {
29802975
}
29812976
if (!decl->getLowerThan().empty()) {
29822977
indent();
2983-
Printer.printKeyword("lowerThan");
2984-
Printer << ": ";
2978+
Printer.printKeyword("lowerThan", Options, ": ");
29852979
if (!decl->getLowerThan().empty()) {
29862980
Printer << decl->getLowerThan()[0].Name;
29872981
for (auto &rel : decl->getLowerThan().slice(1))
@@ -2995,8 +2989,8 @@ void PrintAST::visitPrecedenceGroupDecl(PrecedenceGroupDecl *decl) {
29952989
}
29962990

29972991
void PrintAST::visitPrefixOperatorDecl(PrefixOperatorDecl *decl) {
2998-
Printer.printKeyword("prefix");
2999-
Printer << " " << tok::kw_operator << " ";
2992+
Printer.printKeyword("prefix", Options, " ");
2993+
Printer << tok::kw_operator << " ";
30002994
recordDeclLoc(decl,
30012995
[&]{
30022996
Printer.printName(decl->getName());
@@ -3013,8 +3007,8 @@ void PrintAST::visitPrefixOperatorDecl(PrefixOperatorDecl *decl) {
30133007
}
30143008

30153009
void PrintAST::visitPostfixOperatorDecl(PostfixOperatorDecl *decl) {
3016-
Printer.printKeyword("postfix");
3017-
Printer << " " << tok::kw_operator << " ";
3010+
Printer.printKeyword("postfix", Options, " ");
3011+
Printer << tok::kw_operator << " ";
30183012
recordDeclLoc(decl,
30193013
[&]{
30203014
Printer.printName(decl->getName());
@@ -3051,8 +3045,7 @@ void PrintAST::visitReturnStmt(ReturnStmt *stmt) {
30513045
}
30523046

30533047
void PrintAST::visitYieldStmt(YieldStmt *stmt) {
3054-
Printer.printKeyword("yield");
3055-
Printer << " ";
3048+
Printer.printKeyword("yield", Options, " ");
30563049
bool parens = (stmt->getYields().size() != 1
30573050
|| stmt->getLParenLoc().isValid());
30583051
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

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)