Skip to content

Commit 91e1769

Browse files
committed
A couple of Win32 fixes.
rdar://103071801
1 parent 7d651d5 commit 91e1769

File tree

7 files changed

+67
-104
lines changed

7 files changed

+67
-104
lines changed

include/swift/Runtime/Win32.h

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
//===--- Win32.h - Win32 utility functions ----------------------*- C++ -*-===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors
6+
// Licensed under Apache License v2.0 with Runtime Library Exception
7+
//
8+
// See https://swift.org/LICENSE.txt for license information
9+
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
10+
//
11+
//===----------------------------------------------------------------------===//
12+
//
13+
// Utility functions that are specific to the Windows port.
14+
//
15+
//===----------------------------------------------------------------------===//
16+
17+
#ifndef SWIFT_RUNTIME_WIN32_H
18+
#define SWIFT_RUNTIME_WIN32_H
19+
20+
#ifdef _WIN32
21+
22+
#include "swift/shims/Visibility.h"
23+
24+
#include <wchar.h>
25+
26+
namespace swift {
27+
namespace win32 {
28+
29+
/// Convert a wide string to UTF-8.
30+
///
31+
/// @param str The string to convert.
32+
///
33+
/// @returns The string, converted to UTF-8. The caller is responsible
34+
/// for freeing this string with @c free() when done with it.
35+
///
36+
/// If @a str cannot be converted to UTF-8, a fatal error occurs.
37+
SWIFT_RUNTIME_STDLIB_INTERNAL
38+
char *copyUTF8FromWide(const wchar_t *str);
39+
40+
/// Convert a UTF-8 string to a wide string.
41+
///
42+
/// @param str The string to convert.
43+
///
44+
/// @returns The string, converted to UTF-16. The caller is responsible
45+
/// for freeing this string with @c free() when done with it.
46+
///
47+
/// If @a str cannot be converted to UTF-16, a fatal error occurs.
48+
SWIFT_RUNTIME_STDLIB_INTERNAL
49+
wchar_t *copyWideFromUTF8(const char *str);
50+
51+
}
52+
}
53+
#endif // defined(_WIN32)
54+
55+
#endif // SWIFT_RUNTIME_WIN32_H

include/swift/Threading/Impl/Win32/Win32Defs.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,7 @@ typedef struct _RTL_CONDITION_VARIABLE *PRTL_CONDITION_VARIABLE;
4747
typedef PRTL_CONDITION_VARIABLE PCONDITION_VARIABLE;
4848

4949
// These have to be #defines, to avoid problems with <windows.h>
50-
#define RTL_SRWLOCK_INIT \
51-
{ 0 }
50+
#define RTL_SRWLOCK_INIT {0}
5251
#define SRWLOCK_INIT RTL_SRWLOCK_INIT
5352
#define FLS_OUT_OF_INDEXES ((DWORD)0xFFFFFFFF)
5453

stdlib/public/CommandLineSupport/CommandLine.cpp

Lines changed: 2 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
#include <string>
2626

2727
#include "swift/Runtime/Debug.h"
28+
#include "swift/Runtime/Win32.h"
2829

2930
#include "swift/shims/GlobalObjects.h"
3031
#include "swift/shims/RuntimeStubs.h"
@@ -193,48 +194,6 @@ static void swift::enumerateUnsafeArgv(const F& body) {
193194
#elif defined(_WIN32)
194195
#include <stdlib.h>
195196

196-
namespace swift {
197-
/// Convert an argument, represented by a wide string, to UTF-8.
198-
///
199-
/// @param str The string to convert.
200-
///
201-
/// @returns The string, converted to UTF-8. The caller is responsible for
202-
/// freeing this string when done with it.
203-
///
204-
/// If @a str cannot be converted to UTF-8, a fatal error occurs.
205-
static char *copyUTF8FromWide(wchar_t *str) {
206-
char *result = nullptr;
207-
208-
int resultLength = WideCharToMultiByte(CP_UTF8, WC_ERR_INVALID_CHARS, str,
209-
-1, nullptr, 0, nullptr, nullptr);
210-
if (resultLength <= 0) {
211-
swift::fatalError(0,
212-
"Fatal error: Could not get length of commandline "
213-
"argument '%ls': %lu\n",
214-
str, GetLastError());
215-
}
216-
217-
result = reinterpret_cast<char *>(malloc(resultLength));
218-
if (!result) {
219-
swift::fatalError(0,
220-
"Fatal error: Could not allocate space for commandline "
221-
"argument '%ls': %d\n",
222-
str, errno);
223-
}
224-
225-
resultLength = WideCharToMultiByte(CP_UTF8, WC_ERR_INVALID_CHARS, str, -1,
226-
result, resultLength, nullptr, nullptr);
227-
if (resultLength <= 0) {
228-
swift::fatalError(0,
229-
"Fatal error: Conversion to UTF-8 failed for "
230-
"commandline argument '%ls': %lu\n",
231-
str, GetLastError());
232-
}
233-
234-
return result;
235-
}
236-
}
237-
238197
static char **swift::getUnsafeArgvArgc(int *outArgLen) {
239198
return nullptr;
240199
}
@@ -244,7 +203,7 @@ static void swift::enumerateUnsafeArgv(const F& body) {
244203
int argc = 0;
245204
if (LPWSTR *wargv = CommandLineToArgvW(GetCommandLineW(), &argc)) {
246205
std::for_each(wargv, wargv + argc, [=] (wchar_t *warg) {
247-
auto arg = copyUTF8FromWide(warg);
206+
auto arg = swift::win32::copyUTF8FromWide(warg);
248207
body(argc, arg);
249208
free(arg);
250209
});

stdlib/public/runtime/Paths.cpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,9 @@
1818
#include "swift/Runtime/EnvironmentVariables.h"
1919
#include "swift/Runtime/Debug.h"
2020
#include "swift/Runtime/Paths.h"
21+
#include "swift/Runtime/Win32.h"
2122
#include "swift/Threading/Once.h"
2223

23-
#include "Win32.h"
24-
2524
#include <filesystem>
2625

2726
#if !defined(_WIN32) || defined(__CYGWIN__)
@@ -247,7 +246,7 @@ _swift_initRuntimePath(void *) {
247246
"Unable to obtain Swift runtime path\n");
248247
}
249248

250-
runtimePath = swift::_swift_utf8FromWide(lpFilename);
249+
runtimePath = swift::win32::copyUTF8FromWide(lpFilename);
251250

252251
free(lpFilename);
253252
}
@@ -267,7 +266,7 @@ bool _swift_exists(const char *path)
267266
struct stat st;
268267
return stat(path, &st) == 0;
269268
#else
270-
wchar_t *wszPath = swift::_swift_wideFromUTF8(path);
269+
wchar_t *wszPath = swift::win32::copyWideFromUTF8(path);
271270
bool result = GetFileAttributesW(wszPath) != INVALID_FILE_ATTRIBUTES;
272271
free(wszPath);
273272
return result;

stdlib/public/runtime/Win32.cpp

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
//===--- Win32.cpp - Win32 specific functions -------------------*- C++ -*-===//
1+
//===--- Win32.cpp - Win32 utility functions --------------------*- C++ -*-===//
22
//
33
// This source file is part of the Swift.org open source project
44
//
@@ -15,15 +15,14 @@
1515
//===----------------------------------------------------------------------===//
1616

1717
#include "swift/Runtime/Debug.h"
18-
19-
#include "Win32.h"
18+
#include "swift/Runtime/Win32.h"
2019

2120
#ifdef _WIN32
2221

2322
#include <windows.h>
2423

2524
char *
26-
swift::_swift_utf8FromWide(const wchar_t *str) {
25+
swift::win32::copyUTF8FromWide(const wchar_t *str) {
2726
char *result = nullptr;
2827
int len = ::WideCharToMultiByte(CP_UTF8, WC_ERR_INVALID_CHARS,
2928
str, -1,
@@ -56,7 +55,7 @@ swift::_swift_utf8FromWide(const wchar_t *str) {
5655
}
5756

5857
wchar_t *
59-
swift::_swift_wideFromUTF8(const char *str) {
58+
swift::win32::copyWideFromUTF8(const char *str) {
6059
wchar_t *result = nullptr;
6160
int len = ::MultiByteToWideChar(CP_UTF8, MB_ERR_INVALID_CHARS,
6261
str, -1,
@@ -68,7 +67,7 @@ swift::_swift_wideFromUTF8(const char *str) {
6867
}
6968

7069
result = reinterpret_cast<wchar_t *>(std::malloc(len * sizeof(wchar_t)));
71-
if (!reuslt) {
70+
if (!result) {
7271
swift::fatalError(0, "unable to allocate space to convert '%s': %d\n",
7372
str, errno);
7473
}
@@ -85,6 +84,4 @@ swift::_swift_wideFromUTF8(const char *str) {
8584
return result;
8685
}
8786

88-
} // namespace swift
89-
9087
#endif // defined(_WIN32)

stdlib/public/runtime/Win32.h

Lines changed: 0 additions & 46 deletions
This file was deleted.

test/Runtime/Paths.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
#include <string.h>
2121

2222
#if defined(_WIN32) && !defined(__CYGWIN)
23-
#define stat(x) _stat(x)
23+
#define stat _stat
2424
#define S_IFDIR _S_IFDIR
2525
#endif
2626

0 commit comments

Comments
 (0)