@@ -23,7 +23,7 @@ struct SectionBounds: Sendable {
23
23
24
24
/// An enumeration describing the different sections discoverable by the
25
25
/// testing library.
26
- enum Kind : Int , Equatable , Hashable , CaseIterable {
26
+ enum Kind : Equatable , Hashable , CaseIterable {
27
27
/// The test content metadata section.
28
28
case testContent
29
29
@@ -146,7 +146,7 @@ private let _startCollectingSectionBounds: Void = {
146
146
///
147
147
/// - Returns: An array of structures describing the bounds of all known test
148
148
/// content sections in the current process.
149
- private func _sectionBounds( _ kind: SectionBounds . Kind ) -> [ SectionBounds ] {
149
+ private func _sectionBounds( _ kind: SectionBounds . Kind ) -> some Sequence < SectionBounds > {
150
150
_startCollectingSectionBounds
151
151
return _sectionBounds. withUnsafeMutablePointers { sectionBounds, lock in
152
152
pthread_mutex_lock ( lock)
@@ -169,7 +169,7 @@ private import SwiftShims // For MetadataSections
169
169
///
170
170
/// - Returns: An array of structures describing the bounds of all known test
171
171
/// content sections in the current process.
172
- private func _sectionBounds( _ kind: SectionBounds . Kind ) -> [ SectionBounds ] {
172
+ private func _sectionBounds( _ kind: SectionBounds . Kind ) -> some Sequence < SectionBounds > {
173
173
struct Context {
174
174
var kind : SectionBounds . Kind
175
175
var result = [ SectionBounds] ( )
@@ -297,14 +297,63 @@ private func _sectionBounds(_ kind: SectionBounds.Kind) -> some Sequence<Section
297
297
/// - kind: Ignored.
298
298
///
299
299
/// - Returns: The empty array.
300
- private func _sectionBounds( _ kind: SectionBounds . Kind ) -> [ SectionBounds ] {
300
+ private func _sectionBounds( _ kind: SectionBounds . Kind ) -> some Sequence < SectionBounds > {
301
301
#warning("Platform-specific implementation missing: Runtime test discovery unavailable (dynamic)")
302
- return [ ]
302
+ return EmptyCollection ( )
303
303
}
304
304
#endif
305
305
#else
306
306
// MARK: - Statically-linked implementation
307
307
308
+ /// The bounds of the statically-linked sections of interest to the testing
309
+ /// library.
310
+ ///
311
+ /// We use `@_silgen_name(raw:)` to reference these linker-defined symbols.
312
+ /// Then, to get their addresses, we must use `&` (`withPointer(to:)` is not
313
+ /// guaranteed to avoid a copy in this context.) Hence, they must also be
314
+ /// declared `var` rather than `let`, but they should be treated as read-only.
315
+ #if SWT_TARGET_OS_APPLE
316
+ @_silgen_name ( raw: " section$start$__DATA_CONST$__swift5_tests " ) private nonisolated ( unsafe) var _testContentSectionBegin : CChar
317
+ @_silgen_name ( raw: " section$end$__DATA_CONST$__swift5_tests " ) private nonisolated ( unsafe) var _testContentSectionEnd: CChar
318
+ #if !SWT_NO_LEGACY_TEST_DISCOVERY
319
+ @_silgen_name ( raw: " section$start$__TEXT$__swift5_types " ) private nonisolated ( unsafe) var _typeMetadataSectionBegin : CChar
320
+ @_silgen_name ( raw: " section$end$__TEXT$__swift5_types " ) private nonisolated ( unsafe) var _typeMetadataSectionEnd: CChar
321
+ #endif
322
+ #elseif os(WASI)
323
+ @_silgen_name ( raw: " __start_swift5_tests " ) private nonisolated ( unsafe) var _testContentSectionBegin : CChar
324
+ @_silgen_name ( raw: " __stop_swift5_tests " ) private nonisolated ( unsafe) var _testContentSectionEnd: CChar
325
+ #if !SWT_NO_LEGACY_TEST_DISCOVERY
326
+ @_silgen_name ( raw: " __start_swift5_type_metadata " ) private nonisolated ( unsafe) var _typeMetadataSectionBegin : CChar
327
+ @_silgen_name ( raw: " __stop_swift5_type_metadata " ) private nonisolated ( unsafe) var _typeMetadataSectionEnd: CChar
328
+ #endif
329
+ #else
330
+ #warning("Platform-specific implementation missing: Runtime test discovery unavailable (static)")
331
+ private nonisolated ( unsafe) var _testContentSectionBegin : Void
332
+ private nonisolated ( unsafe) var _testContentSectionEnd: Void
333
+ #if !SWT_NO_LEGACY_TEST_DISCOVERY
334
+ private nonisolated ( unsafe) var _typeMetadataSectionBegin : Void
335
+ private nonisolated ( unsafe) var _typeMetadataSectionEnd: Void
336
+ #endif
337
+
338
+ extension UnsafeRawBufferPointer {
339
+ /// Construct an empty buffer.
340
+ fileprivate init ( _: Void = ( ) , sectionBegin _: UnsafeRawPointer , sectionEnd _: UnsafeRawPointer ) {
341
+ self . init ( start: nil , count: 0 )
342
+ }
343
+ }
344
+ #endif
345
+
346
+ extension UnsafeRawBufferPointer {
347
+ /// Construct a buffer representing a section's byte bounds.
348
+ ///
349
+ /// - Parameters:
350
+ /// - sectionBegin: The address of the first byte of the section.
351
+ /// - sectionEnd: The address of the first byte _after_ the section.
352
+ @_disfavoredOverload fileprivate init ( sectionBegin: UnsafeRawPointer , sectionEnd: UnsafeRawPointer ) {
353
+ self . init ( start: sectionBegin, count: sectionEnd - sectionBegin)
354
+ }
355
+ }
356
+
308
357
/// The common implementation of ``SectionBounds/all(_:)`` for platforms that do
309
358
/// not support dynamic linking.
310
359
///
@@ -313,10 +362,15 @@ private func _sectionBounds(_ kind: SectionBounds.Kind) -> [SectionBounds] {
313
362
///
314
363
/// - Returns: A structure describing the bounds of the type metadata section
315
364
/// contained in the same image as the testing library itself.
316
- private func _sectionBounds( _ kind: SectionBounds . Kind ) -> CollectionOfOne < SectionBounds > {
317
- var ( baseAddress, count) : ( UnsafeRawPointer ? , Int ) = ( nil , 0 )
318
- swt_getStaticallyLinkedSectionBounds ( kind. rawValue, & baseAddress, & count)
319
- let buffer = UnsafeRawBufferPointer ( start: baseAddress, count: count)
365
+ private func _sectionBounds( _ kind: SectionBounds . Kind ) -> some Sequence < SectionBounds > {
366
+ let buffer = switch kind {
367
+ case . testContent:
368
+ UnsafeRawBufferPointer ( sectionBegin: & _testContentSectionBegin, sectionEnd: & _testContentSectionEnd)
369
+ #if !SWT_NO_LEGACY_TEST_DISCOVERY
370
+ case . typeMetadata:
371
+ UnsafeRawBufferPointer ( sectionBegin: & _typeMetadataSectionBegin, sectionEnd: & _typeMetadataSectionEnd)
372
+ #endif
373
+ }
320
374
let sb = SectionBounds ( imageAddress: nil , buffer: buffer)
321
375
return CollectionOfOne ( sb)
322
376
}
0 commit comments