8000 Import _CSwiftSyntax using @_implementationOnly. · swiftlang/swift-syntax@4819c21 · GitHub
[go: up one dir, main page]

Skip to content

Commit 4819c21

Browse files
allevatoakyrtzi
authored andcommitted
Import _CSwiftSyntax using @_implementationOnly.
This lets us add an actual header for the C function it defines, so that Swift can import it directly (while still retaining the property that it is a standalone module) instead of relying on the dlsym trick that fails on Linux unless additional flags are passed to the linker for any binary that depends on SwiftSyntax (see https://bugs.swift.org/browse/SR-11293).
1 parent 1a584ee commit 4819c21

File tree

5 files changed

+15
-37
lines changed

5 files changed

+15
-37
lines changed

Sources/SwiftSyntax/AtomicCounter.swift

Lines changed: 2 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -10,36 +10,12 @@
1010
//
1111
//===----------------------------------------------------------------------===//
1212

13-
// This is using the C function from '_CSwiftSyntax/src/atomic-counter.c' by
14-
// dynamically loading it using `dlsym`. The reason this mechanism is used
15-
// instead of importing a header is so that we preserve the property that
16-
// SwiftSyntax is a self-contained Swift module.
17-
// (also see '_CSwiftSyntax/include/README.md')
18-
19-
#if os(Linux)
20-
import Glibc
21-
#else
22-
import Darwin.C
23-
#endif
24-
25-
#if os(macOS) || os(iOS) || os(watchOS) || os(tvOS)
26-
fileprivate let RTLD_DEFAULT = UnsafeMutableRawPointer(bitPattern: -2)
27-
#elseif os(Linux)
28-
fileprivate let RTLD_DEFAULT = UnsafeMutableRawPointer(bitPattern: 0)
29-
#endif
30-
31-
fileprivate typealias get_unique_counter_ty = (@convention(c) ()->UInt64)
32-
fileprivate let get_unique_counter_str = "cswiftsyntax_get_unique_counter"
13+
@_implementationOnly import _CSwiftSyntax
3314

3415
/// Provides API to get an atomically increasing counter value.
3516
struct AtomicCounter {
3617
/// Get an atomically increasing counter value.
3718
static func next() -> UInt64 {
38-
return AtomicCounter.cswiftsyntax_get_unique_counter()
19+
return cswiftsyntax_get_unique_counter()
3920
}
40-
41-
static fileprivate let cswiftsyntax_get_unique_counter: get_unique_counter_ty = { ()->get_unique_counter_ty in
42-
let ptr = dlsym(RTLD_DEFAULT, get_unique_counter_str)!
43-
return unsafeBitCast(ptr, to: get_unique_counter_ty.self)
44-
}()
4521
}

Sources/_CSwiftSyntax/README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
This module is an implementation detail of SwiftSyntax and does not export
2+
any public API. As such, any uses of it inside SwiftSyntax should import it
3+
with the `@_implementationOnly` attribute (available since Swift 5.1).
4+
5+
This preserves the property that SwiftSyntax is a self-contained Swift
6+
module, meaning that when distributing it, we do not also have to bundle
7+
these private headers or use any dynamic symbol loading tricks that require
8+
additional linker flags to be passed on some platforms (see
9+
[SR-11293](https://bugs.swift.org/browse/SR-11293), for example).

Sources/_CSwiftSyntax/include/README.md

Lines changed: 0 additions & 10 deletions
This file was deleted.
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#include <stdint.h>
2+
3+
uint64_t cswiftsyntax_get_unique_counter(void);

Sources/_CSwiftSyntax/src/atomic-counter.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#include <stdint.h>
1+
#include "atomic-counter.h"
22

33
uint64_t cswiftsyntax_get_unique_counter(void) {
44
static _Atomic uint64_t counter = 0;

0 commit comments

Comments
 (0)
0