Skip to content

Commit 591a16f

Browse files
committed
Try to be more cross-platform
1 parent a94587a commit 591a16f

File tree

1 file changed

+23
-20
lines changed

1 file changed

+23
-20
lines changed

Tests/LLVMTests/FileCheck.swift

Lines changed: 23 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ import Foundation
22

33
#if os(Linux)
44
import Glibc
5+
6+
typealias NSRegularExpression = RegularExpression
57
#else
68
import Darwin
79
#endif
@@ -92,33 +94,34 @@ private func overrideFDAndCollectOutput(file : FileCheckFD, of block : () -> ())
9294
fflush(stdout)
9395
let oldFd = dup(file.fileno)
9496

95-
let template = URL(fileURLWithPath: NSTemporaryDirectory()).appendingPathComponent("output.XXXXXX") as NSURL
96-
var buffer = [Int8](repeating: 0, count: Int(PATH_MAX))
97-
guard template.getFileSystemRepresentation(&buffer, maxLength: buffer.count) else {
98-
return ""
99-
}
97+
let template = URL(fileURLWithPath: NSTemporaryDirectory()).appendingPathComponent("output.XXXXXX")
98+
return template.withUnsafeFileSystemRepresentation { buffer in
99+
guard let buffer = buffer else {
100+
return ""
101+
}
100102

101-
let newFd = mkstemp(&buffer)
102-
guard newFd != -1 else {
103-
return ""
104-
}
103+
let newFd = mkstemp(UnsafeMutablePointer(mutating: buffer))
104+
guard newFd != -1 else {
105+
return ""
106+
}
105107

106-
dup2(newFd, file.fileno)
108+
dup2(newFd, file.fileno)
107109

108-
block()
110+
block()
109111

110-
close(newFd)
111-
fflush(file.filePtr)
112+
close(newFd)
113+
fflush(file.filePtr)
112114

113115

114-
dup2(oldFd, file.fileno)
115-
close(oldFd)
116+
dup2(oldFd, file.fileno)
117+
close(oldFd)
116118

117-
let url = URL(fileURLWithFileSystemRepresentation: buffer, isDirectory: false, relativeTo: nil)
118-
guard let s = try? String(contentsOf: url) else {
119-
return ""
120-
}
121-
return s
119+
let url = URL(fileURLWithFileSystemRepresentation: buffer, isDirectory: false, relativeTo: nil)
120+
guard let s = try? String(contentsOf: url) else {
121+
return ""
122+
}
123+
return s
124+
}
122125
}
123126

124127
func validateCheckPrefixes(_ prefixes : [String]) -> [String]? {

0 commit comments

Comments
 (0)