Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Store test content in a custom metadata section. #880

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions Documentation/Porting.md
Original file line number Diff line number Diff line change
Expand Up @@ -145,8 +145,10 @@ to load that information:
+ let resourceName: Str255 = switch kind {
+ case .testContent:
+ "__swift5_tests"
+#if !SWT_NO_LEGACY_TEST_DISCOVERY
+ case .typeMetadata:
+ "__swift5_types"
+#endif
+ }
+
+ let oldRefNum = CurResFile()
Expand Down Expand Up @@ -219,15 +221,19 @@ diff --git a/Sources/_TestingInternals/Discovery.cpp b/Sources/_TestingInternals
+#elif defined(macintosh)
+extern "C" const char testContentSectionBegin __asm__("...");
+extern "C" const char testContentSectionEnd __asm__("...");
+#if !defined(SWT_NO_LEGACY_TEST_DISCOVERY)
+extern "C" const char typeMetadataSectionBegin __asm__("...");
+extern "C" const char typeMetadataSectionEnd __asm__("...");
+#endif
#else
#warning Platform-specific implementation missing: Runtime test discovery unavailable (static)
static const char testContentSectionBegin = 0;
static const char& testContentSectionEnd = testContentSectionBegin;
#if !defined(SWT_NO_LEGACY_TEST_DISCOVERY)
static const char typeMetadataSectionBegin = 0;
static const char& typeMetadataSectionEnd = testContentSectionBegin;
#endif
#endif
```

These symbols must have unique addresses corresponding to the first byte of the
Expand Down
10 changes: 6 additions & 4 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -89,10 +89,7 @@ let package = Package(
"_Testing_CoreGraphics",
"_Testing_Foundation",
],
swiftSettings: .packageSettings + [
// For testing test content section discovery only
.enableExperimentalFeature("SymbolLinkageMarkers"),
]
swiftSettings: .packageSettings
),

.macro(
Expand Down Expand Up @@ -205,6 +202,11 @@ extension Array where Element == PackageDescription.SwiftSetting {
.enableExperimentalFeature("AccessLevelOnImport"),
.enableUpcomingFeature("InternalImportsByDefault"),

// This setting is enabled in the package, but not in the toolchain build
// (via CMake). Enabling it is dependent on acceptance of the @section
// proposal via Swift Evolution.
.enableExperimentalFeature("SymbolLinkageMarkers"),

// When building as a package, the macro plugin always builds as an
// executable rather than a library.
.define("SWT_NO_LIBRARY_MACRO_PLUGINS"),
Expand Down
34 changes: 34 additions & 0 deletions Sources/Testing/ExitTests/ExitTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,40 @@ extension ExitTest: DiscoverableAsTestContent {
}

typealias TestContentAccessorHint = ID

/// Store the test generator function into the given memory.
///
/// - Parameters:
/// - outValue: The uninitialized memory to store the exit test into.
/// - id: The unique identifier of the exit test to store.
/// - body: The body closure of the exit test to store.
/// - typeAddress: A pointer to the expected type of the exit test as passed
/// to the test content record calling this function.
/// - hintAddress: A pointer to an instance of ``ID`` to use as a hint.
///
/// - Returns: Whether or not an exit test was stored into `outValue`.
///
/// - Warning: This function is used to implement the `#expect(exitsWith:)`
/// macro. Do not use it directly.
public static func __store(
_ id: (UInt64, UInt64),
_ body: @escaping @Sendable () async throws -> Void,
into outValue: UnsafeMutableRawPointer,
asTypeAt typeAddress: UnsafeRawPointer,
withHintAt hintAddress: UnsafeRawPointer? = nil
) -> CBool {
let callerExpectedType = TypeInfo(describing: typeAddress.load(as: Any.Type.self))
let selfType = TypeInfo(describing: Self.self)
guard callerExpectedType == selfType else {
return false
}
let id = ID(id)
if let hintedID = hintAddress?.load(as: ID.self), hintedID != id {
return false
}
outValue.initializeMemory(as: Self.self, to: Self(id: id, body: body))
return true
}
}

@_spi(Experimental) @_spi(ForToolsIntegrationOnly)
Expand Down
6 changes: 4 additions & 2 deletions Sources/Testing/Test+Discovery+Legacy.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,13 @@

private import _TestingInternals

#if !SWT_NO_LEGACY_TEST_DISCOVERY
/// A protocol describing a type that contains tests.
///
/// - Warning: This protocol is used to implement the `@Test` macro. Do not use
/// it directly.
@_alwaysEmitConformanceMetadata
public protocol __TestContainer {
public protocol __TestContainer: Sendable {
/// The set of tests contained by this type.
static var __tests: [Test] { get async }
}
Expand All @@ -31,7 +32,7 @@ let testContainerTypeNameMagic = "__🟠$test_container__"
/// macro. Do not use it directly.
@_alwaysEmitConformanceMetadata
@_spi(Experimental)
public protocol __ExitTestContainer {
public protocol __ExitTestContainer: Sendable {
/// The unique identifier of the exit test.
static var __id: (UInt64, UInt64) { get }

Expand All @@ -43,3 +44,4 @@ public protocol __ExitTestContainer {
/// `__ExitTestContainer` protocol.
let exitTestContainerTypeNameMagic = "__🟠$exit_test_body__"
#endif
#endif
30 changes: 30 additions & 0 deletions Sources/Testing/Test+Discovery.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,30 @@ extension Test {
var rawValue: @Sendable () async -> Test
}

/// Store the test generator function into the given memory.
///
/// - Parameters:
/// - generator: The generator function to store.
/// - outValue: The uninitialized memory to store `generator` into.
/// - typeAddress: A pointer to the expected type of `generator` as passed
/// to the test content record calling this function.
///
/// - Returns: Whether or not `generator` was stored into `outValue`.
///
/// - Warning: This function is used to implement the `@Test` macro. Do not
/// use it directly.
public static func __store(
_ generator: @escaping @Sendable () async -> Test,
into outValue: UnsafeMutableRawPointer,
asTypeAt typeAddress: UnsafeRawPointer
) -> CBool {
guard typeAddress.load(as: Any.Type.self) == Generator.self else {
return false
}
outValue.initializeMemory(as: Generator.self, to: .init(rawValue: generator))
return true
}

/// All available ``Test`` instances in the process, according to the runtime.
///
/// The order of values in this sequence is unspecified.
Expand All @@ -41,6 +65,7 @@ extension Test {
// the legacy and new mechanisms, but we can set an environment variable
// to explicitly select one or the other. When we remove legacy support,
// we can also remove this enumeration and environment variable check.
#if !SWT_NO_LEGACY_TEST_DISCOVERY
let (useNewMode, useLegacyMode) = switch Environment.flag(named: "SWT_USE_LEGACY_TEST_DISCOVERY") {
case .none:
(true, true)
Expand All @@ -49,6 +74,9 @@ extension Test {
case .some(false):
(true, false)
}
#else
let useNewMode = true
#endif

// Walk all test content and gather generator functions, then call them in
// a task group and collate their results.
Expand All @@ -62,6 +90,7 @@ extension Test {
}
}

#if !SWT_NO_LEGACY_TEST_DISCOVERY
// Perform legacy test discovery if needed.
if useLegacyMode && result.isEmpty {
let types = types(withNamesContaining: testContainerTypeNameMagic).lazy
Expand All @@ -75,6 +104,7 @@ extension Test {
result = await taskGroup.reduce(into: result) { $0.formUnion($1) }
}
}
#endif

return result
}
Expand Down
2 changes: 2 additions & 0 deletions Sources/TestingMacros/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ target_sources(TestingMacros PRIVATE
Support/Additions/DeclGroupSyntaxAdditions.swift
Support/Additions/EditorPlaceholderExprSyntaxAdditions.swift
Support/Additions/FunctionDeclSyntaxAdditions.swift
Support/Additions/IntegerLiteralExprSyntaxAdditions.swift
Support/Additions/MacroExpansionContextAdditions.swift
Support/Additions/TokenSyntaxAdditions.swift
Support/Additions/TriviaPieceAdditions.swift
Expand All @@ -103,6 +104,7 @@ target_sources(TestingMacros PRIVATE
Support/DiagnosticMessage+Diagnosing.swift
Support/SourceCodeCapturing.swift
Support/SourceLocationGeneration.swift
Support/TestContentGeneration.swift
TagMacro.swift
TestDeclarationMacro.swift
TestingMacrosMain.swift)
Expand Down
50 changes: 45 additions & 5 deletions Sources/TestingMacros/ConditionMacro.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@
public import SwiftSyntax
public import SwiftSyntaxMacros

#if !hasFeature(SymbolLinkageMarkers) && SWT_NO_LEGACY_TEST_DISCOVERY
#error("Platform-specific misconfiguration: either SymbolLinkageMarkers or legacy test discovery is required to expand #expect(exitsWith:)")
#endif

/// A protocol containing the common implementation for the expansions of the
/// `#expect()` and `#require()` macros.
///
Expand Down Expand Up @@ -450,28 +454,64 @@ extension ExitTestConditionMacro {
"""
)

#if hasFeature(SymbolLinkageMarkers)
// Create a local type that can be discovered at runtime and which contains
// the exit test body.
let enumName = context.makeUniqueName("__🟠$exit_test_body__")
let enumName = context.makeUniqueName("")
let testContentRecordDecl = makeTestContentRecordDecl(
named: .identifier("testContentRecord"),
in: TypeSyntax(IdentifierTypeSyntax(name: enumName)),
ofKind: .exitTest,
accessingWith: .identifier("accessor")
)
decls.append(
"""
#if hasFeature(SymbolLinkageMarkers)
@available(*, deprecated, message: "This type is an implementation detail of the testing library. Do not use it directly.")
enum \(enumName) {
private static let accessor: Testing.__TestContentRecordAccessor = { outValue, type, hint in
Testing.ExitTest.__store(
\(exitTestIDExpr),
\(bodyThunkName),
into: outValue,
asTypeAt: type,
withHintAt: hint
)
}

\(testContentRecordDecl)
}
#endif
"""
)
#endif

#if !SWT_NO_LEGACY_TEST_DISCOVERY
// Emit a legacy type declaration if SymbolLinkageMarkers is off.
let legacyEnumName = context.makeUniqueName("__🟠$exit_test_body__")
decls.append(
"""
@available(*, deprecated, message: "This type is an implementation detail of the testing library. Do not use it directly.")
enum \(enumName): Testing.__ExitTestContainer, Sendable {
enum \(legacyEnumName): Testing.__ExitTestContainer {
static var __id: (Swift.UInt64, Swift.UInt64) {
\(exitTestIDExpr)
}
static var __body: @Sendable () async throws -> Void {
static var __body: @Sendable () async throws -> Swift.Void {
\(bodyThunkName)
}
}
"""
)
#endif

arguments[trailingClosureIndex].expression = ExprSyntax(
ClosureExprSyntax {
for decl in decls {
CodeBlockItemSyntax(item: .decl(decl))
.with(\.trailingTrivia, .newline)
CodeBlockItemSyntax(
leadingTrivia: .newline,
item: .decl(decl),
trailingTrivia: .newline
)
}
}
)
Expand Down
58 changes: 52 additions & 6 deletions Sources/TestingMacros/SuiteDeclarationMacro.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@
public import SwiftSyntax
public import SwiftSyntaxMacros

#if !hasFeature(SymbolLinkageMarkers) && SWT_NO_LEGACY_TEST_DISCOVERY
#error("Platform-specific misconfiguration: either SymbolLinkageMarkers or legacy test discovery is required to expand @Suite")
#endif

/// A type describing the expansion of the `@Suite` attribute macro.
///
/// This type is used to implement the `@Suite` attribute macro. Do not use it
Expand Down Expand Up @@ -127,6 +131,50 @@ public struct SuiteDeclarationMacro: MemberMacro, PeerMacro, Sendable {
// Parse the @Suite attribute.
let attributeInfo = AttributeInfo(byParsing: suiteAttribute, on: declaration, in: context)

let generatorName = context.makeUniqueName("generator")
result.append(
"""
@available(*, deprecated, message: "This property is an implementation detail of the testing library. Do not use it directly.")
@Sendable private static func \(generatorName)() async -> Testing.Test {
.__type(
\(declaration.type.trimmed).self,
\(raw: attributeInfo.functionArgumentList(in: context))
)
}
"""
)

#if hasFeature(SymbolLinkageMarkers)
let accessorName = context.makeUniqueName("accessor")
let accessorDecl: DeclSyntax = """
@available(*, deprecated, message: "This property is an implementation detail of the testing library. Do not use it directly.")
private static let \(accessorName): Testing.__TestContentRecordAccessor = { outValue, type, _ in
Testing.Test.__store(\(generatorName), into: outValue, asTypeAt: type)
}
"""

let testContentRecordDecl = makeTestContentRecordDecl(
named: context.makeUniqueName("testContentRecord"),
in: declaration.type,
ofKind: .testDeclaration,
accessingWith: accessorName,
context: attributeInfo.testContentRecordFlags
)

result.append(
"""
#if hasFeature(SymbolLinkageMarkers)
\(accessorDecl)

\(testContentRecordDecl)
#endif
"""
)
#endif

#if !SWT_NO_LEGACY_TEST_DISCOVERY
// Emit a legacy type declaration if SymbolLinkageMarkers is off.
//
// The emitted type must be public or the compiler can optimize it away
// (since it is not actually used anywhere that the compiler can see.)
//
Expand All @@ -143,16 +191,14 @@ public struct SuiteDeclarationMacro: MemberMacro, PeerMacro, Sendable {
@available(*, deprecated, message: "This type is an implementation detail of the testing library. Do not use it directly.")
enum \(enumName): Testing.__TestContainer {
static var __tests: [Testing.Test] {
get async {[
.__type(
\(declaration.type.trimmed).self,
\(raw: attributeInfo.functionArgumentList(in: context))
)
]}
get async {
[await \(generatorName)()]
}
}
}
"""
)
#endif

return result
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2024 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for Swift project authors
//

import SwiftSyntax

extension IntegerLiteralExprSyntax {
init(_ value: some BinaryInteger, radix: IntegerLiteralExprSyntax.Radix = .decimal) {
let stringValue = "\(radix.literalPrefix)\(String(value, radix: radix.size))"
self.init(literal: .integerLiteral(stringValue))
}
}
Loading