@@ -115,30 +115,34 @@ extension CompilerPlugin {
115
115
/// Main entry point of the plugin — sets up a communication channel with
116
116
/// the plugin host and runs the main message loop.
117
117
public static func main( ) throws {
118
+ let _stdin = _ss_stdin ( )
119
+ let _stdout = _ss_stdout ( )
120
+ let _stderr = _ss_stderr ( )
121
+
118
122
// Duplicate the `stdin` file descriptor, which we will then use for
119
123
// receiving messages from the plugin host.
120
- let inputFD = dup ( fileno ( _ss_stdin ( ) ) )
124
+ let inputFD = dup ( fileno ( _stdin ) )
121
125
guard inputFD >= 0 else {
122
126
internalError ( " Could not duplicate `stdin`: \( describe ( errno: _ss_errno ( ) ) ) . " )
123
127
}
124
128
125
129
// Having duplicated the original standard-input descriptor, we close
126
130
// `stdin` so that attempts by the plugin to read console input (which
127
131
// are usually a mistake) return errors instead of blocking.
128
- guard close ( fileno ( _ss_stdin ( ) ) ) >= 0 else {
132
+ guard close ( fileno ( _stdin ) ) >= 0 else {
129
133
internalError ( " Could not close `stdin`: \( describe ( errno: _ss_errno ( ) ) ) . " )
130
134
}
131
135
132
136
// Duplicate the `stdout` file descriptor, which we will then use for
133
137
// sending messages to the plugin host.
134
- let outputFD = dup ( fileno ( _ss_stdout ( ) ) )
138
+ let outputFD = dup ( fileno ( _stdout ) )
135
139
guard outputFD >= 0 else {
136
140
internalError ( " Could not dup `stdout`: \( describe ( errno: _ss_errno ( ) ) ) . " )
137
141
}
138
142
139
143
// Having duplicated the original standard-output descriptor, redirect
140
144
// `stdout` to `stderr` so that all free-form text output goes there.
141
- guard dup2 ( fileno ( _ss_stderr ( ) ) , fileno ( _ss_stdout ( ) ) ) >= 0 else {
145
+ guard dup2 ( fileno ( _stderr ) , fileno ( _stdout ) ) >= 0 else {
142
146
internalError ( " Could not dup2 `stdout` to `stderr`: \( describe ( errno: _ss_errno ( ) ) ) . " )
143
147
}
144
148
@@ -148,9 +152,9 @@ extension CompilerPlugin {
148
152
// buffer. As a result, on Windows, we completely disable all
149
153
// buffering, which means that partial writes are possible.
150
154
#if os(Windows)
151
- setvbuf ( _ss_stdout ( ) , nil , _IONBF, 0 )
155
+ setvbuf ( _stdout , nil , _IONBF, 0 )
152
156
#else
153
- setvbuf ( _ss_stdout ( ) , nil , _IOLBF, 0 )
157
+ setvbuf ( _stdout , nil , _IOLBF, 0 )
154
158
#endif
155
159
156
160
// Open a message channel for communicating with the plugin host.
@@ -242,7 +246,7 @@ private func describe(errno: CInt) -> String {
242
246
private func _write( _ stream: _ss_ptr_FILE , contentsOf buffer: UnsafeRawBufferPointer ) throws {
243
247
let result = fwrite ( buffer. baseAddress, 1 , buffer. count, stream)
244
248
if result < buffer. count {
245
- throw CompilerPluginError ( message: " write (3) failed: \( describe ( errno: _ss_errno ( ) ) ) " )
249
+ throw CompilerPluginError ( message: " fwrite (3) failed: \( describe ( errno: _ss_errno ( ) ) ) " )
246
250
}
247
251
}
248
252
@@ -259,7 +263,7 @@ private func _reading<T>(_ stream: _ss_ptr_FILE, count: Int, _ fn: (UnsafeRawBuf
259
263
// Input is closed.
260
264
return try fn ( UnsafeRawBufferPointer ( start: nil , count: 0 ) )
261
265
} else {
262
- throw CompilerPluginError ( message: " read(2 ) failed: \( describe ( errno: _ss_errno ( ) ) ) " )
266
+ throw CompilerPluginError ( message: " fread(3 ) failed: \( describe ( errno: _ss_errno ( ) ) ) " )
263
267
}
264
268
}
265
269
return try fn ( UnsafeRawBufferPointer ( buffer) )
0 commit comments