Skip to content

Commit 57335d7

Browse files
authored
Simplify statically-linked section discovery. (#940)
This PR simplifies the code used to discover metadata sections when the testing library is statically linked into a test target. ### Checklist: - [x] Code and documentation should follow the style of the [Style Guide](https://github.com/apple/swift-testing/blob/main/Documentation/StyleGuide.md). - [x] If public symbols are renamed or modified, DocC references should be updated.
1 parent 900bf8c commit 57335d7

File tree

3 files changed

+19
-24
lines changed

3 files changed

+19
-24
lines changed

Sources/Testing/Discovery+Platform.swift

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ struct SectionBounds: Sendable {
2323

2424
/// An enumeration describing the different sections discoverable by the
2525
/// testing library.
26-
enum Kind: Equatable, Hashable, CaseIterable {
26+
enum Kind: Int, Equatable, Hashable, CaseIterable {
2727
/// The test content metadata section.
2828
case testContent
2929

@@ -285,13 +285,9 @@ private func _sectionBounds(_ kind: SectionBounds.Kind) -> [SectionBounds] {
285285
/// - Returns: A structure describing the bounds of the type metadata section
286286
/// contained in the same image as the testing library itself.
287287
private func _sectionBounds(_ kind: SectionBounds.Kind) -> CollectionOfOne<SectionBounds> {
288-
let (sectionBegin, sectionEnd) = switch kind {
289-
case .testContent:
290-
SWTTestContentSectionBounds
291-
case .typeMetadata:
292-
SWTTypeMetadataSectionBounds
293-
}
294-
let buffer = UnsafeRawBufferPointer(start: sectionBegin, count: max(0, sectionEnd - sectionBegin))
288+
var (baseAddress, count): (UnsafeRawPointer?, Int) = (nil, 0)
289+
swt_getStaticallyLinkedSectionBounds(kind.rawValue, &baseAddress, &count)
290+
let buffer = UnsafeRawBufferPointer(start: baseAddress, count: count)
295291
let sb = SectionBounds(imageAddress: nil, buffer: buffer)
296292
return CollectionOfOne(sb)
297293
}

Sources/_TestingInternals/Discovery.cpp

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
#include "Discovery.h"
1212

13+
#include <algorithm>
1314
#include <cstdint>
1415
#include <cstring>
1516
#include <type_traits>
@@ -35,13 +36,16 @@ static const char typeMetadataSectionBegin = 0;
3536
static const char& typeMetadataSectionEnd = typeMetadataSectionBegin;
3637
#endif
3738

38-
const void *_Nonnull const SWTTestContentSectionBounds[2] = {
39-
&testContentSectionBegin, &testContentSectionEnd
39+
static constexpr const char *const staticallyLinkedSectionBounds[][2] = {
40+
{ &testContentSectionBegin, &testContentSectionEnd },
41+
{ &typeMetadataSectionBegin, &typeMetadataSectionEnd },
4042
};
4143

42-
const void *_Nonnull const SWTTypeMetadataSectionBounds[2] = {
43-
&typeMetadataSectionBegin, &typeMetadataSectionEnd
44-
};
44+
void swt_getStaticallyLinkedSectionBounds(size_t kind, const void **outSectionBegin, size_t *outByteCount) {
45+
auto [sectionBegin, sectionEnd] = staticallyLinkedSectionBounds[kind];
46+
*outSectionBegin = sectionBegin;
47+
*outByteCount = std::distance(sectionBegin, sectionEnd);
48+
}
4549
#endif
4650

4751
#pragma mark - Swift ABI

Sources/_TestingInternals/include/Discovery.h

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -32,23 +32,18 @@ SWT_IMPORT_FROM_STDLIB void swift_enumerateAllMetadataSections(
3232

3333
#pragma mark - Statically-linked section bounds
3434

35-
/// The bounds of the test content section statically linked into the image
36-
/// containing Swift Testing.
35+
/// Get the bounds of a statically linked section in this image.
3736
///
38-
/// - Note: This symbol is _declared_, but not _defined_, on platforms with
39-
/// dynamic linking because the `SWT_NO_DYNAMIC_LINKING` C++ macro (not the
40-
/// Swift compiler conditional of the same name) is not consistently declared
41-
/// when Swift files import the `_TestingInternals` C++ module.
42-
SWT_EXTERN const void *_Nonnull const SWTTestContentSectionBounds[2];
43-
44-
/// The bounds of the type metadata section statically linked into the image
45-
/// containing Swift Testing.
37+
/// - Parameters:
38+
/// - kind: The value of `SectionBounds.Kind.rawValue` for the given section.
39+
/// - outSectionBegin: On return, a pointer to the first byte of the section.
40+
/// - outByteCount: On return, the number of bytes in the section.
4641
///
4742
/// - Note: This symbol is _declared_, but not _defined_, on platforms with
4843
/// dynamic linking because the `SWT_NO_DYNAMIC_LINKING` C++ macro (not the
4944
/// Swift compiler conditional of the same name) is not consistently declared
5045
/// when Swift files import the `_TestingInternals` C++ module.
51-
SWT_EXTERN const void *_Nonnull const SWTTypeMetadataSectionBounds[2];
46+
SWT_EXTERN void swt_getStaticallyLinkedSectionBounds(size_t kind, const void *_Nullable *_Nonnull outSectionBegin, size_t *outByteCount);
5247

5348
#pragma mark - Legacy test discovery
5449

0 commit comments

Comments
 (0)