mirror of
https://github.com/apple/swift.git
synced 2026-03-04 18:24:35 +01:00
* Add experimental feature `PlaygroundExtendedCallbacks` which passes more information in `-playground` callbacks Adds the experimental feature `PlaygroundExtendedCallbacks` which (when `-playground` is also passed) causes the playground transform to use alternate forms of the result-value, scope-entry, and scope-exit callbacks that include the module name and file path of the source file. The previous callbacks included integers for the module number and file number, but this was cumbersome to use because it required the caller to create source symbols with magical names formed from the module name and file path that the playground transform knew how to look up. The extended callbacks in the experimental feature instead pass these strings as string literals. This is an experimental feature because of the need to measure the performance impact, and because of the need to provide an option to control which set of callbacks to use so that existing clients of the playground transform can opt into it when ready. It's also likely that we'll want to pass more information in the extended callbacks, such as an indication of the kind of result is being logged (e.g. a loop iteration variable vs a return statement vs a variable assignment). So this should be considered the first of a series of experimental improvements that will then be pitched as an actual, non-experimental v2.0 of the playground transform callback API. Because of the nature of how the playground transform is used, it's much easier to iterate on the functionality in the form of an experimental feature rather than using only desktop debug builds of the Swift compiler. Changes: - define a new experimental feature called `PlaygroundExtendedCallbacks` - modify the playground transform step in sema to pass the module name and file name literals when the experimental feature is set - add a unit test for the extended callbacks There is no change in behaviour when `PlaygroundExtendedCallbacks` is not enabled. rdar://109911742 Co-authored-by: Brent Shank <bshank@apple.com>
100 lines
5.2 KiB
Swift
100 lines
5.2 KiB
Swift
// If you're modifying this file to add or modify function signatures, you
|
|
// 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)]"
|
|
}
|
|
}
|
|
|
|
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, moduleName : String, filePath : String) {
|
|
var object_description : String = ""
|
|
print(object, terminator: "", to: &object_description)
|
|
text = range.text + " " + api + "[" + name + "='" + object_description + "'" + " module: " + moduleName + ". file: " + filePath + "]"
|
|
}
|
|
init(api : String, object : Any, name : String, id : Int, range : SourceRange, moduleFileId : ModuleFileIdentifier) {
|
|
var object_description : String = ""
|
|
print(object, terminator: "", to: &object_description)
|
|
text = moduleFileId.text + " " + range.text + " " + api + "[" + name + "='" + object_description + "']"
|
|
}
|
|
init(api : String, object : Any, name : String, range : SourceRange, moduleFileId : ModuleFileIdentifier) {
|
|
var object_description : String = ""
|
|
print(object, terminator: "", to: &object_description)
|
|
text = moduleFileId.text + " " + range.text + " " + api + "[" + name + "='" + object_description + "']"
|
|
}
|
|
init(api : String, object: Any, range : SourceRange, moduleFileId : ModuleFileIdentifier) {
|
|
var object_description : String = ""
|
|
print(object, terminator: "", to: &object_description)
|
|
text = moduleFileId.text + " " + range.text + " " + api + "['" + object_description + "']"
|
|
}
|
|
init(api : String, range : SourceRange, moduleName : String, filePath: String) {
|
|
text = range.text + " " + api + " " + " module: " + moduleName + ". file: " + filePath
|
|
}
|
|
init(api: String, range : SourceRange, moduleFileId : ModuleFileIdentifier) {
|
|
text = moduleFileId.text + " " + range.text + " " + api
|
|
}
|
|
}
|
|
|
|
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)
|
|
}
|
|
|
|
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)
|
|
}
|
|
|
|
public func __builtin_log_with_id_extended<T>(_ object: T, _ name: String, _ id: Int, _ sl: Int, _ el: Int, _ sc: Int, _ ec: Int, _ moduleName: String, _ filePath: String) -> AnyObject? {
|
|
return LogRecord(api:"__builtin_log_with_id_extended", object:object, name:name, id:id, range:SourceRange(sl:sl, el:el, sc:sc, ec:ec), moduleName:moduleName, filePath:filePath)
|
|
}
|
|
|
|
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)
|
|
}
|
|
|
|
public func __builtin_log_scope_entry_extended(_ sl : Int, _ el : Int, _ sc : Int, _ ec: Int, _ moduleName: String, _ filePath: String) -> AnyObject? {
|
|
return LogRecord(api:"__builtin_log_scope_entry_extended", range:SourceRange(sl:sl, el:el, sc:sc, ec:ec), moduleName:moduleName, filePath:filePath)
|
|
}
|
|
|
|
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)
|
|
}
|
|
|
|
public func __builtin_log_scope_exit_extended(_ sl : Int, _ el : Int, _ sc : Int, _ ec: Int, _ moduleName: String, _ filePath: String) -> AnyObject? {
|
|
return LogRecord(api:"__builtin_log_scope_exit_extended", range:SourceRange(sl:sl, el:el, sc:sc, ec:ec), moduleName:moduleName, filePath:filePath)
|
|
}
|
|
|
|
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)
|
|
}
|
|
|
|
public func __builtin_postPrint_extended(_ sl : Int, _ el : Int, _ sc : Int, _ ec: Int, _ moduleName: String, _ filePath: String) -> AnyObject? {
|
|
return LogRecord(api:"__builtin_postPrint_extended", range:SourceRange(sl:sl, el:el, sc:sc, ec:ec), moduleName:moduleName, filePath:filePath)
|
|
}
|
|
|
|
public func __builtin_send_data(_ object:AnyObject?) {
|
|
print((object as! LogRecord).text)
|
|
}
|
|
|
|
|