Skip to content

Commit 27a1c07

Browse files
committed
Merge 'AtomicBool' into _SwiftSyntaxCShims
1 parent 462e954 commit 27a1c07

File tree

8 files changed

+48
-58
lines changed

8 files changed

+48
-58
lines changed

Package.swift

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,6 @@ let package = Package(
3434
name: "_SwiftSyntaxCShims"
3535
),
3636

37-
.target(
38-
name: "_AtomicBool"
39-
),
40-
4137
.target(
4238
name: "_InstructionCounter"
4339
),
@@ -131,7 +127,7 @@ let package = Package(
131127

132128
.target(
133129
name: "SwiftSyntax",
134-
dependencies: ["_AtomicBool", "SwiftSyntax509", "SwiftSyntax510", "SwiftSyntax600"],
130+
dependencies: ["_SwiftSyntaxCShims", "SwiftSyntax509", "SwiftSyntax510", "SwiftSyntax600"],
135131
exclude: ["CMakeLists.txt"],
136132
swiftSettings: swiftSyntaxSwiftSettings
137133
),

Sources/SwiftCompilerPlugin/CompilerPlugin.swift

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -131,35 +131,31 @@ extension CompilerPlugin {
131131
/// Main entry point of the plugin — sets up a communication channel with
132132
/// the plugin host and runs the main message loop.
133133
public static func main() throws {
134-
let stdin = _ss_stdin()
135-
let stdout = _ss_stdout()
136-
let stderr = _ss_stderr()
137-
138134
// Duplicate the `stdin` file descriptor, which we will then use for
139135
// receiving messages from the plugin host.
140-
let inputFD = dup(fileno(stdin))
136+
let inputFD = dup(fileno(_stdin))
141137
guard inputFD >= 0 else {
142-
internalError("Could not duplicate `stdin`: \(describe(errno: _ss_errno())).")
138+
internalError("Could not duplicate `stdin`: \(describe(errno: _errno)).")
143139
}
144140

145141
// Having duplicated the original standard-input descriptor, we close
146142
// `stdin` so that attempts by the plugin to read console input (which
147143
// are usually a mistake) return errors instead of blocking.
148-
guard close(fileno(stdin)) >= 0 else {
149-
internalError("Could not close `stdin`: \(describe(errno: _ss_errno())).")
144+
guard close(fileno(_stdin)) >= 0 else {
145+
internalError("Could not close `stdin`: \(describe(errno: _errno)).")
150146
}
151147

152148
// Duplicate the `stdout` file descriptor, which we will then use for
153149
// sending messages to the plugin host.
154-
let outputFD = dup(fileno(stdout))
150+
let outputFD = dup(fileno(_stdout))
155151
guard outputFD >= 0 else {
156-
internalError("Could not dup `stdout`: \(describe(errno: _ss_errno())).")
152+
internalError("Could not dup `stdout`: \(describe(errno: _errno)).")
157153
}
158154

159155
// Having duplicated the original standard-output descriptor, redirect
160156
// `stdout` to `stderr` so that all free-form text output goes there.
161-
guard dup2(fileno(stderr), fileno(stdout)) >= 0 else {
162-
internalError("Could not dup2 `stdout` to `stderr`: \(describe(errno: _ss_errno())).")
157+
guard dup2(fileno(_stderr), fileno(_stdout)) >= 0 else {
158+
internalError("Could not dup2 `stdout` to `stderr`: \(describe(errno: _errno)).")
163159
}
164160

165161
#if canImport(ucrt)
@@ -189,7 +185,7 @@ extension CompilerPlugin {
189185

190186
// Private function to report internal errors and then exit.
191187
fileprivate static func internalError(_ message: String) -> Never {
192-
fputs("Internal Error: \(message)\n", _ss_stderr())
188+
fputs("Internal Error: \(message)\n", _stderr)
193189
exit(1)
194190
}
195191
}
@@ -240,7 +236,7 @@ private func _write(_ fd: CInt, contentsOf buffer: UnsafeRawBufferPointer) throw
240236
let endPtr = ptr.advanced(by: buffer.count)
241237
while ptr != endPtr {
242238
switch write(fd, ptr, numericCast(endPtr - ptr)) {
243-
case -1: throw IOError.writeFailed(errno: _ss_errno())
239+
case -1: throw IOError.writeFailed(errno: _errno)
244240
case 0: throw IOError.writeFailed(errno: 0) /* unreachable */
245241
case let n: ptr += Int(n)
246242
}
@@ -255,7 +251,7 @@ private func _read(_ fd: CInt, into buffer: UnsafeMutableRawBufferPointer) throw
255251
let endPtr = ptr.advanced(by: buffer.count)
256252
while ptr != endPtr {
257253
switch read(fd, ptr, numericCast(endPtr - ptr)) {
258-
case -1: throw IOError.readFailed(errno: _ss_errno())
254+
case -1: throw IOError.readFailed(errno: _errno)
259255
case 0: throw IOError.readReachedEndOfInput
260256
case let n: ptr += Int(n)
261257
}

Sources/SwiftSyntax/SyntaxArena.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ fileprivate struct AtomicBool {
2424
}
2525
}
2626
#else
27-
import _AtomicBool
27+
import _SwiftSyntaxCShims
2828
#endif
2929

3030
/// A syntax arena owns the memory for all syntax nodes within it.

Sources/_AtomicBool/include/SwiftSyntaxAtomicBool.h

Lines changed: 0 additions & 26 deletions
This file was deleted.

Sources/_AtomicBool/src/SwiftSyntaxAtomicBool.c renamed to Sources/_SwiftSyntaxCShims/include/_AtomicBool.h

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,26 +2,33 @@
22
//
33
// This source file is part of the Swift.org open source project
44
//
5-
// Copyright (c) 2014 - 2023 Apple Inc. and the Swift project authors
5+
// Copyright (c) 2014 - 2024 Apple Inc. and the Swift project authors
66
// Licensed under Apache License v2.0 with Runtime Library Exception
77
//
88
// See https://swift.org/LICENSE.txt for license information
99
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
1010
//
1111
//===----------------------------------------------------------------------===//
1212

13-
#include "SwiftSyntaxAtomicBool.h"
13+
#include <stdbool.h>
1414

15+
typedef struct {
16+
_Atomic(bool) value;
17+
} AtomicBool;
18+
19+
__attribute__((swift_name("AtomicBool.init(initialValue:)")))
1520
AtomicBool atomic_bool_create(bool initialValue) {
1621
AtomicBool atomic;
1722
atomic.value = initialValue;
1823
return atomic;
1924
}
2025

26+
__attribute__((swift_name("getter:AtomicBool.value(self:)")))
2127
bool atomic_bool_get(AtomicBool *atomic) {
2228
return atomic->value;
2329
}
2430

31+
__attribute__((swift_name("setter:AtomicBool.value(self:_:)")))
2532
void atomic_bool_set(AtomicBool *atomic, bool newValue) {
2633
atomic->value = newValue;
2734
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2024 Apple Inc. and the Swift project authors
6+
// Licensed under Apache License v2.0 with Runtime Library Exception
7+
//
8+
// See https://swift.org/LICENSE.txt for license information
9+
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
10+
//
11+
//===----------------------------------------------------------------------===//
12+
13+
#include <errno.h>
14+
15+
__attribute__((swift_name("getter:_errno()")))
16+
static int swiftsyntax_errno(void) {
17+
return errno;
18+
}

Sources/_SwiftSyntaxCShims/include/_stdio.h

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,23 +10,20 @@
1010
//
1111
//===----------------------------------------------------------------------===//
1212

13-
#include <errno.h>
1413
#include <stdio.h>
1514

16-
typedef FILE *_ss_ptr_FILE;
17-
18-
static _ss_ptr_FILE _ss_stdout(void) {
15+
__attribute__((swift_name("getter:_stdout()")))
16+
static FILE *swiftsyntax_stdout(void) {
1917
return stdout;
2018
}
2119

22-
static _ss_ptr_FILE _ss_stdin(void) {
20+
__attribute__((swift_name("getter:_stdin()")))
21+
static FILE *swiftsyntax_stdin(void) {
2322
return stdin;
2423
}
2524

26-
static _ss_ptr_FILE _ss_stderr(void) {
25+
__attribute__((swift_name("getter:_stderr()")))
26+
static FILE *swiftsyntax_stderr(void) {
2727
return stderr;
2828
}
2929

30-
static int _ss_errno(void) {
31-
return errno;
32-
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
module _SwiftSyntaxCShims {
2+
header "_AtomicBool.h"
3+
header "_errno.h"
24
header "_stdio.h"
35
export *
46
}

0 commit comments

Comments
 (0)