Skip to content

Commit 1689363

Browse files
committed
stdlib: shim errno access
errno is implemented as a macro in many environments. The accessor hidden behind the macro is not a standard function, so we ended up with an implementation specific handling across all the targets. Shim the function in C where it can be hidden behind the CPP. This simplifies the implementation on the swift side.
1 parent 5d76b49 commit 1689363

File tree

2 files changed

+20
-26
lines changed

2 files changed

+20
-26
lines changed

stdlib/public/Platform/Misc.c

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

13+
#include <errno.h>
1314
#include <fcntl.h>
1415
#if !defined(_WIN32) || defined(__CYGWIN__)
1516
#include <semaphore.h>
@@ -104,3 +105,14 @@ SWIFT_CC(swift) extern char **_swift_FreeBSD_getEnv() {
104105
return environ;
105106
}
106107
#endif // defined(__FreeBSD__)
108+
109+
SWIFT_CC(swift)
110+
extern int _swift_Platform_getErrno() {
111+
return errno;
112+
}
113+
114+
SWIFT_CC(swift)
115+
extern void _swift_Platform_setErrno(int value) {
116+
errno = value;
117+
}
118+

stdlib/public/Platform/Platform.swift

Lines changed: 8 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -94,36 +94,18 @@ public func || <T : Boolean>(
9494
// sys/errno.h
9595
//===----------------------------------------------------------------------===//
9696

97+
@_silgen_name("_swift_Platform_getErrno")
98+
func _swift_Platform_getErrno() -> Int32
99+
100+
@_silgen_name("_swift_Platform_setErrno")
101+
func _swift_Platform_setErrno(_: Int32)
102+
97103
public var errno : Int32 {
98104
get {
99-
#if os(OSX) || os(iOS) || os(watchOS) || os(tvOS) || os(FreeBSD) || os(PS4)
100-
return __error().pointee
101-
#elseif os(Android)
102-
return __errno().pointee
103-
#elseif os(Windows)
104-
#if CYGWIN
105-
return __errno().pointee
106-
#else
107-
return _errno().pointee
108-
#endif
109-
#else
110-
return __errno_location().pointee
111-
#endif
105+
return _swift_Platform_getErrno()
112106
}
113107
set(val) {
114-
#if os(OSX) || os(iOS) || os(watchOS) || os(tvOS) || os(FreeBSD) || os(PS4)
115-
return __error().pointee = val
116-
#elseif os(Android)
117-
return __errno().pointee = val
118-
#elseif os(Windows)
119-
#if CYGWIN
120-
return __errno().pointee = val
121-
#else
122-
return _errno().pointee = val
123-
#endif
124-
#else
125-
return __errno_location().pointee = val
126-
#endif
108+
return _swift_Platform_setErrno(val)
127109
}
128110
}
129111

0 commit comments

Comments
 (0)