Skip to content

Commit 6dc8d43

Browse files
committed
Merge 'AtomicBool' into _SwiftSyntaxCShims
Headers are now in 'swiftsyntax' subdirectory to avoid file name conflicts with libc. e.g. '#include <_stdio.h>' referenced from C++ compiler code which might pick up '_SwiftSyntaxCShims/include/_stdio.h'
1 parent 14ff9b3 commit 6dc8d43

File tree

8 files changed

+73
-69
lines changed

8 files changed

+73
-69
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
@@ -136,35 +136,31 @@ extension CompilerPlugin {
136136
/// Main entry point of the plugin — sets up a communication channel with
137137
/// the plugin host and runs the main message loop.
138138
public static func main() throws {
139-
let stdin = _ss_stdin()
140-
let stdout = _ss_stdout()
141-
let stderr = _ss_stderr()
142-
143139
// Duplicate the `stdin` file descriptor, which we will then use for
144140
// receiving messages from the plugin host.
145-
let inputFD = dup(fileno(stdin))
141+
let inputFD = dup(fileno(_stdin))
146142
guard inputFD >= 0 else {
147-
internalError("Could not duplicate `stdin`: \(describe(errno: _ss_errno())).")
143+
internalError("Could not duplicate `stdin`: \(describe(errno: _errno)).")
148144
}
149145

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

157153
// Duplicate the `stdout` file descriptor, which we will then use for
158154
// sending messages to the plugin host.
159-
let outputFD = dup(fileno(stdout))
155+
let outputFD = dup(fileno(_stdout))
160156
guard outputFD >= 0 else {
161-
internalError("Could not dup `stdout`: \(describe(errno: _ss_errno())).")
157+
internalError("Could not dup `stdout`: \(describe(errno: _errno)).")
162158
}
163159

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

170166
#if canImport(ucrt)
@@ -194,7 +190,7 @@ extension CompilerPlugin {
194190

195191
// Private function to report internal errors and then exit.
196192
fileprivate static func internalError(_ message: String) -> Never {
197-
fputs("Internal Error: \(message)\n", _ss_stderr())
193+
fputs("Internal Error: \(message)\n", _stderr)
198194
exit(1)
199195
}
200196
}
@@ -245,7 +241,7 @@ private func _write(_ fd: CInt, contentsOf buffer: UnsafeRawBufferPointer) throw
245241
let endPtr = ptr.advanced(by: buffer.count)
246242
while ptr != endPtr {
247243
switch write(fd, ptr, numericCast(endPtr - ptr)) {
248-
case -1: throw IOError.writeFailed(errno: _ss_errno())
244+
case -1: throw IOError.writeFailed(errno: _errno)
249245
case 0: throw IOError.writeFailed(errno: 0) /* unreachable */
250246
case let n: ptr += Int(n)
251247
}
@@ -260,7 +256,7 @@ private func _read(_ fd: CInt, into buffer: UnsafeMutableRawBufferPointer) throw
260256
let endPtr = ptr.advanced(by: buffer.count)
261257
while ptr != endPtr {
262258
switch read(fd, ptr, numericCast(endPtr - ptr)) {
263-
case -1: throw IOError.readFailed(errno: _ss_errno())
259+
case -1: throw IOError.readFailed(errno: _errno)
264260
case 0: throw IOError.readReachedEndOfInput
265261
case let n: ptr += Int(n)
266262
}

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/src/SwiftSyntaxAtomicBool.c

Lines changed: 0 additions & 27 deletions
This file was deleted.
Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
module _SwiftSyntaxCShims {
2-
header "_stdio.h"
2+
header "swiftsyntax/AtomicBool.h"
3+
header "swiftsyntax/errno.h"
4+
header "swiftsyntax/stdio.h"
35
export *
46
}

Sources/_AtomicBool/include/SwiftSyntaxAtomicBool.h renamed to Sources/_SwiftSyntaxCShims/include/swiftsyntax/AtomicBool.h

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,25 +2,38 @@
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+
#ifndef SWIFTSYNTAX_ATOMICBOOL_H
14+
#define SWIFTSYNTAX_ATOMICBOOL_H
15+
1316
#include <stdbool.h>
1417

1518
typedef struct {
1619
_Atomic(bool) value;
1720
} AtomicBool;
1821

1922
__attribute__((swift_name("AtomicBool.init(initialValue:)")))
20-
AtomicBool atomic_bool_create(bool initialValue);
23+
static inline AtomicBool atomic_bool_create(bool initialValue) {
24+
AtomicBool atomic;
25+
atomic.value = initialValue;
26+
return atomic;
27+
}
2128

2229
__attribute__((swift_name("getter:AtomicBool.value(self:)")))
23-
bool atomic_bool_get(AtomicBool *atomic);
30+
static inline bool atomic_bool_get(AtomicBool *atomic) {
31+
return atomic->value;
32+
}
2433

2534
__attribute__((swift_name("setter:AtomicBool.value(self:_:)")))
26-
void atomic_bool_set(AtomicBool *atomic, bool newValue);
35+
static inline void atomic_bool_set(AtomicBool *atomic, bool newValue) {
36+
atomic->value = newValue;
37+
}
38+
39+
#endif // SWIFTSYNTAX_ATOMICBOOL_H

Sources/_SwiftSyntaxCShims/include/_stdio.h renamed to Sources/_SwiftSyntaxCShims/include/swiftsyntax/errno.h

Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -10,23 +10,14 @@
1010
//
1111
//===----------------------------------------------------------------------===//
1212

13-
#include <errno.h>
14-
#include <stdio.h>
15-
16-
typedef FILE *_ss_ptr_FILE;
13+
#ifndef SWIFTSYNTAX_ERRNO_H
14+
#define SWIFTSYNTAX_ERRNO_H
1715

18-
static _ss_ptr_FILE _ss_stdout(void) {
19-
return stdout;
20-
}
21-
22-
static _ss_ptr_FILE _ss_stdin(void) {
23-
return stdin;
24-
}
25-
26-
static _ss_ptr_FILE _ss_stderr(void) {
27-
return stderr;
28-
}
16+
#include <errno.h>
2917

30-
static int _ss_errno(void) {
18+
__attribute__((swift_name("getter:_errno()")))
19+
static inline int swiftsyntax_errno(void) {
3120
return errno;
3221
}
22+
23+
#endif // SWIFTSYNTAX_ERRNO_H
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
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+
#ifndef SWIFTSYNTAX_STDIO_H
14+
#define SWIFTSYNTAX_STDIO_H
15+
16+
#include <stdio.h>
17+
18+
__attribute__((swift_name("getter:_stdout()")))
19+
static inline FILE *swiftsyntax_stdout(void) {
20+
return stdout;
21+
}
22+
23+
__attribute__((swift_name("getter:_stdin()")))
24+
static inline FILE *swiftsyntax_stdin(void) {
25+
return stdin;
26+
}
27+
28+
__attribute__((swift_name("getter:_stderr()")))
29+
static inline FILE *swiftsyntax_stderr(void) {
30+
return stderr;
31+
}
32+
33+
#endif // SWIFTSYNTAX_STDIO_H

0 commit comments

Comments
 (0)