Skip to content

Fix linker failures compiling Swift on Windows #6819

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 15, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 27 additions & 17 deletions lib/Basic/UUID.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ swift::UUID::UUID(FromRandom_t) {

swift::UUID::UUID(FromTime_t) {
#if defined(_WIN32)
::UUID uuid;
::GUID uuid;
::CoCreateGuid(&uuid);

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

swift::UUID::UUID() {
#if defined(_WIN32)
::UUID uuid = *((::UUID *)&Value);
UuidCreateNil(&uuid);
::GUID uuid = GUID();

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

Optional<swift::UUID> swift::UUID::fromString(const char *s) {
#if defined(_WIN32)
RPC_CSTR t = const_cast<RPC_CSTR>(reinterpret_cast<const unsigned char*>(s));

::UUID uuid;
RPC_STATUS status = UuidFromStringA(t, &uuid);
if (status == RPC_S_INVALID_STRING_UUID) {
int length = strlen(s) + 1;
wchar_t *unicodeString = new wchar_t[length];

size_t convertedChars = 0;
errno_t conversionResult =
mbstowcs_s(&convertedChars, unicodeString, length, s, length);
assert(conversionResult == 0 &&
"expected successful conversion of char* to wchar_t*");

::GUID uuid;
HRESULT parseResult = CLSIDFromString(unicodeString, &uuid);
if (parseResult != 0) {
return None;
}

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

RPC_CSTR str;
UuidToStringA(&uuid, &str);
LPOLESTR unicodeStr;
StringFromCLSID(uuid, &unicodeStr);

char str[StringBufferSize];
int strLen = wcstombs(str, unicodeStr, sizeof(str));

assert(strLen == 37 && "expected ascii convertible output from StringFromCLSID.");
(void)strLen;

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

int swift::UUID::compare(UUID y) const {
#if defined(_WIN32)
RPC_STATUS s;
::UUID uuid1;
::GUID uuid1;
memcpy(&uuid1, Value, Size);

::UUID uuid2;
::GUID uuid2;
memcpy(&uuid2, y.Value, Size);

return UuidCompare(&uuid1, &uuid2, &s);
return memcmp(Value, y.Value, Size);
#else
return uuid_compare(Value, y.Value);
#endif
Expand Down