Skip to content

Commit d49c83c

Browse files
authored
Merge pull request #31093 from hlopko/extern-var
[CxxInterop] Add tests for extern vars
2 parents 35bc2f3 + 95a59d9 commit d49c83c

File tree

7 files changed

+204
-4
lines changed

7 files changed

+204
-4
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
int counter = 12;
2+
3+
int getCounterFromCxx() {
4+
return counter;
5+
}
6+
7+
void setCounterFromCxx(int c) {
8+
counter = c;
9+
}
10+
11+
namespace Namespaced {
12+
int counter = 12;
13+
14+
int getCounterFromCxx() {
15+
return counter;
16+
}
17+
18+
void setCounterFromCxx(int c) {
19+
counter = c;
20+
}
21+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
extern int counter;
2+
3+
int getCounterFromCxx();
4+
void setCounterFromCxx(int);
5+
6+
namespace Namespaced {
7+
extern int counter;
8+
9+
int getCounterFromCxx();
10+
void setCounterFromCxx(int);
11+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module ExternVar {
2+
header "extern-var.h"
3+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
// RUN: %target-swift-emit-ir %s -I %S/Inputs -enable-cxx-interop | %FileCheck %s
2+
3+
import ExternVar
4+
5+
public func getCounter() -> CInt {
6+
return counter
7+
}
8+
9+
// CHECK: @{{counter|"\?counter@@3HA"}} = external {{(dso_local )?}}global i32, align 4
10+
11+
// CHECK: define {{(protected |dllexport )?}}swiftcc i32 @"$s4main10getCounters5Int32VyF"() #0
12+
// CHECK: [[LOAD:%.*]] = load i32, i32* getelementptr inbounds (%Ts5Int32V, %Ts5Int32V* bitcast (i32* @{{counter|"\?counter@@3HA"}} to %Ts5Int32V*), i32 0, i32 0), align 4
13+
// CHECK: ret i32 [[LOAD]]
14+
15+
public func setCounter(_ c: CInt) {
16+
counter = c
17+
}
18+
19+
// CHECK: define {{(protected |dllexport )?}}swiftcc void @"$s4main10setCounteryys5Int32VF"(i32 %0) #0
20+
// CHECK: store i32 %0, i32* getelementptr inbounds (%Ts5Int32V, %Ts5Int32V* bitcast (i32* @{{counter|"\?counter@@3HA"}} to %Ts5Int32V*), i32 0, i32 0), align 4
21+
22+
public func getNamespacedCounter() -> CInt {
23+
return Namespaced.counter
24+
}
25+
26+
// CHECK: define {{(protected |dllexport )?}}swiftcc i32 @"$s4main20getNamespacedCounters5Int32VyF"() #0
27+
//FIXME mangle non-top-level var names to prevent name collisions and check:
28+
// load i32, i32* getelementptr inbounds (%Ts5Int32V, %Ts5Int32V* bitcast (i32* @Namespaced.counter to %Ts5Int32V*), i32 0, i32 0), align 4
29+
// CHECK: ret i32 %1
30+
31+
public func setNamespacedCounter(_ c: CInt) {
32+
Namespaced.counter = c
33+
}
34+
35+
// CHECK: define {{(protected |dllexport )?}}swiftcc void @"$s4main20setNamespacedCounteryys5Int32VF"(i32 %0) #0
36+
//FIXME mangle non-top-level var names to prevent name collisions and check:
37+
// store i32 %0, i32* getelementptr inbounds (%Ts5Int32V, %Ts5Int32V* bitcast (i32* @Namespaced.counter to %Ts5Int32V*), i32 0, i32 0), align 4
38+
39+
func modifyInout(_ c: inout CInt) {
40+
c = 42
41+
}
42+
43+
public func passingVarAsInout() {
44+
modifyInout(&counter)
45+
}
46+
47+
// CHECK: define {{(protected |dllexport )?}}swiftcc void @"$s4main17passingVarAsInoutyyF"() #0
48+
// CHECK: call swiftcc void @"$s4main11modifyInoutyys5Int32VzF"(%Ts5Int32V* nocapture dereferenceable(4) bitcast (i32* @{{counter|"\?counter@@3HA"}} to %Ts5Int32V*))
49+
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
// RUN: %target-swift-emit-sil %s -I %S/Inputs -enable-cxx-interop | %FileCheck %s
2+
3+
import ExternVar
4+
5+
func getCounter() -> CInt {
6+
return counter
7+
}
8+
9+
// CHECK: sil_global @counter : $Int32
10+
11+
// CHECK: sil hidden @$s4main10getCounters5Int32VyF : $@convention(thin) () -> Int32
12+
// CHECK: [[COUNTER:%.*]] = global_addr @counter : $*Int32
13+
// CHECK: [[ACCESS:%.*]] = begin_access [read] [dynamic] [[COUNTER]] : $*Int32
14+
// CHECK: [[LOAD:%.*]] = load [[ACCESS]] : $*Int32
15+
// CHECK: return [[LOAD]] : $Int32
16+
17+
func setCounter(_ c: CInt) {
18+
counter = c
19+
}
20+
21+
// CHECK: sil hidden @$s4main10setCounteryys5Int32VF : $@convention(thin) (Int32) -> ()
22+
// CHECK: [[COUNTER:%.*]] = global_addr @counter : $*Int32
23+
// CHECK: [[ACCESS:%.*]] = begin_access [modify] [dynamic] [[COUNTER]] : $*Int32
24+
// CHECK: store %0 to [[ACCESS]] : $*Int32
25+
26+
func getNamespacedCounter() -> CInt {
27+
return Namespaced.counter
28+
}
29+
30+
// sil hidden @$s4main20getNamespacedCounters5Int32VyF : $@convention(thin) () -> Int32
31+
//FIXME mangle non-top-level var names to prevent name collisions
32+
// %0 = global_addr @Namespaced.counter : $*Int32
33+
// CHECK: [[ACCESS:%.*]] = begin_access [read] [dynamic] %0 : $*Int32
34+
// CHECK: [[LOAD:%.*]] = load [[ACCESS]] : $*Int32
35+
// CHECK: return [[LOAD]] : $Int32
36+
37+
func setNamespacedCounter(_ c: CInt) {
38+
Namespaced.counter = c
39+
}
40+
41+
// CHECK: sil hidden @$s4main20setNamespacedCounteryys5Int32VF : $@convention(thin) (Int32) -> ()
42+
//FIXME mangle non-top-level var names to prevent name collisions
43+
// %1 = global_addr @Namespaced.counter : $*Int32
44+
// CHECK: [[ACCESS:%.*]] = begin_access [modify] [dynamic] %1 : $*Int32
45+
// CHECK: store %0 to [[ACCESS]] : $*Int32
46+
47+
func modifyInout(_ c: inout CInt) {
48+
c = 42
49+
}
50+
51+
func passingVarAsInout() {
52+
modifyInout(&counter)
53+
}
54+
55+
// CHECK: sil hidden @$s4main17passingVarAsInoutyyF : $@convention(thin) () -> ()
56+
// CHECK: [[COUNTER:%.*]] = global_addr @counter : $*Int32
57+
// CHECK: [[ACCESS:%.*]] = begin_access [modify] [dynamic] [[COUNTER]] : $*Int32
58+
// CHECK: [[FUNCTION:%.*]] = function_ref @$s4main11modifyInoutyys5Int32VzF : $@convention(thin) (@inout Int32) -> ()
59+
// CHECK: apply [[FUNCTION]]([[ACCESS]]) : $@convention(thin) (@inout Int32) -> ()
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
// RUN: %empty-directory(%t)
2+
// RUN: %target-clang -c %S/Inputs/extern-var.cc -I %S/Inputs -o %t/extern-var.o
3+
// RUN: %target-build-swift %s -I %S/Inputs -o %t/extern-var %t/extern-var.o -Xfrontend -enable-cxx-interop
4+
// RUN: %target-codesign %t/extern-var
5+
// RUN: %target-run %t/extern-var
6+
//
7+
// REQUIRES: executable_test
8+
9+
import ExternVar
10+
import StdlibUnittest
11+
12+
var ExternVarTestSuite = TestSuite("ExternVarTestSuite")
13+
14+
ExternVarTestSuite.test("write-from-swift") {
15+
counter = 42
16+
expectEqual(42, counter)
17+
expectEqual(42, getCounterFromCxx())
18+
}
19+
20+
ExternVarTestSuite.test("write-from-cxx") {
21+
setCounterFromCxx(84)
22+
expectEqual(84, counter)
23+
expectEqual(84, getCounterFromCxx())
24+
}
25+
26+
//FIXME mangle non-top-level var names to prevent name collisions
27+
// ExternVarTestSuite.test("namespaced-write-from-swift") {
28+
// Namespaced.counter = 42
29+
// expectEqual(42, Namespaced.counter)
30+
// expectEqual(42, amespaced.getCounterFromCxx())
31+
// }
32+
33+
//FIXME mangle non-top-level var names to prevent name collisions
34+
// ExternVarTestSuite.test("namespaced-write-from-cxx") {
35+
// Namespaced.setCounterFromCxx(84)
36+
// expectEqual(84, Namespaced.counter)
37+
// expectEqual(84, Namespaced.getCounterFromCxx())
38+
// }
39+
40+
//FIXME mangle non-top-level var names to prevent name collisions
41+
// ExternVarTestSuite.test("no-collisions") {
42+
// counter = 12
43+
// Namespaced.counter = 42
44+
// expectEqual(12, counter)
45+
// expectEqual(42, Namespaced.counter)
46+
// }
47+
48+
runAllTests()

test/lit.cfg

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1038,6 +1038,7 @@ elif run_os in ['windows-msvc']:
10381038
config.target_shared_library_suffix = '.dll'
10391039
config.target_sdk_name = 'windows'
10401040
config.target_runtime = 'native'
1041+
config.target_cc_options = ""
10411042

10421043
config.target_build_swift = \
10431044
('%r -target %s %s %s %s %s -libc %s' % \
@@ -1065,7 +1066,8 @@ elif run_os in ['windows-msvc']:
10651066
config.target_add_rpath = r''
10661067

10671068
config.target_clang = \
1068-
('clang++ -target %s %s -fobjc-runtime=ios-5.0' % (config.variant_triple, clang_mcp_opt))
1069+
('clang++ -target %s %s %s -fobjc-runtime=ios-5.0' % \
1070+
(config.variant_triple, clang_mcp_opt, config.target_cc_options))
10691071
config.target_ld = \
10701072
('%r -libpath:%s' % (config.link, os.path.join(test_resource_dir, \
10711073
config.target_sdk_name)))
@@ -1105,24 +1107,28 @@ elif (run_os in ['linux-gnu', 'linux-gnueabihf', 'freebsd', 'openbsd', 'windows-
11051107
config.target_shared_library_prefix = 'lib'
11061108
config.target_shared_library_suffix = ".dll"
11071109
config.target_sdk_name = "cygwin"
1110+
config.target_cc_options = ""
11081111
elif run_os == 'windows-gnu':
11091112
lit_config.note("Testing MinGW " + config.variant_triple)
11101113
config.target_object_format = "coff"
11111114
config.target_shared_library_prefix = 'lib'
11121115
config.target_shared_library_suffix = ".dll"
11131116
config.target_sdk_name = "mingw"
1117+
config.target_cc_options = ""
11141118
elif run_os == 'freebsd':
11151119
lit_config.note("Testing FreeBSD " + config.variant_triple)
11161120
config.target_object_format = "elf"
11171121
config.target_shared_library_prefix = 'lib'
11181122
config.target_shared_library_suffix = ".so"
11191123
config.target_sdk_name = "freebsd"
1124+
config.target_cc_options = "-fPIE"
11201125
elif run_os == 'openbsd':
11211126
lit_config.note("Testing OpenBSD " + config.variant_triple)
11221127
config.target_object_format = "elf"
11231128
config.target_shared_library_prefix = 'lib'
11241129
config.target_shared_library_suffix = ".so"
11251130
config.target_sdk_name = "openbsd"
1131+
config.target_cc_options = "-fPIE"
11261132
elif kIsAndroid:
11271133
lit_config.note("Testing Android " + config.variant_triple)
11281134
config.target_object_format = "elf"
@@ -1132,12 +1138,14 @@ elif (run_os in ['linux-gnu', 'linux-gnueabihf', 'freebsd', 'openbsd', 'windows-
11321138
# Needed by several ParseableInterface/swift_build_sdk_interfaces tests on
11331139
# Android
11341140
config.environment['ANDROID_DATA'] = os.environ['ANDROID_DATA']
1141+
config.target_cc_options = "-fPIE"
11351142
else:
11361143
lit_config.note("Testing Linux " + config.variant_triple)
11371144
config.target_object_format = "elf"
11381145
config.target_shared_library_prefix = 'lib'
11391146
config.target_shared_library_suffix = ".so"
11401147
config.target_sdk_name = "linux"
1148+
config.target_cc_options = "-fPIE"
11411149
config.target_runtime = "native"
11421150
config.target_swift_autolink_extract = inferSwiftBinary("swift-autolink-extract")
11431151

@@ -1195,8 +1203,8 @@ elif (run_os in ['linux-gnu', 'linux-gnueabihf', 'freebsd', 'openbsd', 'windows-
11951203
'%s -emit-pcm -target %s' %
11961204
(config.swiftc, config.variant_triple))
11971205
config.target_clang = (
1198-
"clang++ -target %s %s -fobjc-runtime=ios-5.0" %
1199-
(config.variant_triple, clang_mcp_opt))
1206+
"clang++ -target %s %s %s -fobjc-runtime=ios-5.0" %
1207+
(config.variant_triple, clang_mcp_opt, config.target_cc_options))
12001208
config.target_ld = "ld -L%r" % (make_path(test_resource_dir, config.target_sdk_name))
12011209
elif run_os == 'linux-androideabi' or run_os == 'linux-android':
12021210
# The module triple for Android ARMv7 seems to be canonicalized in LLVM
@@ -1205,6 +1213,7 @@ elif run_os == 'linux-androideabi' or run_os == 'linux-android':
12051213
target_specific_module_triple = re.sub(r'androideabi', 'android',
12061214
target_specific_module_triple)
12071215
config.variant_triple = re.sub(r'androideabi', 'android', config.variant_triple)
1216+
config.target_cc_options = "-fPIE"
12081217
def get_architecture_value(**kwargs):
12091218
result = kwargs[run_cpu]
12101219
if result is None:
@@ -1325,7 +1334,7 @@ elif run_os == 'linux-androideabi' or run_os == 'linux-android':
13251334
'clang++',
13261335
'-target', config.variant_triple,
13271336
clang_mcp_opt, android_include_system_paths_opt,
1328-
'-fobjc-runtime=ios-5.0'])
1337+
config.target_cc_options, '-fobjc-runtime=ios-5.0'])
13291338
config.target_ld = ' '.join([
13301339
tools_directory,
13311340
'-L%s' % make_path(test_resource_dir, config.target_sdk_name)])

0 commit comments

Comments
 (0)