Skip to content

Commit 62b12ed

Browse files
authored
Merge pull request #6819 from hughbe/uuid-linkers
Fix linker failures compiling Swift on Windows
2 parents 8d164c5 + 9414f46 commit 62b12ed

File tree

1 file changed

+27
-17
lines changed

1 file changed

+27
-17
lines changed

lib/Basic/UUID.cpp

Lines changed: 27 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ swift::UUID::UUID(FromRandom_t) {
4242

4343
swift::UUID::UUID(FromTime_t) {
4444
#if defined(_WIN32)
45-
::UUID uuid;
45+
::GUID uuid;
4646
::CoCreateGuid(&uuid);
4747

4848
memcpy(Value, &uuid, Size);
@@ -53,8 +53,7 @@ swift::UUID::UUID(FromTime_t) {
5353

5454
swift::UUID::UUID() {
5555
#if defined(_WIN32)
56-
::UUID uuid = *((::UUID *)&Value);
57-
UuidCreateNil(&uuid);
56+
::GUID uuid = GUID();
5857

5958
memcpy(Value, &uuid, Size);
6059
#else
@@ -64,11 +63,18 @@ swift::UUID::UUID() {
6463

6564
Optional<swift::UUID> swift::UUID::fromString(const char *s) {
6665
#if defined(_WIN32)
67-
RPC_CSTR t = const_cast<RPC_CSTR>(reinterpret_cast<const unsigned char*>(s));
68-
69-
::UUID uuid;
70-
RPC_STATUS status = UuidFromStringA(t, &uuid);
71-
if (status == RPC_S_INVALID_STRING_UUID) {
66+
int length = strlen(s) + 1;
67+
wchar_t *unicodeString = new wchar_t[length];
68+
69+
size_t convertedChars = 0;
70+
errno_t conversionResult =
71+
mbstowcs_s(&convertedChars, unicodeString, length, s, length);
72+
assert(conversionResult == 0 &&
73+
"expected successful conversion of char* to wchar_t*");
74+
75+
::GUID uuid;
76+
HRESULT parseResult = CLSIDFromString(unicodeString, &uuid);
77+
if (parseResult != 0) {
7278
return None;
7379
}
7480

@@ -86,14 +92,19 @@ Optional<swift::UUID> swift::UUID::fromString(const char *s) {
8692
void swift::UUID::toString(llvm::SmallVectorImpl<char> &out) const {
8793
out.resize(UUID::StringBufferSize);
8894
#if defined(_WIN32)
89-
::UUID uuid;
95+
::GUID uuid;
9096
memcpy(&uuid, Value, Size);
9197

92-
RPC_CSTR str;
93-
UuidToStringA(&uuid, &str);
98+
LPOLESTR unicodeStr;
99+
StringFromCLSID(uuid, &unicodeStr);
100+
101+
char str[StringBufferSize];
102+
int strLen = wcstombs(str, unicodeStr, sizeof(str));
103+
104+
assert(strLen == 37 && "expected ascii convertible output from StringFromCLSID.");
105+
(void)strLen;
94106

95-
char* signedStr = reinterpret_cast<char*>(str);
96-
memcpy(out.data(), signedStr, StringBufferSize);
107+
memcpy(out.data(), str, StringBufferSize);
97108
#else
98109
uuid_unparse_upper(Value, out.data());
99110
#endif
@@ -104,14 +115,13 @@ void swift::UUID::toString(llvm::SmallVectorImpl<char> &out) const {
104115

105116
int swift::UUID::compare(UUID y) const {
106117
#if defined(_WIN32)
107-
RPC_STATUS s;
108-
::UUID uuid1;
118+
::GUID uuid1;
109119
memcpy(&uuid1, Value, Size);
110120

111-
::UUID uuid2;
121+
::GUID uuid2;
112122
memcpy(&uuid2, y.Value, Size);
113123

114-
return UuidCompare(&uuid1, &uuid2, &s);
124+
return memcmp(Value, y.Value, Size);
115125
#else
116126
return uuid_compare(Value, y.Value);
117127
#endif

0 commit comments

Comments
 (0)