Skip to content

Commit 0a4b049

Browse files
committed
[lldb] Add template and variadic Swift/C++ interop tests
rdar://100284870
1 parent 1fa3362 commit 0a4b049

File tree

10 files changed

+128
-0
lines changed

10 files changed

+128
-0
lines changed
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
SWIFT_SOURCES := main.swift
2+
SWIFT_CXX_INTEROP := 1
3+
SWIFTFLAGS_EXTRAS = -Xcc -I$(SRCDIR)
4+
include Makefile.rules
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
2+
"""
3+
Test that a C++ class is visible in Swift.
4+
"""
5+
from lldbsuite.test.lldbtest import *
6+
from lldbsuite.test.decorators import *
7+
8+
9+
class TestTemplateTypes(TestBase):
10+
11+
@swiftTest
12+
def test(self):
13+
self.build()
14+
self.runCmd('setting set target.experimental.swift-enable-cxx-interop true')
15+
_, _, _, _= lldbutil.run_to_source_breakpoint(
16+
self, 'Set breakpoint here', lldb.SBFileSpec('main.swift'))
17+
18+
# rdar://106455215 validating the typeref type system will trigger an assert in Clang
19+
self.runCmd('settings set symbols.swift-validate-typesystem false')
20+
self.expect('v wrapped', substrs=['Wrapper<CxxClass>', 't', 'a1', '10', 'a2', '20', 'a3', '30'])
21+
22+
# FIXME: rdar://106455215
23+
# self.expect('expr wrapped', substrs=['Wrapper<CxxClass>', 't', 'a1', '10', 'a2', '20', 'a3', '30'])
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import ReturnsCxx
2+
3+
func main() {
4+
var wrapped = returnWrapper()
5+
print(1) // Set breakpoint here
6+
}
7+
main()
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
module ReturnsCxx {
2+
header "returns-cxx.h"
3+
requires cplusplus
4+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
2+
template<class T>
3+
struct Wrapper {
4+
T t;
5+
Wrapper(T t) : t(t) {}
6+
};
7+
8+
struct CxxClass {
9+
long long a1 = 10;
10+
long long a2 = 20;
11+
long long a3 = 30;
12+
};
13+
14+
inline Wrapper<CxxClass> returnWrapper() { return Wrapper<CxxClass>(CxxClass()); }
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
SWIFT_SOURCES := main.swift
2+
SWIFT_CXX_INTEROP := 1
3+
SWIFTFLAGS_EXTRAS = -Xcc -I$(SRCDIR)
4+
include Makefile.rules
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
2+
"""
3+
Test that a C++ class is visible in Swift.
4+
"""
5+
from lldbsuite.test.lldbtest import *
6+
from lldbsuite.test.decorators import *
7+
8+
9+
class TestVariadicTemplateTypes(TestBase):
10+
11+
@swiftTest
12+
def test(self):
13+
self.build()
14+
self.runCmd('setting set target.experimental.swift-enable-cxx-interop true')
15+
_, _, _, _= lldbutil.run_to_source_breakpoint(
16+
self, 'Set breakpoint here', lldb.SBFileSpec('main.swift'))
17+
18+
self.expect('v pair', substrs=['Pair', 'Tuple<OtherCxxClass>', '_t',
19+
'v = false', '_t', 'a1', '10', 'a2', '20', 'a3', '30'])
20+
self.expect('expr pair', substrs=['Pair', 'Tuple<OtherCxxClass>', '_t',
21+
'v = false', '_t', 'a1', '10', 'a2', '20', 'a3', '30'])
22+
23+
# rdar://106459037 (Swift/C++ interop: Variadic templates aren't displayed correctly)
24+
# self.expect('v variadic', substrs=['Tuple<CxxClass, OtherCxxClass>', '_t',
25+
# 'v = false', 'a1', '10', 'a2', '20', 'a3', '30'])
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import ReturnsCxx
2+
3+
func main() {
4+
var pair = returnPair()
5+
var variadic = returnVariadic()
6+
print(1) // Set breakpoint here
7+
}
8+
main()
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
module ReturnsCxx {
2+
header "returns-cxx.h"
3+
requires cplusplus
4+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
2+
template <class... Ts> struct Tuple {};
3+
4+
template <>
5+
struct Tuple<> {
6+
void set() {}
7+
};
8+
9+
template <class T, class... Ts>
10+
struct Tuple<T, Ts...> : Tuple<Ts...> {
11+
Tuple(T t, Ts... ts) : Tuple<Ts...>(ts...), _t(t) {}
12+
13+
void set(T t, Ts... ts) { _t = t; Tuple<Ts...>::set(ts...); }
14+
15+
T first() { return _t; }
16+
Tuple<Ts...> rest() { return *this; }
17+
18+
T _t;
19+
};
20+
21+
struct CxxClass {
22+
long long a1 = 10;
23+
long long a2 = 20;
24+
long long a3 = 30;
25+
};
26+
27+
struct OtherCxxClass {
28+
bool v = false;
29+
};
30+
31+
typedef Tuple<CxxClass, OtherCxxClass> Pair;
32+
33+
inline Pair returnPair() { return Pair(CxxClass(), OtherCxxClass()); }
34+
35+
inline Tuple<OtherCxxClass, CxxClass> returnVariadic() { return Tuple<OtherCxxClass, CxxClass>(OtherCxxClass(), CxxClass()); }

0 commit comments

Comments
 (0)