-
Notifications
You must be signed in to change notification settings - Fork 10.5k
[CxxInterop] Add tests for extern vars #31093
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
Changes from all commits
Commits
Show all changes
23 commits
Select commit
Hold shift + click to select a range
c590d72
Steal files from other branch
hlopko 8ef3494
Rename global-var -> extern-var
hlopko d68ce6e
Comment out failing tests
hlopko aba7326
Update test/Interop/Cxx/extern-var/extern-var-irgen.swift
ee775e6
Add more tests
hlopko f4d897e
Remove uninspired comment
hlopko fdcb330
Add inout tests, add labels
hlopko 6321392
Interleave code and checks
hlopko 8136d8c
Rename test suite var
hlopko 774b655
Reverse args to expectEqual
hlopko cd1560a
Address comments
hlopko cfe463d
Cleanup
hlopko 4be9fd9
Add -fPIC in test/lit.cfg
hlopko 32b2177
Merge branch 'pic-in-target-cc-options' into extern-var
hlopko 937b1e2
Trust -fPIE will come from target_clang
hlopko 238893c
Better integration
hlopko 0e24e6c
Merge branch 'pic-in-target-cc-options' into extern-var
hlopko e6f9085
Add config.target_cc_options to all branches
hlopko a7f349b
Merge branch 'pic-in-target-cc-options' into extern-var
hlopko 5fd1807
Add missing branches
hlopko 4763888
Merge branch 'pic-in-target-cc-options' into extern-var
hlopko 8af9ca0
Make irgen test windows friendly
hlopko 95a59d9
Make irgen tests mac friendly
hlopko File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
int counter = 12; | ||
|
||
int getCounterFromCxx() { | ||
return counter; | ||
} | ||
|
||
void setCounterFromCxx(int c) { | ||
counter = c; | ||
} | ||
|
||
namespace Namespaced { | ||
int counter = 12; | ||
|
||
int getCounterFromCxx() { | ||
return counter; | ||
} | ||
|
||
void setCounterFromCxx(int c) { | ||
counter = c; | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
extern int counter; | ||
|
||
int getCounterFromCxx(); | ||
void setCounterFromCxx(int); | ||
|
||
namespace Namespaced { | ||
extern int counter; | ||
|
||
int getCounterFromCxx(); | ||
void setCounterFromCxx(int); | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
module ExternVar { | ||
header "extern-var.h" | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
// RUN: %target-swift-emit-ir %s -I %S/Inputs -enable-cxx-interop | %FileCheck %s | ||
|
||
import ExternVar | ||
|
||
public func getCounter() -> CInt { | ||
return counter | ||
} | ||
|
||
// CHECK: @{{counter|"\?counter@@3HA"}} = external {{(dso_local )?}}global i32, align 4 | ||
|
||
// CHECK: define {{(protected |dllexport )?}}swiftcc i32 @"$s4main10getCounters5Int32VyF"() #0 | ||
// CHECK: [[LOAD:%.*]] = load i32, i32* getelementptr inbounds (%Ts5Int32V, %Ts5Int32V* bitcast (i32* @{{counter|"\?counter@@3HA"}} to %Ts5Int32V*), i32 0, i32 0), align 4 | ||
// CHECK: ret i32 [[LOAD]] | ||
|
||
public func setCounter(_ c: CInt) { | ||
counter = c | ||
} | ||
|
||
// CHECK: define {{(protected |dllexport )?}}swiftcc void @"$s4main10setCounteryys5Int32VF"(i32 %0) #0 | ||
// CHECK: store i32 %0, i32* getelementptr inbounds (%Ts5Int32V, %Ts5Int32V* bitcast (i32* @{{counter|"\?counter@@3HA"}} to %Ts5Int32V*), i32 0, i32 0), align 4 | ||
|
||
public func getNamespacedCounter() -> CInt { | ||
return Namespaced.counter | ||
} | ||
|
||
// CHECK: define {{(protected |dllexport )?}}swiftcc i32 @"$s4main20getNamespacedCounters5Int32VyF"() #0 | ||
//FIXME mangle non-top-level var names to prevent name collisions and check: | ||
// load i32, i32* getelementptr inbounds (%Ts5Int32V, %Ts5Int32V* bitcast (i32* @Namespaced.counter to %Ts5Int32V*), i32 0, i32 0), align 4 | ||
// CHECK: ret i32 %1 | ||
|
||
public func setNamespacedCounter(_ c: CInt) { | ||
Namespaced.counter = c | ||
} | ||
|
||
// CHECK: define {{(protected |dllexport )?}}swiftcc void @"$s4main20setNamespacedCounteryys5Int32VF"(i32 %0) #0 | ||
//FIXME mangle non-top-level var names to prevent name collisions and check: | ||
// store i32 %0, i32* getelementptr inbounds (%Ts5Int32V, %Ts5Int32V* bitcast (i32* @Namespaced.counter to %Ts5Int32V*), i32 0, i32 0), align 4 | ||
|
||
func modifyInout(_ c: inout CInt) { | ||
c = 42 | ||
} | ||
|
||
public func passingVarAsInout() { | ||
modifyInout(&counter) | ||
} | ||
|
||
// CHECK: define {{(protected |dllexport )?}}swiftcc void @"$s4main17passingVarAsInoutyyF"() #0 | ||
// CHECK: call swiftcc void @"$s4main11modifyInoutyys5Int32VzF"(%Ts5Int32V* nocapture dereferenceable(4) bitcast (i32* @{{counter|"\?counter@@3HA"}} to %Ts5Int32V*)) | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
// RUN: %target-swift-emit-sil %s -I %S/Inputs -enable-cxx-interop | %FileCheck %s | ||
|
||
import ExternVar | ||
|
||
func getCounter() -> CInt { | ||
return counter | ||
} | ||
|
||
// CHECK: sil_global @counter : $Int32 | ||
|
||
// CHECK: sil hidden @$s4main10getCounters5Int32VyF : $@convention(thin) () -> Int32 | ||
// CHECK: [[COUNTER:%.*]] = global_addr @counter : $*Int32 | ||
// CHECK: [[ACCESS:%.*]] = begin_access [read] [dynamic] [[COUNTER]] : $*Int32 | ||
// CHECK: [[LOAD:%.*]] = load [[ACCESS]] : $*Int32 | ||
// CHECK: return [[LOAD]] : $Int32 | ||
|
||
func setCounter(_ c: CInt) { | ||
counter = c | ||
} | ||
|
||
// CHECK: sil hidden @$s4main10setCounteryys5Int32VF : $@convention(thin) (Int32) -> () | ||
// CHECK: [[COUNTER:%.*]] = global_addr @counter : $*Int32 | ||
// CHECK: [[ACCESS:%.*]] = begin_access [modify] [dynamic] [[COUNTER]] : $*Int32 | ||
// CHECK: store %0 to [[ACCESS]] : $*Int32 | ||
|
||
func getNamespacedCounter() -> CInt { | ||
return Namespaced.counter | ||
} | ||
hlopko marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
// sil hidden @$s4main20getNamespacedCounters5Int32VyF : $@convention(thin) () -> Int32 | ||
//FIXME mangle non-top-level var names to prevent name collisions | ||
// %0 = global_addr @Namespaced.counter : $*Int32 | ||
// CHECK: [[ACCESS:%.*]] = begin_access [read] [dynamic] %0 : $*Int32 | ||
// CHECK: [[LOAD:%.*]] = load [[ACCESS]] : $*Int32 | ||
// CHECK: return [[LOAD]] : $Int32 | ||
|
||
func setNamespacedCounter(_ c: CInt) { | ||
Namespaced.counter = c | ||
} | ||
|
||
// CHECK: sil hidden @$s4main20setNamespacedCounteryys5Int32VF : $@convention(thin) (Int32) -> () | ||
//FIXME mangle non-top-level var names to prevent name collisions | ||
// %1 = global_addr @Namespaced.counter : $*Int32 | ||
// CHECK: [[ACCESS:%.*]] = begin_access [modify] [dynamic] %1 : $*Int32 | ||
// CHECK: store %0 to [[ACCESS]] : $*Int32 | ||
|
||
func modifyInout(_ c: inout CInt) { | ||
c = 42 | ||
} | ||
|
||
func passingVarAsInout() { | ||
modifyInout(&counter) | ||
} | ||
|
||
// CHECK: sil hidden @$s4main17passingVarAsInoutyyF : $@convention(thin) () -> () | ||
// CHECK: [[COUNTER:%.*]] = global_addr @counter : $*Int32 | ||
// CHECK: [[ACCESS:%.*]] = begin_access [modify] [dynamic] [[COUNTER]] : $*Int32 | ||
// CHECK: [[FUNCTION:%.*]] = function_ref @$s4main11modifyInoutyys5Int32VzF : $@convention(thin) (@inout Int32) -> () | ||
// CHECK: apply [[FUNCTION]]([[ACCESS]]) : $@convention(thin) (@inout Int32) -> () |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
// RUN: %empty-directory(%t) | ||
// RUN: %target-clang -c %S/Inputs/extern-var.cc -I %S/Inputs -o %t/extern-var.o | ||
// RUN: %target-build-swift %s -I %S/Inputs -o %t/extern-var %t/extern-var.o -Xfrontend -enable-cxx-interop | ||
// RUN: %target-codesign %t/extern-var | ||
// RUN: %target-run %t/extern-var | ||
// | ||
// REQUIRES: executable_test | ||
|
||
import ExternVar | ||
import StdlibUnittest | ||
|
||
var ExternVarTestSuite = TestSuite("ExternVarTestSuite") | ||
|
||
ExternVarTestSuite.test("write-from-swift") { | ||
counter = 42 | ||
expectEqual(42, counter) | ||
expectEqual(42, getCounterFromCxx()) | ||
} | ||
|
||
ExternVarTestSuite.test("write-from-cxx") { | ||
setCounterFromCxx(84) | ||
expectEqual(84, counter) | ||
expectEqual(84, getCounterFromCxx()) | ||
} | ||
hlopko marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
//FIXME mangle non-top-level var names to prevent name collisions | ||
// ExternVarTestSuite.test("namespaced-write-from-swift") { | ||
// Namespaced.counter = 42 | ||
// expectEqual(42, Namespaced.counter) | ||
// expectEqual(42, amespaced.getCounterFromCxx()) | ||
// } | ||
|
||
//FIXME mangle non-top-level var names to prevent name collisions | ||
// ExternVarTestSuite.test("namespaced-write-from-cxx") { | ||
// Namespaced.setCounterFromCxx(84) | ||
// expectEqual(84, Namespaced.counter) | ||
// expectEqual(84, Namespaced.getCounterFromCxx()) | ||
// } | ||
|
||
//FIXME mangle non-top-level var names to prevent name collisions | ||
// ExternVarTestSuite.test("no-collisions") { | ||
// counter = 12 | ||
// Namespaced.counter = 42 | ||
// expectEqual(12, counter) | ||
// expectEqual(42, Namespaced.counter) | ||
// } | ||
|
||
runAllTests() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.