Skip to content

Commit fbcf198

Browse files
committed
[PCMacro] Implemented support for defer statements in the PC macro.
As with the playground transform, defer statements were not supported in the PC macro. This commit addresses that oversight. This partially addresses <rdar://problem/29007242>. (cherry picked from commit 2c04058)
1 parent b8e9bf0 commit fbcf198

File tree

2 files changed

+44
-0
lines changed

2 files changed

+44
-0
lines changed

lib/Sema/PCMacro.cpp

Lines changed: 14 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,18 @@ class Instrumenter : InstrumenterBase {
282284
}
283285
return DCS;
284286
}
287+
288+
DeferStmt *transformDeferStmt(DeferStmt *DS) {
289+
if (auto *FD = DS->getTempDecl()) {
290+
auto Implicit = FD->isImplicit();
291+
FD->setImplicit(false);
292+
auto *D = transformDecl(FD);
293+
D->setImplicit(Implicit);
294+
assert(D == FD);
295+
}
296+
return DS;
297+
298+
}
285299

286300
Decl *transformDecl(Decl *D) {
287301
if (D->isImplicit())

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

0 commit comments

Comments
 (0)