Skip to content

Commit d530e36

Browse files
authored
Add tests for C++ out-of-line member operators (#32718)
1 parent de3af89 commit d530e36

File tree

6 files changed

+71
-0
lines changed

6 files changed

+71
-0
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#include "member-out-of-line.h"
2+
3+
IntBox IntBox::operator+(IntBox rhs) const {
4+
return IntBox{.value = value + rhs.value};
5+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#ifndef TEST_INTEROP_CXX_OPERATORS_INPUTS_MEMBER_OUT_OF_LINE_H
2+
#define TEST_INTEROP_CXX_OPERATORS_INPUTS_MEMBER_OUT_OF_LINE_H
3+
4+
struct IntBox {
5+
int value;
6+
IntBox operator+(IntBox rhs) const;
7+
};
8+
9+
#endif

test/Interop/Cxx/operators/Inputs/module.modulemap

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@ module MemberInline {
22
header "member-inline.h"
33
}
44

5+
module MemberOutOfLine {
6+
header "member-out-of-line.h"
7+
}
8+
59
module NonMemberInline {
610
header "non-member-inline.h"
711
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// RUN: %target-swift-emit-ir %s -I %S/Inputs -enable-cxx-interop | %FileCheck %s
2+
//
3+
// We can't yet call member functions correctly on Windows (SR-13129).
4+
// XFAIL: OS=windows-msvc
5+
6+
import MemberOutOfLine
7+
8+
public func add(_ lhs: inout IntBox, _ rhs: IntBox) -> IntBox { lhs + rhs }
9+
10+
// CHECK: call {{i32|i64}} [[NAME:@_ZNK6IntBoxplES_]](%struct.IntBox* %{{[0-9]+}}, {{i32|\[1 x i32\]|i64|%struct.IntBox\* byval align 4}} %{{[0-9]+}})
11+
// CHECK: declare {{(dso_local )?}}{{i32|i64}} [[NAME]](%struct.IntBox*, {{i32|\[1 x i32\]|i64|%struct.IntBox\* byval\(%struct.IntBox\) align 4}})
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// RUN: %target-swift-emit-sil %s -I %S/Inputs -enable-cxx-interop | %FileCheck %s
2+
//
3+
// We can't yet call member functions correctly on Windows (SR-13129).
4+
// XFAIL: OS=windows-msvc
5+
6+
import MemberOutOfLine
7+
8+
public func add(_ lhs: inout IntBox, _ rhs: IntBox) -> IntBox { lhs + rhs }
9+
10+
// CHECK: bb0([[LHS:%.*]] : $*IntBox, [[RHS:%.*]] : $IntBox):
11+
// CHECK: [[ACCESS:%.*]] = begin_access [modify] [static] [[LHS]] : $*IntBox
12+
// CHECK: [[FUNC:%.*]] = function_ref [[NAME:@_ZNK6IntBoxplES_]] : $@convention(c) (@inout IntBox, IntBox) -> IntBox
13+
// CHECK: apply [[FUNC]]([[ACCESS]], [[RHS]]) : $@convention(c) (@inout IntBox, IntBox) -> IntBox
14+
// CHECK: end_access [[ACCESS]] : $*IntBox
15+
16+
// CHECK: sil [clang IntBox."+"] [[NAME]] : $@convention(c) (@inout IntBox, IntBox) -> IntBox
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// RUN: %empty-directory(%t)
2+
// RUN: %target-clang -c %S/Inputs/member-out-of-line.cpp -I %S/Inputs -o %t/member-out-of-line.o -std=c++17
3+
// RUN: %target-build-swift %s -I %S/Inputs -o %t/member-out-of-line %t/member-out-of-line.o -Xfrontend -enable-cxx-interop
4+
// RUN: %target-codesign %t/member-out-of-line
5+
// RUN: %target-run %t/member-out-of-line
6+
//
7+
// REQUIRES: executable_test
8+
//
9+
// We can't yet call member functions correctly on Windows (SR-13129).
10+
// XFAIL: OS=windows-msvc
11+
12+
import MemberOutOfLine
13+
import StdlibUnittest
14+
15+
var OperatorsTestSuite = TestSuite("Operators")
16+
17+
OperatorsTestSuite.test("plus") {
18+
var lhs = IntBox(value: 42)
19+
let rhs = IntBox(value: 23)
20+
21+
let result = lhs + rhs
22+
23+
expectEqual(65, result.value)
24+
}
25+
26+
runAllTests()

0 commit comments

Comments
 (0)