Skip to content

Commit 3139538

Browse files
authored
Replace wchar_t with CWideChar. (#1412)
Previously, the definition of `CWideChar` was incorrect on Windows and referred to a 32-bit type instead of a 16-bit type. This has been corrected, so we can use Swift's canonical name for the type instead of C's `wchar_t`. ### Checklist: - [x] Code and documentation should follow the style of the [Style Guide](https://github.com/apple/swift-testing/blob/main/Documentation/StyleGuide.md). - [x] If public symbols are renamed or modified, DocC references should be updated.
1 parent 01413a5 commit 3139538

File tree

8 files changed

+12
-12
lines changed

8 files changed

+12
-12
lines changed

Sources/Overlays/_Testing_Foundation/Attachments/Attachment+URL.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -121,14 +121,14 @@ private let _archiverPath: String? = {
121121
return nil
122122
}
123123

124-
return withUnsafeTemporaryAllocation(of: wchar_t.self, capacity: Int(bufferCount)) { buffer -> String? in
124+
return withUnsafeTemporaryAllocation(of: CWideChar.self, capacity: Int(bufferCount)) { buffer -> String? in
125125
let bufferCount = GetSystemDirectoryW(buffer.baseAddress!, UINT(buffer.count))
126126
guard bufferCount > 0 && bufferCount < buffer.count else {
127127
return nil
128128
}
129129

130130
return _archiverName.withCString(encodedAs: UTF16.self) { archiverName -> String? in
131-
var result: UnsafeMutablePointer<wchar_t>?
131+
var result: UnsafeMutablePointer<CWideChar>?
132132

133133
let flags = ULONG(PATHCCH_ALLOW_LONG_PATHS.rawValue)
134134
guard S_OK == PathAllocCombine(buffer.baseAddress!, archiverName, flags, &result) else {

Sources/Testing/Support/Additions/CommandLineAdditions.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ extension CommandLine {
9090
var bufferCount = Int(MAX_PATH)
9191
#endif
9292
while result == nil {
93-
try withUnsafeTemporaryAllocation(of: wchar_t.self, capacity: bufferCount) { buffer in
93+
try withUnsafeTemporaryAllocation(of: CWideChar.self, capacity: bufferCount) { buffer in
9494
SetLastError(DWORD(ERROR_SUCCESS))
9595
_ = GetModuleFileNameW(nil, buffer.baseAddress!, DWORD(buffer.count))
9696
switch GetLastError() {

Sources/Testing/Support/CError.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -76,9 +76,9 @@ extension Win32Error: CustomStringConvertible {
7676
// error message... _unless_ you pass `FORMAT_MESSAGE_ALLOCATE_BUFFER` in
7777
// which case it takes a pointer-to-pointer that it populates with a
7878
// heap-allocated string. However, the signature for FormatMessageW()
79-
// still takes an LPWSTR? (Optional<UnsafeMutablePointer<wchar_t>>), so we
80-
// need to temporarily mis-cast the pointer before we can pass it in.
81-
let count = buffer.withMemoryRebound(to: wchar_t.self) { buffer in
79+
// still takes an LPWSTR? (Optional<UnsafeMutablePointer<CWideChar>>), so
80+
// we need to temporarily mis-cast the pointer before we can pass it in.
81+
let count = buffer.withMemoryRebound(to: CWideChar.self) { buffer in
8282
FormatMessageW(
8383
DWORD(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS | FORMAT_MESSAGE_MAX_WIDTH_MASK),
8484
nil,

Sources/Testing/Support/Environment.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ package enum Environment {
175175
#elseif os(Windows)
176176
name.withCString(encodedAs: UTF16.self) { name in
177177
func getVariable(maxCount: Int) -> String? {
178-
withUnsafeTemporaryAllocation(of: wchar_t.self, capacity: maxCount) { buffer in
178+
withUnsafeTemporaryAllocation(of: CWideChar.self, capacity: maxCount) { buffer in
179179
SetLastError(DWORD(ERROR_SUCCESS))
180180
let count = GetEnvironmentVariableW(name, buffer.baseAddress!, DWORD(buffer.count))
181181
if count == 0 {

Sources/Testing/Support/FileHandle.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -625,7 +625,7 @@ func appendPathComponent(_ pathComponent: String, to path: String) -> String {
625625
#if os(Windows)
626626
path.withCString(encodedAs: UTF16.self) { path in
627627
pathComponent.withCString(encodedAs: UTF16.self) { pathComponent in
628-
withUnsafeTemporaryAllocation(of: wchar_t.self, capacity: (wcslen(path) + wcslen(pathComponent)) * 2 + 1) { buffer in
628+
withUnsafeTemporaryAllocation(of: CWideChar.self, capacity: (wcslen(path) + wcslen(pathComponent)) * 2 + 1) { buffer in
629629
_ = wcscpy_s(buffer.baseAddress, buffer.count, path)
630630
_ = PathCchAppendEx(buffer.baseAddress, buffer.count, pathComponent, ULONG(PATHCCH_ALLOW_LONG_PATHS.rawValue))
631631
return (String.decodeCString(buffer.baseAddress, as: UTF16.self)?.result)!
@@ -734,7 +734,7 @@ let rootDirectoryPath: String = {
734734
// https://devblogs.microsoft.com/oldnewthing/20140723-00/?p=423 .
735735
let count = GetSystemWindowsDirectoryW(nil, 0)
736736
if count > 0 {
737-
withUnsafeTemporaryAllocation(of: wchar_t.self, capacity: Int(count) + 1) { buffer in
737+
withUnsafeTemporaryAllocation(of: CWideChar.self, capacity: Int(count) + 1) { buffer in
738738
_ = GetSystemWindowsDirectoryW(buffer.baseAddress!, UINT(buffer.count))
739739
let rStrip = PathCchStripToRoot(buffer.baseAddress!, buffer.count)
740740
if rStrip == S_OK || rStrip == S_FALSE {

Sources/Testing/Support/Versions.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ let operatingSystemVersion: String = {
7777
// Include Service Pack details if available.
7878
if versionInfo.szCSDVersion.0 != 0 {
7979
withUnsafeBytes(of: versionInfo.szCSDVersion) { szCSDVersion in
80-
szCSDVersion.withMemoryRebound(to: wchar_t.self) { szCSDVersion in
80+
szCSDVersion.withMemoryRebound(to: CWideChar.self) { szCSDVersion in
8181
if let szCSDVersion = String.decodeCString(szCSDVersion.baseAddress!, as: UTF16.self)?.result {
8282
result += " (\(szCSDVersion))"
8383
}

Tests/TestingTests/ABIEntryPointTests.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@ private func withTestingLibraryImageAddress<R>(_ body: (ImageAddress?) throws ->
192192
// ELF-based platforms.
193193
#elseif os(Windows)
194194
let flags = DWORD(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS)
195-
try addressInTestingLibrary.withMemoryRebound(to: wchar_t.self, capacity: MemoryLayout<UnsafeRawPointer>.stride / MemoryLayout<wchar_t>.stride) { addressInTestingLibrary in
195+
try addressInTestingLibrary.withMemoryRebound(to: CWideChar.self, capacity: MemoryLayout<UnsafeRawPointer>.stride / MemoryLayout<CWideChar>.stride) { addressInTestingLibrary in
196196
try #require(GetModuleHandleExW(flags, addressInTestingLibrary, &testingLibraryAddress))
197197
}
198198
defer {

Tests/TestingTests/Support/FileHandleTests.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -271,7 +271,7 @@ func temporaryDirectory() throws -> String {
271271
#elseif os(Android)
272272
Environment.variable(named: "TMPDIR") ?? "/data/local/tmp"
273273
#elseif os(Windows)
274-
try withUnsafeTemporaryAllocation(of: wchar_t.self, capacity: Int(MAX_PATH + 1)) { buffer in
274+
try withUnsafeTemporaryAllocation(of: CWideChar.self, capacity: Int(MAX_PATH + 1)) { buffer in
275275
// NOTE: GetTempPath2W() was introduced in Windows 10 Build 20348.
276276
if 0 == GetTempPathW(DWORD(buffer.count), buffer.baseAddress) {
277277
throw Win32Error(rawValue: GetLastError())

0 commit comments

Comments
 (0)