Skip to content

Add withVaList() for environments without ObjC #339

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 1 commit into from
Dec 12, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 8 additions & 6 deletions stdlib/public/core/VarArgs.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,6 @@
//
//===----------------------------------------------------------------------===//

#if _runtime(_ObjC)
// Excluded due to use of dynamic casting and Builtin.autorelease, neither
// of which correctly work without the ObjC Runtime right now.
// See rdar://problem/18801510

/// Instances of conforming types can be encoded, and appropriately
/// passed, as elements of a C `va_list`.
///
Expand Down Expand Up @@ -82,6 +77,11 @@ public func withVaList<R>(builder: VaListBuilder,
return result
}

#if _runtime(_ObjC)
// Excluded due to use of dynamic casting and Builtin.autorelease, neither
// of which correctly work without the ObjC Runtime right now.
// See rdar://problem/18801510

/// Returns a `CVaListPointer` built from `args` that's backed by
/// autoreleased storage.
///
Expand All @@ -100,6 +100,7 @@ public func getVaList(args: [CVarArgType]) -> CVaListPointer {
Builtin.autorelease(builder)
return builder.va_list()
}
#endif

@warn_unused_result
public func _encodeBitsAsWords<T : CVarArgType>(x: T) -> [Int] {
Expand Down Expand Up @@ -237,13 +238,15 @@ extension UnsafeMutablePointer : CVarArgType {
}
}

#if _runtime(_ObjC)
extension AutoreleasingUnsafeMutablePointer : CVarArgType {
/// Transform `self` into a series of machine words that can be
/// appropriately interpreted by C varargs.
public var _cVarArgEncoding: [Int] {
return _encodeBitsAsWords(self)
}
}
#endif

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

#endif

#endif // _runtime(_ObjC)
24 changes: 16 additions & 8 deletions test/1_stdlib/VarArgs.swift
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
// RUN: %target-run-stdlib-swift -parse-stdlib %s | FileCheck %s
// REQUIRES: executable_test

// XFAIL: linux

import Swift
import CoreGraphics

@_silgen_name("vprintf")
func c_vprintf(format: UnsafePointer<Int8>, _ args: CVaListPointer)
#if _runtime(_ObjC)
import Darwin
import CoreGraphics
#else
import Glibc
typealias CGFloat = Double
#endif

func my_printf(format: String, _ arguments: CVarArgType...) {
withVaList(arguments) {
c_vprintf(format, $0)
vprintf(format, $0)
}
}

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

// 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
withVaList(args) {
c_vprintf(format + "\n", $0)
vprintf(format + "\n", $0)
}
}
test_varArgs1()
Expand All @@ -48,12 +50,18 @@ func test_varArgs3() {
args.append(COpaquePointer(bitPattern: 0x1234_5671))
args.append(UnsafePointer<Int>(bitPattern: 0x1234_5672))
args.append(UnsafeMutablePointer<Float>(bitPattern: 0x1234_5673))

#if _runtime(_ObjC)
args.append(AutoreleasingUnsafeMutablePointer<AnyObject>(
UnsafeMutablePointer<AnyObject>(bitPattern: 0x1234_5674)))
#else
//Linux does not support AutoreleasingUnsafeMutablePointer; put placeholder.
args.append(UnsafeMutablePointer<Float>(bitPattern: 0x1234_5674))
#endif

// CHECK: {{pointers: '(0x)?0*12345670' '(0x)?0*12345671' '(0x)?0*12345672' '(0x)?0*12345673' '(0x)?0*12345674'}}
withVaList(args) {
c_vprintf(format, $0)
vprintf(format, $0)
}
}
test_varArgs3()
Expand Down