Skip to content

Commit 4f8eb5f

Browse files
authored
Merge pull request swiftlang#75469 from swiftlang/susmonteiro/cxx-span-reverse-interop
[cxx-interop] Test reverse interop with std::span
2 parents 0976e83 + 6c1f284 commit 4f8eb5f

File tree

1 file changed

+133
-0
lines changed

1 file changed

+133
-0
lines changed
Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
// RUN: %empty-directory(%t)
2+
// RUN: split-file %s %t
3+
4+
// RUN: %target-swift-frontend -typecheck %t/use-span.swift -typecheck -module-name UseSpan -emit-clang-header-path %t/UseSpan.h -I %t -enable-experimental-cxx-interop -Xcc -std=c++20 -clang-header-expose-decls=all-public
5+
6+
// RUN: %target-interop-build-clangxx -std=c++20 -c %t/use-span.cpp -I %t -o %t/swift-cxx-execution.o
7+
// RUN: %target-interop-build-swift %t/use-span.swift -o %t/swift-cxx-execution -Xlinker %t/swift-cxx-execution.o -module-name UseSpan -Xfrontend -entry-point-function-name -Xfrontend swiftMain -I %t -O -Xcc --std=c++20
8+
9+
// RUN: %target-codesign %t/swift-cxx-execution
10+
// RUN: %target-run %t/swift-cxx-execution | %FileCheck %s
11+
12+
// FIXME swift-ci linux tests do not support std::span
13+
// UNSUPPORTED: OS=linux-gnu
14+
15+
// REQUIRES: executable_test
16+
17+
//--- header.h
18+
#include <string>
19+
#include <span>
20+
21+
using Span = std::span<int>;
22+
using SpanOfString = std::span<std::string>;
23+
24+
static int staticArr[] = {1, 2, 3};
25+
static Span staticSpan = {staticArr};
26+
27+
//--- module.modulemap
28+
module CxxTest {
29+
header "header.h"
30+
requires cplusplus
31+
export *
32+
}
33+
34+
//--- use-span.swift
35+
import CxxTest
36+
37+
@_expose(Cxx)
38+
public func createEmptySpan() -> Span {
39+
return Span()
40+
}
41+
42+
@_expose(Cxx)
43+
public func printSpan() {
44+
print("{\(staticSpan[0]), \(staticSpan[1]), \(staticSpan[2])}")
45+
}
46+
47+
@_expose(Cxx)
48+
public func printSpan(_ sp: Span) {
49+
print("{\(sp[0]), \(sp[1]), \(sp[2])}")
50+
}
51+
52+
@_expose(Cxx)
53+
public func printSpanOfString(_ sp: SpanOfString) {
54+
print("{\(sp[0]), \(sp[1]), \(sp[2])}")
55+
}
56+
57+
@_expose(Cxx)
58+
public func passthroughSpan(_ sp: Span) -> Span {
59+
return sp;
60+
}
61+
62+
@_expose(Cxx)
63+
public func changeSpan(_ sp: inout Span) {
64+
sp[0] = 0;
65+
}
66+
67+
@_expose(Cxx)
68+
public func mapSpan(_ sp: Span) {
69+
let result = sp.map { $0 + 3 }
70+
print(result)
71+
}
72+
73+
@_expose(Cxx)
74+
public func receiveArr(_ arr: inout [Int32]) -> Span {
75+
arr.withUnsafeMutableBufferPointer { ubpointer in
76+
return Span(ubpointer);
77+
}
78+
}
79+
80+
//--- use-span.cpp
81+
#include <cassert>
82+
#include "header.h"
83+
#include "UseSpan.h"
84+
85+
86+
int main() {
87+
using namespace swift;
88+
{
89+
Span emptySpan = UseSpan::createEmptySpan();
90+
assert(emptySpan.empty());
91+
assert(emptySpan.size() == 0);
92+
93+
int arr[] = {4, 5, 6};
94+
Span sp = {arr};
95+
UseSpan::printSpan();
96+
UseSpan::printSpan(sp);
97+
assert(staticSpan[0] == 1);
98+
assert(sp[0] == 4);
99+
}
100+
// CHECK: {1, 2, 3}
101+
// CHECK-NEXT: {4, 5, 6}
102+
{
103+
std::string strArr[] = {"", "a", "abc"};
104+
SpanOfString strSp = {strArr};
105+
UseSpan::printSpanOfString(strSp);
106+
}
107+
// CHECK-NEXT: {, a, abc}
108+
{
109+
int arr[] = {4, 5, 6};
110+
Span sp = {arr};
111+
UseSpan::printSpan(UseSpan::passthroughSpan(sp));
112+
UseSpan::changeSpan(sp);
113+
UseSpan::printSpan(sp);
114+
}
115+
// CHECK-NEXT: {4, 5, 6}
116+
// CHECK-NEXT: {0, 5, 6}
117+
{
118+
int arr[] = {4, 5, 6};
119+
Span sp = {arr};
120+
UseSpan::mapSpan(sp);
121+
}
122+
// CHECK-NEXT: [7, 8, 9]
123+
{
124+
Array<int> arr = Array<int>::init();
125+
arr.append(2);
126+
arr.append(4);
127+
arr.append(6);
128+
Span sp = UseSpan::receiveArr(arr);
129+
UseSpan::printSpan(sp);
130+
}
131+
// CHECK-NEXT: {2, 4, 6}
132+
return 0;
133+
}

0 commit comments

Comments
 (0)