[swift-dev] C Macros and Variadic functions

Xi Ge xi_ge at apple.com
Tue Jan 5 14:12:33 CST 2016

> On Jan 5, 2016, at 11:54 AM, Ryan Lovelett via swift-dev <swift-dev at swift.org> wrote:
> I'm beginning to try to port some of my existing C code over to Swift
> (you have no idea how excited that makes me). Unfortunately, I've hit a
> stumbling block.
> When interacting with some of the system/hardware interfaces in Linux it
> sometimes (often?) becomes necessary to interact with I/O Control
> (ioctl). This introduces two complexities that at least appear to me as
> needing resolution.
> The first is C macros. A lot ioctl interfaces use headers which define
> macros that are specific ioctl request operations. These macros
> typically take the form of `_IO`, `_IOR` or `_IOW` (there are more but
> this should give you a picture). These values are typically bound to a
> specific kernel version and thus are subject to change. I point that out
> because obviously one could just calculate the value and hard-code it
> into the Swift source (which is what I plan to do in the short term) but
> this isn't really a portable solution across kernels. The question is
> what, if anything, is planned around this?
> The second is variadic C functions. In my case I went ahead and
> calculated the result of a C macro and hard coded it into a Swift
> variable (e.g., `let DMX_SET_BUFFER_SIZE: CUnsignedInt = 28461`). This
> was so I could send it to an ioctl call (e.g., `ioctl(fileReference!,
> DMX_SET_BUFFER_SIZE, DVBDVR.DVR_BUFFER_SIZE)`). Unfortunately, this died
> in the compiler with `error: 'ioctl' is unavailable: Variadic function
> is unavailable`. The interesting part of the compiler error was this
> though `SwiftGlibc.ioctl:2:13: note: 'ioctl' has been explicitly marked
> unavailable here`. This seems to imply that it _could_ be on and it just
> isn't. I searched around a bit and found a few PRs [1] [2] [3] that seem
> to indicate that Swift does support variadic C functions. Is there a
> reason why ioctl is not?

Though generally unavailable as variadic C functions (Jordan might be the better person to explain why), 
we still have other reasons to import them as unavailable functions instead of ignoring them. One example is for printing
the complete interface of the underlying Clang module. This message does NOT imply the variadic function is available
in certain circumstances. Do you any suggestions about better error message?

> Sorry if this has been asked or addressed before but I'm at a loss as to
> how to search these mailing lists.
> [1] https://github.com/apple/swift/pull/196
> [2] https://github.com/apple/swift/pull/413
> [3] https://github.com/apple/swift-evolution/pull/38
> _______________________________________________
> swift-dev mailing list
> swift-dev at swift.org
> https://lists.swift.org/mailman/listinfo/swift-dev

More information about the swift-dev mailing list