Skip to content

Commit

Permalink
Merge pull request swiftlang#13885 from cwakamo/fix-defer-playground-…
Browse files Browse the repository at this point in the history
…pcmacro-swift-4.1

[Swift 4.1] Fix the handling of defer statements in the playground transform and PC macro.
  • Loading branch information
bob-wilson committed Jan 13, 2018
2 parents dd739ec + 8c83bf7 commit 2e48f09
Show file tree
Hide file tree
Showing 5 changed files with 132 additions and 0 deletions.
17 changes: 17 additions & 0 deletions lib/Sema/PCMacro.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ class Instrumenter : InstrumenterBase {
return S;
case StmtKind::Brace:
return transformBraceStmt(cast<BraceStmt>(S));
case StmtKind::Defer:
return transformDeferStmt(cast<DeferStmt>(S));
case StmtKind::If:
return transformIfStmt(cast<IfStmt>(S));
case StmtKind::Guard:
Expand Down Expand Up @@ -282,6 +284,21 @@ class Instrumenter : InstrumenterBase {
}
return DCS;
}

DeferStmt *transformDeferStmt(DeferStmt *DS) {
if (auto *FD = DS->getTempDecl()) {
// Temporarily unmark the DeferStmt's FuncDecl as implicit so it is
// transformed (as typically implicit Decls are skipped by the
// transformer).
auto Implicit = FD->isImplicit();
FD->setImplicit(false);
auto *D = transformDecl(FD);
D->setImplicit(Implicit);
assert(D == FD);
}
return DS;

}

Decl *transformDecl(Decl *D) {
if (D->isImplicit())
Expand Down
16 changes: 16 additions & 0 deletions lib/Sema/PlaygroundTransform.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,8 @@ class Instrumenter : InstrumenterBase {
return S;
case StmtKind::Brace:
return transformBraceStmt(cast<BraceStmt>(S));
case StmtKind::Defer:
return transformDeferStmt(cast<DeferStmt>(S));
case StmtKind::If:
return transformIfStmt(cast<IfStmt>(S));
case StmtKind::Guard:
Expand Down Expand Up @@ -153,6 +155,20 @@ class Instrumenter : InstrumenterBase {
}
}

DeferStmt *transformDeferStmt(DeferStmt *DS) {
if (auto *FD = DS->getTempDecl()) {
// Temporarily unmark the DeferStmt's FuncDecl as implicit so it is
// transformed (as typically implicit Decls are skipped by the
// transformer).
auto Implicit = FD->isImplicit();
FD->setImplicit(false);
auto *D = transformDecl(FD);
D->setImplicit(Implicit);
assert(D == FD);
}
return DS;
}

// transform*() return their input if it's unmodified,
// or a modified copy of their input otherwise.
IfStmt *transformIfStmt(IfStmt *IS) {
Expand Down
30 changes: 30 additions & 0 deletions test/PCMacro/defer.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// RUN: rm -rf %t
// RUN: mkdir -p %t
// RUN: cp %s %t/main.swift
// RUN: %target-build-swift -Xfrontend -pc-macro -o %t/main %S/Inputs/PCMacroRuntime.swift %t/main.swift
// RUN: %target-run %t/main | %FileCheck %s
// 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
// RUN: %target-run %t/main | %FileCheck %s
// REQUIRES: executable_test

// FIXME: rdar:https://problem/30234450 PCMacro tests fail on linux in optimized mode
// UNSUPPORTED: OS=linux-gnu

#sourceLocation(file: "main.swift", line: 8)
func foo() {
defer {
2
}
1
}

foo()

// CHECK: [15:1-15:6] pc before
// CHECK-NEXT: [8:1-8:11] pc before
// CHECK-NEXT: [8:1-8:11] pc after
// CHECK-NEXT: [12:3-12:4] pc before
// CHECK-NEXT: [12:3-12:4] pc after
// CHECK-NEXT: [10:5-10:6] pc before
// CHECK-NEXT: [10:5-10:6] pc after
// CHECK-NEXT: [15:1-15:6] pc after
49 changes: 49 additions & 0 deletions test/PCMacro/nested_function.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
// RUN: rm -rf %t
// RUN: mkdir -p %t
// RUN: cp %s %t/main.swift
// RUN: %target-build-swift -Xfrontend -pc-macro -o %t/main %S/Inputs/PCMacroRuntime.swift %t/main.swift
// RUN: %target-run %t/main | %FileCheck %s
// 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
// RUN: %target-run %t/main | %FileCheck %s
// REQUIRES: executable_test

// FIXME: rdar:https://problem/30234450 PCMacro tests fail on linux in optimized mode
// UNSUPPORTED: OS=linux-gnu

#sourceLocation(file: "main.swift", line: 8)
func foo() {
func bar() {
2
}

let baz: () -> Void = {
3
}

1
bar()
baz()
}

foo()

// CHECK: [22:1-22:6] pc before
// CHECK-NEXT: [8:1-8:11] pc before
// CHECK-NEXT: [8:1-8:11] pc after
// CHECK-NEXT: [13:3-15:4] pc before
// CHECK-NEXT: [13:3-15:4] pc after
// CHECK-NEXT: [17:3-17:4] pc before
// CHECK-NEXT: [17:3-17:4] pc after
// CHECK-NEXT: [18:3-18:8] pc before
// CHECK-NEXT: [9:3-9:13] pc before
// CHECK-NEXT: [9:3-9:13] pc after
// CHECK-NEXT: [10:5-10:6] pc before
// CHECK-NEXT: [10:5-10:6] pc after
// CHECK-NEXT: [18:3-18:8] pc after
// CHECK-NEXT: [19:3-19:8] pc before
// CHECK-NEXT: [14:5-14:6] pc before
// CHECK-NEXT: [14:5-14:6] pc after
// CHECK-NEXT: [14:5-14:6] pc before
// CHECK-NEXT: [14:5-14:6] pc after
// CHECK-NEXT: [19:3-19:8] pc after
// CHECK-NEXT: [22:1-22:6] pc after
20 changes: 20 additions & 0 deletions test/PlaygroundTransform/defer.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// RUN: %empty-directory(%t)
// RUN: cp %s %t/main.swift
// RUN: %target-build-swift -Xfrontend -playground -Xfrontend -debugger-support -o %t/main %S/Inputs/PlaygroundsRuntime.swift %t/main.swift
// RUN: %target-run %t/main | %FileCheck %s
// 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
// RUN: %target-run %t/main | %FileCheck %s
// REQUIRES: executable_test
func foo() {
defer {
2
}
1
}
foo()
// CHECK: {{.*}} $builtin_log_scope_entry
// CHECK-NEXT: {{.*}} $builtin_log[='1']
// CHECK-NEXT: {{.*}} $builtin_log_scope_exit
// CHECK-NEXT: {{.*}} $builtin_log_scope_entry
// CHECK-NEXT: {{.*}} $builtin_log[='2']
// CHECK-NEXT: {{.*}} $builtin_log_scope_exit

0 comments on commit 2e48f09

Please sign in to comment.