Skip to content

Commit

Permalink
Test C++ out-of-line operator functions
Browse files Browse the repository at this point in the history
Add a test to verify that C++ out-of-line operator functions are imported correctly.
Includes fixes for armv7 and arm64.

This is part of addressing SR-12748.
  • Loading branch information
MForster authored Jun 5, 2020
1 parent db44a8e commit 0838a65
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 0 deletions.
4 changes: 4 additions & 0 deletions test/Interop/Cxx/operators/Inputs/module.modulemap
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
module NonMemberInline {
header "non-member-inline.h"
}

module NonMemberOutOfLine {
header "non-member-out-of-line.h"
}
5 changes: 5 additions & 0 deletions test/Interop/Cxx/operators/Inputs/non-member-out-of-line.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#include "non-member-out-of-line.h"

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

struct IntBox {
int value;
};

IntBox operator+(IntBox lhs, IntBox rhs);

#endif
8 changes: 8 additions & 0 deletions test/Interop/Cxx/operators/non-member-out-of-line-irgen.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// RUN: %target-swift-emit-ir %s -I %S/Inputs -enable-cxx-interop | %FileCheck %s

import NonMemberOutOfLine

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

// CHECK: call {{i32|i64}} [[NAME:@(_Zpl6IntBoxS_|"\?\?H@YA\?AUIntBox@@U0@0@Z")]]({{i32|\[1 x i32\]|i64}} %{{[0-9]+}}, {{i32|\[1 x i32\]|i64}} %{{[0-9]+}})
// CHECK: declare {{(dso_local )?}}{{i32|i64}} [[NAME]]({{i32|\[1 x i32\]|i64}}, {{i32|\[1 x i32\]|i64}})
10 changes: 10 additions & 0 deletions test/Interop/Cxx/operators/non-member-out-of-line-silgen.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// RUN: %target-swift-emit-sil %s -I %S/Inputs -enable-cxx-interop | %FileCheck %s

import NonMemberOutOfLine

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

// CHECK: [[COUNTER:%.*]] = function_ref [[NAME:@(_Zpl6IntBoxS_|\?\?H@YA\?AUIntBox@@U0@0@Z)]] : $@convention(c) (IntBox, IntBox) -> IntBox
// CHECK: apply [[COUNTER]](%0, %1) : $@convention(c) (IntBox, IntBox) -> IntBox

// CHECK: sil [serializable] [clang "+"] [[NAME]] : $@convention(c) (IntBox, IntBox) -> IntBox
23 changes: 23 additions & 0 deletions test/Interop/Cxx/operators/non-member-out-of-line.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// RUN: %empty-directory(%t)
// 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
// 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
// RUN: %target-codesign %t/non-member-out-of-line
// RUN: %target-run %t/non-member-out-of-line
//
// REQUIRES: executable_test

import NonMemberOutOfLine
import StdlibUnittest

var OperatorsTestSuite = TestSuite("Operators")

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

let result = lhs + rhs

expectEqual(65, result.value)
}

runAllTests()

0 comments on commit 0838a65

Please sign in to comment.