mirror of
https://github.com/apple/swift-log
synced 2026-11-22 03:40:39 +00:00
Support raw-identifier module names in Logger.currentModule. (#535)
When a module name (via `-module-alias`) is a raw identifier (SE-0451), `#fileID` formats the module component enclosed in backticks (for example, `` `/a/b/c`/File.swift ``). If the raw identifier contains '/', splitting on the first '/' in `#fileID` truncates the module name at the slash inside the backticks. When `#fileID` starts with a backtick, advance past the matching closing backtick before searching for the '/' separator that ends the module name, preserving the enclosing backticks.
This commit is contained in:
1 parent
e3da040787
commit
34b131f794
2 files changed
+26
-1
No files matched your search
@@ -1395,7 +1395,18 @@ extension Logger {
|
||||
@inlinable
|
||||
internal static func currentModule(fileID: String = #fileID) -> String {
|
||||
let utf8All = fileID.utf8
|
||||
if let slashIndex = utf8All.firstIndex(of: UInt8(ascii: "/")) {
|
||||
let searchStart: String.UTF8View.Index
|
||||
if utf8All.first == UInt8(ascii: "`") {
|
||||
guard let closingBacktickIndex = utf8All.dropFirst().firstIndex(of: UInt8(ascii: "`")) else {
|
||||
// It should be impossible for a well-formed module to get here, so treat it as an "n/a" case.
|
||||
return "n/a"
|
||||
}
|
||||
// Backticks are part of raw-identifier module names as written in source and mangled, so preserve them.
|
||||
searchStart = utf8All.index(after: closingBacktickIndex)
|
||||
} else {
|
||||
searchStart = utf8All.startIndex
|
||||
}
|
||||
if let slashIndex = utf8All[searchStart...].firstIndex(of: UInt8(ascii: "/")) {
|
||||
return String(fileID[..<slashIndex])
|
||||
} else {
|
||||
return "n/a"
|
||||
|
||||
@@ -1157,6 +1157,20 @@ struct LoggingTest {
|
||||
#expect(logger1.handlerCopies == 0)
|
||||
#expect(logger1[metadataKey: "key0"] == "0")
|
||||
}
|
||||
|
||||
@Test func currentModuleFileIDParsing() {
|
||||
#expect(Logger.currentModule(fileID: "MyModule/MyFile.swift") == "MyModule")
|
||||
#expect(Logger.currentModule(fileID: "MyModule/Subdir/MyFile.swift") == "MyModule")
|
||||
#expect(Logger.currentModule(fileID: "`My.Module`/MyFile.swift") == "`My.Module`")
|
||||
#expect(Logger.currentModule(fileID: "`My/Module`/MyFile.swift") == "`My/Module`")
|
||||
#expect(Logger.currentModule(fileID: "`/a/b/c`/MyFile.swift") == "`/a/b/c`")
|
||||
#expect(Logger.currentModule(fileID: "`/a/b/c`/Subdir/MyFile.swift") == "`/a/b/c`")
|
||||
#expect(Logger.currentModule(fileID: "`Module With Spaces`/MyFile.swift") == "`Module With Spaces`")
|
||||
#expect(Logger.currentModule(fileID: "`Unclosed/MyFile.swift") == "n/a")
|
||||
#expect(Logger.currentModule(fileID: "`NoSlash`") == "n/a")
|
||||
#expect(Logger.currentModule(fileID: "NoSlash.swift") == "n/a")
|
||||
#expect(Logger.currentModule(fileID: "") == "n/a")
|
||||
}
|
||||
}
|
||||
|
||||
extension Logger {
|
||||
|
||||
Reference in new issue
Block a user