Skip to content

Commit 81b1063

Browse files
committed
Foundation: update for BOOL bridging
Now that `BOOL` is bridged to `WindowsBOOL` and `Bool`, clean up the instances of `BOOL` values since they will no longer be available.
1 parent f10462b commit 81b1063

File tree

9 files changed

+63
-77
lines changed

9 files changed

+63
-77
lines changed

Foundation/FileHandle.swift

Lines changed: 15 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,7 @@ open class FileHandle : NSObject {
212212
}
213213

214214
var fiFileInfo: BY_HANDLE_FILE_INFORMATION = BY_HANDLE_FILE_INFORMATION()
215-
if GetFileInformationByHandle(_handle, &fiFileInfo) == FALSE {
215+
if !GetFileInformationByHandle(_handle, &fiFileInfo) {
216216
throw _NSErrorWithWindowsError(GetLastError(), reading: true)
217217
}
218218

@@ -230,10 +230,10 @@ open class FileHandle : NSObject {
230230
MapViewOfFile(hMapping, DWORD(FILE_MAP_READ), 0, 0, szMapSize)
231231

232232
return NSData.NSDataReadResult(bytes: pData, length: Int(szMapSize)) { buffer, length in
233-
if UnmapViewOfFile(buffer) == FALSE {
233+
if !UnmapViewOfFile(buffer) {
234234
fatalError("UnmapViewOfFile failed")
235235
}
236-
if CloseHandle(hMapping) == FALSE {
236+
if !CloseHandle(hMapping) {
237237
fatalError("CloseHandle failed")
238238
}
239239
}
@@ -255,7 +255,7 @@ open class FileHandle : NSObject {
255255
}
256256

257257
var BytesRead: DWORD = 0
258-
if ReadFile(_handle, buffer.advanced(by: total), BytesToRead, &BytesRead, nil) == FALSE {
258+
if !ReadFile(_handle, buffer.advanced(by: total), BytesToRead, &BytesRead, nil) {
259259
free(buffer)
260260
throw _NSErrorWithWindowsError(GetLastError(), reading: true)
261261
}
@@ -349,7 +349,7 @@ open class FileHandle : NSObject {
349349
#if os(Windows)
350350
var BytesRead: DWORD = 0
351351
let BytesToRead: DWORD = DWORD(length)
352-
if ReadFile(_handle, buffer, BytesToRead, &BytesRead, nil) == FALSE {
352+
if !ReadFile(_handle, buffer, BytesToRead, &BytesRead, nil) {
353353
throw _NSErrorWithWindowsError(GetLastError(), reading: true)
354354
}
355355
return Int(BytesRead)
@@ -367,7 +367,7 @@ open class FileHandle : NSObject {
367367
var bytesRemaining = length
368368
while bytesRemaining > 0 {
369369
var bytesWritten: DWORD = 0
370-
if WriteFile(handle, buf.advanced(by: length - bytesRemaining), DWORD(bytesRemaining), &bytesWritten, nil) == FALSE {
370+
if !WriteFile(handle, buf.advanced(by: length - bytesRemaining), DWORD(bytesRemaining), &bytesWritten, nil) {
371371
throw _NSErrorWithErrno(Int32(GetLastError()), reading: false, path: nil)
372372
}
373373
if bytesWritten == 0 {
@@ -399,10 +399,7 @@ open class FileHandle : NSObject {
399399
public init(fileDescriptor fd: Int32, closeOnDealloc closeopt: Bool) {
400400
if (closeopt) {
401401
var handle: HANDLE?
402-
if DuplicateHandle(GetCurrentProcess(),
403-
HANDLE(bitPattern: _get_osfhandle(fd))!,
404-
GetCurrentProcess(), &handle,
405-
0, FALSE, DWORD(DUPLICATE_SAME_ACCESS)) == FALSE {
402+
if !DuplicateHandle(GetCurrentProcess(), HANDLE(bitPattern: _get_osfhandle(fd))!, GetCurrentProcess(), &handle, 0, false, DWORD(DUPLICATE_SAME_ACCESS)) {
406403
fatalError("DuplicateHandle() failed: \(GetLastError())")
407404
}
408405
_close(fd)
@@ -512,7 +509,7 @@ open class FileHandle : NSObject {
512509

513510
#if os(Windows)
514511
var liPointer: LARGE_INTEGER = LARGE_INTEGER(QuadPart: 0)
515-
guard SetFilePointerEx(_handle, LARGE_INTEGER(QuadPart: 0), &liPointer, DWORD(FILE_CURRENT)) != FALSE else {
512+
guard SetFilePointerEx(_handle, LARGE_INTEGER(QuadPart: 0), &liPointer, DWORD(FILE_CURRENT)) else {
516513
throw _NSErrorWithWindowsError(GetLastError(), reading: true)
517514
}
518515
return UInt64(liPointer.QuadPart)
@@ -532,7 +529,7 @@ open class FileHandle : NSObject {
532529

533530
#if os(Windows)
534531
var liPointer: LARGE_INTEGER = LARGE_INTEGER(QuadPart: 0)
535-
guard SetFilePointerEx(_handle, LARGE_INTEGER(QuadPart: 0), &liPointer, DWORD(FILE_END)) != FALSE else {
532+
guard SetFilePointerEx(_handle, LARGE_INTEGER(QuadPart: 0), &liPointer, DWORD(FILE_END)) else {
536533
throw _NSErrorWithWindowsError(GetLastError(), reading: true)
537534
}
538535
return UInt64(liPointer.QuadPart)
@@ -550,7 +547,7 @@ open class FileHandle : NSObject {
550547
guard _isPlatformHandleValid else { throw NSError(domain: NSCocoaErrorDomain, code: CocoaError.fileReadUnknown.rawValue) }
551548

552549
#if os(Windows)
553-
guard SetFilePointerEx(_handle, LARGE_INTEGER(QuadPart: LONGLONG(offset)), nil, DWORD(FILE_BEGIN)) != FALSE else {
550+
guard SetFilePointerEx(_handle, LARGE_INTEGER(QuadPart: LONGLONG(offset)), nil, DWORD(FILE_BEGIN)) else {
554551
throw _NSErrorWithWindowsError(GetLastError(), reading: true)
555552
}
556553
#else
@@ -565,11 +562,10 @@ open class FileHandle : NSObject {
565562
guard _isPlatformHandleValid else { throw NSError(domain: NSCocoaErrorDomain, code: CocoaError.fileWriteUnknown.rawValue) }
566563

567564
#if os(Windows)
568-
guard SetFilePointerEx(_handle, LARGE_INTEGER(QuadPart: LONGLONG(offset)),
569-
nil, DWORD(FILE_BEGIN)) != FALSE else {
565+
guard SetFilePointerEx(_handle, LARGE_INTEGER(QuadPart: LONGLONG(offset)), nil, DWORD(FILE_BEGIN)) else {
570566
throw _NSErrorWithWindowsError(GetLastError(), reading: false)
571567
}
572-
guard SetEndOfFile(_handle) != FALSE else {
568+
guard SetEndOfFile(_handle) else {
573569
throw _NSErrorWithWindowsError(GetLastError(), reading: false)
574570
}
575571
#else
@@ -583,7 +579,7 @@ open class FileHandle : NSObject {
583579
guard self != FileHandle._nulldeviceFileHandle else { return }
584580

585581
#if os(Windows)
586-
guard FlushFileBuffers(_handle) != FALSE else {
582+
guard FlushFileBuffers(_handle) else {
587583
throw _NSErrorWithWindowsError(GetLastError(), reading: false)
588584
}
589585
#else
@@ -626,7 +622,7 @@ open class FileHandle : NSObject {
626622
privateAsyncVariablesLock.unlock()
627623

628624
#if os(Windows)
629-
guard CloseHandle(_handle) != FALSE else {
625+
guard CloseHandle(_handle) else {
630626
throw _NSErrorWithWindowsError(GetLastError(), reading: true)
631627
}
632628
_handle = INVALID_HANDLE_VALUE
@@ -971,7 +967,7 @@ open class Pipe: NSObject {
971967
#if os(Windows)
972968
var hReadPipe: HANDLE?
973969
var hWritePipe: HANDLE?
974-
if CreatePipe(&hReadPipe, &hWritePipe, nil, 0) == FALSE {
970+
if !CreatePipe(&hReadPipe, &hWritePipe, nil, 0) {
975971
fatalError("CreatePipe failed")
976972
}
977973
self.fileHandleForReading = FileHandle(handle: hReadPipe!,

Foundation/FileManager+Win32.swift

Lines changed: 22 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ extension FileManager {
4141
var wszPathNames: UnsafeMutableBufferPointer<WCHAR> = UnsafeMutableBufferPointer<WCHAR>.allocate(capacity: Int(dwCChReturnLength + 1))
4242
defer { wszPathNames.deallocate() }
4343

44-
if GetVolumePathNamesForVolumeNameW(wszVolumeName.baseAddress, wszPathNames.baseAddress, DWORD(wszPathNames.count), &dwCChReturnLength) == FALSE {
44+
if !GetVolumePathNamesForVolumeNameW(wszVolumeName.baseAddress, wszPathNames.baseAddress, DWORD(wszPathNames.count), &dwCChReturnLength) {
4545
// TODO(compnerd) handle error
4646
continue
4747
}
@@ -55,7 +55,7 @@ extension FileManager {
5555
urls.append(URL(fileURLWithPath: path, isDirectory: true))
5656
pPath += DWORD(path.length + 1)
5757
} while pPath < dwCChReturnLength
58-
} while FindNextVolumeW(hVolumes, wszVolumeName.baseAddress, DWORD(wszVolumeName.count)) != FALSE
58+
} while FindNextVolumeW(hVolumes, wszVolumeName.baseAddress, DWORD(wszVolumeName.count))
5959
return urls
6060
}
6161
internal func _urls(for directory: SearchPathDirectory, in domainMask: SearchPathDomainMask) -> [URL] {
@@ -174,13 +174,13 @@ extension FileManager {
174174
var saAttributes: SECURITY_ATTRIBUTES =
175175
SECURITY_ATTRIBUTES(nLength: DWORD(MemoryLayout<SECURITY_ATTRIBUTES>.size),
176176
lpSecurityDescriptor: nil,
177-
bInheritHandle: FALSE)
177+
bInheritHandle: false)
178178
let psaAttributes: UnsafeMutablePointer<SECURITY_ATTRIBUTES> =
179179
UnsafeMutablePointer<SECURITY_ATTRIBUTES>(&saAttributes)
180180

181181

182182
try path.withCString(encodedAs: UTF16.self) {
183-
if CreateDirectoryW($0, psaAttributes) == FALSE {
183+
if !CreateDirectoryW($0, psaAttributes) {
184184
// FIXME(compnerd) pass along path
185185
throw _NSErrorWithWindowsError(GetLastError(), reading: false)
186186
}
@@ -208,7 +208,7 @@ extension FileManager {
208208
}
209209

210210
try closure(path, Int32(ffd.dwFileAttributes))
211-
} while FindNextFileW(hDirectory, &ffd) != FALSE
211+
} while FindNextFileW(hDirectory, &ffd)
212212
}
213213
}
214214

@@ -229,7 +229,7 @@ extension FileManager {
229229
internal func windowsFileAttributes(atPath path: String) throws -> WIN32_FILE_ATTRIBUTE_DATA {
230230
var faAttributes: WIN32_FILE_ATTRIBUTE_DATA = WIN32_FILE_ATTRIBUTE_DATA()
231231
return try path.withCString(encodedAs: UTF16.self) {
232-
if GetFileAttributesExW($0, GetFileExInfoStandard, &faAttributes) == FALSE {
232+
if !GetFileAttributesExW($0, GetFileExInfoStandard, &faAttributes) {
233233
throw _NSErrorWithWindowsError(GetLastError(), reading: true)
234234
}
235235
return faAttributes
@@ -244,14 +244,14 @@ extension FileManager {
244244
let szVolumePath: UnsafeMutableBufferPointer<WCHAR> = UnsafeMutableBufferPointer<WCHAR>.allocate(capacity: Int(dwLength + 1))
245245
defer { szVolumePath.deallocate() }
246246

247-
guard GetVolumePathNameW($0, szVolumePath.baseAddress, dwLength) != FALSE else {
247+
guard GetVolumePathNameW($0, szVolumePath.baseAddress, dwLength) else {
248248
throw _NSErrorWithWindowsError(GetLastError(), reading: true)
249249
}
250250

251251
var liTotal: ULARGE_INTEGER = ULARGE_INTEGER()
252252
var liFree: ULARGE_INTEGER = ULARGE_INTEGER()
253253

254-
guard GetDiskFreeSpaceExW(szVolumePath.baseAddress, nil, &liTotal, &liFree) != FALSE else {
254+
guard GetDiskFreeSpaceExW(szVolumePath.baseAddress, nil, &liTotal, &liFree) else {
255255
throw _NSErrorWithWindowsError(GetLastError(), reading: true)
256256
}
257257

@@ -271,7 +271,7 @@ extension FileManager {
271271

272272
try path.withCString(encodedAs: UTF16.self) { name in
273273
try destPath.withCString(encodedAs: UTF16.self) { dest in
274-
guard CreateSymbolicLinkW(name, dest, dwFlags) != FALSE else {
274+
guard CreateSymbolicLinkW(name, dest, dwFlags) != 0 else {
275275
throw _NSErrorWithWindowsError(GetLastError(), reading: false)
276276
}
277277
}
@@ -303,7 +303,7 @@ extension FileManager {
303303
internal func _copyRegularFile(atPath srcPath: String, toPath dstPath: String, variant: String = "Copy") throws {
304304
try srcPath.withCString(encodedAs: UTF16.self) { src in
305305
try dstPath.withCString(encodedAs: UTF16.self) { dst in
306-
if CopyFileW(src, dst, FALSE) == FALSE {
306+
if !CopyFileW(src, dst, false) {
307307
throw _NSErrorWithWindowsError(GetLastError(), reading: false)
308308
}
309309
}
@@ -365,7 +365,7 @@ extension FileManager {
365365

366366
try srcPath.withCString(encodedAs: UTF16.self) { src in
367367
try dstPath.withCString(encodedAs: UTF16.self) { dst in
368-
if MoveFileExW(src, dst, DWORD(MOVEFILE_COPY_ALLOWED | MOVEFILE_WRITE_THROUGH)) == FALSE {
368+
if !MoveFileExW(src, dst, DWORD(MOVEFILE_COPY_ALLOWED | MOVEFILE_WRITE_THROUGH)) {
369369
throw _NSErrorWithWindowsError(GetLastError(), reading: false)
370370
}
371371
}
@@ -383,7 +383,7 @@ extension FileManager {
383383
case .typeRegular:
384384
try srcPath.withCString(encodedAs: UTF16.self) { src in
385385
try dstPath.withCString(encodedAs: UTF16.self) { dst in
386-
if CreateHardLinkW(src, dst, nil) == FALSE {
386+
if !CreateHardLinkW(src, dst, nil) {
387387
throw _NSErrorWithWindowsError(GetLastError(), reading: false)
388388
}
389389
}
@@ -408,7 +408,7 @@ extension FileManager {
408408
}
409409
let faAttributes = try! windowsFileAttributes(atPath: path)
410410
if faAttributes.dwFileAttributes & DWORD(FILE_ATTRIBUTE_DIRECTORY) == 0 {
411-
if path.withCString(encodedAs: UTF16.self, DeleteFileW) == 0 {
411+
if !path.withCString(encodedAs: UTF16.self, DeleteFileW) {
412412
throw _NSErrorWithWindowsError(GetLastError(), reading: false)
413413
}
414414
return
@@ -421,7 +421,7 @@ extension FileManager {
421421
guard alreadyConfirmed || shouldRemoveItemAtPath(itemPath, isURL: isURL) else {
422422
continue
423423
}
424-
guard path.withCString(encodedAs: UTF16.self, RemoveDirectoryW) == 0 else {
424+
guard !path.withCString(encodedAs: UTF16.self, RemoveDirectoryW) else {
425425
continue
426426
}
427427
guard GetLastError() == ERROR_DIR_NOT_EMPTY else {
@@ -451,11 +451,11 @@ extension FileManager {
451451
guard alreadyConfirmed || shouldRemoveItemAtPath(itemPath, isURL: isURL) else {
452452
continue
453453
}
454-
if itemPath.withCString(encodedAs: UTF16.self, DeleteFileW) == 0 {
454+
if !itemPath.withCString(encodedAs: UTF16.self, DeleteFileW) {
455455
throw _NSErrorWithWindowsError(GetLastError(), reading: false)
456456
}
457457
}
458-
} while(FindNextFileW(h, &ffd) != 0)
458+
} while FindNextFileW(h, &ffd)
459459
} catch {
460460
if !shouldProceedAfterError(error, removingItemAtPath: itemPath, isURL: isURL) {
461461
throw error
@@ -475,7 +475,7 @@ extension FileManager {
475475

476476
@discardableResult
477477
internal func _changeCurrentDirectoryPath(_ path: String) -> Bool {
478-
return path.withCString(encodedAs: UTF16.self) { SetCurrentDirectoryW($0) != FALSE }
478+
return path.withCString(encodedAs: UTF16.self) { SetCurrentDirectoryW($0) }
479479
}
480480

481481
internal func _fileExists(atPath path: String, isDirectory: UnsafeMutablePointer<ObjCBool>?) -> Bool {
@@ -597,7 +597,7 @@ extension FileManager {
597597
internal func _appendSymlinkDestination(_ dest: String, toPath: String) -> String {
598598
var isAbsolutePath: Bool = false
599599
dest.withCString(encodedAs: UTF16.self) {
600-
isAbsolutePath = PathIsRelativeW($0) == FALSE
600+
isAbsolutePath = !PathIsRelativeW($0)
601601
}
602602

603603
if isAbsolutePath {
@@ -645,7 +645,7 @@ extension FileManager {
645645
|| (ffd.dwFileAttributes & DWORD(FILE_ATTRIBUTE_HIDDEN) == 0)) {
646646
files.append(URL(fileURLWithPath: joinPath(prefix: dirFSR, suffix: file)))
647647
}
648-
} while(FindNextFileW(h, &ffd) != 0)
648+
} while FindNextFileW(h, &ffd)
649649
return files
650650
}
651651
while let url = _stack.popLast() {
@@ -699,18 +699,16 @@ extension FileManager.NSPathDirectoryEnumerator {
699699
(fsr.flatMap { String(utf8String: $0) })?.withCString(encodedAs: UTF16.self) { f($0) }
700700
}
701701
}
702-
let result = withURLCString(url: baseURL) { pszFrom -> BOOL? in
702+
guard withURLCString(url: baseURL, { pszFrom -> Bool? in
703703
withURLCString(url: url) { pszTo in
704704
let fromAttrs = GetFileAttributesW(pszFrom)
705705
let toAttrs = GetFileAttributesW(pszTo)
706706
guard fromAttrs != INVALID_FILE_ATTRIBUTES, toAttrs != INVALID_FILE_ATTRIBUTES else {
707-
return FALSE
707+
return false
708708
}
709709
return PathRelativePathToW(relativePath.baseAddress, pszFrom, fromAttrs, pszTo, toAttrs)
710710
}
711-
}
712-
713-
guard result == TRUE, let (path, _) = String.decodeCString(relativePath.baseAddress, as: UTF16.self) else {
711+
}) == true, let (path, _) = String.decodeCString(relativePath.baseAddress, as: UTF16.self) else {
714712
return nil
715713
}
716714
_currentItemPath = path

Foundation/FileManager.swift

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -997,10 +997,7 @@ public struct FileAttributeType : RawRepresentable, Equatable, Hashable {
997997
}
998998
defer { CloseHandle(fileHandle) }
999999
var tagInfo = FILE_ATTRIBUTE_TAG_INFO()
1000-
guard 0 != GetFileInformationByHandleEx(fileHandle,
1001-
FileAttributeTagInfo,
1002-
&tagInfo,
1003-
DWORD(MemoryLayout<FILE_ATTRIBUTE_TAG_INFO>.size)) else {
1000+
guard GetFileInformationByHandleEx(fileHandle, FileAttributeTagInfo, &tagInfo, DWORD(MemoryLayout<FILE_ATTRIBUTE_TAG_INFO>.size)) else {
10041001
self = .typeUnknown
10051002
return
10061003
}

Foundation/Host.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ open class Host: NSObject {
4545
return "localhost"
4646
}
4747
defer { hostname.deallocate() }
48-
guard GetComputerNameExA(ComputerNameDnsHostname, hostname, &dwLength) != FALSE else {
48+
guard GetComputerNameExA(ComputerNameDnsHostname, hostname, &dwLength) else {
4949
return "localhost"
5050
}
5151
return String(cString: hostname)

Foundation/NSLock.swift

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -92,15 +92,15 @@ open class NSLock: NSObject, NSLocking {
9292

9393
open func `try`() -> Bool {
9494
#if os(Windows)
95-
return TryAcquireSRWLockExclusive(mutex) != FALSE
95+
return TryAcquireSRWLockExclusive(mutex) != 0
9696
#else
9797
return pthread_mutex_trylock(mutex) == 0
9898
#endif
9999
}
100100

101101
open func lock(before limit: Date) -> Bool {
102102
#if os(Windows)
103-
if TryAcquireSRWLockExclusive(mutex) != FALSE {
103+
if TryAcquireSRWLockExclusive(mutex) != 0 {
104104
return true
105105
}
106106
#else
@@ -287,15 +287,15 @@ open class NSRecursiveLock: NSObject, NSLocking {
287287

288288
open func `try`() -> Bool {
289289
#if os(Windows)
290-
return TryEnterCriticalSection(mutex) != FALSE
290+
return TryEnterCriticalSection(mutex)
291291
#else
292292
return pthread_mutex_trylock(mutex) == 0
293293
#endif
294294
}
295295

296296
open func lock(before limit: Date) -> Bool {
297297
#if os(Windows)
298-
if TryEnterCriticalSection(mutex) != FALSE {
298+
if TryEnterCriticalSection(mutex) {
299299
return true
300300
}
301301
#else
@@ -370,8 +370,7 @@ open class NSCondition: NSObject, NSLocking {
370370

371371
open func wait(until limit: Date) -> Bool {
372372
#if os(Windows)
373-
return SleepConditionVariableSRW(cond, mutex, timeoutFrom(date: limit),
374-
0) != FALSE
373+
return SleepConditionVariableSRW(cond, mutex, timeoutFrom(date: limit), 0)
375374
#else
376375
guard var timeout = timeSpecFrom(date: limit) else {
377376
return false
@@ -450,7 +449,7 @@ private func timedLock(mutex: _MutexPointer, endTime: Date,
450449
SleepConditionVariableSRW(timeoutCond, timeoutMutex,
451450
timeoutFrom(date: endTime), 0)
452451
ReleaseSRWLockExclusive(timeoutMutex)
453-
if TryAcquireSRWLockExclusive(mutex) != FALSE {
452+
if TryAcquireSRWLockExclusive(mutex) != 0 {
454453
return true
455454
}
456455
} while timeoutFrom(date: endTime) != 0
@@ -465,7 +464,7 @@ private func timedLock(mutex: _RecursiveMutexPointer, endTime: Date,
465464
SleepConditionVariableSRW(timeoutCond, timeoutMutex,
466465
timeoutFrom(date: endTime), 0)
467466
ReleaseSRWLockExclusive(timeoutMutex)
468-
if TryEnterCriticalSection(mutex) != FALSE {
467+
if TryEnterCriticalSection(mutex) {
469468
return true
470469
}
471470
} while timeoutFrom(date: endTime) != 0

0 commit comments

Comments
 (0)