Skip to content

[5.1] Upgrade PCMacro/PlaygroundTransform to support module/file IDs #24811

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
merged 1 commit into from
May 29, 2019
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
23 changes: 12 additions & 11 deletions lib/Frontend/Frontend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -807,6 +807,18 @@ void CompilerInstance::parseAndCheckTypesUpTo(
options.WarnLongExpressionTypeChecking,
options.SolverExpressionTimeThreshold,
options.SwitchCheckingInvocationThreshold);

if (!Context->hadError() && Invocation.getFrontendOptions().PCMacro) {
performPCMacro(SF, PersistentState.getTopLevelContext());
}

// Playground transform knows to look out for PCMacro's changes and not
// to playground log them.
if (!Context->hadError() &&
Invocation.getFrontendOptions().PlaygroundTransform) {
performPlaygroundTransform(
SF, Invocation.getFrontendOptions().PlaygroundHighPerformance);
}
});

if (Invocation.isCodeCompletion()) {
Expand Down Expand Up @@ -947,17 +959,6 @@ void CompilerInstance::parseAndTypeCheckMainFileUpTo(
performDebuggerTestingTransform(MainFile);
}

if (mainIsPrimary && !Context->hadError() &&
Invocation.getFrontendOptions().PCMacro) {
performPCMacro(MainFile, PersistentState.getTopLevelContext());
}

// Playground transform knows to look out for PCMacro's changes and not
// to playground log them.
if (mainIsPrimary && !Context->hadError() &&
Invocation.getFrontendOptions().PlaygroundTransform)
performPlaygroundTransform(
MainFile, Invocation.getFrontendOptions().PlaygroundHighPerformance);
if (!mainIsPrimary) {
performNameBinding(MainFile);
}
Expand Down
37 changes: 37 additions & 0 deletions lib/Sema/InstrumenterSupport.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@

#include "InstrumenterSupport.h"
#include "swift/AST/DiagnosticSuppression.h"
#include "swift/Demangling/Punycode.h"
#include "llvm/Support/Path.h"

using namespace swift;
using namespace swift::instrumenter_support;
Expand Down Expand Up @@ -75,6 +77,41 @@ class ErrorFinder : public ASTWalker {
};
} // end anonymous namespace

InstrumenterBase::InstrumenterBase(ASTContext &C, DeclContext *DC)
: Context(C), TypeCheckDC(DC), CF(*this) {
// Prefixes for module and file vars
const std::string builtinPrefix = "__builtin";
const std::string modulePrefix = "_pg_module_";
const std::string filePrefix = "_pg_file_";

// Setup Module identifier
std::string moduleName = TypeCheckDC->getParentModule()->getName().str();
Identifier moduleIdentifier =
Context.getIdentifier(builtinPrefix + modulePrefix + moduleName);

SmallVector<ValueDecl *, 1> results;
TypeCheckDC->getParentModule()->lookupValue(
{}, moduleIdentifier, NLKind::UnqualifiedLookup, results);

ModuleIdentifier = (results.size() == 1) ? moduleIdentifier : Identifier();

// Setup File identifier
StringRef filePath = TypeCheckDC->getParentSourceFile()->getFilename();
StringRef fileName = llvm::sys::path::stem(filePath);

std::string filePunycodeName;
Punycode::encodePunycodeUTF8(fileName, filePunycodeName, true);
Identifier fileIdentifier =
Context.getIdentifier(builtinPrefix + modulePrefix + moduleName +
filePrefix + filePunycodeName);

results.clear();
TypeCheckDC->getParentModule()->lookupValue(
{}, fileIdentifier, NLKind::UnqualifiedLookup, results);

FileIdentifier = (results.size() == 1) ? fileIdentifier : Identifier();
}

void InstrumenterBase::anchor() {}

bool InstrumenterBase::doTypeCheckImpl(ASTContext &Ctx, DeclContext *DC,
Expand Down
7 changes: 6 additions & 1 deletion lib/Sema/InstrumenterSupport.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,12 @@ template <class E> class Added {
class InstrumenterBase {

protected:
InstrumenterBase() : CF(*this) {}
ASTContext &Context;
DeclContext *TypeCheckDC;
Identifier ModuleIdentifier;
Identifier FileIdentifier;

InstrumenterBase(ASTContext &C, DeclContext *DC);
virtual ~InstrumenterBase() = default;
virtual void anchor();
virtual BraceStmt *transformBraceStmt(BraceStmt *BS,
Expand Down
42 changes: 33 additions & 9 deletions lib/Sema/PCMacro.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,11 @@ namespace {

class Instrumenter : InstrumenterBase {
private:
ASTContext &Context;
DeclContext *TypeCheckDC;
unsigned &TmpNameIndex;

public:
Instrumenter(ASTContext &C, DeclContext *DC, unsigned &TmpNameIndex)
: Context(C), TypeCheckDC(DC), TmpNameIndex(TmpNameIndex) {}
: InstrumenterBase(C, DC), TmpNameIndex(TmpNameIndex) {}

Stmt *transformStmt(Stmt *S) {
switch (S->getKind()) {
Expand Down Expand Up @@ -536,15 +534,28 @@ class Instrumenter : InstrumenterBase {
Expr *StartColumn = IntegerLiteralExpr::createFromUnsigned(Context, StartLC.second);
Expr *EndColumn = IntegerLiteralExpr::createFromUnsigned(Context, EndLC.second);

llvm::SmallVector<Expr *, 5> ArgsWithSourceRange{};
Expr *ModuleExpr =
!ModuleIdentifier.empty()
? (Expr *)new (Context) UnresolvedDeclRefExpr(
ModuleIdentifier, DeclRefKind::Ordinary, DeclNameLoc(SR.End))
: (Expr *)IntegerLiteralExpr::createFromUnsigned(Context, 0);

ArgsWithSourceRange.append({StartLine, EndLine, StartColumn, EndColumn});
Expr *FileExpr =
!FileIdentifier.empty()
? (Expr *)new (Context) UnresolvedDeclRefExpr(
FileIdentifier, DeclRefKind::Ordinary, DeclNameLoc(SR.End))
: (Expr *)IntegerLiteralExpr::createFromUnsigned(Context, 0);

llvm::SmallVector<Expr *, 6> ArgsWithSourceRange{};

ArgsWithSourceRange.append(
{StartLine, EndLine, StartColumn, EndColumn, ModuleExpr, FileExpr});

UnresolvedDeclRefExpr *BeforeLoggerRef = new (Context)
UnresolvedDeclRefExpr(Context.getIdentifier("__builtin_pc_before"),
DeclRefKind::Ordinary, DeclNameLoc(SR.End));
BeforeLoggerRef->setImplicit(true);
SmallVector<Identifier, 4> ArgLabels(ArgsWithSourceRange.size(),
SmallVector<Identifier, 6> ArgLabels(ArgsWithSourceRange.size(),
Identifier());
ApplyExpr *BeforeLoggerCall = CallExpr::createImplicit(
Context, BeforeLoggerRef, ArgsWithSourceRange, ArgLabels);
Expand Down Expand Up @@ -603,17 +614,30 @@ class Instrumenter : InstrumenterBase {
Expr *StartColumn = IntegerLiteralExpr::createFromUnsigned(Context, StartLC.second);
Expr *EndColumn = IntegerLiteralExpr::createFromUnsigned(Context, EndLC.second);

llvm::SmallVector<Expr *, 4> ArgsWithSourceRange{};
Expr *ModuleExpr =
!ModuleIdentifier.empty()
? (Expr *)new (Context) UnresolvedDeclRefExpr(
ModuleIdentifier, DeclRefKind::Ordinary, DeclNameLoc(SR.End))
: (Expr *)IntegerLiteralExpr::createFromUnsigned(Context, 0);

Expr *FileExpr =
!FileIdentifier.empty()
? (Expr *)new (Context) UnresolvedDeclRefExpr(
FileIdentifier, DeclRefKind::Ordinary, DeclNameLoc(SR.End))
: (Expr *)IntegerLiteralExpr::createFromUnsigned(Context, 0);

llvm::SmallVector<Expr *, 6> ArgsWithSourceRange{};

ArgsWithSourceRange.append({StartLine, EndLine, StartColumn, EndColumn});
ArgsWithSourceRange.append(
{StartLine, EndLine, StartColumn, EndColumn, ModuleExpr, FileExpr});

UnresolvedDeclRefExpr *LoggerRef = new (Context)
UnresolvedDeclRefExpr(Context.getIdentifier(LoggerName),
DeclRefKind::Ordinary, DeclNameLoc(SR.End));

LoggerRef->setImplicit(true);

SmallVector<Identifier, 4> ArgLabels(ArgsWithSourceRange.size(),
SmallVector<Identifier, 6> ArgLabels(ArgsWithSourceRange.size(),
Identifier());
ApplyExpr *LoggerCall = CallExpr::createImplicit(
Context, LoggerRef, ArgsWithSourceRange, ArgLabels);
Expand Down
21 changes: 16 additions & 5 deletions lib/Sema/PlaygroundTransform.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,6 @@ namespace {
class Instrumenter : InstrumenterBase {
private:
std::mt19937_64 &RNG;
ASTContext &Context;
DeclContext *TypeCheckDC;
unsigned &TmpNameIndex;
bool HighPerformance;

Expand Down Expand Up @@ -117,7 +115,7 @@ class Instrumenter : InstrumenterBase {
public:
Instrumenter(ASTContext &C, DeclContext *DC, std::mt19937_64 &RNG, bool HP,
unsigned &TmpNameIndex)
: RNG(RNG), Context(C), TypeCheckDC(DC), TmpNameIndex(TmpNameIndex),
: InstrumenterBase(C, DC), RNG(RNG), TmpNameIndex(TmpNameIndex),
HighPerformance(HP) {}

Stmt *transformStmt(Stmt *S) {
Expand Down Expand Up @@ -811,17 +809,30 @@ class Instrumenter : InstrumenterBase {
Expr *StartColumn = IntegerLiteralExpr::createFromUnsigned(Context, StartLC.second);
Expr *EndColumn = IntegerLiteralExpr::createFromUnsigned(Context, EndLC.second);

Expr *ModuleExpr =
!ModuleIdentifier.empty()
? (Expr *)new (Context) UnresolvedDeclRefExpr(
ModuleIdentifier, DeclRefKind::Ordinary, DeclNameLoc(SR.End))
: (Expr *)IntegerLiteralExpr::createFromUnsigned(Context, 0);

Expr *FileExpr =
!FileIdentifier.empty()
? (Expr *)new (Context) UnresolvedDeclRefExpr(
FileIdentifier, DeclRefKind::Ordinary, DeclNameLoc(SR.End))
: (Expr *)IntegerLiteralExpr::createFromUnsigned(Context, 0);

llvm::SmallVector<Expr *, 6> ArgsWithSourceRange(Args.begin(), Args.end());

ArgsWithSourceRange.append({StartLine, EndLine, StartColumn, EndColumn});
ArgsWithSourceRange.append(
{StartLine, EndLine, StartColumn, EndColumn, ModuleExpr, FileExpr});

UnresolvedDeclRefExpr *LoggerRef = new (Context)
UnresolvedDeclRefExpr(Context.getIdentifier(LoggerName),
DeclRefKind::Ordinary, DeclNameLoc(SR.End));

LoggerRef->setImplicit(true);

SmallVector<Identifier, 4> ArgLabels(ArgsWithSourceRange.size(),
SmallVector<Identifier, 6> ArgLabels(ArgsWithSourceRange.size(),
Identifier());
ApplyExpr *LoggerCall = CallExpr::createImplicit(
Context, LoggerRef, ArgsWithSourceRange, ArgLabels);
Expand Down
4 changes: 2 additions & 2 deletions test/IRGen/playground.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ import Swift
@objc class C { }

private func __builtin_log_scope_entry(_ startLine: Int, _ startColumn: Int,
_ endLine: Int, _ endColumn: Int) { }
_ endLine: Int, _ endColumn: Int, _ moduleID: Int, _ fileID: Int) { }
private func __builtin_log_scope_exit(_ startLine: Int, _ startColumn: Int,
_ endLine: Int, _ endColumn: Int) { }
_ endLine: Int, _ endColumn: Int, _ moduleID: Int, _ fileID: Int) { }
private func __builtin_send_data<T>(_ object: T) { }

public func anchor() {}
Expand Down
8 changes: 4 additions & 4 deletions test/PCMacro/Inputs/PCMacroRuntime.swift
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
// This is the minimal amount of runtime required to operate
// lib/Sema/PCMacro.cpp successfully.

func __builtin_pc_before(_ sl : Int, _ el : Int, _ sc : Int, _ ec: Int) {
print("[\(sl):\(sc)-\(el):\(ec)] pc before")
public func __builtin_pc_before(_ sl : Int, _ el : Int, _ sc : Int, _ ec: Int, _ moduleId : Int, _ fileId : Int) {
print("[\(moduleId):\(fileId)] [\(sl):\(sc)-\(el):\(ec)] pc before")
}

func __builtin_pc_after(_ sl : Int, _ el : Int, _ sc : Int, _ ec: Int) {
print("[\(sl):\(sc)-\(el):\(ec)] pc after")
public func __builtin_pc_after(_ sl : Int, _ el : Int, _ sc : Int, _ ec: Int, _ moduleId : Int, _ fileId : Int) {
print("[\(moduleId):\(fileId)] [\(sl):\(sc)-\(el):\(ec)] pc after")
}
11 changes: 11 additions & 0 deletions test/PCMacro/Inputs/PlaygroundModuleAndFileIDs.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// test (module-name) -> 1 (module-id)
internal let __builtin_pg_module_test: Int = 1

// test (module-name) -> 1 (module-id)
internal let __builtin_pg_module_test2: Int = 1

// test (module-name), main (file-name), main_ (file-name-punycode) -> 2 (file-id)
internal let __builtin_pg_module_test_pg_file_main_: Int = 2

// test (module-name), main (file-name), main_ (file-name-punycode) -> 2 (file-id)
internal let __builtin_pg_module_test2_pg_file_main_: Int = 2
51 changes: 32 additions & 19 deletions test/PCMacro/Inputs/SilentPlaygroundsRuntime.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,50 +12,63 @@ struct SourceRange {
}
}

struct ModuleFileIdentifier {
let moduleId : Int
let fileId : Int
var text : String {
return "[\(moduleId):\(fileId)]"
}
}

class LogRecord {
let text : String

init(api : String, object : Any, name : String, id : Int, range : SourceRange) {
init(api : String, object : Any, name : String, id : Int, range : SourceRange, moduleFileId : ModuleFileIdentifier) {
var object_description : String = ""
print(object, terminator: "", to: &object_description)
text = range.text + " " + api + "[" + name + "='" + object_description + "']"
text = moduleFileId.text + " " + range.text + " " + api + "[" + name + "='" + object_description + "']"
}
init(api : String, object : Any, name : String, range : SourceRange) {
init(api : String, object : Any, name : String, range : SourceRange, moduleFileId : ModuleFileIdentifier) {
var object_description : String = ""
print(object, terminator: "", to: &object_description)
text = range.text + " " + api + "[" + name + "='" + object_description + "']"
text = moduleFileId.text + " " + range.text + " " + api + "[" + name + "='" + object_description + "']"
}
init(api : String, object: Any, range : SourceRange) {
init(api : String, object: Any, range : SourceRange, moduleFileId : ModuleFileIdentifier) {
var object_description : String = ""
print(object, terminator: "", to: &object_description)
text = range.text + " " + api + "['" + object_description + "']"
text = moduleFileId.text + " " + range.text + " " + api + "['" + object_description + "']"
}
init(api: String, range : SourceRange) {
text = range.text + " " + api
init(api: String, range : SourceRange, moduleFileId : ModuleFileIdentifier) {
text = moduleFileId.text + " " + range.text + " " + api
}
}

func __builtin_log<T>(_ object : T, _ name : String, _ sl : Int, _ el : Int, _ sc : Int, _ ec: Int) -> AnyObject? {
return LogRecord(api:"__builtin_log", object:object, name:name, range : SourceRange(sl:sl, el:el, sc:sc, ec:ec))
public func __builtin_log<T>(_ object : T, _ name : String, _ sl : Int, _ el : Int, _ sc : Int, _ ec: Int, _ moduleId : Int, _ fileId : Int) -> AnyObject? {
let moduleFileId = ModuleFileIdentifier(moduleId:moduleId, fileId:fileId)
return LogRecord(api:"__builtin_log", object:object, name:name, range:SourceRange(sl:sl, el:el, sc:sc, ec:ec), moduleFileId:moduleFileId)
}

func __builtin_log_with_id<T>(_ object : T, _ name : String, _ id : Int, _ sl : Int, _ el : Int, _ sc : Int, _ ec: Int) -> AnyObject? {
return LogRecord(api:"__builtin_log", object:object, name:name, id:id, range : SourceRange(sl:sl, el:el, sc:sc, ec:ec))
public func __builtin_log_with_id<T>(_ object : T, _ name : String, _ id : Int, _ sl : Int, _ el : Int, _ sc : Int, _ ec: Int, _ moduleId : Int, _ fileId : Int) -> AnyObject? {
let moduleFileId = ModuleFileIdentifier(moduleId:moduleId, fileId:fileId)
return LogRecord(api:"__builtin_log", object:object, name:name, id:id, range:SourceRange(sl:sl, el:el, sc:sc, ec:ec), moduleFileId:moduleFileId)
}

func __builtin_log_scope_entry(_ sl : Int, _ el : Int, _ sc : Int, _ ec: Int) -> AnyObject? {
return LogRecord(api:"__builtin_log_scope_entry", range : SourceRange(sl:sl, el:el, sc:sc, ec:ec))
public func __builtin_log_scope_entry(_ sl : Int, _ el : Int, _ sc : Int, _ ec: Int, _ moduleId : Int, _ fileId : Int) -> AnyObject? {
let moduleFileId = ModuleFileIdentifier(moduleId:moduleId, fileId:fileId)
return LogRecord(api:"__builtin_log_scope_entry", range:SourceRange(sl:sl, el:el, sc:sc, ec:ec), moduleFileId:moduleFileId)
}

func __builtin_log_scope_exit(_ sl : Int, _ el : Int, _ sc : Int, _ ec: Int) -> AnyObject? {
return LogRecord(api:"__builtin_log_scope_exit", range : SourceRange(sl:sl, el:el, sc:sc, ec:ec))
public func __builtin_log_scope_exit(_ sl : Int, _ el : Int, _ sc : Int, _ ec: Int, _ moduleId : Int, _ fileId : Int) -> AnyObject? {
let moduleFileId = ModuleFileIdentifier(moduleId:moduleId, fileId:fileId)
return LogRecord(api:"__builtin_log_scope_exit", range:SourceRange(sl:sl, el:el, sc:sc, ec:ec), moduleFileId:moduleFileId)
}

func __builtin_postPrint(_ sl : Int, _ el : Int, _ sc : Int, _ ec: Int) -> AnyObject? {
return LogRecord(api:"__builtin_postPrint", range : SourceRange(sl:sl, el:el, sc:sc, ec:ec))
public func __builtin_postPrint(_ sl : Int, _ el : Int, _ sc : Int, _ ec: Int, _ moduleId : Int, _ fileId : Int) -> AnyObject? {
let moduleFileId = ModuleFileIdentifier(moduleId:moduleId, fileId:fileId)
return LogRecord(api:"__builtin_postPrint", range:SourceRange(sl:sl, el:el, sc:sc, ec:ec), moduleFileId:moduleFileId)
}

func __builtin_send_data(_ object:AnyObject?) {
public func __builtin_send_data(_ object:AnyObject?) {
let would_print = ((object as! LogRecord).text)
}

Expand Down
7 changes: 5 additions & 2 deletions test/PCMacro/defer.swift
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
// RUN: %empty-directory(%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-build-swift -force-single-frontend-invocation -module-name PlaygroundSupport -emit-module-path %t/PlaygroundSupport.swiftmodule -parse-as-library -c -o %t/PlaygroundSupport.o %S/Inputs/PCMacroRuntime.swift %S/Inputs/SilentPlaygroundsRuntime.swift
// RUN: %target-build-swift -Xfrontend -pc-macro -o %t/main -I=%t %t/PlaygroundSupport.o %t/main.swift
// RUN: %target-codesign %t/main
// RUN: %target-run %t/main | %FileCheck %s
// RUN: %target-build-swift -Xfrontend -pc-macro -Xfrontend -playground -Xfrontend -debugger-support -o %t/main2 %S/Inputs/PCMacroRuntime.swift %t/main.swift %S/Inputs/SilentPlaygroundsRuntime.swift
// RUN: %target-build-swift -Xfrontend -pc-macro -Xfrontend -playground -Xfrontend -debugger-support -o %t/main2 -I=%t %t/PlaygroundSupport.o %t/main.swift
// RUN: %target-codesign %t/main2
// RUN: %target-run %t/main2 | %FileCheck %s
// REQUIRES: executable_test

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

import PlaygroundSupport

#sourceLocation(file: "main.swift", line: 8)
func foo() {
defer {
Expand Down
7 changes: 5 additions & 2 deletions test/PCMacro/didset.swift
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
// RUN: %empty-directory(%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-build-swift -force-single-frontend-invocation -module-name PlaygroundSupport -emit-module-path %t/PlaygroundSupport.swiftmodule -parse-as-library -c -o %t/PlaygroundSupport.o %S/Inputs/PCMacroRuntime.swift %S/Inputs/SilentPlaygroundsRuntime.swift
// RUN: %target-build-swift -Xfrontend -pc-macro -o %t/main -I=%t %t/PlaygroundSupport.o %t/main.swift
// RUN: %target-codesign %t/main
// RUN: %target-run %t/main | %FileCheck %s
// RUN: %target-build-swift -Xfrontend -pc-macro -Xfrontend -playground -o %t/main2 %S/Inputs/PCMacroRuntime.swift %t/main.swift %S/Inputs/SilentPlaygroundsRuntime.swift
// RUN: %target-build-swift -Xfrontend -pc-macro -Xfrontend -playground -Xfrontend -debugger-support -o %t/main2 -I=%t %t/PlaygroundSupport.o %t/main.swift
// RUN: %target-codesign %t/main2
// RUN: %target-run %t/main2 | %FileCheck %s
// REQUIRES: executable_test

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

import PlaygroundSupport

#sourceLocation(file: "main.swift", line: 10)
struct S {
var a : [Int] = [] {
Expand Down
Loading