Skip to content

Commit f7473a7

Browse files
authored
Test C++ out-of-line operator functions (swiftlang#32081)
Add tests to verify that C++ out-of-line operator functions are imported correctly.
1 parent f76169b commit f7473a7

6 files changed

+60
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
11
module NonMemberInline {
22
header "non-member-inline.h"
33
}
4+
5+
module NonMemberOutOfLine {
6+
header "non-member-out-of-line.h"
7+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#include "non-member-out-of-line.h"
2+
3+
IntBox operator+(IntBox lhs, IntBox rhs) {
4+
return IntBox{.value = lhs.value + rhs.value};
5+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#ifndef TEST_INTEROP_CXX_OPERATORS_INPUTS_NON_MEMBER_OUT_OF_LINE_H
2+
#define TEST_INTEROP_CXX_OPERATORS_INPUTS_NON_MEMBER_OUT_OF_LINE_H
3+
4+
struct IntBox {
5+
int value;
6+
};
7+
8+
IntBox operator+(IntBox lhs, IntBox rhs);
9+
10+
#endif
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// RUN: %target-swift-emit-ir %s -I %S/Inputs -enable-cxx-interop | %FileCheck %s
2+
3+
import NonMemberOutOfLine
4+
5+
public func add(_ lhs: IntBox, _ rhs: IntBox) -> IntBox { lhs + rhs }
6+
7+
// CHECK: call i32 [[NAME:@(_Zpl6IntBoxS_|"\?\?H@YA\?AUIntBox@@U0@0@Z")]](i32 %{{[0-9]+}}, i32 %{{[0-9]+}})
8+
// CHECK: declare {{(dso_local )?}}i32 [[NAME]](i32, i32)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// RUN: %target-swift-emit-sil %s -I %S/Inputs -enable-cxx-interop | %FileCheck %s
2+
3+
import NonMemberOutOfLine
4+
5+
public func add(_ lhs: IntBox, _ rhs: IntBox) -> IntBox { lhs + rhs }
6+
7+
// CHECK: [[COUNTER:%.*]] = function_ref [[NAME:@(_Zpl6IntBoxS_|\?\?H@YA\?AUIntBox@@U0@0@Z)]] : $@convention(c) (IntBox, IntBox) -> IntBox
8+
// CHECK: apply [[COUNTER]](%0, %1) : $@convention(c) (IntBox, IntBox) -> IntBox
9+
10+
// CHECK: sil [serializable] [clang "+"] [[NAME]] : $@convention(c) (IntBox, IntBox) -> IntBox
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// RUN: %empty-directory(%t)
2+
// RUN: %target-clang -c %S/Inputs/non-member-out-of-line.cpp -I %S/Inputs -o %t/non-member-out-of-line.o -std=c++17
3+
// RUN: %target-build-swift %s -I %S/Inputs -o %t/non-member-out-of-line %t/non-member-out-of-line.o -Xfrontend -enable-cxx-interop
4+
// RUN: %target-codesign %t/non-member-out-of-line
5+
// RUN: %target-run %t/non-member-out-of-line
6+
//
7+
// REQUIRES: executable_test
8+
9+
import NonMemberOutOfLine
10+
import StdlibUnittest
11+
12+
var OperatorsTestSuite = TestSuite("Operators")
13+
14+
OperatorsTestSuite.test("plus") {
15+
let lhs = IntBox(value: 42)
16+
let rhs = IntBox(value: 23)
17+
18+
let result = lhs + rhs
19+
20+
expectEqual(65, result.value)
21+
}
22+
23+
runAllTests()

0 commit comments

Comments
 (0)