Skip to content

Commit 7dae980

Browse files
authored
Merge pull request #6401 from augusto2112/test-ivars-and-stl-interop
[lldb] Add tests for Swift class with C++ ivars and STL types in Swift
2 parents 534ac82 + cc8fcdc commit 7dae980

File tree

12 files changed

+208
-2
lines changed

12 files changed

+208
-2
lines changed

lldb/packages/Python/lldbsuite/test/make/Makefile.rules

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -352,7 +352,14 @@ ifeq "$(MAKE_GMODULES)" "YES"
352352
endif
353353

354354
CFLAGS += $(CFLAGS_EXTRAS)
355-
CXXFLAGS += -std=c++11 $(CFLAGS) $(ARCH_CXXFLAGS)
355+
356+
ifeq "$(SWIFT_CXX_INTEROP)" "1"
357+
CXXFLAGS += -std=c++17
358+
else
359+
CXXFLAGS += -std=c++11
360+
endif
361+
CXXFLAGS += $(CFLAGS) $(ARCH_CXXFLAGS)
362+
356363
LD = $(CC)
357364
LDFLAGS ?= $(CFLAGS)
358365
LDFLAGS += $(LD_EXTRAS) $(ARCH_LDFLAGS)
@@ -602,6 +609,7 @@ endif
602609
#----------------------------------------------------------------------
603610
ifeq "$(SWIFT_CXX_INTEROP)" "1"
604611
SWIFTFLAGS += -Xfrontend -enable-experimental-cxx-interop -Xfrontend -validate-tbd-against-ir=none
612+
SWIFTFLAGS += -Xcc -std=c++17
605613
endif
606614

607615
#----------------------------------------------------------------------
@@ -659,7 +667,7 @@ VPATHSOURCES=$(patsubst %,$(VPATH)/%,$(SWIFT_SOURCES)) $(patsubst %,$(VPATH)/%,$
659667
$(SWIFT_FE) -c -primary-file $< \
660668
$(filter-out $(VPATH)/$<,$(filter-out $<,$(VPATHSOURCES))) \
661669
$(SWIFT_FEFLAGS) $(SWIFT_HFLAGS) $(PARSE_AS_LIBRARY) \
662-
-module-name $(MODULENAME) -emit-module-path \
670+
-module-name $(MODULENAME) -emit-module-path \
663671
$(patsubst %.o,$(BUILDDIR)/%.partial.swiftmodule,$@) \
664672
-o $(BUILDDIR)/$@
665673

lldb/source/Plugins/TypeSystem/Swift/TypeSystemSwiftTypeRef.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -913,6 +913,16 @@ CompilerType TypeSystemSwiftTypeRef::GetBuiltinRawPointerType() {
913913
}
914914

915915

916+
static bool IsImportedType(swift::Demangle::NodePointer node) {
917+
if (!node)
918+
return false;
919+
if (node->hasText() && node->getText() == "__C")
920+
return true;
921+
if (node->hasChildren())
922+
return IsImportedType(node->getFirstChild());
923+
return false;
924+
}
925+
916926
swift::Demangle::NodePointer
917927
TypeSystemSwiftTypeRef::GetSwiftified(swift::Demangle::Demangler &dem,
918928
swift::Demangle::NodePointer node,
@@ -3273,6 +3283,12 @@ bool TypeSystemSwiftTypeRef::IsImportedType(opaque_compiler_type_t type,
32733283
"an imported type",
32743284
AsMangledName(type));
32753285

3286+
if (!::IsImportedType(node))
3287+
return false;
3288+
// Early return if we don't need to look up the original type.
3289+
if (!original_type)
3290+
return true;
3291+
32763292
// This is an imported Objective-C type; look it up in the debug info.
32773293
llvm::SmallVector<CompilerContext, 2> decl_context;
32783294
if (!IsClangImportedType(node, decl_context))
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: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
2+
"""
3+
Test that a Swift class with C++ ivars is printed correctly
4+
"""
5+
from lldbsuite.test.lldbtest import *
6+
from lldbsuite.test.decorators import *
7+
8+
9+
class TestSwiftClassWithCxxIvars(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 swiftClass', substrs=['SwiftClass', 'cxxClass', 'a1', '10', 'a2', '20', 'a3', '30',
19+
'cxxSubclass', 'a1', '10', 'a2', '20', 'a3', '30', 'a4', '40'])
20+
self.expect('expr swiftClass', substrs=['SwiftClass', 'cxxClass', 'a1', '10', 'a2', '20', 'a3', '30',
21+
'cxxSubclass', 'a1', '10', 'a2', '20', 'a3', '30', 'a4', '40'])
22+
23+
self.expect('v swiftStruct', substrs=['SwiftStruct', 'cxxClass', 'a1', '10', 'a2', '20', 'a3', '30',
24+
'cxxSubclass', 'a1', '10', 'a2', '20', 'a3', '30', 'a4', '40'])
25+
self.expect('expr swiftStruct', substrs=['SwiftStruct', 'cxxClass', 'a1', '10', 'a2', '20', 'a3', '30',
26+
'cxxSubclass', 'a1', '10', 'a2', '20', 'a3', '30', 'a4', '40'])
27+
28+
29+
self.expect('v swiftEnum1', substrs=['SwiftEnum', 'first', 'a1', '10', 'a2', '20', 'a3', '30'])
30+
self.expect('expr swiftEnum1', substrs=['SwiftEnum', 'first', 'a1', '10', 'a2', '20', 'a3', '30'])
31+
32+
self.expect('v swiftEnum2', substrs=['SwiftEnum', 'second', 'a1', '10', 'a2', '20', 'a3', '30',
33+
'a4', '40'])
34+
self.expect('expr swiftEnum2', substrs=['SwiftEnum', 'second', 'a1', '10', 'a2', '20', 'a3', '30',
35+
'a4', '40'])
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import ReturnsClass
2+
3+
class SwiftClass {
4+
let cxxClass = CxxClass()
5+
let cxxSubclass = InheritedCxxClass()
6+
}
7+
8+
struct SwiftStruct {
9+
let cxxClass = CxxClass()
10+
let cxxSubclass = InheritedCxxClass()
11+
}
12+
13+
enum SwiftEnum {
14+
case first(CxxClass)
15+
case second(InheritedCxxClass)
16+
17+
}
18+
19+
func main() {
20+
let swiftClass = SwiftClass()
21+
let swiftStruct = SwiftStruct()
22+
let swiftEnum1 = SwiftEnum.first(CxxClass())
23+
let swiftEnum2 = SwiftEnum.second(InheritedCxxClass())
24+
print(1) // Set breakpoint here
25+
}
26+
main()
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
module ReturnsClass {
2+
header "returns-class.h"
3+
requires cplusplus
4+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
2+
struct CxxClass {
3+
long long a1 = 10;
4+
long long a2 = 20;
5+
long long a3 = 30;
6+
};
7+
8+
struct InheritedCxxClass: CxxClass {
9+
long long a4 = 40;
10+
};
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) -Xlinker -lm
4+
include Makefile.rules
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
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 TestSTLTypes(TestBase):
10+
11+
@skipIfLinux # rdar://106532498
12+
@skipIf(setting=('symbols.use-swift-clangimporter', 'false')) # rdar://106438227 (TestSTLTypes fails when clang importer is disabled)
13+
@swiftTest
14+
def test(self):
15+
self.build()
16+
self.runCmd('setting set target.experimental.swift-enable-cxx-interop true')
17+
_, _, _, _= lldbutil.run_to_source_breakpoint(
18+
self, 'Set breakpoint here', lldb.SBFileSpec('main.swift'))
19+
20+
self.expect('v map', substrs=['CxxMap', 'first = 1, second = 3',
21+
'first = 2, second = 2', 'first = 3, second = 3'])
22+
23+
# This should work (rdar://106374745), check all 'expr' cases after it's fixed.
24+
self.expect('expr map', substrs=['error while processing module import: failed to '
25+
'get module "std" from AST context'], error=True)
26+
27+
self.expect('v optional', substrs=['CxxOptional', 'optional', 'Has Value=true',
28+
'Value = "In optional!"'])
29+
30+
self.expect('v emptyOptional', substrs=['CxxOptional', 'emptyOptional',
31+
'Has Value=false'])
32+
33+
self.expect('v set', substrs=['CxxSet', 'size=3', '3.7', '4.2', '9.19'])
34+
35+
self.expect('v string', substrs=['string', 'Hello from C++!'])
36+
37+
self.expect('v unorderedMap', substrs=['CxxUnorderedMap',
38+
'(first = 3, second = "three")', '(first = 2, second = "two")',
39+
'(first = 1, second = "one")'], ordered=False)
40+
41+
self.expect('v unorderedSet', substrs=['CxxUnorderedSet',
42+
'first', 'second', 'third'], ordered=False)
43+
44+
self.expect('v vector', substrs=['CxxVector', '[0] = 4.1', '[1] = 3.7',
45+
'[2] = 9.19'])
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import ReturnsSTL
2+
import CxxStdlib
3+
4+
func main() {
5+
var map = returnMap()
6+
var optional = returnOptional()
7+
var emptyOptional = returnEmtpyOptional()
8+
var set = returnSet()
9+
var string = returnString()
10+
var unorderedMap = returnUnorderedMap()
11+
var unorderedSet = returnUnorderedSet()
12+
var vector = returnVector()
13+
14+
print(1) // Set breakpoint here
15+
}
16+
main()

0 commit comments

Comments
 (0)