Skip to content

Commit 3e26b2d

Browse files
author
Harlan Haskins
committed
Clean up function body printing throughout
1 parent 9344ac0 commit 3e26b2d

File tree

6 files changed

+101
-54
lines changed

6 files changed

+101
-54
lines changed

include/swift/AST/Decl.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5000,8 +5000,7 @@ class AbstractFunctionDecl : public GenericContext, public ValueDecl {
50005000
/// BodyKind::TypeChecked.
50015001
BraceStmt *Body;
50025002

5003-
/// This enum member is active if getBodyKind() is BodyKind::Deserialized or
5004-
/// BodyKind::TypeChecked.
5003+
/// This enum member is active if getBodyKind() is BodyKind::Deserialized.
50055004
StringRef BodyStringRepresentation;
50065005

50075006
/// This enum member is active if getBodyKind() == BodyKind::Synthesize.

include/swift/Serialization/ModuleFormat.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -983,7 +983,7 @@ namespace decls_block {
983983
// - its generic parameters, if any
984984
// - its parameter patterns,
985985
// - the foreign error convention, if any
986-
// - inlinable bldy text, if any
986+
// - inlinable body text, if any
987987
>;
988988

989989
using VarLayout = BCRecordLayout<

lib/AST/ASTPrinter.cpp

Lines changed: 30 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -654,6 +654,7 @@ class PrintAST : public ASTVisitor<PrintAST> {
654654

655655
void printAttributes(const Decl *D);
656656
void printTypedPattern(const TypedPattern *TP);
657+
void printBraceStmt(const BraceStmt *stmt, bool newlineIfEmpty = true);
657658

658659
public:
659660
void printPattern(const Pattern *pattern);
@@ -695,6 +696,7 @@ class PrintAST : public ASTVisitor<PrintAST> {
695696
void printGenericDeclGenericParams(GenericContext *decl);
696697
void printGenericDeclGenericRequirements(GenericContext *decl);
697698
void printInherited(const Decl *decl);
699+
void printBodyIfNecessary(const AbstractFunctionDecl *decl);
698700

699701
void printEnumElement(EnumElementDecl *elt);
700702

@@ -1480,6 +1482,29 @@ bool PrintAST::shouldPrint(const Decl *D, bool Notify) {
14801482
return Result;
14811483
}
14821484

1485+
void PrintAST::printBraceStmt(const BraceStmt *stmt, bool newlineIfEmpty) {
1486+
Printer << "{";
1487+
if (printASTNodes(stmt->getElements()) || newlineIfEmpty) {
1488+
Printer.printNewline();
1489+
indent();
1490+
}
1491+
Printer << "}";
1492+
}
1493+
1494+
void PrintAST::printBodyIfNecessary(const AbstractFunctionDecl *decl) {
1495+
if (auto BodyFunc = Options.FunctionBody) {
1496+
BodyFunc(decl, Printer);
1497+
indent();
1498+
return;
1499+
}
1500+
1501+
if (!Options.FunctionDefinitions || !decl->getBody())
1502+
return;
1503+
1504+
Printer << " ";
1505+
printBraceStmt(decl->getBody(), /*newlineIfEmpty*/!isa<AccessorDecl>(decl));
1506+
}
1507+
14831508
static bool isAccessorAssumedNonMutating(AccessorKind kind) {
14841509
switch (kind) {
14851510
case AccessorKind::Get:
@@ -2540,19 +2565,7 @@ void PrintAST::visitAccessorDecl(AccessorDecl *decl) {
25402565
});
25412566
}
25422567

2543-
if (auto BodyFunc = Options.FunctionBody) {
2544-
BodyFunc(decl, Printer);
2545-
indent();
2546-
return;
2547-
}
2548-
2549-
Printer << " {";
2550-
if (Options.FunctionDefinitions && decl->getBody())
2551-
if (printASTNodes(decl->getBody()->getElements())) {
2552-
Printer.printNewline();
2553-
indent();
2554-
}
2555-
Printer << "}";
2568+
printBodyIfNecessary(decl);
25562569
}
25572570

25582571
void PrintAST::visitFuncDecl(FuncDecl *decl) {
@@ -2624,13 +2637,7 @@ void PrintAST::visitFuncDecl(FuncDecl *decl) {
26242637
printGenericDeclGenericRequirements(decl);
26252638
}
26262639

2627-
if (auto BodyFunc = Options.FunctionBody) {
2628-
BodyFunc(decl, Printer);
2629-
indent();
2630-
} else if (Options.FunctionDefinitions && decl->getBody()) {
2631-
Printer << " ";
2632-
visit(decl->getBody());
2633-
}
2640+
printBodyIfNecessary(decl);
26342641
}
26352642

26362643
void PrintAST::printEnumElement(EnumElementDecl *elt) {
@@ -2799,13 +2806,7 @@ void PrintAST::visitConstructorDecl(ConstructorDecl *decl) {
27992806

28002807
printGenericDeclGenericRequirements(decl);
28012808

2802-
if (auto BodyFunc = Options.FunctionBody) {
2803-
BodyFunc(decl, Printer);
2804-
indent();
2805-
} else if (Options.FunctionDefinitions && decl->getBody()) {
2806-
Printer << " ";
2807-
visit(decl->getBody());
2808-
}
2809+
printBodyIfNecessary(decl);
28092810
}
28102811

28112812
void PrintAST::visitDestructorDecl(DestructorDecl *decl) {
@@ -2817,12 +2818,7 @@ void PrintAST::visitDestructorDecl(DestructorDecl *decl) {
28172818
Printer << "deinit";
28182819
});
28192820

2820-
if (!Options.FunctionDefinitions || !decl->getBody()) {
2821-
return;
2822-
}
2823-
2824-
Printer << " ";
2825-
visit(decl->getBody());
2821+
printBodyIfNecessary(decl);
28262822
}
28272823

28282824
void PrintAST::visitInfixOperatorDecl(InfixOperatorDecl *decl) {
@@ -2932,11 +2928,7 @@ void PrintAST::visitMissingMemberDecl(MissingMemberDecl *decl) {
29322928
}
29332929

29342930
void PrintAST::visitBraceStmt(BraceStmt *stmt) {
2935-
Printer << "{";
2936-
printASTNodes(stmt->getElements());
2937-
Printer.printNewline();
2938-
indent();
2939-
Printer << "}";
2931+
printBraceStmt(stmt);
29402932
}
29412933

29422934
void PrintAST::visitReturnStmt(ReturnStmt *stmt) {

test/IDE/reconstruct_type_from_mangled_name.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -291,11 +291,11 @@ fileprivate func privateFunction(_ d: VeryPrivateData) {}
291291
struct HasSubscript {
292292
// CHECK: decl: subscript(t: Int) -> Int { get set }
293293
subscript(_ t: Int) -> Int {
294-
// CHECK: decl: get {} for '' usr=s:14swift_ide_test12HasSubscriptVyS2icig
294+
// CHECK: decl: get for '' usr=s:14swift_ide_test12HasSubscriptVyS2icig
295295
get {
296296
return t
297297
}
298-
// CHECK: decl: set {} for '' usr=s:14swift_ide_test12HasSubscriptVyS2icis
298+
// CHECK: decl: set for '' usr=s:14swift_ide_test12HasSubscriptVyS2icis
299299
set {}
300300
}
301301
}
@@ -310,14 +310,14 @@ struct HasGenericSubscript<T> {
310310
// CHECK: decl: FAILURE for 't'
311311
subscript<U>(_ t: T) -> U {
312312

313-
// CHECK: decl: get {} for '' usr=s:14swift_ide_test19HasGenericSubscriptVyqd__xcluig
313+
// CHECK: decl: get for '' usr=s:14swift_ide_test19HasGenericSubscriptVyqd__xcluig
314314
// FIXME
315315
// CHECK: dref: FAILURE for 't'
316316
get {
317317
return t as! U
318318
}
319319

320-
// CHECK: decl: set {} for '' usr=s:14swift_ide_test19HasGenericSubscriptVyqd__xcluis
320+
// CHECK: decl: set for '' usr=s:14swift_ide_test19HasGenericSubscriptVyqd__xcluis
321321
set {}
322322
}
323323
}

test/ModuleInterface/inlinable-function.swift

Lines changed: 57 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// RUN: %empty-directory(%t)
22
// RUN: %target-swift-frontend -emit-module -o %t/Test.swiftmodule -emit-interface-path %t/Test.swiftinterface -module-name Test %s
33
// RUN: %FileCheck %s < %t/Test.swiftinterface
4-
// RUN: %target-swift-frontend -emit-module -o /dev/null -merge-modules %t/Test.swiftmodule -emit-interface-path - -module-name Test | %FileCheck %s
4+
// RUN: %target-swift-frontend -emit-module -o /dev/null -merge-modules %t/Test.swiftmodule -disable-objc-attr-requires-foundation-module -emit-interface-path - -module-name Test | %FileCheck %s
55

66
// CHECK: public struct Foo : Hashable {
77
public struct Foo: Hashable {
@@ -136,6 +136,25 @@ public struct Foo: Hashable {
136136
print("Not inlinable")
137137
}
138138

139+
// CHECK: public init(value: [[INT]]) {
140+
// CHECK-NEXT: topLevelUsableFromInline()
141+
// CHECK-NEXT: noAccessors = value
142+
// CHECK-NEXT: hasDidSet = value
143+
// CHECK-NEXT: }
144+
@inlinable public init(value: Int) {
145+
topLevelUsableFromInline()
146+
noAccessors = value
147+
hasDidSet = value
148+
}
149+
150+
// CHECK: public init(){{$}}
151+
// CHECK-NOT: noAccessors = 0
152+
// CHECK-NOT: hasDidSet = 0
153+
public init() {
154+
noAccessors = 0
155+
hasDidSet = 0
156+
}
157+
139158
// CHECK: {{^}}}
140159
}
141160

@@ -156,3 +175,40 @@ internal func topLevelUsableFromInline() {
156175
@inlinable public func topLevelInlinable() {
157176
topLevelUsableFromInline()
158177
}
178+
179+
// CHECK: public class HasInlinableDeinit {
180+
public class HasInlinableDeinit {
181+
// CHECK: public init(){{$}}
182+
public init() {}
183+
184+
// CHECK: @objc @inlinable deinit {
185+
// CHECK-NEXT: print("goodbye")
186+
// CHECK-NEXT: }
187+
@inlinable deinit {
188+
print("goodbye")
189+
}
190+
191+
// CHECK-NEXT: }
192+
}
193+
194+
// CHECK: public class HasStandardDeinit {
195+
public class HasStandardDeinit {
196+
// CHECK: public init(){{$}}
197+
public init() {}
198+
199+
// CHECK: @objc deinit{{$}}
200+
deinit {
201+
print("goodbye")
202+
}
203+
204+
// CHECK-NEXT: }
205+
}
206+
207+
// CHECK: public class HasDefaultDeinit {
208+
public class HasDefaultDeinit {
209+
// CHECK: public init(){{$}}
210+
public init() {}
211+
212+
// CHECK: @objc deinit{{$}}
213+
// CHECK-NEXT: }
214+
}

test/SourceKit/DocSupport/doc_source_file.swift.response

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2025,7 +2025,7 @@
20252025
key.usr: "s:4main2CCC4extVSivg",
20262026
key.offset: 748,
20272027
key.length: 11,
2028-
key.fully_annotated_decl: "<decl.function.accessor.getter><decl.name>get</decl.name> {}</decl.function.accessor.getter>"
2028+
key.fully_annotated_decl: "<decl.function.accessor.getter><decl.name>get</decl.name></decl.function.accessor.getter>"
20292029
}
20302030
]
20312031
},
@@ -2069,14 +2069,14 @@
20692069
key.usr: "s:4main16ComputedPropertyC5valueSivg",
20702070
key.offset: 853,
20712071
key.length: 51,
2072-
key.fully_annotated_decl: "<decl.function.accessor.getter><decl.name>get</decl.name> {}</decl.function.accessor.getter>"
2072+
key.fully_annotated_decl: "<decl.function.accessor.getter><decl.name>get</decl.name></decl.function.accessor.getter>"
20732073
},
20742074
{
20752075
key.kind: source.lang.swift.decl.function.accessor.setter,
20762076
key.usr: "s:4main16ComputedPropertyC5valueSivs",
20772077
key.offset: 910,
20782078
key.length: 49,
2079-
key.fully_annotated_decl: "<decl.function.accessor.setter><decl.name>set(newVal)</decl.name> {}</decl.function.accessor.setter>"
2079+
key.fully_annotated_decl: "<decl.function.accessor.setter><decl.name>set(newVal)</decl.name></decl.function.accessor.setter>"
20802080
},
20812081
{
20822082
key.kind: source.lang.swift.decl.var.instance,
@@ -2089,7 +2089,7 @@
20892089
key.usr: "s:4main16ComputedPropertyC8readOnlySivg",
20902090
key.offset: 987,
20912091
key.length: 11,
2092-
key.fully_annotated_decl: "<decl.function.accessor.getter><decl.name>get</decl.name> {}</decl.function.accessor.getter>"
2092+
key.fully_annotated_decl: "<decl.function.accessor.getter><decl.name>get</decl.name></decl.function.accessor.getter>"
20932093
}
20942094
]
20952095
},
@@ -2205,14 +2205,14 @@
22052205
key.usr: "s:4main3CC2CyS2icig",
22062206
key.offset: 1162,
22072207
key.length: 25,
2208-
key.fully_annotated_decl: "<decl.function.accessor.getter><decl.name>get</decl.name> {}</decl.function.accessor.getter>"
2208+
key.fully_annotated_decl: "<decl.function.accessor.getter><decl.name>get</decl.name></decl.function.accessor.getter>"
22092209
},
22102210
{
22112211
key.kind: source.lang.swift.decl.function.accessor.setter,
22122212
key.usr: "s:4main3CC2CyS2icis",
22132213
key.offset: 1193,
22142214
key.length: 31,
2215-
key.fully_annotated_decl: "<decl.function.accessor.setter><decl.name>set(vvv)</decl.name> {}</decl.function.accessor.setter>"
2215+
key.fully_annotated_decl: "<decl.function.accessor.setter><decl.name>set(vvv)</decl.name></decl.function.accessor.setter>"
22162216
}
22172217
]
22182218
},
@@ -2269,7 +2269,7 @@
22692269
key.usr: "s:4main12globReadOnlyAA2S2Vvg",
22702270
key.offset: 1449,
22712271
key.length: 25,
2272-
key.fully_annotated_decl: "<decl.function.accessor.getter><decl.name>get</decl.name> {}</decl.function.accessor.getter>"
2272+
key.fully_annotated_decl: "<decl.function.accessor.getter><decl.name>get</decl.name></decl.function.accessor.getter>"
22732273
},
22742274
{
22752275
key.kind: source.lang.swift.decl.function.free,
@@ -2395,7 +2395,7 @@
23952395
{
23962396
key.kind: source.lang.swift.decl.function.accessor.getter,
23972397
key.usr: "s:4main5Prot2P1pSivg",
2398-
key.fully_annotated_decl: "<decl.function.accessor.getter><decl.name>get</decl.name> {}</decl.function.accessor.getter>"
2398+
key.fully_annotated_decl: "<decl.function.accessor.getter><decl.name>get</decl.name></decl.function.accessor.getter>"
23992399
},
24002400
{
24012401
key.kind: source.lang.swift.decl.function.method.instance,

0 commit comments

Comments
 (0)