Skip to content

Commit 1cc2fdd

Browse files
committed
Merge pull request #339 from cielavenir/patch-1
Add withVaList() for environments without ObjC
2 parents 80da1c3 + 6773f93 commit 1cc2fdd

File tree

2 files changed

+24
-14
lines changed

2 files changed

+24
-14
lines changed

stdlib/public/core/VarArgs.swift

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,6 @@
1010
//
1111
//===----------------------------------------------------------------------===//
1212

13-
#if _runtime(_ObjC)
14-
// Excluded due to use of dynamic casting and Builtin.autorelease, neither
15-
// of which correctly work without the ObjC Runtime right now.
16-
// See rdar://problem/18801510
17-
1813
/// Instances of conforming types can be encoded, and appropriately
1914
/// passed, as elements of a C `va_list`.
2015
///
@@ -82,6 +77,11 @@ public func withVaList<R>(builder: VaListBuilder,
8277
return result
8378
}
8479

80+
#if _runtime(_ObjC)
81+
// Excluded due to use of dynamic casting and Builtin.autorelease, neither
82+
// of which correctly work without the ObjC Runtime right now.
83+
// See rdar://problem/18801510
84+
8585
/// Returns a `CVaListPointer` built from `args` that's backed by
8686
/// autoreleased storage.
8787
///
@@ -100,6 +100,7 @@ public func getVaList(args: [CVarArgType]) -> CVaListPointer {
100100
Builtin.autorelease(builder)
101101
return builder.va_list()
102102
}
103+
#endif
103104

104105
@warn_unused_result
105106
public func _encodeBitsAsWords<T : CVarArgType>(x: T) -> [Int] {
@@ -237,13 +238,15 @@ extension UnsafeMutablePointer : CVarArgType {
237238
}
238239
}
239240

241+
#if _runtime(_ObjC)
240242
extension AutoreleasingUnsafeMutablePointer : CVarArgType {
241243
/// Transform `self` into a series of machine words that can be
242244
/// appropriately interpreted by C varargs.
243245
public var _cVarArgEncoding: [Int] {
244246
return _encodeBitsAsWords(self)
245247
}
246248
}
249+
#endif
247250

248251
extension Float : _CVarArgPassedAsDouble, _CVarArgAlignedType {
249252
/// Transform `self` into a series of machine words that can be
@@ -429,4 +432,3 @@ final public class VaListBuilder {
429432

430433
#endif
431434

432-
#endif // _runtime(_ObjC)

test/1_stdlib/VarArgs.swift

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,19 @@
11
// RUN: %target-run-stdlib-swift -parse-stdlib %s | FileCheck %s
22
// REQUIRES: executable_test
33

4-
// XFAIL: linux
5-
64
import Swift
7-
import CoreGraphics
85

9-
@_silgen_name("vprintf")
10-
func c_vprintf(format: UnsafePointer<Int8>, _ args: CVaListPointer)
6+
#if _runtime(_ObjC)
7+
import Darwin
8+
import CoreGraphics
9+
#else
10+
import Glibc
11+
typealias CGFloat = Double
12+
#endif
1113

1214
func my_printf(format: String, _ arguments: CVarArgType...) {
1315
withVaList(arguments) {
14-
c_vprintf(format, $0)
16+
vprintf(format, $0)
1517
}
1618
}
1719

@@ -35,7 +37,7 @@ func test_varArgs1() {
3537

3638
// CHECK: dig it: 0 0 -1 1 -2 2 -3 3 -4 4 -5 5 -6 6 -7 7 -8 8 -9 9 -10 10 -11 11
3739
withVaList(args) {
38-
c_vprintf(format + "\n", $0)
40+
vprintf(format + "\n", $0)
3941
}
4042
}
4143
test_varArgs1()
@@ -48,12 +50,18 @@ func test_varArgs3() {
4850
args.append(COpaquePointer(bitPattern: 0x1234_5671))
4951
args.append(UnsafePointer<Int>(bitPattern: 0x1234_5672))
5052
args.append(UnsafeMutablePointer<Float>(bitPattern: 0x1234_5673))
53+
54+
#if _runtime(_ObjC)
5155
args.append(AutoreleasingUnsafeMutablePointer<AnyObject>(
5256
UnsafeMutablePointer<AnyObject>(bitPattern: 0x1234_5674)))
57+
#else
58+
//Linux does not support AutoreleasingUnsafeMutablePointer; put placeholder.
59+
args.append(UnsafeMutablePointer<Float>(bitPattern: 0x1234_5674))
60+
#endif
5361

5462
// CHECK: {{pointers: '(0x)?0*12345670' '(0x)?0*12345671' '(0x)?0*12345672' '(0x)?0*12345673' '(0x)?0*12345674'}}
5563
withVaList(args) {
56-
c_vprintf(format, $0)
64+
vprintf(format, $0)
5765
}
5866
}
5967
test_varArgs3()

0 commit comments

Comments
 (0)