Skip to content

Commit 50c048d

Browse files
committed
Add ObjectiveC bridging stub test support
This allows for testing the autogenerated bridging stub code.
1 parent b92870f commit 50c048d

File tree

7 files changed

+204
-1
lines changed

7 files changed

+204
-1
lines changed

benchmark/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ include(AddSwiftBenchmarkSuite)
1616

1717
set(SWIFT_BENCH_MODULES
1818
single-source/unit-tests/ObjectiveCBridging
19+
single-source/unit-tests/ObjectiveCBridgingStubs
1920
single-source/unit-tests/StackPromo
2021
single-source/Ackermann
2122
single-source/AngryPhonebook

benchmark/cmake/modules/AddSwiftBenchmarkSuite.cmake

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,8 @@ function (swift_benchmark_compile_archopts)
4949
"-${BENCH_COMPILE_ARCHOPTS_OPT}"
5050
"-D" "INTERNAL_CHECKS_ENABLED"
5151
"-D" "SWIFT_ENABLE_OBJECT_LITERALS"
52-
"-no-link-objc-runtime")
52+
"-no-link-objc-runtime"
53+
"-I" "${srcdir}/utils/ObjectiveCTests")
5354

5455
# Always optimize the driver modules.
5556
# Note that we compile the driver for Ounchecked also with -Ounchecked
@@ -281,10 +282,33 @@ function (swift_benchmark_compile_archopts)
281282
set(OUTPUT_EXEC "${benchmark-bin-dir}/Benchmark_${BENCH_COMPILE_ARCHOPTS_OPT}-${target}")
282283
endif()
283284

285+
set(objcfile "${objdir}/ObjectiveCTests.o")
286+
add_custom_command(
287+
OUTPUT "${objcfile}"
288+
DEPENDS "${srcdir}/utils/ObjectiveCTests/ObjectiveCTests.m"
289+
"${srcdir}/utils/ObjectiveCTests/ObjectiveCTests.h"
290+
COMMAND
291+
"${CLANG_EXEC}"
292+
"-fno-stack-protector"
293+
"-fPIC"
294+
"-Werror=date-time"
295+
"-fcolor-diagnostics"
296+
"-O3"
297+
"-target" "${target}"
298+
"-isysroot" "${sdk}"
299+
"-arch" "${BENCH_COMPILE_ARCHOPTS_ARCH}"
300+
"-F" "${sdk}/../../../Developer/Library/Frameworks"
301+
"-m${triple_platform}-version-min=${ver}"
302+
"-I" "${srcdir}/utils/ObjectiveCTests"
303+
"${srcdir}/utils/ObjectiveCTests/ObjectiveCTests.m"
304+
"-c"
305+
"-o" "${objcfile}")
306+
284307
add_custom_command(
285308
OUTPUT "${OUTPUT_EXEC}"
286309
DEPENDS
287310
${bench_library_objects} ${SWIFT_BENCH_OBJFILES}
311+
"${objcfile}"
288312
"adhoc-sign-swift-stdlib-${BENCH_COMPILE_ARCHOPTS_PLATFORM}"
289313
COMMAND
290314
"${CLANG_EXEC}"
@@ -306,6 +330,7 @@ function (swift_benchmark_compile_archopts)
306330
"-Xlinker" "@executable_path/../lib/swift/${BENCH_COMPILE_ARCHOPTS_PLATFORM}"
307331
${bench_library_objects}
308332
${SWIFT_BENCH_OBJFILES}
333+
${objcfile}
309334
"-o" "${OUTPUT_EXEC}"
310335
COMMAND
311336
"codesign" "-f" "-s" "-" "${OUTPUT_EXEC}")
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
//===---------- ObjectiveCBridgingStubs.swift -----------------------------===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2014 - 2016 Apple Inc. and the Swift project authors
6+
// Licensed under Apache License v2.0 with Runtime Library Exception
7+
//
8+
// See http://swift.org/LICENSE.txt for license information
9+
// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
10+
//
11+
//===----------------------------------------------------------------------===//
12+
13+
import TestsUtils
14+
import Foundation
15+
import ObjectiveCTests
16+
17+
@inline(never)
18+
func testObjectiveCBridgeStubFromNSString() {
19+
let b = BridgeTester()
20+
var str = ""
21+
for _ in 0 ..< 10_000 {
22+
str = b.testToString()
23+
}
24+
CheckResults(str != "" && str == "Default string value no tagged pointer", "Wrong value returned")
25+
}
26+
27+
@inline(never)
28+
public func run_ObjectiveCBridgeStubFromNSString(N: Int) {
29+
for _ in 0 ..< N {
30+
testObjectiveCBridgeStubFromNSString()
31+
}
32+
}
33+
34+
35+
@inline(never)
36+
func testObjectiveCBridgeStubToNSString() {
37+
let b = BridgeTester()
38+
let str = "hello world"
39+
for _ in 0 ..< 10_000 {
40+
b.test(from: str)
41+
}
42+
}
43+
44+
@inline(never)
45+
public func run_ObjectiveCBridgeStubToNSString(N: Int) {
46+
for _ in 0 ..< N {
47+
testObjectiveCBridgeStubToNSString()
48+
}
49+
}
50+
@inline(never)
51+
func testObjectiveCBridgeStubFromArrayOfNSString() {
52+
let b = BridgeTester()
53+
var arr : [String] = []
54+
var str = ""
55+
for _ in 0 ..< 10_000 {
56+
arr = b.testToArrayOfStrings()
57+
str = arr[0]
58+
}
59+
CheckResults(str != "" && str == "Default string value no tagged pointer", "Wrong value returned")
60+
}
61+
62+
@inline(never)
63+
public func run_ObjectiveCBridgeStubFromArrayOfNSString(N: Int) {
64+
for _ in 0 ..< N {
65+
testObjectiveCBridgeStubFromArrayOfNSString()
66+
}
67+
}
68+
@inline(never)
69+
func testObjectiveCBridgeStubToArrayOfNSString() {
70+
let b = BridgeTester()
71+
let str = "hello world"
72+
let arr = [ str, str, str, str, str,
73+
str, str, str, str, str ]
74+
for _ in 0 ..< 10_000 {
75+
b.test(fromArrayOf: arr)
76+
}
77+
}
78+
79+
@inline(never)
80+
public func run_ObjectiveCBridgeStubToArrayOfNSString(N: Int) {
81+
for _ in 0 ..< N {
82+
testObjectiveCBridgeStubToArrayOfNSString()
83+
}
84+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
//===---------------- ObjectiveCTests.h -----------------------------------===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2014 - 2016 Apple Inc. and the Swift project authors
6+
// Licensed under Apache License v2.0 with Runtime Library Exception
7+
//
8+
// See http://swift.org/LICENSE.txt for license information
9+
// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
10+
//
11+
//===----------------------------------------------------------------------===//
12+
13+
#import <Foundation/Foundation.h>
14+
15+
@interface BridgeTester : NSObject {
16+
NSString *myString;
17+
NSArray<NSString *> *myArrayOfStrings;
18+
}
19+
20+
- (id)init;
21+
- (void)testFromString:(NSString *) str;
22+
- (NSString *)testToString;
23+
- (void)testFromArrayOfStrings:(NSArray<NSString *> *)arr;
24+
- (NSArray<NSString *> *)testToArrayOfStrings;
25+
26+
@end
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
//===---------------- ObjectiveCTests.h -----------------------------------===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2014 - 2016 Apple Inc. and the Swift project authors
6+
// Licensed under Apache License v2.0 with Runtime Library Exception
7+
//
8+
// See http://swift.org/LICENSE.txt for license information
9+
// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
10+
//
11+
//===----------------------------------------------------------------------===//
12+
13+
#import "ObjectiveCTests.h"
14+
15+
@implementation BridgeTester
16+
17+
- (id)init {
18+
self = [super init];
19+
if (!self)
20+
return self;
21+
myString = @"Default string value no tagged pointer";
22+
id mutableArray = [NSMutableArray new];
23+
for (int i = 0; i < 10; ++i) {
24+
[mutableArray addObject: myString];
25+
}
26+
myArrayOfStrings = [mutableArray copy];
27+
return self;
28+
}
29+
30+
- (NSString *)testToString {
31+
return myString;
32+
}
33+
34+
- (void)testFromString:(NSString *)str {
35+
unichar c = [str characterAtIndex:0];
36+
}
37+
- (void)testFromArrayOfStrings:(NSArray<NSString *> *)arr {
38+
// Get an element to force lazy bridging to happen.
39+
id str = [arr objectAtIndex:0];
40+
}
41+
42+
- (NSArray<NSString *> *)testToArrayOfStrings {
43+
return myArrayOfStrings;
44+
}
45+
46+
47+
@end
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
//===--- module.map -------------------------------------------------------===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2014 - 2016 Apple Inc. and the Swift project authors
6+
// Licensed under Apache License v2.0 with Runtime Library Exception
7+
//
8+
// See http://swift.org/LICENSE.txt for license information
9+
// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
10+
//
11+
//===----------------------------------------------------------------------===//
12+
13+
module ObjectiveCTests {
14+
header "ObjectiveCTests.h"
15+
}

benchmark/utils/main.swift

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ import NSStringConversion
6464
import NopDeinit
6565
import ObjectAllocation
6666
import ObjectiveCBridging
67+
import ObjectiveCBridgingStubs
6768
import OpenClose
6869
import Phonebook
6970
import PolymorphicCalls
@@ -166,6 +167,10 @@ precommitTests = [
166167
"ObjectiveCBridgeFromNSSetAnyObjectToString": run_ObjectiveCBridgeFromNSSetAnyObjectToString,
167168
"ObjectiveCBridgeFromNSSetAnyObjectToStringForced": run_ObjectiveCBridgeFromNSSetAnyObjectToStringForced,
168169
"ObjectiveCBridgeToNSSet": run_ObjectiveCBridgeToNSSet,
170+
"ObjectiveCBridgeStubFromNSString": run_ObjectiveCBridgeStubFromNSString,
171+
"ObjectiveCBridgeStubToNSString": run_ObjectiveCBridgeStubToNSString,
172+
"ObjectiveCBridgeStubFromArrayOfNSString": run_ObjectiveCBridgeStubFromArrayOfNSString,
173+
"ObjectiveCBridgeStubToArrayOfNSString": run_ObjectiveCBridgeStubToArrayOfNSString,
169174
"OpenClose": run_OpenClose,
170175
"Phonebook": run_Phonebook,
171176
"PolymorphicCalls": run_PolymorphicCalls,

0 commit comments

Comments
 (0)