Skip to content

[5.0][SR 5641] Implement support for defer in the playground transform and PC macro. #14124

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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://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://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