Skip to content

Commit

Permalink
[PlaygroundTransform] embed source ranges in data packets before send…
Browse files Browse the repository at this point in the history
…ing them.

<rdar://problem/25043276>
  • Loading branch information
scallanan committed Apr 15, 2016
1 parent 6a660e2 commit a0fa099
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 62 deletions.
73 changes: 32 additions & 41 deletions lib/Sema/PlaygroundTransform.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -971,37 +971,6 @@ class Instrumenter {
SourceRange SR) {
Expr *LoggerArgs = nullptr;

if (Args.size() == 1) {
LoggerArgs = new (Context) ParenExpr(SourceLoc(),
Args[0],
SourceLoc(),
false);
} else {
LoggerArgs = TupleExpr::createImplicit(Context, Args, { });
}

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

LoggerRef->setImplicit(true);

ApplyExpr *LoggerCall = new (Context) CallExpr(LoggerRef, LoggerArgs, true,
Type());
Added<ApplyExpr*> AddedLogger(LoggerCall);

if (!doTypeCheck(Context, TypeCheckDC, AddedLogger)) {
return nullptr;
}

return buildLoggerCallWithApply(AddedLogger, SR);
}

// Assumes Apply has already been type-checked.
Added<Stmt *> buildLoggerCallWithApply(Added<ApplyExpr*> Apply,
SourceRange SR) {
std::pair<unsigned, unsigned> StartLC =
Context.SourceMgr.getLineAndColumn(SR.Start);

Expand Down Expand Up @@ -1030,6 +999,34 @@ class Instrumenter {
Expr *EndColumn = new (Context) IntegerLiteralExpr(end_column_buf,
SR.End, true);

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

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

LoggerArgs = TupleExpr::createImplicit(Context, ArgsWithSourceRange, { });

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

LoggerRef->setImplicit(true);

ApplyExpr *LoggerCall = new (Context) CallExpr(LoggerRef, LoggerArgs, true,
Type());
Added<ApplyExpr*> AddedLogger(LoggerCall);

if (!doTypeCheck(Context, TypeCheckDC, AddedLogger)) {
return nullptr;
}

return buildLoggerCallWithApply(AddedLogger, SR);
}

// Assumes Apply has already been type-checked.
Added<Stmt *> buildLoggerCallWithApply(Added<ApplyExpr*> Apply,
SourceRange SR) {
std::pair<PatternBindingDecl *, VarDecl *> PV =
buildPatternAndVariable(*Apply);

Expand All @@ -1039,16 +1036,10 @@ class Instrumenter {
AccessSemantics::Ordinary,
Apply->getType());

Expr *SendDataArgExprs[] = {
DRE,
StartLine,
EndLine,
StartColumn,
EndColumn
};

TupleExpr *SendDataArgs = TupleExpr::createImplicit(Context,
SendDataArgExprs, { });
ParenExpr *SendDataArgs = new (Context) ParenExpr(SourceLoc(),
DRE,
SourceLoc(),
false);
UnresolvedDeclRefExpr *SendDataRef =
new (Context) UnresolvedDeclRefExpr(
Context.getIdentifier("$builtin_send_data"),
Expand Down
54 changes: 33 additions & 21 deletions test/PlaygroundTransform/Inputs/PlaygroundsRuntime.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,49 +2,61 @@
// should be notifying the maintainer of PlaygroundLogger and also the
// maintainer of lib/Sema/PlaygroundTransform.cpp.

struct SourceRange {
let sl : Int
let el : Int
let sc : Int
let ec : Int
var text : String {
return "[\(sl):\(sc)-\(el):\(ec)]"
}
}

class LogRecord {
let text : String
init(api : String, object : Any, name : String, id : Int) {

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

func $builtin_log<T>(_ object : T, _ name : String) -> AnyObject? {
return LogRecord(api:"$builtin_log", object:object, name:name)
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))
}

func $builtin_log_with_id<T>(_ object : T, _ name : String, _ id : Int) -> AnyObject? {
return LogRecord(api:"$builtin_log", object:object, name:name, id:id)
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))
}

func $builtin_log_scope_entry() -> AnyObject? {
return LogRecord(api:"$builtin_log_scope_entry")
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))
}

func $builtin_log_scope_exit() -> AnyObject? {
return LogRecord(api:"$builtin_log_scope_exit")
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))
}

func $builtin_postPrint() -> AnyObject? {
return LogRecord(api:"$builtin_postPrint")
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))
}

func $builtin_send_data(_ object:AnyObject?, _ sl: Int, _ el: Int, _ sc: Int, _ ec: Int) {
let loc = "[\(sl):\(sc)-\(el):\(ec)]"
print(loc + " " + (object as! LogRecord).text)
func $builtin_send_data(object:AnyObject?) {
print((object as! LogRecord).text)
}


0 comments on commit a0fa099

Please sign in to comment.