Skip to content

Commit 9da3746

Browse files
committed
[ASTPrinter] More expression printing
1 parent 6e051cc commit 9da3746

File tree

4 files changed

+66
-14
lines changed

4 files changed

+66
-14
lines changed

include/swift/AST/PrintOptions.h

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -647,14 +647,7 @@ struct PrintOptions {
647647
///
648648
/// This is only intended for debug output.
649649
static PrintOptions printEverything() {
650-
PrintOptions result = printVerbose();
651-
result.ExcludeAttrList.clear();
652-
result.ExcludeAttrList.push_back(DAK_FixedLayout);
653-
result.PrintStorageRepresentationAttrs = true;
654-
result.AbstractAccessors = false;
655-
result.PrintAccess = true;
656-
result.SkipEmptyExtensionDecls = false;
657-
result.SkipMissingMemberPlaceholders = false;
650+
PrintOptions result = printDeclarations();
658651
result.FunctionDefinitions = true;
659652
result.PrintExprs = true;
660653
return result;

lib/AST/ASTPrinter.cpp

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1316,6 +1316,7 @@ void PrintAST::printPattern(const Pattern *pattern) {
13161316

13171317
case PatternKind::OptionalSome:
13181318
printPattern(cast<OptionalSomePattern>(pattern)->getSubPattern());
1319+
Printer << '?';
13191320
break;
13201321

13211322
case PatternKind::Bool:
@@ -4366,7 +4367,6 @@ void PrintAST::visitLazyInitializerExpr(LazyInitializerExpr *expr) {
43664367
}
43674368

43684369
void PrintAST::visitOpenExistentialExpr(OpenExistentialExpr *expr) {
4369-
visit(expr->getValueProvidingExpr());
43704370
visit(expr->getExistentialValue());
43714371
visit(expr->getSubExpr());
43724372
}
@@ -4399,13 +4399,22 @@ void PrintAST::visitUnresolvedMemberExpr(UnresolvedMemberExpr *expr) {
43994399
}
44004400

44014401
void PrintAST::visitDiscardAssignmentExpr(DiscardAssignmentExpr *expr) {
4402+
Printer << "_";
44024403
}
44034404

44044405
void PrintAST::visitEditorPlaceholderExpr(EditorPlaceholderExpr *expr) {
44054406
}
44064407

44074408
void PrintAST::visitForcedCheckedCastExpr(ForcedCheckedCastExpr *expr) {
44084409
visit(expr->getSubExpr());
4410+
Printer << " as! ";
4411+
printType(expr->getCastType());
4412+
}
4413+
4414+
void PrintAST::visitConditionalCheckedCastExpr(ConditionalCheckedCastExpr *expr) {
4415+
visit(expr->getSubExpr());
4416+
Printer << " as? ";
4417+
printType(expr->getCastType());
44094418
}
44104419

44114420
void PrintAST::visitOverloadedDeclRefExpr(OverloadedDeclRefExpr *expr) {
@@ -4468,10 +4477,6 @@ void PrintAST::visitClassMetatypeToObjectExpr(ClassMetatypeToObjectExpr *expr) {
44684477
void PrintAST::visitAppliedPropertyWrapperExpr(AppliedPropertyWrapperExpr *expr) {
44694478
}
44704479

4471-
void PrintAST::visitConditionalCheckedCastExpr(ConditionalCheckedCastExpr *expr) {
4472-
visit(expr->getSubExpr());
4473-
}
4474-
44754480
void PrintAST::visitDifferentiableFunctionExpr(DifferentiableFunctionExpr *expr) {
44764481
visit(expr->getSubExpr());
44774482
}
@@ -4535,8 +4540,8 @@ void PrintAST::visitBraceStmt(BraceStmt *stmt) {
45354540
}
45364541

45374542
void PrintAST::visitReturnStmt(ReturnStmt *stmt) {
4543+
Printer << tok::kw_return;
45384544
if (stmt->hasResult()) {
4539-
Printer << tok::kw_return;
45404545
Printer << " ";
45414546
visit(stmt->getResult());
45424547
}

test/expr/print/protocol.swift

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// RUN: %target-swift-frontend -print-ast %s 2>&1 | %FileCheck %s
2+
3+
protocol Archivable {
4+
func archive(version: String)
5+
}
6+
// CHECK: internal protocol Archivable {
7+
// CHECK: func archive(version: String)
8+
// CHECK: }
9+
10+
func archive(_ a: Archivable) {
11+
a.archive(version: "1")
12+
}
13+
// CHECK: internal func archive(_ a: Archivable) {
14+
// CHECK: a.archive(version: "1")
15+
// CHECK: }
16+
17+
func cast(_ a: Any) {
18+
let conditional = a as? Archivable
19+
let forced = a as! Archivable
20+
}
21+
// CHECK: internal func cast(_ a: Any) {
22+
// CHECK: @_hasInitialValue private let conditional: Archivable? = a as? Archivable
23+
// CHECK: @_hasInitialValue private let forced: Archivable = a as! Archivable
24+
// CHECK: }

test/expr/print/switch.swift

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,33 @@ func test(payload: Payload) -> Int? {
2626
// CHECK-LABEL: return nil
2727
// CHECK-LABEL: }
2828
// CHECK-LABEL:}
29+
30+
func process(payload: Payload) {
31+
if case .empty = payload {
32+
return
33+
}
34+
_ = test(payload: payload)
35+
}
36+
// CHECK-LABEL: internal func process(payload: Payload) {
37+
// CHECK-LABEL: if .empty = payload {
38+
// CHECK-LABEL: return
39+
// CHECK-LABEL: }
40+
// CHECK-LABEL: _ = test(payload: payload)
41+
// CHECK-LABEL: }
42+
43+
func foo(_ x: Int?) {
44+
switch x {
45+
case let x?:
46+
break
47+
case nil:
48+
break
49+
}
50+
}
51+
// CHECK-LABEL: internal func foo(_ x: Int?) {
52+
// CHECK-LABEL: switch x {
53+
// CHECK-LABEL: case let x?:
54+
// CHECK-LABEL: break
55+
// CHECK-LABEL: case .none:
56+
// CHECK-LABEL: break
57+
// CHECK-LABEL: }
58+
// CHECK-LABEL: }

0 commit comments

Comments
 (0)