Skip to content

Add OGGraphTracing API #127

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

Merged
merged 2 commits into from
Jun 21, 2025
Merged
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
4 changes: 2 additions & 2 deletions Package.resolved

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 0 additions & 12 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -196,15 +196,6 @@ let openGraphCompatibilityTestTarget = Target.testTarget(
cSettings: sharedCSettings,
swiftSettings: sharedSwiftSettings
)
let openGraphSPICompatibilityTestTarget = Target.testTarget(
name: "OpenGraph_SPICompatibilityTests",
dependencies: [
.product(name: "Numerics", package: "swift-numerics"),
],
exclude: ["README.md"],
cSettings: sharedCSettings,
swiftSettings: sharedSwiftSettings
)

// MARK: - Package

Expand All @@ -226,7 +217,6 @@ let package = Package(
openGraphSPITestTarget,
openGraphShimsTestTarget,
openGraphCompatibilityTestTarget,
openGraphSPICompatibilityTestTarget,
],
cxxLanguageStandard: .cxx20
)
Expand Down Expand Up @@ -278,10 +268,8 @@ if attributeGraphCondition {
let compatibilityTestCondition = envEnable("OPENGRAPH_COMPATIBILITY_TEST")
if compatibilityTestCondition && attributeGraphCondition {
openGraphCompatibilityTestTarget.addCompatibilitySettings()
openGraphSPICompatibilityTestTarget.addCompatibilitySettings()
} else {
openGraphCompatibilityTestTarget.dependencies.append("OpenGraph")
openGraphSPICompatibilityTestTarget.dependencies.append("OpenGraph")
}

extension [Platform] {
Expand Down
17 changes: 17 additions & 0 deletions Sources/OpenGraph_SPI/Graph/OGGraphTracing.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
//
// OGGraphTracing.mm
// OpenGraph_SPI

#include "OGGraphTracing.h"

void OGGraphStartTracing(_Nullable OGGraphRef graph, OGGraphTraceFlags options) {
OGGraphStartTracing2(graph, options, NULL);
}

void OGGraphStartTracing2(_Nullable OGGraphRef graph, OGGraphTraceFlags options, _Nullable CFArrayRef array) {
// TODO
}

void OGGraphStopTracing(_Nullable OGGraphRef graph) {
// TODO
}
37 changes: 37 additions & 0 deletions Sources/OpenGraph_SPI/include/OGGraphTracing.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
//
// OGGraphTracing.h
// OpenGraph_SPI

#ifndef OGGraphTracing_hpp
#define OGGraphTracing_hpp

#include "OGBase.h"
#include "OGGraph.h"

typedef OG_OPTIONS(uint32_t, OGGraphTraceFlags) {
OGGraphTraceFlags_0 = 0,
OGGraphTraceFlags_1 = 1 << 0,
OGGraphTraceFlags_2 = 1 << 1,
} OG_SWIFT_NAME(OGGraphRef.TraceFlags);

OG_ASSUME_NONNULL_BEGIN

OG_EXTERN_C_BEGIN

OG_EXPORT
OG_REFINED_FOR_SWIFT
void OGGraphStartTracing(_Nullable OGGraphRef graph, OGGraphTraceFlags options) OG_SWIFT_NAME(OGGraphRef.startTracing(_:options:));

OG_EXPORT
OG_REFINED_FOR_SWIFT
void OGGraphStartTracing2(_Nullable OGGraphRef graph, OGGraphTraceFlags options, _Nullable CFArrayRef array);

OG_EXPORT
OG_REFINED_FOR_SWIFT
void OGGraphStopTracing(_Nullable OGGraphRef graph) OG_SWIFT_NAME(OGGraphRef.stopTracing(_:));

OG_EXTERN_C_END

OG_ASSUME_NONNULL_END

#endif /* OGGraphTracing_hpp */
1 change: 1 addition & 0 deletions Sources/OpenGraph_SPI/include/OpenGraph-umbrella.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include "OGDebugServer.h"
#include "OGGraph.h"
#include "OGGraphContext.h"
#include "OGGraphTracing.h"
#include "OGInputOptions.h"
#include "OGSearchOptions.h"
#include "OGSubgraph.h"
Expand Down
4 changes: 1 addition & 3 deletions Tests/OpenGraphCompatibilityTests/Graph/GraphTests.swift
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
//
// GraphTests.swift
//
//
//
// OpenGraphCompatibilityTests

import Testing
import Foundation
Expand Down
26 changes: 26 additions & 0 deletions Tests/OpenGraphCompatibilityTests/Graph/GraphTracingTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
//
// GraphTracingTests.swift
// OpenGraphCompatibilityTests

import Testing

struct GraphTracingTests {
@Test
func tracing() {
let graph = Graph()
Graph.startTracing(graph, options: [])
Graph.stopTracing(graph)
}

@Test
func tracingAll() {
Graph.startTracing(nil, options: [])
Graph.stopTracing(nil)
}

@Test
func options() {
let option = Graph.TraceFlags(rawValue: 1)
#expect(option == ._1)
}
}
135 changes: 131 additions & 4 deletions Tests/OpenGraphCompatibilityTests/Runtime/MetadataTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,153 @@

import Testing

@Suite(.enabled(if: swiftToolchainSupported))
struct MetadataTests {
class T1 {
var a = 0
var b: Double = 0
}

struct T2 {
var a: Int
var b: Double
}

enum T3 {
case a, b
}


enum T4: Equatable {
case none
case empty
case int(Int)
case double(Double)
}

protocol P {
var a: Int { get }
var b: Double { get }
}


@Test
func kind() {
#expect(Metadata(T1.self).kind == .class)
#expect(Metadata(T2.self).kind == .struct)
#expect(Metadata(T3.self).kind == .enum)

#expect(Metadata(Void?.self).kind == .optional)
#expect(Metadata(Int?.self).kind == .optional)
#expect(Metadata(T1?.self).kind == .optional)
#expect(Metadata((T1, T2)?.self).kind == .optional)

#expect(Metadata(Void.self).kind == .tuple)
#expect(Metadata((Int, Double?).self).kind == .tuple)
#expect(Metadata((T1, T2, T3).self).kind == .tuple)

#expect(Metadata((() -> Void).self).kind == .function)

#expect(Metadata(P.self).kind == .existential)
#expect(Metadata((any P).self).kind == .existential)

#expect(Metadata(P.Protocol.self).kind == .metatype)
#expect(Metadata(type(of: Int.self)).kind == .metatype)
}

@Test
func enumType() {
var none = T4.none
var empty = T4.empty
var int = T4.int(1)
var double = T4.double(2.0)
let metadata = Metadata(T4.self)

withUnsafePointer(to: none) { #expect(metadata.enumTag($0) == 2) }
withUnsafePointer(to: empty) { #expect(metadata.enumTag($0) == 3) }
withUnsafePointer(to: int) { #expect(metadata.enumTag($0) == 0) }
withUnsafePointer(to: double) { #expect(metadata.enumTag($0) == 1) }

withUnsafeMutablePointer(to: &int) {
metadata.projectEnumData($0)
#expect(UnsafeMutableRawPointer($0).assumingMemoryBound(to: Int.self).pointee == 1)
}
withUnsafeMutablePointer(to: &double) {
metadata.projectEnumData($0)
#expect(UnsafeMutableRawPointer($0).assumingMemoryBound(to: Double.self).pointee.isApproximatelyEqual(to: 2.0))
}
withUnsafeMutablePointer(to: &none) {
#expect($0.pointee == .none)
metadata.injectEnumTag(tag: 3, $0)
#expect($0.pointee == .empty)
}
withUnsafeMutablePointer(to: &empty) {
#expect($0.pointee == .empty)
metadata.injectEnumTag(tag: 2, $0)
#expect($0.pointee == .none)
}
withUnsafeMutablePointer(to: &int) {
#expect($0.pointee == .int(1))

metadata.injectEnumTag(tag: 1, $0)
#expect(metadata.enumTag($0) == 1)
#expect($0.pointee == .double(Double(bitPattern: 1)))

metadata.injectEnumTag(tag: 2, $0)
#expect($0.pointee == .none)

metadata.injectEnumTag(tag: 3, $0)
#expect($0.pointee == .empty)
}
}

#if OPENGRAPH_SUPPORT_2024_API
func descriptor() {
let t1 = Metadata(T1.self).descriptor
let t2 = Metadata(T2.self).descriptor
let t3 = Metadata(T3.self).descriptor
let p = Metadata(P.self).descriptor
let optionalP = Metadata(P?.self).descriptor

#expect(t1 != nil)
#expect(t2 != nil)
#expect(t3 != nil)

#expect(p == nil)
#expect(optionalP != nil)

#expect(t1 == Metadata(T1.self).descriptor)
}
#endif

@Test
func nominalDescriptor() {
let t1 = Metadata(T1.self).nominalDescriptor
let t2 = Metadata(T2.self).nominalDescriptor
let t3 = Metadata(T3.self).nominalDescriptor
let p = Metadata(P.self).nominalDescriptor
let optionalP = Metadata(P?.self).nominalDescriptor

#expect(t1 == nil)
#expect(t2 != nil)
#expect(t3 != nil)
#expect(p == nil)
#expect(optionalP != nil)
}

@Test
func nominalDescriptorName() throws {
let t1 = Metadata(T1.self).nominalDescriptorName
let t2 = Metadata(T2.self).nominalDescriptorName
let t3 = Metadata(T3.self).nominalDescriptorName
let p = Metadata(P.self).nominalDescriptorName
let optionalP = Metadata(P?.self).nominalDescriptorName

#expect(t1 == nil)
try #expect(String(cString: #require(t2)) == "T2")
try #expect(String(cString: #require(t3)) == "T3")
#expect(p == nil)
try #expect(String(cString: #require(optionalP)) == "Optional")
}

@Test(.disabled(if: !compatibilityTestEnabled, "Metadata is not implemented"))
func description() {
#expect(Metadata(T1.self).description == "MetadataTests.T1")
Expand Down
47 changes: 44 additions & 3 deletions Tests/OpenGraphCompatibilityTests/Runtime/TupleTypeTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,57 @@ struct TupleTypeTests {
var a = 0
var b: Double = 0
}

struct T2 {
var a: Int = 0
var b: Double = 0
}

enum T3 {
case a, b
}


@Test
func newTupleType() {
let elements: [Metadata] = [Metadata(T1.self), Metadata(T2.self), Metadata(T3.self)]
let tupleType = TupleType(count: 3, elements: elements)
#expect(tupleType.rawValue == Metadata((T1, T2, T3).self).rawValue)
}

@Test
func tupleCount() {
#expect(TupleType(T1.self).count == 1)
#expect(TupleType([T1.self, T2.self]).count == 2)
#expect(TupleType([T1.self, T2.self, T3.self]).count == 3)
}

@Test
func tupleSize() {
#expect(TupleType(T1.self).size == 8)
#expect(TupleType([T1.self, T2.self]).size == 24)
#expect(TupleType([T1.self, T2.self, T3.self]).size == 25)
}

@Test
func tupleElement() {
let tupleType = TupleType([T1.self, T2.self, T3.self])
#expect(tupleType.elementType(at: 0) == Metadata(T1.self))
#expect(tupleType.elementType(at: 1) == Metadata(T2.self))
#expect(tupleType.elementType(at: 2) == Metadata(T3.self))

#expect(tupleType.elementSize(at: 0) == 8)
#expect(tupleType.elementSize(at: 1) == 16)
#expect(tupleType.elementSize(at: 2) == 1)

#expect(tupleType.elementOffset(at: 0) == 0)
#expect(tupleType.elementOffset(at: 1) == 8)
#expect(tupleType.elementOffset(at: 2) == 24)

#expect(tupleType.elementOffset(at: 0, type: Metadata(T1.self)) == 0)
#expect(tupleType.elementOffset(at: 1, type: Metadata(T2.self)) == 8)
#expect(tupleType.elementOffset(at: 2, type: Metadata(T3.self)) == 24)
}

@Test
func initType() {
#expect(TupleType(T1.self).rawValue == Metadata(T1.self).rawValue)
Expand Down
Loading