Skip to content

Add tests for C++ out-of-line member operators #32718

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jul 7, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions test/Interop/Cxx/operators/Inputs/member-out-of-line.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#include "member-out-of-line.h"

IntBox IntBox::operator+(IntBox rhs) const {
return IntBox{.value = value + rhs.value};
}
9 changes: 9 additions & 0 deletions test/Interop/Cxx/operators/Inputs/member-out-of-line.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#ifndef TEST_INTEROP_CXX_OPERATORS_INPUTS_MEMBER_OUT_OF_LINE_H
#define TEST_INTEROP_CXX_OPERATORS_INPUTS_MEMBER_OUT_OF_LINE_H

struct IntBox {
int value;
IntBox operator+(IntBox rhs) const;
};

#endif
4 changes: 4 additions & 0 deletions test/Interop/Cxx/operators/Inputs/module.modulemap
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@ module MemberInline {
header "member-inline.h"
}

module MemberOutOfLine {
header "member-out-of-line.h"
}

module NonMemberInline {
header "non-member-inline.h"
}
Expand Down
11 changes: 11 additions & 0 deletions test/Interop/Cxx/operators/member-out-of-line-irgen.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// RUN: %target-swift-emit-ir %s -I %S/Inputs -enable-cxx-interop | %FileCheck %s
//
// We can't yet call member functions correctly on Windows (SR-13129).
// XFAIL: OS=windows-msvc

import MemberOutOfLine

public func add(_ lhs: inout IntBox, _ rhs: IntBox) -> IntBox { lhs + rhs }

// CHECK: call {{i32|i64}} [[NAME:@_ZNK6IntBoxplES_]](%struct.IntBox* %{{[0-9]+}}, {{i32|\[1 x i32\]|i64|%struct.IntBox\* byval align 4}} %{{[0-9]+}})
// CHECK: declare {{(dso_local )?}}{{i32|i64}} [[NAME]](%struct.IntBox*, {{i32|\[1 x i32\]|i64|%struct.IntBox\* byval\(%struct.IntBox\) align 4}})
16 changes: 16 additions & 0 deletions test/Interop/Cxx/operators/member-out-of-line-silgen.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// RUN: %target-swift-emit-sil %s -I %S/Inputs -enable-cxx-interop | %FileCheck %s
//
// We can't yet call member functions correctly on Windows (SR-13129).
// XFAIL: OS=windows-msvc

import MemberOutOfLine

public func add(_ lhs: inout IntBox, _ rhs: IntBox) -> IntBox { lhs + rhs }

// CHECK: bb0([[LHS:%.*]] : $*IntBox, [[RHS:%.*]] : $IntBox):
// CHECK: [[ACCESS:%.*]] = begin_access [modify] [static] [[LHS]] : $*IntBox
// CHECK: [[FUNC:%.*]] = function_ref [[NAME:@_ZNK6IntBoxplES_]] : $@convention(c) (@inout IntBox, IntBox) -> IntBox
// CHECK: apply [[FUNC]]([[ACCESS]], [[RHS]]) : $@convention(c) (@inout IntBox, IntBox) -> IntBox
// CHECK: end_access [[ACCESS]] : $*IntBox

// CHECK: sil [clang IntBox."+"] [[NAME]] : $@convention(c) (@inout IntBox, IntBox) -> IntBox
26 changes: 26 additions & 0 deletions test/Interop/Cxx/operators/member-out-of-line.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// RUN: %empty-directory(%t)
// RUN: %target-clang -c %S/Inputs/member-out-of-line.cpp -I %S/Inputs -o %t/member-out-of-line.o -std=c++17
// RUN: %target-build-swift %s -I %S/Inputs -o %t/member-out-of-line %t/member-out-of-line.o -Xfrontend -enable-cxx-interop
// RUN: %target-codesign %t/member-out-of-line
// RUN: %target-run %t/member-out-of-line
//
// REQUIRES: executable_test
//
// We can't yet call member functions correctly on Windows (SR-13129).
// XFAIL: OS=windows-msvc

import MemberOutOfLine
import StdlibUnittest

var OperatorsTestSuite = TestSuite("Operators")

OperatorsTestSuite.test("plus") {
var lhs = IntBox(value: 42)
let rhs = IntBox(value: 23)

let result = lhs + rhs

expectEqual(65, result.value)
}

runAllTests()