Skip to content

Commit b040b4d

Browse files
authored
Merge pull request #14124 from cwakamo/playground-transform-defer-statements-5.0
2 parents 0ffca8b + 739b574 commit b040b4d

File tree

5 files changed

+132
-0
lines changed

5 files changed

+132
-0
lines changed

lib/Sema/PCMacro.cpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,8 @@ class Instrumenter : InstrumenterBase {
5252
return S;
5353
case StmtKind::Brace:
5454
return transformBraceStmt(cast<BraceStmt>(S));
55+
case StmtKind::Defer:
56+
return transformDeferStmt(cast<DeferStmt>(S));
5557
case StmtKind::If:
5658
return transformIfStmt(cast<IfStmt>(S));
5759
case StmtKind::Guard:
@@ -282,6 +284,21 @@ class Instrumenter : InstrumenterBase {
282284
}
283285
return DCS;
284286
}
287+
288+
DeferStmt *transformDeferStmt(DeferStmt *DS) {
289+
if (auto *FD = DS->getTempDecl()) {
290+
// Temporarily unmark the DeferStmt's FuncDecl as implicit so it is
291+
// transformed (as typically implicit Decls are skipped by the
292+
// transformer).
293+
auto Implicit = FD->isImplicit();
294+
FD->setImplicit(false);
295+
auto *D = transformDecl(FD);
296+
D->setImplicit(Implicit);
297+
assert(D == FD);
298+
}
299+
return DS;
300+
301+
}
285302

286303
Decl *transformDecl(Decl *D) {
287304
if (D->isImplicit())

lib/Sema/PlaygroundTransform.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,8 @@ class Instrumenter : InstrumenterBase {
126126
return S;
127127
case StmtKind::Brace:
128128
return transformBraceStmt(cast<BraceStmt>(S));
129+
case StmtKind::Defer:
130+
return transformDeferStmt(cast<DeferStmt>(S));
129131
case StmtKind::If:
130132
return transformIfStmt(cast<IfStmt>(S));
131133
case StmtKind::Guard:
@@ -153,6 +155,20 @@ class Instrumenter : InstrumenterBase {
153155
}
154156
}
155157

158+
DeferStmt *transformDeferStmt(DeferStmt *DS) {
159+
if (auto *FD = DS->getTempDecl()) {
160+
// Temporarily unmark the DeferStmt's FuncDecl as implicit so it is
161+
// transformed (as typically implicit Decls are skipped by the
162+
// transformer).
163+
auto Implicit = FD->isImplicit();
164+
FD->setImplicit(false);
165+
auto *D = transformDecl(FD);
166+
D->setImplicit(Implicit);
167+
assert(D == FD);
168+
}
169+
return DS;
170+
}
171+
156172
// transform*() return their input if it's unmodified,
157173
// or a modified copy of their input otherwise.
158174
IfStmt *transformIfStmt(IfStmt *IS) {

test/PCMacro/defer.swift

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// RUN: rm -rf %t
2+
// RUN: mkdir -p %t
3+
// RUN: cp %s %t/main.swift
4+
// RUN: %target-build-swift -Xfrontend -pc-macro -o %t/main %S/Inputs/PCMacroRuntime.swift %t/main.swift
5+
// RUN: %target-run %t/main | %FileCheck %s
6+
// RUN: %target-build-swift -Xfrontend -pc-macro -Xfrontend -playground -Xfrontend -debugger-support -o %t/main %S/Inputs/PCMacroRuntime.swift %t/main.swift %S/Inputs/SilentPlaygroundsRuntime.swift
7+
// RUN: %target-run %t/main | %FileCheck %s
8+
// REQUIRES: executable_test
9+
10+
// FIXME: rdar://problem/30234450 PCMacro tests fail on linux in optimized mode
11+
// UNSUPPORTED: OS=linux-gnu
12+
13+
#sourceLocation(file: "main.swift", line: 8)
14+
func foo() {
15+
defer {
16+
2
17+
}
18+
1
19+
}
20+
21+
foo()
22+
23+
// CHECK: [15:1-15:6] pc before
24+
// CHECK-NEXT: [8:1-8:11] pc before
25+
// CHECK-NEXT: [8:1-8:11] pc after
26+
// CHECK-NEXT: [12:3-12:4] pc before
27+
// CHECK-NEXT: [12:3-12:4] pc after
28+
// CHECK-NEXT: [10:5-10:6] pc before
29+
// CHECK-NEXT: [10:5-10:6] pc after
30+
// CHECK-NEXT: [15:1-15:6] pc after

test/PCMacro/nested_function.swift

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
// RUN: rm -rf %t
2+
// RUN: mkdir -p %t
3+
// RUN: cp %s %t/main.swift
4+
// RUN: %target-build-swift -Xfrontend -pc-macro -o %t/main %S/Inputs/PCMacroRuntime.swift %t/main.swift
5+
// RUN: %target-run %t/main | %FileCheck %s
6+
// RUN: %target-build-swift -Xfrontend -pc-macro -Xfrontend -playground -Xfrontend -debugger-support -o %t/main %S/Inputs/PCMacroRuntime.swift %t/main.swift %S/Inputs/SilentPlaygroundsRuntime.swift
7+
// RUN: %target-run %t/main | %FileCheck %s
8+
// REQUIRES: executable_test
9+
10+
// FIXME: rdar://problem/30234450 PCMacro tests fail on linux in optimized mode
11+
// UNSUPPORTED: OS=linux-gnu
12+
13+
#sourceLocation(file: "main.swift", line: 8)
14+
func foo() {
15+
func bar() {
16+
2
17+
}
18+
19+
let baz: () -> Void = {
20+
3
21+
}
22+
23+
1
24+
bar()
25+
baz()
26+
}
27+
28+
foo()
29+
30+
// CHECK: [22:1-22:6] pc before
31+
// CHECK-NEXT: [8:1-8:11] pc before
32+
// CHECK-NEXT: [8:1-8:11] pc after
33+
// CHECK-NEXT: [13:3-15:4] pc before
34+
// CHECK-NEXT: [13:3-15:4] pc after
35+
// CHECK-NEXT: [17:3-17:4] pc before
36+
// CHECK-NEXT: [17:3-17:4] pc after
37+
// CHECK-NEXT: [18:3-18:8] pc before
38+
// CHECK-NEXT: [9:3-9:13] pc before
39+
// CHECK-NEXT: [9:3-9:13] pc after
40+
// CHECK-NEXT: [10:5-10:6] pc before
41+
// CHECK-NEXT: [10:5-10:6] pc after
42+
// CHECK-NEXT: [18:3-18:8] pc after
43+
// CHECK-NEXT: [19:3-19:8] pc before
44+
// CHECK-NEXT: [14:5-14:6] pc before
45+
// CHECK-NEXT: [14:5-14:6] pc after
46+
// CHECK-NEXT: [14:5-14:6] pc before
47+
// CHECK-NEXT: [14:5-14:6] pc after
48+
// CHECK-NEXT: [19:3-19:8] pc after
49+
// CHECK-NEXT: [22:1-22:6] pc after

test/PlaygroundTransform/defer.swift

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// RUN: %empty-directory(%t)
2+
// RUN: cp %s %t/main.swift
3+
// RUN: %target-build-swift -Xfrontend -playground -Xfrontend -debugger-support -o %t/main %S/Inputs/PlaygroundsRuntime.swift %t/main.swift
4+
// RUN: %target-run %t/main | %FileCheck %s
5+
// RUN: %target-build-swift -Xfrontend -pc-macro -Xfrontend -playground -Xfrontend -debugger-support -o %t/main %S/Inputs/PlaygroundsRuntime.swift %S/Inputs/SilentPCMacroRuntime.swift %t/main.swift
6+
// RUN: %target-run %t/main | %FileCheck %s
7+
// REQUIRES: executable_test
8+
func foo() {
9+
defer {
10+
2
11+
}
12+
1
13+
}
14+
foo()
15+
// CHECK: {{.*}} $builtin_log_scope_entry
16+
// CHECK-NEXT: {{.*}} $builtin_log[='1']
17+
// CHECK-NEXT: {{.*}} $builtin_log_scope_exit
18+
// CHECK-NEXT: {{.*}} $builtin_log_scope_entry
19+
// CHECK-NEXT: {{.*}} $builtin_log[='2']
20+
// CHECK-NEXT: {{.*}} $builtin_log_scope_exit

0 commit comments

Comments
 (0)